@rollup/plugin-commonjs 15.0.0 → 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 +9 -0
- package/README.md +43 -12
- package/dist/index.es.js +39 -15
- package/dist/index.es.js.map +1 -1
- package/dist/index.js +39 -15
- package/dist/index.js.map +1 -1
- package/package.json +4 -4
- package/types/index.d.ts +111 -24
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,14 @@
|
|
|
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
|
+
|
|
3
12
|
## v15.0.0
|
|
4
13
|
|
|
5
14
|
_2020-08-13_
|
package/README.md
CHANGED
|
@@ -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
|
|
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
|
|
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
|
-
|
|
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
|
|
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.
|
|
121
|
+
Sometimes you have to leave require statements unconverted. Pass an array containing the IDs or an `id => boolean` function.
|
|
122
122
|
|
|
123
123
|
### `esmExternals`
|
|
124
124
|
|
|
125
|
-
Type: `boolean | string[]
|
|
125
|
+
Type: `boolean | string[] | ((id: string) => boolean)`
|
|
126
126
|
Default: `false`
|
|
127
127
|
|
|
128
|
-
Controls how imports from external dependencies
|
|
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
|
|
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
|
|
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
143
|
|
|
144
144
|
### `requireReturnsDefault`
|
|
145
145
|
|
|
146
146
|
Type: `boolean | "auto" | "preferred" | ((id: string) => boolean | "auto" | "preferred")`<br>
|
|
147
147
|
Default: `false`
|
|
148
148
|
|
|
149
|
-
Controls what is returned when requiring an ES module
|
|
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.
|
|
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
|
const dep = require('dep');
|
|
190
190
|
console.log(dep);
|
|
191
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
|
|
192
223
|
// output
|
|
193
224
|
import * as dep from 'dep';
|
|
194
225
|
|
package/dist/index.es.js
CHANGED
|
@@ -51,11 +51,11 @@ export function getDefaultExportFromCjs (x) {
|
|
|
51
51
|
|
|
52
52
|
export function createCommonjsModule(fn, basedir, module) {
|
|
53
53
|
return module = {
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
54
|
+
path: basedir,
|
|
55
|
+
exports: {},
|
|
56
|
+
require: function (path, base) {
|
|
57
|
+
return commonjsRequire(path, (base === undefined || base === null) ? module.path : base);
|
|
58
|
+
}
|
|
59
59
|
}, fn(module, module.exports), module.exports;
|
|
60
60
|
}
|
|
61
61
|
|
|
@@ -66,6 +66,21 @@ export function getDefaultExportFromNamespaceIfPresent (n) {
|
|
|
66
66
|
export function getDefaultExportFromNamespaceIfNotNamed (n) {
|
|
67
67
|
return n && Object.prototype.hasOwnProperty.call(n, 'default') && Object.keys(n).length === 1 ? n['default'] : n;
|
|
68
68
|
}
|
|
69
|
+
|
|
70
|
+
export function getAugmentedNamespace(n) {
|
|
71
|
+
if (n.__esModule) return n;
|
|
72
|
+
var a = Object.defineProperty({}, '__esModule', {value: true});
|
|
73
|
+
Object.keys(n).forEach(function (k) {
|
|
74
|
+
var d = Object.getOwnPropertyDescriptor(n, k);
|
|
75
|
+
Object.defineProperty(a, k, d.get ? d : {
|
|
76
|
+
enumerable: true,
|
|
77
|
+
get: function () {
|
|
78
|
+
return n[k];
|
|
79
|
+
}
|
|
80
|
+
});
|
|
81
|
+
});
|
|
82
|
+
return a;
|
|
83
|
+
}
|
|
69
84
|
`;
|
|
70
85
|
|
|
71
86
|
const HELPER_NON_DYNAMIC = `
|
|
@@ -1080,10 +1095,12 @@ function getUnknownRequireProxy(id, requireReturnsDefault) {
|
|
|
1080
1095
|
const name = getName(id);
|
|
1081
1096
|
const exported =
|
|
1082
1097
|
requireReturnsDefault === 'auto'
|
|
1083
|
-
? `import {getDefaultExportFromNamespaceIfNotNamed} from "${HELPERS_ID}"; export default /*@__PURE__*/getDefaultExportFromNamespaceIfNotNamed(${name})
|
|
1098
|
+
? `import {getDefaultExportFromNamespaceIfNotNamed} from "${HELPERS_ID}"; export default /*@__PURE__*/getDefaultExportFromNamespaceIfNotNamed(${name});`
|
|
1084
1099
|
: requireReturnsDefault === 'preferred'
|
|
1085
|
-
? `import {getDefaultExportFromNamespaceIfPresent} from "${HELPERS_ID}"; export default /*@__PURE__*/getDefaultExportFromNamespaceIfPresent(${name})
|
|
1086
|
-
:
|
|
1100
|
+
? `import {getDefaultExportFromNamespaceIfPresent} from "${HELPERS_ID}"; export default /*@__PURE__*/getDefaultExportFromNamespaceIfPresent(${name});`
|
|
1101
|
+
: !requireReturnsDefault
|
|
1102
|
+
? `import {getAugmentedNamespace} from "${HELPERS_ID}"; export default /*@__PURE__*/getAugmentedNamespace(${name});`
|
|
1103
|
+
: `export default ${name};`;
|
|
1087
1104
|
return `import * as ${name} from ${JSON.stringify(id)}; ${exported}`;
|
|
1088
1105
|
}
|
|
1089
1106
|
|
|
@@ -1116,11 +1133,15 @@ async function getStaticRequireProxy(
|
|
|
1116
1133
|
return `import { __moduleExports } from ${JSON.stringify(id)}; export default __moduleExports;`;
|
|
1117
1134
|
} else if (isCjs === null) {
|
|
1118
1135
|
return getUnknownRequireProxy(id, requireReturnsDefault);
|
|
1136
|
+
} else if (!requireReturnsDefault) {
|
|
1137
|
+
return `import {getAugmentedNamespace} from "${HELPERS_ID}"; import * as ${name} from ${JSON.stringify(
|
|
1138
|
+
id
|
|
1139
|
+
)}; export default /*@__PURE__*/getAugmentedNamespace(${name});`;
|
|
1119
1140
|
} else if (
|
|
1120
1141
|
requireReturnsDefault !== true &&
|
|
1121
|
-
(
|
|
1142
|
+
(requireReturnsDefault === 'namespace' ||
|
|
1122
1143
|
!esModulesWithDefaultExport.has(id) ||
|
|
1123
|
-
(
|
|
1144
|
+
(requireReturnsDefault === 'auto' && esModulesWithNamedExports.has(id)))
|
|
1124
1145
|
) {
|
|
1125
1146
|
return `import * as ${name} from ${JSON.stringify(id)}; export default ${name};`;
|
|
1126
1147
|
}
|
|
@@ -1357,11 +1378,14 @@ function commonjs(options = {}) {
|
|
|
1357
1378
|
|
|
1358
1379
|
transform(code, id) {
|
|
1359
1380
|
const extName = extname(id);
|
|
1360
|
-
if (
|
|
1361
|
-
|
|
1362
|
-
|
|
1363
|
-
|
|
1364
|
-
|
|
1381
|
+
if (
|
|
1382
|
+
extName !== '.cjs' &&
|
|
1383
|
+
id !== DYNAMIC_PACKAGES_ID &&
|
|
1384
|
+
!id.startsWith(DYNAMIC_JSON_PREFIX) &&
|
|
1385
|
+
(!filter(id) || !extensions.includes(extName))
|
|
1386
|
+
) {
|
|
1387
|
+
setIsCjsPromise(id, null);
|
|
1388
|
+
return null;
|
|
1365
1389
|
}
|
|
1366
1390
|
|
|
1367
1391
|
let transformed;
|