@rollup/plugin-commonjs
- Version 26.0.1
- Published
- 258 kB
- 6 dependencies
- MIT license
Install
npm i @rollup/plugin-commonjs
yarn add @rollup/plugin-commonjs
pnpm add @rollup/plugin-commonjs
Overview
Convert CommonJS modules to ES2015
Index
Functions
Interfaces
Type Aliases
Functions
function commonjs
commonjs: (options?: RollupCommonJSOptions) => Plugin;
Convert CommonJS modules to ES6, so they can be included in a Rollup bundle
Interfaces
interface RollupCommonJSOptions
interface RollupCommonJSOptions {}
property defaultIsModuleExports
defaultIsModuleExports?: | DefaultIsModuleExportsOption | ((id: string) => DefaultIsModuleExportsOption);
"auto"
property dynamicRequireRoot
dynamicRequireRoot?: string;
To avoid long paths when using the
dynamicRequireTargets
option, you can use this option to specify a directory that is a common parent for all files that use dynamic require statements. Using a directory higher up such as/
may lead to unnecessarily long paths in the generated code and may expose directory names on your machine like your home directory name. By default, it uses the current working directory.
property dynamicRequireTargets
dynamicRequireTargets?: string | ReadonlyArray<string>;
Some modules contain dynamic
require
calls, or require modules that contain circular dependencies, which are not handled well by static imports. Including those modules asdynamicRequireTargets
will simulate a CommonJS (NodeJS-like) environment for them with support for dynamic dependencies. It also enablesstrictRequires
for those modules.Note: In extreme cases, this feature may result in some paths being rendered as absolute in the final bundle. The plugin tries to avoid exposing paths from the local machine, but if you are
dynamicRequirePaths
with paths that are far away from your project's folder, that may require replacing strings like"/Users/John/Desktop/foo-project/"
->"/"
.
property esmExternals
esmExternals?: boolean | ReadonlyArray<string> | ((id: string) => boolean);
Controls how to render imports from external dependencies. By default, this plugin assumes that all external dependencies are CommonJS. This means they are rendered as default imports to be compatible with e.g. NodeJS where ES modules can only import a default export from a CommonJS dependency.
If you set
esmExternals
totrue
, this plugin assumes that all external dependencies are ES modules and respect therequireReturnsDefault
option. If that option is not set, they will be rendered as namespace imports.You can also supply an array of ids to be treated as ES modules, or a function that will be passed each external id to determine whether it is an ES module. false
property exclude
exclude?: FilterPattern;
A picomatch pattern, or array of patterns, which specifies the files in the build the plugin should _ignore_. By default, all files with extensions other than those in
extensions
or".cjs"
are ignored, but you can exclude additional files. See also theinclude
option. undefined
property extensions
extensions?: ReadonlyArray<string>;
For extensionless imports, search for extensions other than .js in the order specified. Note that you need to make sure that non-JavaScript files are transpiled by another plugin first. [ '.js' ]
property ignore
ignore?: ReadonlyArray<string> | ((id: string) => boolean);
Sometimes you have to leave require statements unconverted. Pass an array containing the IDs or a
id => boolean
function. []
property ignoreDynamicRequires
ignoreDynamicRequires?: boolean;
Some
require
calls cannot be resolved statically to be translated to imports. When this option is set tofalse
, the generated code will either directly throw an error when such a call is encountered or, whendynamicRequireTargets
is used, when such a call cannot be resolved with a configured dynamic require target. Setting this option totrue
will instead leave therequire
call in the code or use it as a fallback fordynamicRequireTargets
. false
property ignoreGlobal
ignoreGlobal?: boolean;
If true then uses of
global
won't be dealt with by this plugin false
property ignoreTryCatch
ignoreTryCatch?: | boolean | 'remove' | ReadonlyArray<string> | ((id: string) => boolean | 'remove');
In most cases, where
require
calls are inside atry-catch
clause, they should be left unconverted as it requires an optional dependency that may or may not be installed beside the rolled up package. Due to the conversion ofrequire
to a staticimport
- the call is hoisted to the top of the file, outside thetry-catch
clause.-
true
: Default. Allrequire
calls inside atry
will be left unconverted. -false
: Allrequire
calls inside atry
will be converted as if thetry-catch
clause is not there. -remove
: Remove allrequire
calls from inside anytry
block. -string[]
: Pass an array containing the IDs to left unconverted. -((id: string) => boolean|'remove')
: Pass a function that controls individual IDs.true
property include
include?: FilterPattern;
A picomatch pattern, or array of patterns, which specifies the files in the build the plugin should operate on. By default, all files with extension
".cjs"
or those inextensions
are included, but you can narrow this list by only including specific files. These files will be analyzed and transpiled if either the analysis does not find ES module specific statements ortransformMixedEsModules
istrue
. undefined
property requireReturnsDefault
requireReturnsDefault?: | RequireReturnsDefaultOption | ((id: string) => RequireReturnsDefaultOption);
Controls what is returned when requiring an ES module from a CommonJS file. When using the
esmExternals
option, this will also apply to external modules. By default, this plugin will render those imports as namespace imports i.e.// inputconst foo = require('foo');// outputimport * as foo from 'foo';However, there are some situations where this may not be desired. For these situations, you can change Rollup's behaviour either globally or per module. To change it globally, set the
requireReturnsDefault
option to one of the following values:-
false
: This is the default, requiring an ES module returns its namespace. This is the only option that will also add a marker__esModule: true
to the namespace to support interop patterns in CommonJS modules that are transpiled ES modules. -"namespace"
: Likefalse
, requiring an ES module returns its namespace, but the plugin does not add the__esModule
marker and thus creates more efficient code. For external dependencies when usingesmExternals: true
, no additional interop code is generated. -"auto"
: This is complementary to howoutput.exports: "auto"
works in Rollup: If a module has a default export and no named exports, requiring that module returns the default export. In all other cases, the namespace is returned. For external dependencies when usingesmExternals: true
, a corresponding interop helper is added. -"preferred"
: If a module has a default export, requiring that module always returns the default export, no matter whether additional named exports exist. This is similar to how previous versions of this plugin worked. Again for external dependencies when usingesmExternals: true
, an interop helper is added. -true
: This will always try to return the default export on require without checking if it actually exists. This can throw at build time if there is no default export. This is how external dependencies are handled whenesmExternals
is not used. The advantage over the other options is that, likefalse
, this does not add an interop helper for external dependencies, keeping the code lean.To change this for individual modules, you can supply a function for
requireReturnsDefault
instead. This function will then be called once for each required ES module or external dependency with the corresponding id and allows you to return different values for different modules. false
property sourceMap
sourceMap?: boolean;
If false, skips source map generation for CommonJS modules. This will improve performance. true
property strictRequires
strictRequires?: boolean | FilterPattern;
By default, this plugin will try to hoist
require
statements as imports to the top of each file. While this works well for many code bases and allows for very efficient ESM output, it does not perfectly capture CommonJS semantics as the order of side effects like log statements may change. But it is especially problematic when there are circularrequire
calls between CommonJS modules as those often rely on the lazy execution of nestedrequire
calls.Setting this option to
true
will wrap all CommonJS files in functions which are executed when they are required for the first time, preserving NodeJS semantics. Note that this can have an impact on the size and performance of the generated code.The default value of
"auto"
will only wrap CommonJS files when they are part of a CommonJS dependency cycle, e.g. an index file that is required by many of its dependencies. All other CommonJS files are hoisted. This is the recommended setting for most code bases.false
will entirely prevent wrapping and hoist all files. This may still work depending on the nature of cyclic dependencies but will often cause problems.You can also provide a picomatch pattern, or array of patterns, to only specify a subset of files which should be wrapped in functions for proper
require
semantics."debug"
works like"auto"
but after bundling, it will display a warning containing a list of ids that have been wrapped which can be used as picomatch pattern for fine-tuning. "auto"
property transformMixedEsModules
transformMixedEsModules?: boolean;
Instructs the plugin whether to enable mixed module transformations. This is useful in scenarios with modules that contain a mix of ES
import
statements and CommonJSrequire
expressions. Set totrue
ifrequire
calls should be transformed to imports in mixed modules, orfalse
if therequire
expressions should survive the transformation. The latter can be important if the code contains environment detection, or you are coding for an environment with special treatment forrequire
calls such as ElectronJS. See also theignore
option. false
Type Aliases
type DefaultIsModuleExportsOption
type DefaultIsModuleExportsOption = boolean | 'auto';
type RequireReturnsDefaultOption
type RequireReturnsDefaultOption = boolean | 'auto' | 'preferred' | 'namespace';
Package Files (1)
Dependencies (6)
Dev Dependencies (8)
Peer Dependencies (1)
Badge
To add a badge like this oneto your package's README, use the codes available below.
You may also use Shields.io to create a custom badge linking to https://www.jsdocs.io/package/@rollup/plugin-commonjs
.
- Markdown[![jsDocs.io](https://img.shields.io/badge/jsDocs.io-reference-blue)](https://www.jsdocs.io/package/@rollup/plugin-commonjs)
- HTML<a href="https://www.jsdocs.io/package/@rollup/plugin-commonjs"><img src="https://img.shields.io/badge/jsDocs.io-reference-blue" alt="jsDocs.io"></a>
- Updated .
Package analyzed in 2912 ms. - Missing or incorrect documentation? Open an issue for this package.