Skip to content

Commit 09cc21a

Browse files
committed
docs(commonjs): improve documentation and update types
1 parent 87623e3 commit 09cc21a

File tree

3 files changed

+109
-35
lines changed

3 files changed

+109
-35
lines changed

packages/commonjs/README.md

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -76,21 +76,21 @@ commonjs({
7676
Type: `string | string[]`<br>
7777
Default: `null`
7878

79-
A [minimatch pattern](https://github.com/isaacs/minimatch), or array of patterns, which specifies the files in the build the plugin should _ignore_. By default non-CommonJS modules are ignored.
79+
A [minimatch pattern](https://github.com/isaacs/minimatch), 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 the `include` option.
8080

8181
### `include`
8282

8383
Type: `string | string[]`<br>
8484
Default: `null`
8585

86-
A [minimatch pattern](https://github.com/isaacs/minimatch), or array of patterns, which specifies the files in the build the plugin should operate on. By default CommonJS modules are targeted.
86+
A [minimatch pattern](https://github.com/isaacs/minimatch), 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 in `extensions` 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 or `transformMixedEsModules` is `true`.
8787

8888
### `extensions`
8989

9090
Type: `string[]`<br>
9191
Default: `['.js']`
9292

93-
Search for extensions other than .js in the order specified.
93+
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.
9494

9595
### `ignoreGlobal`
9696

@@ -104,28 +104,28 @@ If true, uses of `global` won't be dealt with by this plugin.
104104
Type: `boolean`<br>
105105
Default: `true`
106106

107-
If false, skips source map generation for CommonJS modules.
107+
If false, skips source map generation for CommonJS modules. This will improve performance.
108108

109109
### `transformMixedEsModules`
110110

111111
Type: `boolean`<br>
112112
Default: `false`
113113

114-
Instructs the plugin whether or not to enable mixed module transformations. This is useful in scenarios with mixed ES and CommonJS modules. Set to `true` if it's known that `require` calls should be transformed, or `false` if the code contains env detection and the `require` should survive a transformation.
114+
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 CommonJS `require` expressions. Set to `true` if `require` calls should be transformed to imports in mixed modules, or `false` if the `require` 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 for `require` calls such as [ElectronJS](https://www.electronjs.org/). See also the "ignore" option.
115115

116116
### `ignore`
117117

118118
Type: `string[] | ((id: string) => boolean)`<br>
119119
Default: `[]`
120120

121-
Sometimes you have to leave require statements unconverted. Pass an array containing the IDs or an `id => boolean` function. Only use this option if you know what you're doing!
121+
Sometimes you have to leave require statements unconverted. Pass an array containing the IDs or an `id => boolean` function.
122122

123123
### `esmExternals`
124124

125-
Type: `boolean | string[] || ((id: string) => boolean)`
125+
Type: `boolean | string[] | ((id: string) => boolean)`
126126
Default: `false`
127127

128-
Controls how imports from external dependencies are rendered. By default, all external dependencies are assumed to be 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:
128+
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:
129129

130130
```js
131131
// input
@@ -137,16 +137,16 @@ import foo from 'foo';
137137

138138
This is likely not desired for ES module dependencies: Here `require` should usually return the namespace to be compatible with how bundled modules are handled.
139139

140-
If you set `esmExternals` to `true`, all external dependencies are assumed to be ES modules and will adhere to the `requireReturnsDefault` option. If that option is not set, they will be rendered as namespace imports.
140+
If you set `esmExternals` to `true`, this plugins assumes that all external dependencies are ES modules and will adhere to the `requireReturnsDefault` option. If that option is not set, they will be rendered as namespace imports.
141141

142-
You can also supply an array of ids that are to be treated as ES modules, or a function that will be passed each external id to determine if it is an ES module.
142+
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 if it is an ES module.
143143

144144
### `requireReturnsDefault`
145145

146146
Type: `boolean | "auto" | "preferred" | ((id: string) => boolean | "auto" | "preferred")`<br>
147147
Default: `false`
148148

149-
Controls what is returned when requiring an ES module or external dependency from a CommonJS file. By default, this plugin will render it as a namespace import, i.e.
149+
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.
150150

151151
```js
152152
// input

packages/commonjs/test/types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@ const config: import('rollup').RollupOptions = {
99
plugins: [
1010
commonjs({
1111
include: 'node_modules/**',
12+
esmExternals: ['foo', 'bar'],
1213
exclude: ['node_modules/foo/**', 'node_modules/bar/**', /node_modules/],
1314
extensions: ['.js', '.coffee'],
1415
ignoreGlobal: false,
16+
requireReturnsDefault: 'auto',
1517
sourceMap: false,
1618
transformMixedEsModules: false,
1719
ignore: ['conditional-runtime-dependency'],

packages/commonjs/types/index.d.ts

Lines changed: 96 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,130 @@
11
import { FilterPattern } from '@rollup/pluginutils';
22
import { Plugin } from 'rollup';
33

4+
type RequireReturnsDefaultOption = boolean | 'auto' | 'preferred';
5+
46
interface RollupCommonJSOptions {
57
/**
6-
* non-CommonJS modules will be ignored, but you can also
7-
* specifically include/exclude files
8+
* A minimatch pattern, or array of patterns, which specifies the files in
9+
* the build the plugin should operate on. By default, all files with
10+
* extension `".cjs"` or those in `extensions` are included, but you can narrow
11+
* this list by only including specific files. These files will be analyzed
12+
* and transpiled if either the analysis does not find ES module specific
13+
* statements or `transformMixedEsModules` is `true`.
814
* @default undefined
915
*/
1016
include?: FilterPattern;
1117
/**
12-
* non-CommonJS modules will be ignored, but you can also
13-
* specifically include/exclude files
18+
* A minimatch pattern, or array of patterns, which specifies the files in
19+
* the build the plugin should _ignore_. By default, all files with
20+
* extensions other than those in `extensions` or `".cjs"` are ignored, but you
21+
* can exclude additional files. See also the `include` option.
1422
* @default undefined
1523
*/
1624
exclude?: FilterPattern;
1725
/**
18-
* search for files other than .js files (must already
19-
* be transpiled by a previous plugin!)
26+
* For extensionless imports, search for extensions other than .js in the
27+
* order specified. Note that you need to make sure that non-JavaScript files
28+
* are transpiled by another plugin first.
2029
* @default [ '.js' ]
2130
*/
22-
extensions?: ReadonlyArray<string | RegExp>;
31+
extensions?: ReadonlyArray<string>;
2332
/**
24-
* if true then uses of `global` won't be dealt with by this plugin
33+
* If true then uses of `global` won't be dealt with by this plugin
2534
* @default false
2635
*/
2736
ignoreGlobal?: boolean;
2837
/**
29-
* if false then skip sourceMap generation for CommonJS modules
38+
* If false, skips source map generation for CommonJS modules. This will improve performance.
3039
* @default true
3140
*/
3241
sourceMap?: boolean;
3342
/**
34-
* Instructs the plugin whether or not to enable mixed module transformations. This is useful in scenarios with mixed ES and CommonJS modules. Set to `true` if it's known that `require` calls should be transformed, or `false` if the code contains env detection and the `require` should survive a transformation.
43+
* Instructs the plugin whether to enable mixed module transformations. This
44+
* is useful in scenarios with modules that contain a mix of ES `import`
45+
* statements and CommonJS `require` expressions. Set to `true` if `require`
46+
* calls should be transformed to imports in mixed modules, or `false` if the
47+
* `require` expressions should survive the transformation. The latter can be
48+
* important if the code contains environment detection, or you are coding
49+
* for an environment with special treatment for `require` calls such as
50+
* ElectronJS. See also the `ignore` option.
3551
* @default false
3652
*/
3753
transformMixedEsModules?: boolean;
3854
/**
39-
* sometimes you have to leave require statements
40-
* unconverted. Pass an array containing the IDs
41-
* or a `id => boolean` function. Only use this
42-
* option if you know what you're doing!
55+
* Sometimes you have to leave require statements unconverted. Pass an array
56+
* containing the IDs or a `id => boolean` function.
57+
* @default []
58+
*/
59+
ignore?: ReadonlyArray<string> | ((id: string) => boolean);
60+
/**
61+
* Controls how to render imports from external dependencies. By default,
62+
* this plugin assumes that all external dependencies are CommonJS. This
63+
* means they are rendered as default imports to be compatible with e.g.
64+
* NodeJS where ES modules can only import a default export from a CommonJS
65+
* dependency.
66+
*
67+
* If you set `esmExternals` to `true`, this plugins assumes that all
68+
* external dependencies are ES modules and respect the
69+
* `requireReturnsDefault` option. If that option is not set, they will be
70+
* rendered as namespace imports.
71+
*
72+
* You can also supply an array of ids to be treated as ES modules, or a
73+
* function that will be passed each external id to determine if it is an ES
74+
* module.
75+
* @default false
76+
*/
77+
esmExternals?: boolean | ReadonlyArray<string> | ((id: string) => boolean);
78+
/**
79+
* Controls what is returned when requiring an ES module from a CommonJS file.
80+
* When using the `esmExternals` option, this will also apply to external
81+
* modules. By default, this plugin will render those imports as namespace
82+
* imports. However there are some situations where this may not be desired.
83+
* For these situations, you can change Rollup's behaviour either globally or
84+
* per module. To change it globally, set the `requireReturnsDefault` option
85+
* to one of the following values:
86+
*
87+
* - `false`: This is the default, requiring an ES module returns its
88+
* namespace. For external dependencies when using `esmExternals: true`, no
89+
* additional interop code is generated.
90+
* - `"auto"`: This is complementary to how `output.exports: "auto"` works in
91+
* Rollup: If a module has a default export and no named exports, requiring
92+
* that module returns the default export. In all other cases, the namespace
93+
* is returned. For external dependencies when using `esmExternals: true`, a
94+
* corresponding interop helper is added.
95+
* - `"preferred"`: If a module has a default export, requiring that module
96+
* always returns the default export, no matter whether additional named
97+
* exports exist. This is similar to how previous versions of this plugin
98+
* worked. Again for external dependencies when using `esmExternals: true`, an
99+
* interop helper is added.
100+
* - `true`: This will always try to return the default export on require
101+
* without checking if it actually exists. This can throw at build time if
102+
* there is no default export. This is how external dependencies are handled
103+
* when `esmExternals` is not used. The advantage over the other options is
104+
* that, like `false`, this does not add an interop helper for external
105+
* dependencies, keeping the code lean.
106+
*
107+
* To change this for individual modules, you can supply a function for
108+
* `requireReturnsDefault` instead. This function will then be called once for
109+
* each required ES module or external dependency with the corresponding id
110+
* and allows you to return different values for different modules.
111+
* @default false
43112
*/
44-
ignore?: ReadonlyArray<string | ((id: string) => boolean)>;
113+
requireReturnsDefault?:
114+
| RequireReturnsDefaultOption
115+
| ((id: string) => RequireReturnsDefaultOption);
45116
/**
46-
* Some modules contain dynamic `require` calls, or require modules that contain
47-
* circular dependencies, which are not handled well by static imports.
48-
* Including those modules as `dynamicRequireTargets` will simulate a CommonJS (NodeJS-like)
49-
* environment for them with support for dynamic and circular dependencies.
117+
* Some modules contain dynamic `require` calls, or require modules that
118+
* contain circular dependencies, which are not handled well by static
119+
* imports. Including those modules as `dynamicRequireTargets` will simulate a
120+
* CommonJS (NodeJS-like) environment for them with support for dynamic and
121+
* circular dependencies.
50122
*
51-
* Note: In extreme cases, this feature may result in some paths being rendered as
52-
* absolute in the final bundle. The plugin tries to avoid exposing paths from
53-
* the local machine, but if you are `dynamicRequirePaths` with paths that are
54-
* far away from your project's folder, that may require replacing strings
55-
* like `"/Users/John/Desktop/foo-project/"` -> `"/"`.
123+
* Note: In extreme cases, this feature may result in some paths being
124+
* rendered as absolute in the final bundle. The plugin tries to avoid
125+
* exposing paths from the local machine, but if you are `dynamicRequirePaths`
126+
* with paths that are far away from your project's folder, that may require
127+
* replacing strings like `"/Users/John/Desktop/foo-project/"` -> `"/"`.
56128
*/
57129
dynamicRequireTargets?: string | ReadonlyArray<string>;
58130
}

0 commit comments

Comments
 (0)