Liferay NPM Bundler Plugin: Exclude Imports
liferay-npm-bundler-plugin-exclude-imports is a plugin designed for the liferay-npm-bundler tool, currently at version 2.32.2. Its core function is to allow developers to explicitly exclude specific imported dependencies from the final JavaScript bundle generated for Liferay DXP projects. This is critical for managing bundle sizes and leveraging Liferay's OSGi module runtime, where certain dependencies might be provided globally or by other deployed modules, preventing duplication. While it facilitates fine-grained control over bundling, the liferay-npm-bundler itself, and by extension this plugin, has been officially deprecated as of Liferay 2024 Q4/Portal GA129, with plans for future removal in favor of standard JavaScript tooling like Esbuild, Webpack, or Vite. Releases are part of the broader liferay-frontend-projects monorepo, suggesting its lifecycle is tied to the evolution of Liferay's frontend tooling, although this specific package does not have frequent individual releases. It's a key tool for legacy Liferay DXP projects requiring fine-grained control over OSGi-compliant module bundling.
Common errors
-
Module not found: Error: Can't resolve 'some-dependency-name' in 'path/to/your/module'
cause A dependency was excluded by the liferay-npm-bundler-plugin-exclude-imports plugin but is not available at runtime in the Liferay DXP environment.fixReview your `.npmbundlerrc` configuration and ensure 'some-dependency-name' is either removed from the exclusion list, or confirm that it is correctly provided by another OSGi bundle or the Liferay runtime. Inspect the generated OSGi bundle to verify its contents. -
LiferayNPMBundler: Plugin 'exclude-imports' not found or invalid.
cause The liferay-npm-bundler could not locate or correctly instantiate the plugin, often due to a typo in the plugin name within `.npmbundlerrc` or an installation issue.fixVerify that `liferay-npm-bundler-plugin-exclude-imports` is correctly installed in your project's `node_modules`. Check for typos in the plugin name `"exclude-imports"` within your `.npmbundlerrc` file under the `plugins` array. Ensure the `.npmbundlerrc` file is valid JSON and correctly placed.
Warnings
- breaking The underlying liferay-npm-bundler, for which this is a plugin, has been officially deprecated as of Liferay 2024 Q4/Portal GA129. It is planned for future removal. This means the plugin will become obsolete with the bundler's deprecation and eventual removal.
- gotcha Incorrectly excluding a dependency that is not actually provided by the Liferay runtime or another OSGi module will lead to runtime errors (e.g., 'Module not found' or `undefined` references) in the browser. This plugin bypasses standard bundling, operating on the assumption that the excluded dependency is externally available.
- gotcha The liferay-npm-bundler's plugin loading mechanism can be sensitive to configuration. Ensure the plugin name ('exclude-imports') is correctly specified in your `.npmbundlerrc` file within the appropriate `plugins`, `pre-plugins`, or `post-plugins` section, and that any specific plugin options are correctly nested under a `config['exclude-imports']` object.
Install
-
npm install liferay-npm-bundler-plugin-exclude-imports -
yarn add liferay-npm-bundler-plugin-exclude-imports -
pnpm add liferay-npm-bundler-plugin-exclude-imports
Imports
- ExcludeImportsPlugin
import { ExcludeImportsPlugin } from 'liferay-npm-bundler-plugin-exclude-imports';import ExcludeImportsPlugin from 'liferay-npm-bundler-plugin-exclude-imports';
- ExcludeImportsPlugin
const ExcludeImportsPlugin = require('liferay-npm-bundler-plugin-exclude-imports'); - ExcludeImportsPluginType
import { ExcludeImportsPluginType } from 'liferay-npm-bundler-plugin-exclude-imports';import type { ExcludeImportsPluginType } from 'liferay-npm-bundler-plugin-exclude-imports';
Quickstart
{
"*": {
"plugins": [
"exclude-imports"
],
"config": {
"exclude-imports": {
"excludedPackages": [
"some-common-utility",
"@liferay/some-portal-api"
]
}
}
},
"my-custom-module": {
"plugins": [
"exclude-imports"
],
"config": {
"exclude-imports": {
"excludedPackages": [
"react",
"react-dom"
]
}
}
}
}