@rollup/plugin-commonjs 13.0.2 → 16.0.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 +52 -0
- package/LICENSE +21 -0
- package/README.md +187 -14
- package/dist/index.es.js +713 -656
- package/dist/index.es.js.map +1 -1
- package/dist/index.js +723 -661
- package/dist/index.js.map +1 -1
- package/package.json +21 -24
- package/types/index.d.ts +111 -24
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,57 @@
|
|
|
1
1
|
# @rollup/plugin-commonjs ChangeLog
|
|
2
2
|
|
|
3
|
+
## v16.0.0
|
|
4
|
+
|
|
5
|
+
_2020-10-27_
|
|
6
|
+
|
|
7
|
+
### Breaking Changes
|
|
8
|
+
|
|
9
|
+
- feat!: Expose cjs detection and support offline caching (#604)
|
|
10
|
+
|
|
11
|
+
### Bugfixes
|
|
12
|
+
|
|
13
|
+
- fix: avoid wrapping `commonjsRegister` call in `createCommonjsModule(...)` (#602)
|
|
14
|
+
- fix: register dynamic modules when a different loader (i.e typescript) loads the entry file (#599)
|
|
15
|
+
- fix: fixed access to node_modules dynamic module with subfolder (i.e 'logform/json') (#601)
|
|
16
|
+
|
|
17
|
+
### Features
|
|
18
|
+
|
|
19
|
+
- feat: pass type of import to node-resolve (#611)
|
|
20
|
+
|
|
21
|
+
## v15.1.0
|
|
22
|
+
|
|
23
|
+
_2020-09-21_
|
|
24
|
+
|
|
25
|
+
### Features
|
|
26
|
+
|
|
27
|
+
- feat: inject \_\_esModule marker into ES namespaces and add Object prototype (#552)
|
|
28
|
+
- feat: add requireReturnsDefault to types (#579)
|
|
29
|
+
|
|
30
|
+
## v15.0.0
|
|
31
|
+
|
|
32
|
+
_2020-08-13_
|
|
33
|
+
|
|
34
|
+
### Breaking Changes
|
|
35
|
+
|
|
36
|
+
- feat!: return the namespace by default when requiring ESM (#507)
|
|
37
|
+
- fix!: fix interop when importing CJS that is transpiled ESM from an actual ESM (#501)
|
|
38
|
+
|
|
39
|
+
### Bugfixes
|
|
40
|
+
|
|
41
|
+
- fix: add .cjs to default file extensions. (#524)
|
|
42
|
+
|
|
43
|
+
### Updates
|
|
44
|
+
|
|
45
|
+
- chore: update dependencies (fe399e2)
|
|
46
|
+
|
|
47
|
+
## v14.0.0
|
|
48
|
+
|
|
49
|
+
_2020-07-13_
|
|
50
|
+
|
|
51
|
+
### Release Notes
|
|
52
|
+
|
|
53
|
+
This restores the fixes from v13.0.1, but as a semver compliant major version.
|
|
54
|
+
|
|
3
55
|
## v13.0.2
|
|
4
56
|
|
|
5
57
|
_2020-07-13_
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2019 RollupJS Plugin Contributors (https://github.com/rollup/plugins/graphs/contributors)
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
|
13
|
+
all copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
+
THE SOFTWARE.
|
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: `
|
|
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: `
|
|
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
|
|
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: `
|
|
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
|
|
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: `
|
|
90
|
+
Type: `string[]`<br>
|
|
91
91
|
Default: `['.js']`
|
|
92
92
|
|
|
93
|
-
|
|
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: `
|
|
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: `
|
|
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: `
|
|
111
|
+
Type: `boolean`<br>
|
|
112
112
|
Default: `false`
|
|
113
113
|
|
|
114
|
-
Instructs the plugin whether
|
|
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: `
|
|
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.
|
|
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 | "namespace" | "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
|
|
|
@@ -158,6 +311,26 @@ ES modules are _always_ parsed in strict mode. That means that certain non-stric
|
|
|
158
311
|
|
|
159
312
|
Luckily, there is absolutely no good reason _not_ to use strict mode for everything — so the solution to this problem is to lobby the authors of those modules to update them.
|
|
160
313
|
|
|
314
|
+
## Inter-plugin-communication
|
|
315
|
+
|
|
316
|
+
This plugin exposes the result of its CommonJS file type detection for other plugins to use. You can access it via `this.getModuleInfo` or the `moduleParsed` hook:
|
|
317
|
+
|
|
318
|
+
```js
|
|
319
|
+
function cjsDetectionPlugin() {
|
|
320
|
+
return {
|
|
321
|
+
name: 'cjs-detection',
|
|
322
|
+
moduleParsed({
|
|
323
|
+
id,
|
|
324
|
+
meta: {
|
|
325
|
+
commonjs: { isCommonJS }
|
|
326
|
+
}
|
|
327
|
+
}) {
|
|
328
|
+
console.log(`File ${id} is CommonJS: ${isCommonJS}`);
|
|
329
|
+
}
|
|
330
|
+
};
|
|
331
|
+
}
|
|
332
|
+
```
|
|
333
|
+
|
|
161
334
|
## Meta
|
|
162
335
|
|
|
163
336
|
[CONTRIBUTING](/.github/CONTRIBUTING.md)
|