@rollup/plugin-commonjs 13.0.1 → 15.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,47 @@
1
1
  # @rollup/plugin-commonjs ChangeLog
2
2
 
3
+ ## v15.1.0
4
+
5
+ _2020-09-21_
6
+
7
+ ### Features
8
+
9
+ - feat: inject \_\_esModule marker into ES namespaces and add Object prototype (#552)
10
+ - feat: add requireReturnsDefault to types (#579)
11
+
12
+ ## v15.0.0
13
+
14
+ _2020-08-13_
15
+
16
+ ### Breaking Changes
17
+
18
+ - feat!: return the namespace by default when requiring ESM (#507)
19
+ - fix!: fix interop when importing CJS that is transpiled ESM from an actual ESM (#501)
20
+
21
+ ### Bugfixes
22
+
23
+ - fix: add .cjs to default file extensions. (#524)
24
+
25
+ ### Updates
26
+
27
+ - chore: update dependencies (fe399e2)
28
+
29
+ ## v14.0.0
30
+
31
+ _2020-07-13_
32
+
33
+ ### Release Notes
34
+
35
+ This restores the fixes from v13.0.1, but as a semver compliant major version.
36
+
37
+ ## v13.0.2
38
+
39
+ _2020-07-13_
40
+
41
+ ### Rollback
42
+
43
+ Rolls back breaking change in v13.0.1 whereby the exported `unwrapExports` method was removed.
44
+
3
45
  ## v13.0.1
4
46
 
5
47
  _2020-07-12_
package/README.md CHANGED
@@ -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.
@@ -73,52 +73,205 @@ commonjs({
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
- 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
- Type: `String` | `Array[...String]`<br>
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
- Type: `Array[...String]`<br>
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
 
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
- 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
- Type: `Boolean`<br>
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
- Type: `Array[...String | (String) => Boolean]`<br>
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.
122
+
123
+ ### `esmExternals`
124
+
125
+ Type: `boolean | string[] | ((id: string) => boolean)`
126
+ Default: `false`
127
+
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
+
130
+ ```js
131
+ // input
132
+ const foo = require('foo');
133
+
134
+ // output
135
+ import foo from '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`, 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
+
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.
143
+
144
+ ### `requireReturnsDefault`
145
+
146
+ Type: `boolean | "auto" | "preferred" | ((id: string) => boolean | "auto" | "preferred")`<br>
147
+ Default: `false`
148
+
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
+
151
+ ```js
152
+ // input
153
+ const foo = require('foo');
154
+
155
+ // output
156
+ import * as foo from '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
+ const dep = require('./dep.js');
167
+ console.log(dep.default);
168
+
169
+ // input: dep.js
170
+ export default '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. 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
+
187
+ ```js
188
+ // input
189
+ const dep = require('dep');
190
+ console.log(dep);
191
+
192
+ // output
193
+ import * as dep$1 from 'dep';
194
+
195
+ function getAugmentedNamespace(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.
221
+
222
+ ```js
223
+ // output
224
+ import * as dep from 'dep';
225
+
226
+ console.log(dep);
227
+ ```
228
+
229
+ - `"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:
230
+
231
+ ```js
232
+ // output
233
+ import * as dep$1 from 'dep';
234
+
235
+ function getDefaultExportFromNamespaceIfNotNamed(n) {
236
+ return n &&
237
+ Object.prototype.hasOwnProperty.call(n, 'default') &&
238
+ Object.keys(n).length === 1
239
+ ? n['default']
240
+ : n;
241
+ }
242
+
243
+ var dep = getDefaultExportFromNamespaceIfNotNamed(dep$1);
244
+
245
+ console.log(dep);
246
+ ```
247
+
248
+ - `"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:
249
+
250
+ ```js
251
+ // output
252
+ import * as dep$1 from 'dep';
253
+
254
+ function getDefaultExportFromNamespaceIfPresent(n) {
255
+ return n && Object.prototype.hasOwnProperty.call(n, 'default')
256
+ ? n['default']
257
+ : n;
258
+ }
259
+
260
+ var dep = getDefaultExportFromNamespaceIfPresent(dep$1);
261
+
262
+ console.log(dep);
263
+ ```
264
+
265
+ - `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:
266
+
267
+ ```js
268
+ // output
269
+ import dep from 'dep';
270
+
271
+ console.log(dep);
272
+ ```
273
+
274
+ 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.
122
275
 
123
276
  ## Using with @rollup/plugin-node-resolve
124
277