@rollup/plugin-commonjs 26.0.3 → 28.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/README.md +28 -4
- package/dist/cjs/index.js +48 -23
- package/dist/es/index.js +48 -23
- package/package.json +4 -3
package/README.md
CHANGED
|
@@ -49,13 +49,13 @@ When used together with the node-resolve plugin
|
|
|
49
49
|
### `strictRequires`
|
|
50
50
|
|
|
51
51
|
Type: `"auto" | boolean | "debug" | string[]`<br>
|
|
52
|
-
Default: `
|
|
52
|
+
Default: `true`
|
|
53
53
|
|
|
54
|
-
|
|
54
|
+
Historically, this plugin tried to hoist `require` statements as imports to the top of each file. While this works well for many code bases and allows for very efficient ESM output, it does not perfectly capture CommonJS semantics as the initialisation order of required modules will be different. The resultant side effects can include log statements being emitted in a different order, and some code that is dependent on the initialisation order of polyfills in require statements may not work. But it is especially problematic when there are circular `require` calls between CommonJS modules as those often rely on the lazy execution of nested `require` calls.
|
|
55
55
|
|
|
56
|
-
|
|
56
|
+
The default value of `true` will wrap all CommonJS files in functions which are executed when they are required for the first time, preserving NodeJS semantics. This is the safest setting and should be used if the generated code does not work correctly with `"auto"`. Note that `strictRequires: true` can have a small impact on the size and performance of generated code, but less so if the code is minified.
|
|
57
57
|
|
|
58
|
-
|
|
58
|
+
Setting this option to `"auto"` will only wrap CommonJS files when they are part of a CommonJS dependency cycle, e.g. an index file that is required by some of its dependencies, or if they are only required in a potentially "conditional" way like from within an if-statement or a function. All other CommonJS files are hoisted. This is the recommended setting for most code bases. Note that the detection of conditional requires can be subject to race conditions if there are both conditional and unconditional requires of the same file, which in edge cases may result in inconsistencies between builds. If you think this is a problem for you, you can avoid this by using any value other than `"auto"` or `"debug"`.
|
|
59
59
|
|
|
60
60
|
`false` will entirely prevent wrapping and hoist all files. This may still work depending on the nature of cyclic dependencies but will often cause problems.
|
|
61
61
|
|
|
@@ -386,6 +386,30 @@ For these situations, you can change Rollup's behaviour either globally or per m
|
|
|
386
386
|
|
|
387
387
|
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.
|
|
388
388
|
|
|
389
|
+
## Using CommonJS files as entry points
|
|
390
|
+
|
|
391
|
+
With this plugin, you can also use CommonJS files as entry points. This means, however, that when you are bundling to an ES module, your bundle will only have a default export. If you want named exports instead, you should use an ES module entry point instead that reexports from your CommonJS entry point, e.g.
|
|
392
|
+
|
|
393
|
+
```js
|
|
394
|
+
// main.cjs, the CommonJS entry
|
|
395
|
+
exports.foo = 'foo';
|
|
396
|
+
exports.bar = 'bar';
|
|
397
|
+
|
|
398
|
+
// main.mjs, the ES module entry
|
|
399
|
+
export { foo, bar } from './main.cjs';
|
|
400
|
+
|
|
401
|
+
// rollup.config.mjs
|
|
402
|
+
export default {
|
|
403
|
+
input: 'main.mjs',
|
|
404
|
+
output: {
|
|
405
|
+
format: 'es',
|
|
406
|
+
file: 'bundle.mjs'
|
|
407
|
+
}
|
|
408
|
+
};
|
|
409
|
+
```
|
|
410
|
+
|
|
411
|
+
When bundling to CommonJS, i.e `output.format === 'cjs'`, make sure that you do not set `output.exports` to `'named'`. The default value of `'auto'` will usually work, but you can also set it explicitly to `'default'`. That makes sure that Rollup assigns the default export that was generated for your CommonJS entry point to `module.exports`, and semantics do not change.
|
|
412
|
+
|
|
389
413
|
## Using with @rollup/plugin-node-resolve
|
|
390
414
|
|
|
391
415
|
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):
|
package/dist/cjs/index.js
CHANGED
|
@@ -6,12 +6,12 @@ var path = require('path');
|
|
|
6
6
|
var pluginutils = require('@rollup/pluginutils');
|
|
7
7
|
var fs = require('fs');
|
|
8
8
|
var getCommonDir = require('commondir');
|
|
9
|
-
var
|
|
9
|
+
var fdir = require('fdir');
|
|
10
10
|
var estreeWalker = require('estree-walker');
|
|
11
11
|
var MagicString = require('magic-string');
|
|
12
12
|
var isReference = require('is-reference');
|
|
13
13
|
|
|
14
|
-
var version = "
|
|
14
|
+
var version = "28.0.0";
|
|
15
15
|
var peerDependencies = {
|
|
16
16
|
rollup: "^2.68.0||^3.0.0||^4.0.0"
|
|
17
17
|
};
|
|
@@ -123,10 +123,10 @@ function capitalize(name) {
|
|
|
123
123
|
|
|
124
124
|
function getStrictRequiresFilter({ strictRequires }) {
|
|
125
125
|
switch (strictRequires) {
|
|
126
|
-
case true:
|
|
127
|
-
return { strictRequiresFilter: () => true, detectCyclesAndConditional: false };
|
|
128
126
|
// eslint-disable-next-line no-undefined
|
|
129
127
|
case undefined:
|
|
128
|
+
case true:
|
|
129
|
+
return { strictRequiresFilter: () => true, detectCyclesAndConditional: false };
|
|
130
130
|
case 'auto':
|
|
131
131
|
case 'debug':
|
|
132
132
|
case null:
|
|
@@ -178,8 +178,13 @@ function getDynamicRequireModules(patterns, dynamicRequireRoot) {
|
|
|
178
178
|
isNegated
|
|
179
179
|
? dynamicRequireModules.delete(targetPath)
|
|
180
180
|
: dynamicRequireModules.set(targetPath, resolvedPath);
|
|
181
|
-
|
|
182
|
-
|
|
181
|
+
// eslint-disable-next-line new-cap
|
|
182
|
+
for (const path$1 of new fdir.fdir()
|
|
183
|
+
.withBasePath()
|
|
184
|
+
.withDirs()
|
|
185
|
+
.glob(isNegated ? pattern.substr(1) : pattern)
|
|
186
|
+
.crawl()
|
|
187
|
+
.sync()
|
|
183
188
|
.sort((a, b) => a.localeCompare(b, 'en'))) {
|
|
184
189
|
const resolvedPath = path.resolve(path$1);
|
|
185
190
|
const requirePath = normalizePathSlashes(resolvedPath);
|
|
@@ -331,12 +336,19 @@ const isWrappedId = (id, suffix) => id.endsWith(suffix);
|
|
|
331
336
|
const wrapId = (id, suffix) => `\0${id}${suffix}`;
|
|
332
337
|
const unwrapId = (wrappedId, suffix) => wrappedId.slice(1, -suffix.length);
|
|
333
338
|
|
|
339
|
+
// A proxy module when a module is required from non-wrapped CommonJS. Is different for ESM and CommonJS requires.
|
|
334
340
|
const PROXY_SUFFIX = '?commonjs-proxy';
|
|
341
|
+
// Indicates that a required module is wrapped commonjs and needs special handling.
|
|
335
342
|
const WRAPPED_SUFFIX = '?commonjs-wrapped';
|
|
343
|
+
// Indicates that a required module is external
|
|
336
344
|
const EXTERNAL_SUFFIX = '?commonjs-external';
|
|
345
|
+
// A helper module that contains the exports object of a module
|
|
337
346
|
const EXPORTS_SUFFIX = '?commonjs-exports';
|
|
347
|
+
// A helper module that contains the module object of a module, e.g. when module.exports is reassigned
|
|
338
348
|
const MODULE_SUFFIX = '?commonjs-module';
|
|
349
|
+
// A special proxy for CommonJS entry points
|
|
339
350
|
const ENTRY_SUFFIX = '?commonjs-entry';
|
|
351
|
+
// A proxy when wrapped ESM is required from CommonJS
|
|
340
352
|
const ES_IMPORT_SUFFIX = '?commonjs-es-import';
|
|
341
353
|
|
|
342
354
|
const DYNAMIC_MODULES_ID = '\0commonjs-dynamic-modules';
|
|
@@ -449,26 +461,28 @@ function getEntryProxy(id, defaultIsModuleExports, getModuleInfo, shebang) {
|
|
|
449
461
|
}
|
|
450
462
|
return shebang + code;
|
|
451
463
|
}
|
|
452
|
-
const result = getEsImportProxy(id, defaultIsModuleExports);
|
|
464
|
+
const result = getEsImportProxy(id, defaultIsModuleExports, true);
|
|
453
465
|
return {
|
|
454
466
|
...result,
|
|
455
467
|
code: shebang + result.code
|
|
456
468
|
};
|
|
457
469
|
}
|
|
458
470
|
|
|
459
|
-
function getEsImportProxy(id, defaultIsModuleExports) {
|
|
471
|
+
function getEsImportProxy(id, defaultIsModuleExports, moduleSideEffects) {
|
|
460
472
|
const name = getName(id);
|
|
461
473
|
const exportsName = `${name}Exports`;
|
|
462
474
|
const requireModule = `require${capitalize(name)}`;
|
|
463
475
|
let code =
|
|
464
476
|
`import { getDefaultExportFromCjs } from "${HELPERS_ID}";\n` +
|
|
465
477
|
`import { __require as ${requireModule} } from ${JSON.stringify(id)};\n` +
|
|
466
|
-
`var ${exportsName} = ${requireModule}();\n` +
|
|
478
|
+
`var ${exportsName} = ${moduleSideEffects ? '' : '/*@__PURE__*/ '}${requireModule}();\n` +
|
|
467
479
|
`export { ${exportsName} as __moduleExports };`;
|
|
468
480
|
if (defaultIsModuleExports === true) {
|
|
469
481
|
code += `\nexport { ${exportsName} as default };`;
|
|
482
|
+
} else if (defaultIsModuleExports === false) {
|
|
483
|
+
code += `\nexport default ${exportsName}.default;`;
|
|
470
484
|
} else {
|
|
471
|
-
code +=
|
|
485
|
+
code += `\nexport default /*@__PURE__*/getDefaultExportFromCjs(${exportsName});`;
|
|
472
486
|
}
|
|
473
487
|
return {
|
|
474
488
|
code,
|
|
@@ -523,11 +537,7 @@ function getResolveId(extensions, isPossibleCjsId) {
|
|
|
523
537
|
// All logic below is specific to ES imports.
|
|
524
538
|
// Also, if we do not skip this logic for requires that are resolved while
|
|
525
539
|
// transforming a commonjs file, it can easily lead to deadlocks.
|
|
526
|
-
if (
|
|
527
|
-
customOptions &&
|
|
528
|
-
customOptions['node-resolve'] &&
|
|
529
|
-
customOptions['node-resolve'].isRequire
|
|
530
|
-
) {
|
|
540
|
+
if (customOptions?.['node-resolve']?.isRequire) {
|
|
531
541
|
return null;
|
|
532
542
|
}
|
|
533
543
|
const currentlyResolvingForParent = currentlyResolving.get(importer);
|
|
@@ -811,13 +821,14 @@ function getRequireResolver(extensions, detectCyclesAndConditional, currentlyRes
|
|
|
811
821
|
// eslint-disable-next-line no-multi-assign
|
|
812
822
|
const isCommonJS = (parentMeta.isRequiredCommonJS[dependencyId] =
|
|
813
823
|
getTypeForFullyAnalyzedModule(dependencyId));
|
|
824
|
+
const isWrappedCommonJS = isCommonJS === IS_WRAPPED_COMMONJS;
|
|
814
825
|
fullyAnalyzedModules[dependencyId] = true;
|
|
815
826
|
return {
|
|
827
|
+
wrappedModuleSideEffects:
|
|
828
|
+
isWrappedCommonJS && rollupContext.getModuleInfo(dependencyId).moduleSideEffects,
|
|
816
829
|
source: sources[index].source,
|
|
817
830
|
id: allowProxy
|
|
818
|
-
?
|
|
819
|
-
? wrapId(dependencyId, WRAPPED_SUFFIX)
|
|
820
|
-
: wrapId(dependencyId, PROXY_SUFFIX)
|
|
831
|
+
? wrapId(dependencyId, isWrappedCommonJS ? WRAPPED_SUFFIX : PROXY_SUFFIX)
|
|
821
832
|
: dependencyId,
|
|
822
833
|
isCommonJS
|
|
823
834
|
};
|
|
@@ -1405,7 +1416,7 @@ function processRequireExpressions(
|
|
|
1405
1416
|
magicString
|
|
1406
1417
|
) {
|
|
1407
1418
|
const generateRequireName = getGenerateRequireName();
|
|
1408
|
-
for (const { source, id: resolvedId, isCommonJS } of requireTargets) {
|
|
1419
|
+
for (const { source, id: resolvedId, isCommonJS, wrappedModuleSideEffects } of requireTargets) {
|
|
1409
1420
|
const requires = requiresBySource[source];
|
|
1410
1421
|
const name = generateRequireName(requires);
|
|
1411
1422
|
let usesRequired = false;
|
|
@@ -1424,7 +1435,11 @@ function processRequireExpressions(
|
|
|
1424
1435
|
} else if (canConvertRequire) {
|
|
1425
1436
|
needsImport = true;
|
|
1426
1437
|
if (isCommonJS === IS_WRAPPED_COMMONJS) {
|
|
1427
|
-
magicString.overwrite(
|
|
1438
|
+
magicString.overwrite(
|
|
1439
|
+
node.start,
|
|
1440
|
+
node.end,
|
|
1441
|
+
`${wrappedModuleSideEffects ? '' : '/*@__PURE__*/ '}${name}()`
|
|
1442
|
+
);
|
|
1428
1443
|
} else if (usesReturnValue) {
|
|
1429
1444
|
usesRequired = true;
|
|
1430
1445
|
magicString.overwrite(node.start, node.end, name);
|
|
@@ -1531,6 +1546,7 @@ async function transformCommonjs(
|
|
|
1531
1546
|
const topLevelAssignments = new Set();
|
|
1532
1547
|
const topLevelDefineCompiledEsmExpressions = [];
|
|
1533
1548
|
const replacedGlobal = [];
|
|
1549
|
+
const replacedThis = [];
|
|
1534
1550
|
const replacedDynamicRequires = [];
|
|
1535
1551
|
const importedVariables = new Set();
|
|
1536
1552
|
const indentExclusionRanges = [];
|
|
@@ -1798,7 +1814,7 @@ async function transformCommonjs(
|
|
|
1798
1814
|
if (lexicalDepth === 0 && !classBodyDepth) {
|
|
1799
1815
|
uses.global = true;
|
|
1800
1816
|
if (!ignoreGlobal) {
|
|
1801
|
-
|
|
1817
|
+
replacedThis.push(node);
|
|
1802
1818
|
}
|
|
1803
1819
|
}
|
|
1804
1820
|
return;
|
|
@@ -1873,6 +1889,11 @@ async function transformCommonjs(
|
|
|
1873
1889
|
storeName: true
|
|
1874
1890
|
});
|
|
1875
1891
|
}
|
|
1892
|
+
for (const node of replacedThis) {
|
|
1893
|
+
magicString.overwrite(node.start, node.end, exportsName, {
|
|
1894
|
+
storeName: true
|
|
1895
|
+
});
|
|
1896
|
+
}
|
|
1876
1897
|
for (const node of replacedDynamicRequires) {
|
|
1877
1898
|
magicString.overwrite(
|
|
1878
1899
|
node.start,
|
|
@@ -2219,7 +2240,7 @@ function commonjs(options = {}) {
|
|
|
2219
2240
|
}
|
|
2220
2241
|
},
|
|
2221
2242
|
|
|
2222
|
-
load(id) {
|
|
2243
|
+
async load(id) {
|
|
2223
2244
|
if (id === HELPERS_ID) {
|
|
2224
2245
|
return getHelpersModule();
|
|
2225
2246
|
}
|
|
@@ -2265,7 +2286,11 @@ function commonjs(options = {}) {
|
|
|
2265
2286
|
|
|
2266
2287
|
if (isWrappedId(id, ES_IMPORT_SUFFIX)) {
|
|
2267
2288
|
const actualId = unwrapId(id, ES_IMPORT_SUFFIX);
|
|
2268
|
-
return getEsImportProxy(
|
|
2289
|
+
return getEsImportProxy(
|
|
2290
|
+
actualId,
|
|
2291
|
+
getDefaultIsModuleExports(actualId),
|
|
2292
|
+
(await this.load({ id: actualId })).moduleSideEffects
|
|
2293
|
+
);
|
|
2269
2294
|
}
|
|
2270
2295
|
|
|
2271
2296
|
if (id === DYNAMIC_MODULES_ID) {
|
package/dist/es/index.js
CHANGED
|
@@ -2,12 +2,12 @@ import { basename, extname, dirname, relative, resolve, join, sep } from 'path';
|
|
|
2
2
|
import { makeLegalIdentifier, createFilter, attachScopes, extractAssignedNames } from '@rollup/pluginutils';
|
|
3
3
|
import { existsSync, readFileSync, statSync } from 'fs';
|
|
4
4
|
import getCommonDir from 'commondir';
|
|
5
|
-
import {
|
|
5
|
+
import { fdir } from 'fdir';
|
|
6
6
|
import { walk } from 'estree-walker';
|
|
7
7
|
import MagicString from 'magic-string';
|
|
8
8
|
import isReference from 'is-reference';
|
|
9
9
|
|
|
10
|
-
var version = "
|
|
10
|
+
var version = "28.0.0";
|
|
11
11
|
var peerDependencies = {
|
|
12
12
|
rollup: "^2.68.0||^3.0.0||^4.0.0"
|
|
13
13
|
};
|
|
@@ -119,10 +119,10 @@ function capitalize(name) {
|
|
|
119
119
|
|
|
120
120
|
function getStrictRequiresFilter({ strictRequires }) {
|
|
121
121
|
switch (strictRequires) {
|
|
122
|
-
case true:
|
|
123
|
-
return { strictRequiresFilter: () => true, detectCyclesAndConditional: false };
|
|
124
122
|
// eslint-disable-next-line no-undefined
|
|
125
123
|
case undefined:
|
|
124
|
+
case true:
|
|
125
|
+
return { strictRequiresFilter: () => true, detectCyclesAndConditional: false };
|
|
126
126
|
case 'auto':
|
|
127
127
|
case 'debug':
|
|
128
128
|
case null:
|
|
@@ -174,8 +174,13 @@ function getDynamicRequireModules(patterns, dynamicRequireRoot) {
|
|
|
174
174
|
isNegated
|
|
175
175
|
? dynamicRequireModules.delete(targetPath)
|
|
176
176
|
: dynamicRequireModules.set(targetPath, resolvedPath);
|
|
177
|
-
|
|
178
|
-
|
|
177
|
+
// eslint-disable-next-line new-cap
|
|
178
|
+
for (const path of new fdir()
|
|
179
|
+
.withBasePath()
|
|
180
|
+
.withDirs()
|
|
181
|
+
.glob(isNegated ? pattern.substr(1) : pattern)
|
|
182
|
+
.crawl()
|
|
183
|
+
.sync()
|
|
179
184
|
.sort((a, b) => a.localeCompare(b, 'en'))) {
|
|
180
185
|
const resolvedPath = resolve(path);
|
|
181
186
|
const requirePath = normalizePathSlashes(resolvedPath);
|
|
@@ -327,12 +332,19 @@ const isWrappedId = (id, suffix) => id.endsWith(suffix);
|
|
|
327
332
|
const wrapId = (id, suffix) => `\0${id}${suffix}`;
|
|
328
333
|
const unwrapId = (wrappedId, suffix) => wrappedId.slice(1, -suffix.length);
|
|
329
334
|
|
|
335
|
+
// A proxy module when a module is required from non-wrapped CommonJS. Is different for ESM and CommonJS requires.
|
|
330
336
|
const PROXY_SUFFIX = '?commonjs-proxy';
|
|
337
|
+
// Indicates that a required module is wrapped commonjs and needs special handling.
|
|
331
338
|
const WRAPPED_SUFFIX = '?commonjs-wrapped';
|
|
339
|
+
// Indicates that a required module is external
|
|
332
340
|
const EXTERNAL_SUFFIX = '?commonjs-external';
|
|
341
|
+
// A helper module that contains the exports object of a module
|
|
333
342
|
const EXPORTS_SUFFIX = '?commonjs-exports';
|
|
343
|
+
// A helper module that contains the module object of a module, e.g. when module.exports is reassigned
|
|
334
344
|
const MODULE_SUFFIX = '?commonjs-module';
|
|
345
|
+
// A special proxy for CommonJS entry points
|
|
335
346
|
const ENTRY_SUFFIX = '?commonjs-entry';
|
|
347
|
+
// A proxy when wrapped ESM is required from CommonJS
|
|
336
348
|
const ES_IMPORT_SUFFIX = '?commonjs-es-import';
|
|
337
349
|
|
|
338
350
|
const DYNAMIC_MODULES_ID = '\0commonjs-dynamic-modules';
|
|
@@ -445,26 +457,28 @@ function getEntryProxy(id, defaultIsModuleExports, getModuleInfo, shebang) {
|
|
|
445
457
|
}
|
|
446
458
|
return shebang + code;
|
|
447
459
|
}
|
|
448
|
-
const result = getEsImportProxy(id, defaultIsModuleExports);
|
|
460
|
+
const result = getEsImportProxy(id, defaultIsModuleExports, true);
|
|
449
461
|
return {
|
|
450
462
|
...result,
|
|
451
463
|
code: shebang + result.code
|
|
452
464
|
};
|
|
453
465
|
}
|
|
454
466
|
|
|
455
|
-
function getEsImportProxy(id, defaultIsModuleExports) {
|
|
467
|
+
function getEsImportProxy(id, defaultIsModuleExports, moduleSideEffects) {
|
|
456
468
|
const name = getName(id);
|
|
457
469
|
const exportsName = `${name}Exports`;
|
|
458
470
|
const requireModule = `require${capitalize(name)}`;
|
|
459
471
|
let code =
|
|
460
472
|
`import { getDefaultExportFromCjs } from "${HELPERS_ID}";\n` +
|
|
461
473
|
`import { __require as ${requireModule} } from ${JSON.stringify(id)};\n` +
|
|
462
|
-
`var ${exportsName} = ${requireModule}();\n` +
|
|
474
|
+
`var ${exportsName} = ${moduleSideEffects ? '' : '/*@__PURE__*/ '}${requireModule}();\n` +
|
|
463
475
|
`export { ${exportsName} as __moduleExports };`;
|
|
464
476
|
if (defaultIsModuleExports === true) {
|
|
465
477
|
code += `\nexport { ${exportsName} as default };`;
|
|
478
|
+
} else if (defaultIsModuleExports === false) {
|
|
479
|
+
code += `\nexport default ${exportsName}.default;`;
|
|
466
480
|
} else {
|
|
467
|
-
code +=
|
|
481
|
+
code += `\nexport default /*@__PURE__*/getDefaultExportFromCjs(${exportsName});`;
|
|
468
482
|
}
|
|
469
483
|
return {
|
|
470
484
|
code,
|
|
@@ -519,11 +533,7 @@ function getResolveId(extensions, isPossibleCjsId) {
|
|
|
519
533
|
// All logic below is specific to ES imports.
|
|
520
534
|
// Also, if we do not skip this logic for requires that are resolved while
|
|
521
535
|
// transforming a commonjs file, it can easily lead to deadlocks.
|
|
522
|
-
if (
|
|
523
|
-
customOptions &&
|
|
524
|
-
customOptions['node-resolve'] &&
|
|
525
|
-
customOptions['node-resolve'].isRequire
|
|
526
|
-
) {
|
|
536
|
+
if (customOptions?.['node-resolve']?.isRequire) {
|
|
527
537
|
return null;
|
|
528
538
|
}
|
|
529
539
|
const currentlyResolvingForParent = currentlyResolving.get(importer);
|
|
@@ -807,13 +817,14 @@ function getRequireResolver(extensions, detectCyclesAndConditional, currentlyRes
|
|
|
807
817
|
// eslint-disable-next-line no-multi-assign
|
|
808
818
|
const isCommonJS = (parentMeta.isRequiredCommonJS[dependencyId] =
|
|
809
819
|
getTypeForFullyAnalyzedModule(dependencyId));
|
|
820
|
+
const isWrappedCommonJS = isCommonJS === IS_WRAPPED_COMMONJS;
|
|
810
821
|
fullyAnalyzedModules[dependencyId] = true;
|
|
811
822
|
return {
|
|
823
|
+
wrappedModuleSideEffects:
|
|
824
|
+
isWrappedCommonJS && rollupContext.getModuleInfo(dependencyId).moduleSideEffects,
|
|
812
825
|
source: sources[index].source,
|
|
813
826
|
id: allowProxy
|
|
814
|
-
?
|
|
815
|
-
? wrapId(dependencyId, WRAPPED_SUFFIX)
|
|
816
|
-
: wrapId(dependencyId, PROXY_SUFFIX)
|
|
827
|
+
? wrapId(dependencyId, isWrappedCommonJS ? WRAPPED_SUFFIX : PROXY_SUFFIX)
|
|
817
828
|
: dependencyId,
|
|
818
829
|
isCommonJS
|
|
819
830
|
};
|
|
@@ -1401,7 +1412,7 @@ function processRequireExpressions(
|
|
|
1401
1412
|
magicString
|
|
1402
1413
|
) {
|
|
1403
1414
|
const generateRequireName = getGenerateRequireName();
|
|
1404
|
-
for (const { source, id: resolvedId, isCommonJS } of requireTargets) {
|
|
1415
|
+
for (const { source, id: resolvedId, isCommonJS, wrappedModuleSideEffects } of requireTargets) {
|
|
1405
1416
|
const requires = requiresBySource[source];
|
|
1406
1417
|
const name = generateRequireName(requires);
|
|
1407
1418
|
let usesRequired = false;
|
|
@@ -1420,7 +1431,11 @@ function processRequireExpressions(
|
|
|
1420
1431
|
} else if (canConvertRequire) {
|
|
1421
1432
|
needsImport = true;
|
|
1422
1433
|
if (isCommonJS === IS_WRAPPED_COMMONJS) {
|
|
1423
|
-
magicString.overwrite(
|
|
1434
|
+
magicString.overwrite(
|
|
1435
|
+
node.start,
|
|
1436
|
+
node.end,
|
|
1437
|
+
`${wrappedModuleSideEffects ? '' : '/*@__PURE__*/ '}${name}()`
|
|
1438
|
+
);
|
|
1424
1439
|
} else if (usesReturnValue) {
|
|
1425
1440
|
usesRequired = true;
|
|
1426
1441
|
magicString.overwrite(node.start, node.end, name);
|
|
@@ -1527,6 +1542,7 @@ async function transformCommonjs(
|
|
|
1527
1542
|
const topLevelAssignments = new Set();
|
|
1528
1543
|
const topLevelDefineCompiledEsmExpressions = [];
|
|
1529
1544
|
const replacedGlobal = [];
|
|
1545
|
+
const replacedThis = [];
|
|
1530
1546
|
const replacedDynamicRequires = [];
|
|
1531
1547
|
const importedVariables = new Set();
|
|
1532
1548
|
const indentExclusionRanges = [];
|
|
@@ -1794,7 +1810,7 @@ async function transformCommonjs(
|
|
|
1794
1810
|
if (lexicalDepth === 0 && !classBodyDepth) {
|
|
1795
1811
|
uses.global = true;
|
|
1796
1812
|
if (!ignoreGlobal) {
|
|
1797
|
-
|
|
1813
|
+
replacedThis.push(node);
|
|
1798
1814
|
}
|
|
1799
1815
|
}
|
|
1800
1816
|
return;
|
|
@@ -1869,6 +1885,11 @@ async function transformCommonjs(
|
|
|
1869
1885
|
storeName: true
|
|
1870
1886
|
});
|
|
1871
1887
|
}
|
|
1888
|
+
for (const node of replacedThis) {
|
|
1889
|
+
magicString.overwrite(node.start, node.end, exportsName, {
|
|
1890
|
+
storeName: true
|
|
1891
|
+
});
|
|
1892
|
+
}
|
|
1872
1893
|
for (const node of replacedDynamicRequires) {
|
|
1873
1894
|
magicString.overwrite(
|
|
1874
1895
|
node.start,
|
|
@@ -2215,7 +2236,7 @@ function commonjs(options = {}) {
|
|
|
2215
2236
|
}
|
|
2216
2237
|
},
|
|
2217
2238
|
|
|
2218
|
-
load(id) {
|
|
2239
|
+
async load(id) {
|
|
2219
2240
|
if (id === HELPERS_ID) {
|
|
2220
2241
|
return getHelpersModule();
|
|
2221
2242
|
}
|
|
@@ -2261,7 +2282,11 @@ function commonjs(options = {}) {
|
|
|
2261
2282
|
|
|
2262
2283
|
if (isWrappedId(id, ES_IMPORT_SUFFIX)) {
|
|
2263
2284
|
const actualId = unwrapId(id, ES_IMPORT_SUFFIX);
|
|
2264
|
-
return getEsImportProxy(
|
|
2285
|
+
return getEsImportProxy(
|
|
2286
|
+
actualId,
|
|
2287
|
+
getDefaultIsModuleExports(actualId),
|
|
2288
|
+
(await this.load({ id: actualId })).moduleSideEffects
|
|
2289
|
+
);
|
|
2265
2290
|
}
|
|
2266
2291
|
|
|
2267
2292
|
if (id === DYNAMIC_MODULES_ID) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rollup/plugin-commonjs",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "28.0.0",
|
|
4
4
|
"publishConfig": {
|
|
5
5
|
"access": "public"
|
|
6
6
|
},
|
|
@@ -50,9 +50,10 @@
|
|
|
50
50
|
"@rollup/pluginutils": "^5.0.1",
|
|
51
51
|
"commondir": "^1.0.1",
|
|
52
52
|
"estree-walker": "^2.0.2",
|
|
53
|
-
"
|
|
53
|
+
"fdir": "^6.1.1",
|
|
54
54
|
"is-reference": "1.2.1",
|
|
55
|
-
"magic-string": "^0.30.3"
|
|
55
|
+
"magic-string": "^0.30.3",
|
|
56
|
+
"picomatch": "^2.3.1"
|
|
56
57
|
},
|
|
57
58
|
"devDependencies": {
|
|
58
59
|
"@rollup/plugin-json": "^5.0.0",
|