Liferay npm Bundler React Preset

2.0.1 · deprecated · verified Tue Apr 21

The `liferay-npm-bundler-preset-reactjs` package, currently at stable version 2.0.1, provides a pre-configured solution for integrating React applications with the Liferay Portal ecosystem. It functions as a configuration preset for the `liferay-npm-bundler`, streamlining the setup by including necessary Babel presets like `babel-preset-liferay-standard` and Liferay-specific plugins such as `liferay-npm-bundler-plugin-replace-browser-modules`. This preset is designed to adapt standard React development workflows for Liferay's widget-based architecture and its unique AMD loading mechanism. It handles complexities like package namespacing for module isolation and facilitates dependency sharing across multiple widgets, which differentiates it from generic bundlers like Webpack that produce single JavaScript files. While the preset itself is stable, the underlying `liferay-npm-bundler` is officially deprecated as of Liferay 2024.Q4/Portal GA129 and is slated for removal, encouraging developers to transition to standard JavaScript build tools like esbuild or Webpack for future projects. This impacts the long-term relevance and future release cadence of this preset, which is now primarily in a maintenance or deprecated state.

Common errors

Warnings

Install

Quickstart

Demonstrates how to set up a basic Liferay widget project to utilize the `liferay-npm-bundler-preset-reactjs` for automatic Babel transpilation and Liferay-specific bundling of a simple React component for deployment.

# 1. Create a new Liferay-compatible project structure (example)
mkdir my-react-widget
cd my-react-widget
npm init -y

# 2. Install the React preset as a dev dependency
npm install --save-dev liferay-npm-bundler-preset-reactjs

# 3. Create a .npmbundlerrc file in your project root to use the preset
echo '{ "preset": "liferay-npm-bundler-preset-reactjs" }' > .npmbundlerrc

# 4. Create a simple React component file (e.g., src/main/resources/META-INF/resources/index.js)
mkdir -p src/main/resources/META-INF/resources
cat << EOF > src/main/resources/META-INF/resources/index.js
import React from 'react';
import ReactDOM from 'react-dom';

const App = () => {
  return (
    <div>
      <h1>Hello from Liferay React Widget!</h1>
      <p>This component is bundled using liferay-npm-bundler-preset-reactjs.</p>
    </div>
  );
};

// For Liferay portlets, components are often rendered into a specific DOM element
// and exported as a function for Liferay to invoke.
class MyReactPortlet extends React.Component {
    render() {
        return <App />;
    }
}

export default function(portletId) {
    ReactDOM.render(<MyReactPortlet />, document.getElementById(portletId));
}
EOF

# 5. Add React and ReactDOM as development dependencies
npm install --save-dev react react-dom
# Note: In a deployed Liferay DXP environment, React/ReactDOM are often provided as shared libraries.
# The bundler is designed to inject peer dependencies if needed.

# 6. (Optional) Run the Liferay npm bundler (requires 'liferay-npm-bundler' to be installed globally or as a dev dep)
# npm install -g liferay-npm-bundler
# liferay-npm-bundler

view raw JSON →