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
Copy file name to clipboardExpand all lines: packages/commonjs/README.md
+137-15
Original file line number
Diff line number
Diff line change
@@ -34,9 +34,9 @@ export default {
34
34
input:'src/index.js',
35
35
output: {
36
36
dir:'output',
37
-
format:'cjs'
37
+
format:'cjs',
38
38
},
39
-
plugins: [commonjs()]
39
+
plugins: [commonjs()],
40
40
};
41
41
```
42
42
@@ -46,7 +46,7 @@ Then call `rollup` either via the [CLI](https://www.rollupjs.org/guide/en/#comma
46
46
47
47
### `dynamicRequireTargets`
48
48
49
-
Type: `String|Array[String]`<br>
49
+
Type: `string | string[]`<br>
50
50
Default: `[]`
51
51
52
52
Some modules contain dynamic `require` calls, or require modules that contain circular dependencies, which are not handled well by static imports.
@@ -66,60 +66,182 @@ commonjs({
66
66
'!node_modules/logform/index.js',
67
67
'!node_modules/logform/format.js',
68
68
'!node_modules/logform/levels.js',
69
-
'!node_modules/logform/browser.js'
70
-
]
69
+
'!node_modules/logform/browser.js',
70
+
],
71
71
});
72
72
```
73
73
74
74
### `exclude`
75
75
76
-
Type: `String` | `Array[...String]`<br>
76
+
Type: `string | string[]`<br>
77
77
Default: `null`
78
78
79
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.
80
80
81
81
### `include`
82
82
83
-
Type: `String` | `Array[...String]`<br>
83
+
Type: `string | string[]`<br>
84
84
Default: `null`
85
85
86
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.
87
87
88
88
### `extensions`
89
89
90
-
Type: `Array[...String]`<br>
90
+
Type: `string[]`<br>
91
91
Default: `['.js']`
92
92
93
93
Search for extensions other than .js in the order specified.
94
94
95
95
### `ignoreGlobal`
96
96
97
-
Type: `Boolean`<br>
97
+
Type: `boolean`<br>
98
98
Default: `false`
99
99
100
100
If true, uses of `global` won't be dealt with by this plugin.
101
101
102
102
### `sourceMap`
103
103
104
-
Type: `Boolean`<br>
104
+
Type: `boolean`<br>
105
105
Default: `true`
106
106
107
107
If false, skips source map generation for CommonJS modules.
108
108
109
109
### `transformMixedEsModules`
110
110
111
-
Type: `Boolean`<br>
111
+
Type: `boolean`<br>
112
112
Default: `false`
113
113
114
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.
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!
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:
129
+
130
+
```js
131
+
// input
132
+
constfoo=require('foo');
133
+
134
+
// output
135
+
importfoofrom'foo';
136
+
```
137
+
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
+
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.
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.
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.
150
+
151
+
```js
152
+
// input
153
+
constfoo=require('foo');
154
+
155
+
// output
156
+
import*asfoofrom'foo';
157
+
```
158
+
159
+
This is in line with how other bundlers handle this situation and is also the most likely behaviour in case Node should ever support this. However there are some situations where this may not be desired:
160
+
161
+
- There is code in an external dependency that cannot be changed where a `require` statement expects the default export to be returned from an ES module.
162
+
- If the imported module is in the same bundle, Rollup will generate a namespace object for the imported module which can increase bundle size unnecessarily:
163
+
164
+
```js
165
+
// input: main.js
166
+
constdep=require('./dep.js');
167
+
console.log(dep.default);
168
+
169
+
// input: dep.js
170
+
exportdefault'foo';
171
+
172
+
// output
173
+
var dep ='foo';
174
+
175
+
var dep$1 =/*#__PURE__*/Object.freeze({
176
+
__proto__:null,
177
+
default: dep,
178
+
});
179
+
180
+
console.log(dep$1.default);
181
+
```
182
+
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
+
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:
186
+
187
+
```js
188
+
// input
189
+
constdep=require('dep');
190
+
console.log(dep);
191
+
192
+
// output
193
+
import*asdepfrom'dep';
194
+
195
+
console.log(dep);
196
+
```
197
+
198
+
-`"auto"`: This is complementary to how [`output.exports`](https://rollupjs.org/guide/en/#outputexports): `"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 using `esmExternals: true`, a corresponding interop helper is added:
var dep =getDefaultExportFromNamespaceIfNotNamed(dep$1);
213
+
214
+
console.log(dep);
215
+
```
216
+
217
+
-`"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 using `esmExternals: true`, an interop helper is added:
return n &&Object.prototype.hasOwnProperty.call(n, 'default')
225
+
? n['default']
226
+
: n;
227
+
}
228
+
229
+
var dep =getDefaultExportFromNamespaceIfPresent(dep$1);
230
+
231
+
console.log(dep);
232
+
```
233
+
234
+
-`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 when `esmExternals` is not used. The advantage over the other options is that, like `false`, this does not add an interop helper for external dependencies, keeping the code lean:
235
+
236
+
```js
237
+
// output
238
+
importdepfrom'dep';
239
+
240
+
console.log(dep);
241
+
```
242
+
243
+
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.
244
+
123
245
## Using with @rollup/plugin-node-resolve
124
246
125
247
Since most CommonJS packages you are importing are probably dependencies in `node_modules`, you may need to use [@rollup/plugin-node-resolve](https://github.com/rollup/plugins/tree/master/packages/node-resolve):
@@ -134,9 +256,9 @@ export default {
134
256
output: {
135
257
file:'bundle.js',
136
258
format:'iife',
137
-
name:'MyModule'
259
+
name:'MyModule',
138
260
},
139
-
plugins: [resolve(), commonjs()]
261
+
plugins: [resolve(), commonjs()],
140
262
};
141
263
```
142
264
@@ -146,7 +268,7 @@ Symlinks are common in monorepos and are also created by the `npm link` command.
0 commit comments