Liferay npm Bundler Plugin for Create React App
The `liferay-npm-bundler-plugin-create-react-app` is a specialized plugin designed to facilitate the integration of projects bootstrapped with Create React App (CRA) into the Liferay Portal environment. It works in conjunction with `liferay-npm-bundler` to transform standard CRA output into OSGi bundles, making them deployable as Liferay widgets or portlets. This plugin manages the necessary adaptations for module loading, asset paths, and other build configurations specific to Liferay's ecosystem, which typically relies on an AMD loader. While the current stable version is 2.14.1, the underlying `liferay-npm-bundler` toolset is deprecated as of Liferay 2024.Q4 (Portal GA129) and is planned for future removal, meaning this plugin is primarily relevant for older Liferay DXP/Portal versions. Its key differentiator is simplifying the process of making CRA projects compatible with Liferay's unique bundling requirements without extensive manual configuration.
Common errors
-
Invariant Violation: Invalid hook call. Hooks can only be called inside of the body of a function component.
cause This error typically indicates that multiple instances of React are being loaded into the application. This often occurs when using `yarn` as a package manager, especially with UI libraries like Material UI, which can lead to duplicate React installations.fixSwitch your project's package manager from `yarn` to `npm`. Delete your `node_modules` directory and `package-lock.json` (if present), then run `npm install` to ensure a single, consistent React installation. -
Error: ENOENT: no such file or directory, stat '.../build/static/css/main.<hash>.css'
cause The `liferay-npm-bundler-plugin-create-react-app` plugin, or related bundler processes, expects a bundled CSS file to be present in the Create React App `build/static/css` output directory. This error occurs if no CSS file is generated by CRA because your project's entry point (e.g., `index.js`) does not contain an `import` statement for a stylesheet.fixEnsure that your Create React App project's main entry point, typically `src/index.js`, includes an explicit import statement for a CSS file (e.g., `import './index.css';`). This will trigger CRA to generate the expected CSS bundle that the Liferay bundler plugin anticipates.
Warnings
- breaking The underlying `liferay-npm-bundler` tool, which this plugin extends, is deprecated as of Liferay 2024.Q4 (Portal GA129) and is slated for future removal. New Liferay DXP projects should consider migrating to Client Extensions and modern build tools like Vite, as the `liferay-npm-bundler` ecosystem is no longer actively developed for new features.
- gotcha When developing Create React App projects for Liferay, strict adherence to `npm` for package management is often required. Using `yarn` (especially with UI libraries like Material UI) can lead to 'Invariant Violation: Invalid hook call' errors due to duplicate React instances being bundled.
- gotcha Create React App projects might fail to bundle correctly if the main `index.js` file does not explicitly import a CSS file (e.g., `import './index.css';`). The plugin's internal logic may assume the presence of a `static/css` directory in the build output, which might be missing otherwise.
- gotcha Compatibility issues can arise with different React versions and JSX runtimes. Liferay has historically used React 16 with a Classic JSX runtime. Using React 18 or configuring an 'automatic' JSX runtime in your CRA project might require specific configurations or adjustments to avoid conflicts with Liferay's embedded React instances.
Install
-
npm install liferay-npm-bundler-plugin-create-react-app -
yarn add liferay-npm-bundler-plugin-create-react-app -
pnpm add liferay-npm-bundler-plugin-create-react-app
Imports
- Liferay npm Bundler Plugin Configuration
import { createReactAppPlugin } from 'liferay-npm-bundler-plugin-create-react-app';/* In .npmbundlerrc */ { "*": { "plugins": [ "create-react-app" ] } }
Quickstart
{
"//": "This .npmbundlerrc file configures the Liferay npm Bundler for a Create React App project.",
"//": "It activates the 'create-react-app' plugin to handle CRA-specific build adjustments.",
"*": { "plugins": [ "create-react-app" ] },
"//": "For a comprehensive setup, the 'liferay-npm-bundler-preset-create-react-app' preset is often preferred:",
"//": "{\n \"preset\": \"liferay-npm-bundler-preset-create-react-app\"\n}",
"//": "This example assumes a standard Create React App project structure.",
"//": "Ensure your package.json name is unique and follows Liferay's naming conventions for client extensions.",
"//": "To build, navigate to your project root and run: npx liferay-npm-bundler"
}