You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
feat(commonjs): inject __esModule marker into ES namespaces and add Object prototype (#552)
* feat(commonjs): inject __esModule marker into ES namespaces
* docs(commonjs): improve documentation and update types
* feat(commonjs): Integrate into requireReturnsDefault option
* refactor(commonjs): add early exit for namespace augmentation
Copy file name to clipboardExpand all lines: packages/commonjs/README.md
+43-12Lines changed: 43 additions & 12 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -76,21 +76,21 @@ commonjs({
76
76
Type: `string | string[]`<br>
77
77
Default: `null`
78
78
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.
80
80
81
81
### `include`
82
82
83
83
Type: `string | string[]`<br>
84
84
Default: `null`
85
85
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`.
87
87
88
88
### `extensions`
89
89
90
90
Type: `string[]`<br>
91
91
Default: `['.js']`
92
92
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.
94
94
95
95
### `ignoreGlobal`
96
96
@@ -104,28 +104,28 @@ If true, uses of `global` won't be dealt with by this plugin.
104
104
Type: `boolean`<br>
105
105
Default: `true`
106
106
107
-
If false, skips source map generation for CommonJS modules.
107
+
If false, skips source map generation for CommonJS modules. This will improve performance.
108
108
109
109
### `transformMixedEsModules`
110
110
111
111
Type: `boolean`<br>
112
112
Default: `false`
113
113
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.
115
115
116
116
### `ignore`
117
117
118
118
Type: `string[] | ((id: string) => boolean)`<br>
119
119
Default: `[]`
120
120
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.
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:
129
129
130
130
```js
131
131
// input
@@ -137,16 +137,16 @@ import foo from 'foo';
137
137
138
138
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.
139
139
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.
141
141
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.
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.
150
150
151
151
```js
152
152
// input
@@ -182,13 +182,44 @@ This is in line with how other bundlers handle this situation and is also the mo
182
182
183
183
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:
184
184
185
-
-`false`: This is the default, requiring an ES module returns its namespace. For external dependencies when using `esmExternals: true`, no additional interop code is generated:
185
+
-`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.
186
186
187
187
```js
188
188
// input
189
189
constdep=require('dep');
190
190
console.log(dep);
191
191
192
+
// output
193
+
import*asdep$1from'dep';
194
+
195
+
functiongetAugmentedNamespace(n) {
196
+
var a =Object.defineProperty({}, '__esModule', { value:true });
197
+
Object.keys(n).forEach(function (k) {
198
+
var d =Object.getOwnPropertyDescriptor(n, k);
199
+
Object.defineProperty(
200
+
a,
201
+
k,
202
+
d.get
203
+
? d
204
+
: {
205
+
enumerable:true,
206
+
get:function () {
207
+
return n[k];
208
+
},
209
+
}
210
+
);
211
+
});
212
+
return a;
213
+
}
214
+
215
+
var dep =/*@__PURE__*/getAugmentedNamespace(dep$1);
216
+
217
+
console.log(dep);
218
+
```
219
+
220
+
-`"namespace"`: Like `false`, 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 using `esmExternals: true`, no additional interop code is generated.
0 commit comments