@rollup/plugin-commonjs 18.1.0 → 20.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 +32 -0
- package/README.md +3 -1
- package/dist/index.es.js +433 -331
- package/dist/index.es.js.map +1 -1
- package/dist/index.js +432 -330
- package/dist/index.js.map +1 -1
- package/package.json +10 -9
package/dist/index.js
CHANGED
|
@@ -18,7 +18,7 @@ var MagicString__default = /*#__PURE__*/_interopDefaultLegacy(MagicString);
|
|
|
18
18
|
var isReference__default = /*#__PURE__*/_interopDefaultLegacy(isReference);
|
|
19
19
|
|
|
20
20
|
var peerDependencies = {
|
|
21
|
-
rollup: "^2.
|
|
21
|
+
rollup: "^2.38.3"
|
|
22
22
|
};
|
|
23
23
|
|
|
24
24
|
function tryParse(parse, code, id) {
|
|
@@ -92,6 +92,8 @@ const unwrapId = (wrappedId, suffix) => wrappedId.slice(1, -suffix.length);
|
|
|
92
92
|
const PROXY_SUFFIX = '?commonjs-proxy';
|
|
93
93
|
const REQUIRE_SUFFIX = '?commonjs-require';
|
|
94
94
|
const EXTERNAL_SUFFIX = '?commonjs-external';
|
|
95
|
+
const EXPORTS_SUFFIX = '?commonjs-exports';
|
|
96
|
+
const MODULE_SUFFIX = '?commonjs-module';
|
|
95
97
|
|
|
96
98
|
const DYNAMIC_REGISTER_SUFFIX = '?commonjs-dynamic-register';
|
|
97
99
|
const DYNAMIC_JSON_PREFIX = '\0commonjs-dynamic-json:';
|
|
@@ -138,33 +140,38 @@ export function getAugmentedNamespace(n) {
|
|
|
138
140
|
const FAILED_REQUIRE_ERROR = `throw new Error('Could not dynamically require "' + path + '". Please configure the dynamicRequireTargets or/and ignoreDynamicRequires option of @rollup/plugin-commonjs appropriately for this require call to work.');`;
|
|
139
141
|
|
|
140
142
|
const HELPER_NON_DYNAMIC = `
|
|
141
|
-
export function createCommonjsModule(fn) {
|
|
142
|
-
var module = { exports: {} }
|
|
143
|
-
return fn(module, module.exports), module.exports;
|
|
144
|
-
}
|
|
145
|
-
|
|
146
143
|
export function commonjsRequire (path) {
|
|
147
144
|
${FAILED_REQUIRE_ERROR}
|
|
148
145
|
}
|
|
149
146
|
`;
|
|
150
147
|
|
|
151
148
|
const getDynamicHelpers = (ignoreDynamicRequires) => `
|
|
152
|
-
export function
|
|
153
|
-
return
|
|
154
|
-
path:
|
|
149
|
+
export function createModule(modulePath) {
|
|
150
|
+
return {
|
|
151
|
+
path: modulePath,
|
|
155
152
|
exports: {},
|
|
156
153
|
require: function (path, base) {
|
|
157
|
-
return commonjsRequire(path,
|
|
154
|
+
return commonjsRequire(path, base == null ? modulePath : base);
|
|
158
155
|
}
|
|
159
|
-
}
|
|
156
|
+
};
|
|
160
157
|
}
|
|
161
158
|
|
|
162
159
|
export function commonjsRegister (path, loader) {
|
|
163
160
|
DYNAMIC_REQUIRE_LOADERS[path] = loader;
|
|
164
161
|
}
|
|
165
162
|
|
|
163
|
+
export function commonjsRegisterOrShort (path, to) {
|
|
164
|
+
const resolvedPath = commonjsResolveImpl(path, null, true);
|
|
165
|
+
if (resolvedPath !== null && DYNAMIC_REQUIRE_CACHE[resolvedPath]) {
|
|
166
|
+
DYNAMIC_REQUIRE_CACHE[path] = DYNAMIC_REQUIRE_CACHE[resolvedPath];
|
|
167
|
+
} else {
|
|
168
|
+
DYNAMIC_REQUIRE_SHORTS[path] = to;
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
|
|
166
172
|
const DYNAMIC_REQUIRE_LOADERS = Object.create(null);
|
|
167
173
|
const DYNAMIC_REQUIRE_CACHE = Object.create(null);
|
|
174
|
+
const DYNAMIC_REQUIRE_SHORTS = Object.create(null);
|
|
168
175
|
const DEFAULT_PARENT_MODULE = {
|
|
169
176
|
id: '<' + 'rollup>', exports: {}, parent: undefined, filename: null, loaded: false, children: [], paths: []
|
|
170
177
|
};
|
|
@@ -269,10 +276,13 @@ export function commonjsResolveImpl (path, originalModuleDir, testCache) {
|
|
|
269
276
|
const resolvedPath = relPath + CHECKED_EXTENSIONS[extensionIndex];
|
|
270
277
|
if (DYNAMIC_REQUIRE_CACHE[resolvedPath]) {
|
|
271
278
|
return resolvedPath;
|
|
272
|
-
}
|
|
279
|
+
}
|
|
280
|
+
if (DYNAMIC_REQUIRE_SHORTS[resolvedPath]) {
|
|
281
|
+
return resolvedPath;
|
|
282
|
+
}
|
|
273
283
|
if (DYNAMIC_REQUIRE_LOADERS[resolvedPath]) {
|
|
274
284
|
return resolvedPath;
|
|
275
|
-
}
|
|
285
|
+
}
|
|
276
286
|
}
|
|
277
287
|
if (!shouldTryNodeModules) break;
|
|
278
288
|
const nextDir = normalize(originalModuleDir + '/..');
|
|
@@ -291,10 +301,17 @@ export function commonjsResolve (path, originalModuleDir) {
|
|
|
291
301
|
}
|
|
292
302
|
|
|
293
303
|
export function commonjsRequire (path, originalModuleDir) {
|
|
294
|
-
|
|
304
|
+
let resolvedPath = commonjsResolveImpl(path, originalModuleDir, true);
|
|
295
305
|
if (resolvedPath !== null) {
|
|
296
306
|
let cachedModule = DYNAMIC_REQUIRE_CACHE[resolvedPath];
|
|
297
307
|
if (cachedModule) return cachedModule.exports;
|
|
308
|
+
let shortTo = DYNAMIC_REQUIRE_SHORTS[resolvedPath];
|
|
309
|
+
if (shortTo) {
|
|
310
|
+
cachedModule = DYNAMIC_REQUIRE_CACHE[shortTo];
|
|
311
|
+
if (cachedModule)
|
|
312
|
+
return cachedModule.exports;
|
|
313
|
+
resolvedPath = commonjsResolveImpl(shortTo, null, true);
|
|
314
|
+
}
|
|
298
315
|
const loader = DYNAMIC_REQUIRE_LOADERS[resolvedPath];
|
|
299
316
|
if (loader) {
|
|
300
317
|
DYNAMIC_REQUIRE_CACHE[resolvedPath] = cachedModule = {
|
|
@@ -335,16 +352,20 @@ function getHelpersModule(isDynamicRequireModulesEnabled, ignoreDynamicRequires)
|
|
|
335
352
|
|
|
336
353
|
/* eslint-disable import/prefer-default-export */
|
|
337
354
|
|
|
338
|
-
function deconflict(
|
|
355
|
+
function deconflict(scopes, globals, identifier) {
|
|
339
356
|
let i = 1;
|
|
340
357
|
let deconflicted = pluginutils.makeLegalIdentifier(identifier);
|
|
358
|
+
const hasConflicts = () =>
|
|
359
|
+
scopes.some((scope) => scope.contains(deconflicted)) || globals.has(deconflicted);
|
|
341
360
|
|
|
342
|
-
while (
|
|
361
|
+
while (hasConflicts()) {
|
|
343
362
|
deconflicted = pluginutils.makeLegalIdentifier(`${identifier}_${i}`);
|
|
344
363
|
i += 1;
|
|
345
364
|
}
|
|
346
|
-
|
|
347
|
-
scope
|
|
365
|
+
|
|
366
|
+
for (const scope of scopes) {
|
|
367
|
+
scope.declarations[deconflicted] = true;
|
|
368
|
+
}
|
|
348
369
|
|
|
349
370
|
return deconflicted;
|
|
350
371
|
}
|
|
@@ -354,8 +375,7 @@ function getName(id) {
|
|
|
354
375
|
if (name !== 'index') {
|
|
355
376
|
return name;
|
|
356
377
|
}
|
|
357
|
-
|
|
358
|
-
return pluginutils.makeLegalIdentifier(segments[segments.length - 1]);
|
|
378
|
+
return pluginutils.makeLegalIdentifier(path.basename(path.dirname(id)));
|
|
359
379
|
}
|
|
360
380
|
|
|
361
381
|
function normalizePathSlashes(path) {
|
|
@@ -387,15 +407,13 @@ function getPackageEntryPoint(dirPath) {
|
|
|
387
407
|
}
|
|
388
408
|
|
|
389
409
|
function getDynamicPackagesModule(dynamicRequireModuleDirPaths, commonDir) {
|
|
390
|
-
let code = `const
|
|
410
|
+
let code = `const commonjsRegisterOrShort = require('${HELPERS_ID}?commonjsRegisterOrShort');`;
|
|
391
411
|
for (const dir of dynamicRequireModuleDirPaths) {
|
|
392
412
|
const entryPoint = getPackageEntryPoint(dir);
|
|
393
413
|
|
|
394
|
-
code += `\
|
|
414
|
+
code += `\ncommonjsRegisterOrShort(${JSON.stringify(
|
|
395
415
|
getVirtualPathForDynamicRequirePath(dir, commonDir)
|
|
396
|
-
)},
|
|
397
|
-
module.exports = require(${JSON.stringify(normalizePathSlashes(path.join(dir, entryPoint)))});
|
|
398
|
-
});`;
|
|
416
|
+
)}, ${JSON.stringify(getVirtualPathForDynamicRequirePath(path.join(dir, entryPoint), commonDir))});`;
|
|
399
417
|
}
|
|
400
418
|
return code;
|
|
401
419
|
}
|
|
@@ -406,28 +424,18 @@ function getDynamicPackagesEntryIntro(
|
|
|
406
424
|
) {
|
|
407
425
|
let dynamicImports = Array.from(
|
|
408
426
|
dynamicRequireModuleSet,
|
|
409
|
-
(dynamicId) => `require(${JSON.stringify(
|
|
427
|
+
(dynamicId) => `require(${JSON.stringify(wrapId(dynamicId, DYNAMIC_REGISTER_SUFFIX))});`
|
|
410
428
|
).join('\n');
|
|
411
429
|
|
|
412
430
|
if (dynamicRequireModuleDirPaths.length) {
|
|
413
|
-
dynamicImports += `require(${JSON.stringify(
|
|
431
|
+
dynamicImports += `require(${JSON.stringify(
|
|
432
|
+
wrapId(DYNAMIC_PACKAGES_ID, DYNAMIC_REGISTER_SUFFIX)
|
|
433
|
+
)});`;
|
|
414
434
|
}
|
|
415
435
|
|
|
416
436
|
return dynamicImports;
|
|
417
437
|
}
|
|
418
438
|
|
|
419
|
-
function wrapModuleRegisterProxy(id) {
|
|
420
|
-
return wrapId(id, DYNAMIC_REGISTER_SUFFIX);
|
|
421
|
-
}
|
|
422
|
-
|
|
423
|
-
function unwrapModuleRegisterProxy(id) {
|
|
424
|
-
return unwrapId(id, DYNAMIC_REGISTER_SUFFIX);
|
|
425
|
-
}
|
|
426
|
-
|
|
427
|
-
function isModuleRegisterProxy(id) {
|
|
428
|
-
return isWrappedId(id, DYNAMIC_REGISTER_SUFFIX);
|
|
429
|
-
}
|
|
430
|
-
|
|
431
439
|
function isDynamicModuleImport(id, dynamicRequireModuleSet) {
|
|
432
440
|
const normalizedPath = normalizePathSlashes(id);
|
|
433
441
|
return dynamicRequireModuleSet.has(normalizedPath) && !normalizedPath.endsWith('.json');
|
|
@@ -460,37 +468,37 @@ function getDynamicRequirePaths(patterns) {
|
|
|
460
468
|
return { dynamicRequireModuleSet, dynamicRequireModuleDirPaths };
|
|
461
469
|
}
|
|
462
470
|
|
|
463
|
-
function
|
|
464
|
-
let
|
|
465
|
-
if (
|
|
471
|
+
function getCommonJSMetaPromise(commonJSMetaPromises, id) {
|
|
472
|
+
let commonJSMetaPromise = commonJSMetaPromises.get(id);
|
|
473
|
+
if (commonJSMetaPromise) return commonJSMetaPromise.promise;
|
|
466
474
|
|
|
467
475
|
const promise = new Promise((resolve) => {
|
|
468
|
-
|
|
476
|
+
commonJSMetaPromise = {
|
|
469
477
|
resolve,
|
|
470
478
|
promise: null
|
|
471
479
|
};
|
|
472
|
-
|
|
480
|
+
commonJSMetaPromises.set(id, commonJSMetaPromise);
|
|
473
481
|
});
|
|
474
|
-
|
|
482
|
+
commonJSMetaPromise.promise = promise;
|
|
475
483
|
|
|
476
484
|
return promise;
|
|
477
485
|
}
|
|
478
486
|
|
|
479
|
-
function
|
|
480
|
-
const
|
|
481
|
-
if (
|
|
482
|
-
if (
|
|
483
|
-
|
|
484
|
-
|
|
487
|
+
function setCommonJSMetaPromise(commonJSMetaPromises, id, commonjsMeta) {
|
|
488
|
+
const commonJSMetaPromise = commonJSMetaPromises.get(id);
|
|
489
|
+
if (commonJSMetaPromise) {
|
|
490
|
+
if (commonJSMetaPromise.resolve) {
|
|
491
|
+
commonJSMetaPromise.resolve(commonjsMeta);
|
|
492
|
+
commonJSMetaPromise.resolve = null;
|
|
485
493
|
}
|
|
486
494
|
} else {
|
|
487
|
-
|
|
495
|
+
commonJSMetaPromises.set(id, { promise: Promise.resolve(commonjsMeta), resolve: null });
|
|
488
496
|
}
|
|
489
497
|
}
|
|
490
498
|
|
|
491
499
|
// e.g. id === "commonjsHelpers?commonjsRegister"
|
|
492
500
|
function getSpecificHelperProxy(id) {
|
|
493
|
-
return `export {${id.split('?')[1]} as default} from
|
|
501
|
+
return `export {${id.split('?')[1]} as default} from "${HELPERS_ID}";`;
|
|
494
502
|
}
|
|
495
503
|
|
|
496
504
|
function getUnknownRequireProxy(id, requireReturnsDefault) {
|
|
@@ -531,16 +539,16 @@ async function getStaticRequireProxy(
|
|
|
531
539
|
requireReturnsDefault,
|
|
532
540
|
esModulesWithDefaultExport,
|
|
533
541
|
esModulesWithNamedExports,
|
|
534
|
-
|
|
542
|
+
commonJsMetaPromises
|
|
535
543
|
) {
|
|
536
544
|
const name = getName(id);
|
|
537
|
-
const
|
|
538
|
-
if (
|
|
539
|
-
return `
|
|
540
|
-
} else if (
|
|
545
|
+
const commonjsMeta = await getCommonJSMetaPromise(commonJsMetaPromises, id);
|
|
546
|
+
if (commonjsMeta && commonjsMeta.isCommonJS) {
|
|
547
|
+
return `export { __moduleExports as default } from ${JSON.stringify(id)};`;
|
|
548
|
+
} else if (commonjsMeta === null) {
|
|
541
549
|
return getUnknownRequireProxy(id, requireReturnsDefault);
|
|
542
550
|
} else if (!requireReturnsDefault) {
|
|
543
|
-
return `import {getAugmentedNamespace} from "${HELPERS_ID}"; import * as ${name} from ${JSON.stringify(
|
|
551
|
+
return `import { getAugmentedNamespace } from "${HELPERS_ID}"; import * as ${name} from ${JSON.stringify(
|
|
544
552
|
id
|
|
545
553
|
)}; export default /*@__PURE__*/getAugmentedNamespace(${name});`;
|
|
546
554
|
} else if (
|
|
@@ -551,7 +559,7 @@ async function getStaticRequireProxy(
|
|
|
551
559
|
) {
|
|
552
560
|
return `import * as ${name} from ${JSON.stringify(id)}; export default ${name};`;
|
|
553
561
|
}
|
|
554
|
-
return `export {default} from ${JSON.stringify(id)};`;
|
|
562
|
+
return `export { default } from ${JSON.stringify(id)};`;
|
|
555
563
|
}
|
|
556
564
|
|
|
557
565
|
/* eslint-disable no-param-reassign, no-undefined */
|
|
@@ -588,12 +596,17 @@ function getResolveId(extensions) {
|
|
|
588
596
|
}
|
|
589
597
|
|
|
590
598
|
return function resolveId(importee, rawImporter) {
|
|
599
|
+
if (isWrappedId(importee, MODULE_SUFFIX) || isWrappedId(importee, EXPORTS_SUFFIX)) {
|
|
600
|
+
return importee;
|
|
601
|
+
}
|
|
602
|
+
|
|
591
603
|
const importer =
|
|
592
|
-
rawImporter &&
|
|
593
|
-
?
|
|
604
|
+
rawImporter && isWrappedId(rawImporter, DYNAMIC_REGISTER_SUFFIX)
|
|
605
|
+
? unwrapId(rawImporter, DYNAMIC_REGISTER_SUFFIX)
|
|
594
606
|
: rawImporter;
|
|
595
607
|
|
|
596
|
-
//
|
|
608
|
+
// Except for exports, proxies are only importing resolved ids,
|
|
609
|
+
// no need to resolve again
|
|
597
610
|
if (importer && isWrappedId(importer, PROXY_SUFFIX)) {
|
|
598
611
|
return importee;
|
|
599
612
|
}
|
|
@@ -607,9 +620,9 @@ function getResolveId(extensions) {
|
|
|
607
620
|
} else if (isRequiredModule) {
|
|
608
621
|
importee = unwrapId(importee, REQUIRE_SUFFIX);
|
|
609
622
|
|
|
610
|
-
isModuleRegistration =
|
|
623
|
+
isModuleRegistration = isWrappedId(importee, DYNAMIC_REGISTER_SUFFIX);
|
|
611
624
|
if (isModuleRegistration) {
|
|
612
|
-
importee =
|
|
625
|
+
importee = unwrapId(importee, DYNAMIC_REGISTER_SUFFIX);
|
|
613
626
|
}
|
|
614
627
|
}
|
|
615
628
|
|
|
@@ -636,7 +649,7 @@ function getResolveId(extensions) {
|
|
|
636
649
|
resolved.id = wrapId(resolved.id, resolved.external ? EXTERNAL_SUFFIX : PROXY_SUFFIX);
|
|
637
650
|
resolved.external = false;
|
|
638
651
|
} else if (resolved && isModuleRegistration) {
|
|
639
|
-
resolved.id =
|
|
652
|
+
resolved.id = wrapId(resolved.id, DYNAMIC_REGISTER_SUFFIX);
|
|
640
653
|
} else if (!resolved && (isProxyModule || isRequiredModule)) {
|
|
641
654
|
return { id: wrapId(importee, EXTERNAL_SUFFIX), external: false };
|
|
642
655
|
}
|
|
@@ -736,8 +749,6 @@ function isDefineCompiledEsm(node) {
|
|
|
736
749
|
}
|
|
737
750
|
|
|
738
751
|
function getDefinePropertyCallName(node, targetName) {
|
|
739
|
-
const targetNames = targetName.split('.');
|
|
740
|
-
|
|
741
752
|
const {
|
|
742
753
|
callee: { object, property }
|
|
743
754
|
} = node;
|
|
@@ -745,6 +756,7 @@ function getDefinePropertyCallName(node, targetName) {
|
|
|
745
756
|
if (!property || property.type !== 'Identifier' || property.name !== 'defineProperty') return;
|
|
746
757
|
if (node.arguments.length !== 3) return;
|
|
747
758
|
|
|
759
|
+
const targetNames = targetName.split('.');
|
|
748
760
|
const [target, key, value] = node.arguments;
|
|
749
761
|
if (targetNames.length === 1) {
|
|
750
762
|
if (target.type !== 'Identifier' || target.name !== targetNames[0]) {
|
|
@@ -771,128 +783,199 @@ function getDefinePropertyCallName(node, targetName) {
|
|
|
771
783
|
return { key: key.value, value: valueProperty.value };
|
|
772
784
|
}
|
|
773
785
|
|
|
774
|
-
function isLocallyShadowed(name, scope) {
|
|
775
|
-
while (scope.parent) {
|
|
776
|
-
if (scope.declarations[name]) {
|
|
777
|
-
return true;
|
|
778
|
-
}
|
|
779
|
-
// eslint-disable-next-line no-param-reassign
|
|
780
|
-
scope = scope.parent;
|
|
781
|
-
}
|
|
782
|
-
return false;
|
|
783
|
-
}
|
|
784
|
-
|
|
785
786
|
function isShorthandProperty(parent) {
|
|
786
787
|
return parent && parent.type === 'Property' && parent.shorthand;
|
|
787
788
|
}
|
|
788
789
|
|
|
789
|
-
function
|
|
790
|
-
|
|
790
|
+
function hasDefineEsmProperty(node) {
|
|
791
|
+
return node.properties.some((property) => {
|
|
792
|
+
if (
|
|
793
|
+
property.type === 'Property' &&
|
|
794
|
+
property.key.type === 'Identifier' &&
|
|
795
|
+
property.key.name === '__esModule' &&
|
|
796
|
+
isTruthy(property.value)
|
|
797
|
+
) {
|
|
798
|
+
return true;
|
|
799
|
+
}
|
|
800
|
+
return false;
|
|
801
|
+
});
|
|
802
|
+
}
|
|
791
803
|
|
|
804
|
+
function wrapCode(magicString, uses, moduleName, exportsName) {
|
|
805
|
+
const args = [];
|
|
806
|
+
const passedArgs = [];
|
|
807
|
+
if (uses.module) {
|
|
808
|
+
args.push('module');
|
|
809
|
+
passedArgs.push(moduleName);
|
|
810
|
+
}
|
|
811
|
+
if (uses.exports) {
|
|
812
|
+
args.push('exports');
|
|
813
|
+
passedArgs.push(exportsName);
|
|
814
|
+
}
|
|
792
815
|
magicString
|
|
793
816
|
.trim()
|
|
794
|
-
.prepend(`
|
|
795
|
-
.append(
|
|
796
|
-
`\n}${virtualDynamicRequirePath ? `, ${JSON.stringify(virtualDynamicRequirePath)}` : ''});`
|
|
797
|
-
);
|
|
817
|
+
.prepend(`(function (${args.join(', ')}) {\n`)
|
|
818
|
+
.append(`\n}(${passedArgs.join(', ')}));`);
|
|
798
819
|
}
|
|
799
820
|
|
|
800
821
|
function rewriteExportsAndGetExportsBlock(
|
|
801
822
|
magicString,
|
|
802
823
|
moduleName,
|
|
824
|
+
exportsName,
|
|
803
825
|
wrapped,
|
|
804
|
-
|
|
805
|
-
|
|
826
|
+
moduleExportsAssignments,
|
|
827
|
+
firstTopLevelModuleExportsAssignment,
|
|
828
|
+
exportsAssignmentsByName,
|
|
829
|
+
topLevelAssignments,
|
|
806
830
|
defineCompiledEsmExpressions,
|
|
807
|
-
|
|
808
|
-
isRestorableCompiledEsm,
|
|
831
|
+
deconflictedExportNames,
|
|
809
832
|
code,
|
|
810
|
-
uses,
|
|
811
833
|
HELPERS_NAME,
|
|
834
|
+
exportMode,
|
|
835
|
+
detectWrappedDefault,
|
|
812
836
|
defaultIsModuleExports
|
|
813
837
|
) {
|
|
814
|
-
const
|
|
815
|
-
const
|
|
816
|
-
|
|
838
|
+
const exports = [];
|
|
839
|
+
const exportDeclarations = [];
|
|
840
|
+
|
|
841
|
+
if (exportMode === 'replace') {
|
|
842
|
+
getExportsForReplacedModuleExports(
|
|
843
|
+
magicString,
|
|
844
|
+
exports,
|
|
845
|
+
exportDeclarations,
|
|
846
|
+
moduleExportsAssignments,
|
|
847
|
+
firstTopLevelModuleExportsAssignment,
|
|
848
|
+
exportsName
|
|
849
|
+
);
|
|
850
|
+
} else {
|
|
851
|
+
exports.push(`${exportsName} as __moduleExports`);
|
|
852
|
+
if (wrapped) {
|
|
853
|
+
getExportsWhenWrapping(
|
|
854
|
+
exportDeclarations,
|
|
855
|
+
exportsName,
|
|
856
|
+
detectWrappedDefault,
|
|
857
|
+
HELPERS_NAME,
|
|
858
|
+
defaultIsModuleExports
|
|
859
|
+
);
|
|
860
|
+
} else {
|
|
861
|
+
getExports(
|
|
862
|
+
magicString,
|
|
863
|
+
exports,
|
|
864
|
+
exportDeclarations,
|
|
865
|
+
moduleExportsAssignments,
|
|
866
|
+
exportsAssignmentsByName,
|
|
867
|
+
deconflictedExportNames,
|
|
868
|
+
topLevelAssignments,
|
|
869
|
+
moduleName,
|
|
870
|
+
exportsName,
|
|
871
|
+
defineCompiledEsmExpressions,
|
|
872
|
+
HELPERS_NAME,
|
|
873
|
+
defaultIsModuleExports
|
|
874
|
+
);
|
|
875
|
+
}
|
|
876
|
+
}
|
|
877
|
+
if (exports.length) {
|
|
878
|
+
exportDeclarations.push(`export { ${exports.join(', ')} };`);
|
|
879
|
+
}
|
|
817
880
|
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
const namedExportProperties = [];
|
|
881
|
+
return `\n\n${exportDeclarations.join('\n')}`;
|
|
882
|
+
}
|
|
821
883
|
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
884
|
+
function getExportsForReplacedModuleExports(
|
|
885
|
+
magicString,
|
|
886
|
+
exports,
|
|
887
|
+
exportDeclarations,
|
|
888
|
+
moduleExportsAssignments,
|
|
889
|
+
firstTopLevelModuleExportsAssignment,
|
|
890
|
+
exportsName
|
|
891
|
+
) {
|
|
892
|
+
for (const { left } of moduleExportsAssignments) {
|
|
893
|
+
magicString.overwrite(left.start, left.end, exportsName);
|
|
894
|
+
}
|
|
895
|
+
magicString.prependRight(firstTopLevelModuleExportsAssignment.left.start, 'var ');
|
|
896
|
+
exports.push(`${exportsName} as __moduleExports`);
|
|
897
|
+
exportDeclarations.push(`export default ${exportsName};`);
|
|
898
|
+
}
|
|
827
899
|
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
900
|
+
function getExportsWhenWrapping(
|
|
901
|
+
exportDeclarations,
|
|
902
|
+
exportsName,
|
|
903
|
+
detectWrappedDefault,
|
|
904
|
+
HELPERS_NAME,
|
|
905
|
+
defaultIsModuleExports
|
|
906
|
+
) {
|
|
907
|
+
exportDeclarations.push(
|
|
908
|
+
`export default ${
|
|
909
|
+
detectWrappedDefault && defaultIsModuleExports === 'auto'
|
|
910
|
+
? `/*@__PURE__*/${HELPERS_NAME}.getDefaultExportFromCjs(${exportsName})`
|
|
911
|
+
: defaultIsModuleExports === false
|
|
912
|
+
? `${exportsName}.default`
|
|
913
|
+
: exportsName
|
|
914
|
+
};`
|
|
915
|
+
);
|
|
916
|
+
}
|
|
842
917
|
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
918
|
+
function getExports(
|
|
919
|
+
magicString,
|
|
920
|
+
exports,
|
|
921
|
+
exportDeclarations,
|
|
922
|
+
moduleExportsAssignments,
|
|
923
|
+
exportsAssignmentsByName,
|
|
924
|
+
deconflictedExportNames,
|
|
925
|
+
topLevelAssignments,
|
|
926
|
+
moduleName,
|
|
927
|
+
exportsName,
|
|
928
|
+
defineCompiledEsmExpressions,
|
|
929
|
+
HELPERS_NAME,
|
|
930
|
+
defaultIsModuleExports
|
|
931
|
+
) {
|
|
932
|
+
let deconflictedDefaultExportName;
|
|
933
|
+
// Collect and rewrite module.exports assignments
|
|
934
|
+
for (const { left } of moduleExportsAssignments) {
|
|
935
|
+
magicString.overwrite(left.start, left.end, `${moduleName}.exports`);
|
|
936
|
+
}
|
|
937
|
+
|
|
938
|
+
// Collect and rewrite named exports
|
|
939
|
+
for (const [exportName, { nodes }] of exportsAssignmentsByName) {
|
|
940
|
+
const deconflicted = deconflictedExportNames[exportName];
|
|
941
|
+
let needsDeclaration = true;
|
|
942
|
+
for (const node of nodes) {
|
|
943
|
+
let replacement = `${deconflicted} = ${exportsName}.${exportName}`;
|
|
944
|
+
if (needsDeclaration && topLevelAssignments.has(node)) {
|
|
945
|
+
replacement = `var ${replacement}`;
|
|
946
|
+
needsDeclaration = false;
|
|
847
947
|
}
|
|
948
|
+
magicString.overwrite(node.start, node.left.end, replacement);
|
|
848
949
|
}
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
if (!hasModuleExportsAssignment) {
|
|
852
|
-
const moduleExports = `{\n${namedExportProperties.join(',\n')}\n}`;
|
|
853
|
-
magicString
|
|
854
|
-
.trim()
|
|
855
|
-
.append(
|
|
856
|
-
`\n\nvar ${moduleName} = ${
|
|
857
|
-
isRestorableCompiledEsm
|
|
858
|
-
? `/*#__PURE__*/Object.defineProperty(${moduleExports}, '__esModule', {value: true})`
|
|
859
|
-
: moduleExports
|
|
860
|
-
};`
|
|
861
|
-
);
|
|
950
|
+
if (needsDeclaration) {
|
|
951
|
+
magicString.prepend(`var ${deconflicted};\n`);
|
|
862
952
|
}
|
|
863
|
-
}
|
|
864
953
|
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
if (defaultIsModuleExports === 'auto') {
|
|
868
|
-
if (isRestorableCompiledEsm) {
|
|
869
|
-
defaultExport.push(`export default ${deconflictedDefaultExportName || moduleName};`);
|
|
870
|
-
} else if (
|
|
871
|
-
(wrapped || deconflictedDefaultExportName) &&
|
|
872
|
-
(defineCompiledEsmExpressions.length > 0 || code.includes('__esModule'))
|
|
873
|
-
) {
|
|
874
|
-
// eslint-disable-next-line no-param-reassign
|
|
875
|
-
uses.commonjsHelpers = true;
|
|
876
|
-
defaultExport.push(
|
|
877
|
-
`export default /*@__PURE__*/${HELPERS_NAME}.getDefaultExportFromCjs(${moduleName});`
|
|
878
|
-
);
|
|
879
|
-
} else {
|
|
880
|
-
defaultExport.push(`export default ${moduleName};`);
|
|
881
|
-
}
|
|
882
|
-
} else if (defaultIsModuleExports === true) {
|
|
883
|
-
defaultExport.push(`export default ${moduleName};`);
|
|
884
|
-
} else if (defaultIsModuleExports === false) {
|
|
885
|
-
if (deconflictedDefaultExportName) {
|
|
886
|
-
defaultExport.push(`export default ${deconflictedDefaultExportName};`);
|
|
954
|
+
if (exportName === 'default') {
|
|
955
|
+
deconflictedDefaultExportName = deconflicted;
|
|
887
956
|
} else {
|
|
888
|
-
|
|
957
|
+
exports.push(exportName === deconflicted ? exportName : `${deconflicted} as ${exportName}`);
|
|
889
958
|
}
|
|
890
959
|
}
|
|
891
960
|
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
961
|
+
// Collect and rewrite exports.__esModule assignments
|
|
962
|
+
let isRestorableCompiledEsm = false;
|
|
963
|
+
for (const expression of defineCompiledEsmExpressions) {
|
|
964
|
+
isRestorableCompiledEsm = true;
|
|
965
|
+
const moduleExportsExpression =
|
|
966
|
+
expression.type === 'CallExpression' ? expression.arguments[0] : expression.left.object;
|
|
967
|
+
magicString.overwrite(moduleExportsExpression.start, moduleExportsExpression.end, exportsName);
|
|
968
|
+
}
|
|
969
|
+
|
|
970
|
+
if (!isRestorableCompiledEsm || defaultIsModuleExports === true) {
|
|
971
|
+
exportDeclarations.push(`export default ${exportsName};`);
|
|
972
|
+
} else if (moduleExportsAssignments.length === 0 || defaultIsModuleExports === false) {
|
|
973
|
+
exports.push(`${deconflictedDefaultExportName || exportsName} as default`);
|
|
974
|
+
} else {
|
|
975
|
+
exportDeclarations.push(
|
|
976
|
+
`export default /*@__PURE__*/${HELPERS_NAME}.getDefaultExportFromCjs(${exportsName});`
|
|
977
|
+
);
|
|
978
|
+
}
|
|
896
979
|
}
|
|
897
980
|
|
|
898
981
|
function isRequireStatement(node, scope) {
|
|
@@ -1010,44 +1093,46 @@ function getRequireHandlers() {
|
|
|
1010
1093
|
topLevelDeclarations,
|
|
1011
1094
|
topLevelRequireDeclarators,
|
|
1012
1095
|
reassignedNames,
|
|
1013
|
-
|
|
1014
|
-
dynamicRegisterSources
|
|
1096
|
+
helpersName,
|
|
1097
|
+
dynamicRegisterSources,
|
|
1098
|
+
moduleName,
|
|
1099
|
+
exportsName,
|
|
1100
|
+
id,
|
|
1101
|
+
exportMode
|
|
1015
1102
|
) {
|
|
1016
|
-
const removedDeclarators = getDeclaratorsReplacedByImportsAndSetImportNames(
|
|
1017
|
-
topLevelRequireDeclarators,
|
|
1018
|
-
requiredByNode,
|
|
1019
|
-
reassignedNames
|
|
1020
|
-
);
|
|
1021
1103
|
setRemainingImportNamesAndRewriteRequires(
|
|
1022
1104
|
requireExpressionsWithUsedReturnValue,
|
|
1023
1105
|
requiredByNode,
|
|
1024
1106
|
magicString
|
|
1025
1107
|
);
|
|
1026
|
-
|
|
1027
|
-
|
|
1028
|
-
|
|
1029
|
-
|
|
1030
|
-
|
|
1031
|
-
|
|
1032
|
-
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
|
|
1040
|
-
|
|
1041
|
-
|
|
1042
|
-
|
|
1043
|
-
|
|
1044
|
-
|
|
1045
|
-
|
|
1046
|
-
|
|
1047
|
-
|
|
1048
|
-
|
|
1049
|
-
|
|
1050
|
-
|
|
1108
|
+
const imports = [];
|
|
1109
|
+
imports.push(`import * as ${helpersName} from "${HELPERS_ID}";`);
|
|
1110
|
+
if (exportMode === 'module') {
|
|
1111
|
+
imports.push(
|
|
1112
|
+
`import { __module as ${moduleName}, exports as ${exportsName} } from ${JSON.stringify(
|
|
1113
|
+
wrapId(id, MODULE_SUFFIX)
|
|
1114
|
+
)}`
|
|
1115
|
+
);
|
|
1116
|
+
} else if (exportMode === 'exports') {
|
|
1117
|
+
imports.push(
|
|
1118
|
+
`import { __exports as ${exportsName} } from ${JSON.stringify(wrapId(id, EXPORTS_SUFFIX))}`
|
|
1119
|
+
);
|
|
1120
|
+
}
|
|
1121
|
+
for (const source of dynamicRegisterSources) {
|
|
1122
|
+
imports.push(`import ${JSON.stringify(wrapId(source, REQUIRE_SUFFIX))};`);
|
|
1123
|
+
}
|
|
1124
|
+
for (const source of requiredSources) {
|
|
1125
|
+
if (!source.startsWith('\0')) {
|
|
1126
|
+
imports.push(`import ${JSON.stringify(wrapId(source, REQUIRE_SUFFIX))};`);
|
|
1127
|
+
}
|
|
1128
|
+
const { name, nodesUsingRequired } = requiredBySource[source];
|
|
1129
|
+
imports.push(
|
|
1130
|
+
`import ${nodesUsingRequired.length ? `${name} from ` : ''}${JSON.stringify(
|
|
1131
|
+
source.startsWith('\0') ? source : wrapId(source, PROXY_SUFFIX)
|
|
1132
|
+
)};`
|
|
1133
|
+
);
|
|
1134
|
+
}
|
|
1135
|
+
return imports.length ? `${imports.join('\n')}\n\n` : '';
|
|
1051
1136
|
}
|
|
1052
1137
|
|
|
1053
1138
|
return {
|
|
@@ -1057,30 +1142,6 @@ function getRequireHandlers() {
|
|
|
1057
1142
|
};
|
|
1058
1143
|
}
|
|
1059
1144
|
|
|
1060
|
-
function getDeclaratorsReplacedByImportsAndSetImportNames(
|
|
1061
|
-
topLevelRequireDeclarators,
|
|
1062
|
-
requiredByNode,
|
|
1063
|
-
reassignedNames
|
|
1064
|
-
) {
|
|
1065
|
-
const removedDeclarators = new Set();
|
|
1066
|
-
for (const declarator of topLevelRequireDeclarators) {
|
|
1067
|
-
const { required } = requiredByNode.get(declarator.init);
|
|
1068
|
-
if (!required.name) {
|
|
1069
|
-
const potentialName = declarator.id.name;
|
|
1070
|
-
if (
|
|
1071
|
-
!reassignedNames.has(potentialName) &&
|
|
1072
|
-
!required.nodesUsingRequired.some((node) =>
|
|
1073
|
-
isLocallyShadowed(potentialName, requiredByNode.get(node).scope)
|
|
1074
|
-
)
|
|
1075
|
-
) {
|
|
1076
|
-
required.name = potentialName;
|
|
1077
|
-
removedDeclarators.add(declarator);
|
|
1078
|
-
}
|
|
1079
|
-
}
|
|
1080
|
-
}
|
|
1081
|
-
return removedDeclarators;
|
|
1082
|
-
}
|
|
1083
|
-
|
|
1084
1145
|
function setRemainingImportNamesAndRewriteRequires(
|
|
1085
1146
|
requireExpressionsWithUsedReturnValue,
|
|
1086
1147
|
requiredByNode,
|
|
@@ -1102,25 +1163,6 @@ function setRemainingImportNamesAndRewriteRequires(
|
|
|
1102
1163
|
}
|
|
1103
1164
|
}
|
|
1104
1165
|
|
|
1105
|
-
function removeDeclaratorsFromDeclarations(topLevelDeclarations, removedDeclarators, magicString) {
|
|
1106
|
-
for (const declaration of topLevelDeclarations) {
|
|
1107
|
-
let keepDeclaration = false;
|
|
1108
|
-
let [{ start }] = declaration.declarations;
|
|
1109
|
-
for (const declarator of declaration.declarations) {
|
|
1110
|
-
if (removedDeclarators.has(declarator)) {
|
|
1111
|
-
magicString.remove(start, declarator.end);
|
|
1112
|
-
} else if (!keepDeclaration) {
|
|
1113
|
-
magicString.remove(start, declarator.start);
|
|
1114
|
-
keepDeclaration = true;
|
|
1115
|
-
}
|
|
1116
|
-
start = declarator.end;
|
|
1117
|
-
}
|
|
1118
|
-
if (!keepDeclaration) {
|
|
1119
|
-
magicString.remove(declaration.start, declaration.end);
|
|
1120
|
-
}
|
|
1121
|
-
}
|
|
1122
|
-
}
|
|
1123
|
-
|
|
1124
1166
|
/* eslint-disable no-param-reassign, no-shadow, no-underscore-dangle, no-continue */
|
|
1125
1167
|
|
|
1126
1168
|
const exportsPattern = /^(?:module\.)?exports(?:\.([a-zA-Z_$][a-zA-Z_$0-9]*))?$/;
|
|
@@ -1150,9 +1192,9 @@ function transformCommonjs(
|
|
|
1150
1192
|
module: false,
|
|
1151
1193
|
exports: false,
|
|
1152
1194
|
global: false,
|
|
1153
|
-
require: false
|
|
1154
|
-
commonjsHelpers: false
|
|
1195
|
+
require: false
|
|
1155
1196
|
};
|
|
1197
|
+
let usesDynamicRequire = false;
|
|
1156
1198
|
const virtualDynamicRequirePath =
|
|
1157
1199
|
isDynamicRequireModulesEnabled && getVirtualPathForDynamicRequirePath(path.dirname(id), commonDir);
|
|
1158
1200
|
let scope = pluginutils.attachScopes(ast, 'scope');
|
|
@@ -1160,13 +1202,11 @@ function transformCommonjs(
|
|
|
1160
1202
|
let programDepth = 0;
|
|
1161
1203
|
let currentTryBlockEnd = null;
|
|
1162
1204
|
let shouldWrap = false;
|
|
1163
|
-
const defineCompiledEsmExpressions = [];
|
|
1164
1205
|
|
|
1165
1206
|
const globals = new Set();
|
|
1166
1207
|
|
|
1167
1208
|
// TODO technically wrong since globals isn't populated yet, but ¯\_(ツ)_/¯
|
|
1168
|
-
const HELPERS_NAME = deconflict(scope, globals, 'commonjsHelpers');
|
|
1169
|
-
const namedExports = {};
|
|
1209
|
+
const HELPERS_NAME = deconflict([scope], globals, 'commonjsHelpers');
|
|
1170
1210
|
const dynamicRegisterSources = new Set();
|
|
1171
1211
|
let hasRemovedRequire = false;
|
|
1172
1212
|
|
|
@@ -1183,8 +1223,13 @@ function transformCommonjs(
|
|
|
1183
1223
|
const topLevelDeclarations = [];
|
|
1184
1224
|
const topLevelRequireDeclarators = new Set();
|
|
1185
1225
|
const skippedNodes = new Set();
|
|
1186
|
-
const
|
|
1187
|
-
const
|
|
1226
|
+
const moduleAccessScopes = new Set([scope]);
|
|
1227
|
+
const exportsAccessScopes = new Set([scope]);
|
|
1228
|
+
const moduleExportsAssignments = [];
|
|
1229
|
+
let firstTopLevelModuleExportsAssignment = null;
|
|
1230
|
+
const exportsAssignmentsByName = new Map();
|
|
1231
|
+
const topLevelAssignments = new Set();
|
|
1232
|
+
const topLevelDefineCompiledEsmExpressions = [];
|
|
1188
1233
|
|
|
1189
1234
|
estreeWalker.walk(ast, {
|
|
1190
1235
|
enter(node, parent) {
|
|
@@ -1224,30 +1269,46 @@ function transformCommonjs(
|
|
|
1224
1269
|
uses[flattened.name] = true;
|
|
1225
1270
|
|
|
1226
1271
|
// we're dealing with `module.exports = ...` or `[module.]exports.foo = ...` –
|
|
1227
|
-
if (
|
|
1228
|
-
|
|
1272
|
+
if (flattened.keypath === 'module.exports') {
|
|
1273
|
+
moduleExportsAssignments.push(node);
|
|
1274
|
+
if (programDepth > 3) {
|
|
1275
|
+
moduleAccessScopes.add(scope);
|
|
1276
|
+
} else if (!firstTopLevelModuleExportsAssignment) {
|
|
1277
|
+
firstTopLevelModuleExportsAssignment = node;
|
|
1278
|
+
}
|
|
1279
|
+
|
|
1280
|
+
if (defaultIsModuleExports === false) {
|
|
1281
|
+
shouldWrap = true;
|
|
1282
|
+
} else if (defaultIsModuleExports === 'auto') {
|
|
1283
|
+
if (node.right.type === 'ObjectExpression') {
|
|
1284
|
+
if (hasDefineEsmProperty(node.right)) {
|
|
1285
|
+
shouldWrap = true;
|
|
1286
|
+
}
|
|
1287
|
+
} else if (defaultIsModuleExports === false) {
|
|
1288
|
+
shouldWrap = true;
|
|
1289
|
+
}
|
|
1290
|
+
}
|
|
1229
1291
|
} else if (exportName === KEY_COMPILED_ESM) {
|
|
1230
|
-
|
|
1231
|
-
|
|
1232
|
-
|
|
1233
|
-
|
|
1234
|
-
|
|
1292
|
+
if (programDepth > 3) {
|
|
1293
|
+
shouldWrap = true;
|
|
1294
|
+
} else {
|
|
1295
|
+
topLevelDefineCompiledEsmExpressions.push(node);
|
|
1296
|
+
}
|
|
1235
1297
|
} else {
|
|
1236
|
-
|
|
1298
|
+
const exportsAssignments = exportsAssignmentsByName.get(exportName) || {
|
|
1299
|
+
nodes: [],
|
|
1300
|
+
scopes: new Set()
|
|
1301
|
+
};
|
|
1302
|
+
exportsAssignments.nodes.push(node);
|
|
1303
|
+
exportsAssignments.scopes.add(scope);
|
|
1304
|
+
exportsAccessScopes.add(scope);
|
|
1305
|
+
exportsAssignmentsByName.set(exportName, exportsAssignments);
|
|
1306
|
+
if (programDepth <= 3) {
|
|
1307
|
+
topLevelAssignments.add(node);
|
|
1308
|
+
}
|
|
1237
1309
|
}
|
|
1238
1310
|
|
|
1239
1311
|
skippedNodes.add(node.left);
|
|
1240
|
-
|
|
1241
|
-
if (flattened.keypath === 'module.exports' && node.right.type === 'ObjectExpression') {
|
|
1242
|
-
node.right.properties.forEach((prop) => {
|
|
1243
|
-
if (prop.computed || !('key' in prop) || prop.key.type !== 'Identifier') return;
|
|
1244
|
-
const { name } = prop.key;
|
|
1245
|
-
if (name === pluginutils.makeLegalIdentifier(name)) namedExports[name] = true;
|
|
1246
|
-
});
|
|
1247
|
-
return;
|
|
1248
|
-
}
|
|
1249
|
-
|
|
1250
|
-
if (exportsPatternMatch[1]) namedExports[exportsPatternMatch[1]] = true;
|
|
1251
1312
|
} else {
|
|
1252
1313
|
for (const name of pluginutils.extractAssignedNames(node.left)) {
|
|
1253
1314
|
reassignedNames.add(name);
|
|
@@ -1259,7 +1320,7 @@ function transformCommonjs(
|
|
|
1259
1320
|
if (programDepth === 3 && parent.type === 'ExpressionStatement') {
|
|
1260
1321
|
// skip special handling for [module.]exports until we know we render this
|
|
1261
1322
|
skippedNodes.add(node.arguments[0]);
|
|
1262
|
-
|
|
1323
|
+
topLevelDefineCompiledEsmExpressions.push(node);
|
|
1263
1324
|
} else {
|
|
1264
1325
|
shouldWrap = true;
|
|
1265
1326
|
}
|
|
@@ -1287,7 +1348,6 @@ function transformCommonjs(
|
|
|
1287
1348
|
storeName: true
|
|
1288
1349
|
}
|
|
1289
1350
|
);
|
|
1290
|
-
uses.commonjsHelpers = true;
|
|
1291
1351
|
return;
|
|
1292
1352
|
}
|
|
1293
1353
|
|
|
@@ -1314,13 +1374,13 @@ function transformCommonjs(
|
|
|
1314
1374
|
}
|
|
1315
1375
|
|
|
1316
1376
|
let sourceId = getRequireStringArg(node);
|
|
1317
|
-
const isDynamicRegister =
|
|
1377
|
+
const isDynamicRegister = isWrappedId(sourceId, DYNAMIC_REGISTER_SUFFIX);
|
|
1318
1378
|
if (isDynamicRegister) {
|
|
1319
|
-
sourceId =
|
|
1379
|
+
sourceId = unwrapId(sourceId, DYNAMIC_REGISTER_SUFFIX);
|
|
1320
1380
|
if (sourceId.endsWith('.json')) {
|
|
1321
1381
|
sourceId = DYNAMIC_JSON_PREFIX + sourceId;
|
|
1322
1382
|
}
|
|
1323
|
-
dynamicRegisterSources.add(
|
|
1383
|
+
dynamicRegisterSources.add(wrapId(sourceId, DYNAMIC_REGISTER_SUFFIX));
|
|
1324
1384
|
} else {
|
|
1325
1385
|
if (
|
|
1326
1386
|
!sourceId.endsWith('.json') &&
|
|
@@ -1338,7 +1398,7 @@ function transformCommonjs(
|
|
|
1338
1398
|
path.dirname(id) === '.' ? null /* default behavior */ : virtualDynamicRequirePath
|
|
1339
1399
|
)})`
|
|
1340
1400
|
);
|
|
1341
|
-
|
|
1401
|
+
usesDynamicRequire = true;
|
|
1342
1402
|
}
|
|
1343
1403
|
return;
|
|
1344
1404
|
}
|
|
@@ -1395,7 +1455,6 @@ function transformCommonjs(
|
|
|
1395
1455
|
magicString.overwrite(node.start, node.end, `${HELPERS_NAME}.commonjsRequire`, {
|
|
1396
1456
|
storeName: true
|
|
1397
1457
|
});
|
|
1398
|
-
uses.commonjsHelpers = true;
|
|
1399
1458
|
}
|
|
1400
1459
|
}
|
|
1401
1460
|
|
|
@@ -1419,8 +1478,7 @@ function transformCommonjs(
|
|
|
1419
1478
|
});
|
|
1420
1479
|
}
|
|
1421
1480
|
}
|
|
1422
|
-
|
|
1423
|
-
uses.commonjsHelpers = true;
|
|
1481
|
+
usesDynamicRequire = true;
|
|
1424
1482
|
return;
|
|
1425
1483
|
case 'module':
|
|
1426
1484
|
case 'exports':
|
|
@@ -1433,11 +1491,12 @@ function transformCommonjs(
|
|
|
1433
1491
|
magicString.overwrite(node.start, node.end, `${HELPERS_NAME}.commonjsGlobal`, {
|
|
1434
1492
|
storeName: true
|
|
1435
1493
|
});
|
|
1436
|
-
uses.commonjsHelpers = true;
|
|
1437
1494
|
}
|
|
1438
1495
|
return;
|
|
1439
1496
|
case 'define':
|
|
1440
|
-
magicString.overwrite(node.start, node.end, 'undefined', {
|
|
1497
|
+
magicString.overwrite(node.start, node.end, 'undefined', {
|
|
1498
|
+
storeName: true
|
|
1499
|
+
});
|
|
1441
1500
|
return;
|
|
1442
1501
|
default:
|
|
1443
1502
|
globals.add(name);
|
|
@@ -1449,7 +1508,6 @@ function transformCommonjs(
|
|
|
1449
1508
|
magicString.overwrite(node.start, node.end, `${HELPERS_NAME}.commonjsRequire`, {
|
|
1450
1509
|
storeName: true
|
|
1451
1510
|
});
|
|
1452
|
-
uses.commonjsHelpers = true;
|
|
1453
1511
|
skippedNodes.add(node.object);
|
|
1454
1512
|
skippedNodes.add(node.property);
|
|
1455
1513
|
}
|
|
@@ -1468,7 +1526,6 @@ function transformCommonjs(
|
|
|
1468
1526
|
magicString.overwrite(node.start, node.end, `${HELPERS_NAME}.commonjsGlobal`, {
|
|
1469
1527
|
storeName: true
|
|
1470
1528
|
});
|
|
1471
|
-
uses.commonjsHelpers = true;
|
|
1472
1529
|
}
|
|
1473
1530
|
}
|
|
1474
1531
|
return;
|
|
@@ -1485,7 +1542,9 @@ function transformCommonjs(
|
|
|
1485
1542
|
flattened.keypath === 'module' ||
|
|
1486
1543
|
flattened.keypath === 'exports'
|
|
1487
1544
|
) {
|
|
1488
|
-
magicString.overwrite(node.start, node.end, `'object'`, {
|
|
1545
|
+
magicString.overwrite(node.start, node.end, `'object'`, {
|
|
1546
|
+
storeName: false
|
|
1547
|
+
});
|
|
1489
1548
|
}
|
|
1490
1549
|
}
|
|
1491
1550
|
return;
|
|
@@ -1503,23 +1562,22 @@ function transformCommonjs(
|
|
|
1503
1562
|
}
|
|
1504
1563
|
});
|
|
1505
1564
|
|
|
1506
|
-
|
|
1507
|
-
|
|
1508
|
-
|
|
1509
|
-
|
|
1510
|
-
|
|
1511
|
-
|
|
1512
|
-
defineCompiledEsmExpressions[0].end
|
|
1513
|
-
);
|
|
1514
|
-
} else {
|
|
1515
|
-
shouldWrap = true;
|
|
1516
|
-
uses.exports = true;
|
|
1517
|
-
}
|
|
1565
|
+
const nameBase = getName(id);
|
|
1566
|
+
const exportsName = deconflict([...exportsAccessScopes], globals, nameBase);
|
|
1567
|
+
const moduleName = deconflict([...moduleAccessScopes], globals, `${nameBase}Module`);
|
|
1568
|
+
const deconflictedExportNames = Object.create(null);
|
|
1569
|
+
for (const [exportName, { scopes }] of exportsAssignmentsByName) {
|
|
1570
|
+
deconflictedExportNames[exportName] = deconflict([...scopes], globals, exportName);
|
|
1518
1571
|
}
|
|
1519
1572
|
|
|
1520
1573
|
// We cannot wrap ES/mixed modules
|
|
1521
|
-
shouldWrap =
|
|
1522
|
-
|
|
1574
|
+
shouldWrap =
|
|
1575
|
+
!isEsModule &&
|
|
1576
|
+
!disableWrap &&
|
|
1577
|
+
(shouldWrap || (uses.exports && moduleExportsAssignments.length > 0));
|
|
1578
|
+
const detectWrappedDefault =
|
|
1579
|
+
shouldWrap &&
|
|
1580
|
+
(topLevelDefineCompiledEsmExpressions.length > 0 || code.indexOf('__esModule') >= 0);
|
|
1523
1581
|
|
|
1524
1582
|
if (
|
|
1525
1583
|
!(
|
|
@@ -1528,17 +1586,15 @@ function transformCommonjs(
|
|
|
1528
1586
|
uses.module ||
|
|
1529
1587
|
uses.exports ||
|
|
1530
1588
|
uses.require ||
|
|
1531
|
-
|
|
1589
|
+
usesDynamicRequire ||
|
|
1532
1590
|
hasRemovedRequire ||
|
|
1533
|
-
|
|
1591
|
+
topLevelDefineCompiledEsmExpressions.length > 0
|
|
1534
1592
|
) &&
|
|
1535
1593
|
(ignoreGlobal || !uses.global)
|
|
1536
1594
|
) {
|
|
1537
1595
|
return { meta: { commonjs: { isCommonJS: false } } };
|
|
1538
1596
|
}
|
|
1539
1597
|
|
|
1540
|
-
const moduleName = deconflict(scope, globals, getName(id));
|
|
1541
|
-
|
|
1542
1598
|
let leadingComment = '';
|
|
1543
1599
|
if (code.startsWith('/*')) {
|
|
1544
1600
|
const commentEnd = code.indexOf('*/', 2) + 2;
|
|
@@ -1546,34 +1602,53 @@ function transformCommonjs(
|
|
|
1546
1602
|
magicString.remove(0, commentEnd).trim();
|
|
1547
1603
|
}
|
|
1548
1604
|
|
|
1605
|
+
const exportMode = shouldWrap
|
|
1606
|
+
? uses.module
|
|
1607
|
+
? 'module'
|
|
1608
|
+
: 'exports'
|
|
1609
|
+
: firstTopLevelModuleExportsAssignment
|
|
1610
|
+
? exportsAssignmentsByName.size === 0 && topLevelDefineCompiledEsmExpressions.length === 0
|
|
1611
|
+
? 'replace'
|
|
1612
|
+
: 'module'
|
|
1613
|
+
: moduleExportsAssignments.length === 0
|
|
1614
|
+
? 'exports'
|
|
1615
|
+
: 'module';
|
|
1616
|
+
|
|
1617
|
+
const importBlock = rewriteRequireExpressionsAndGetImportBlock(
|
|
1618
|
+
magicString,
|
|
1619
|
+
topLevelDeclarations,
|
|
1620
|
+
topLevelRequireDeclarators,
|
|
1621
|
+
reassignedNames,
|
|
1622
|
+
HELPERS_NAME,
|
|
1623
|
+
dynamicRegisterSources,
|
|
1624
|
+
moduleName,
|
|
1625
|
+
exportsName,
|
|
1626
|
+
id,
|
|
1627
|
+
exportMode
|
|
1628
|
+
);
|
|
1629
|
+
|
|
1549
1630
|
const exportBlock = isEsModule
|
|
1550
1631
|
? ''
|
|
1551
1632
|
: rewriteExportsAndGetExportsBlock(
|
|
1552
1633
|
magicString,
|
|
1553
1634
|
moduleName,
|
|
1635
|
+
exportsName,
|
|
1554
1636
|
shouldWrap,
|
|
1555
|
-
|
|
1556
|
-
|
|
1557
|
-
|
|
1558
|
-
|
|
1559
|
-
|
|
1637
|
+
moduleExportsAssignments,
|
|
1638
|
+
firstTopLevelModuleExportsAssignment,
|
|
1639
|
+
exportsAssignmentsByName,
|
|
1640
|
+
topLevelAssignments,
|
|
1641
|
+
topLevelDefineCompiledEsmExpressions,
|
|
1642
|
+
deconflictedExportNames,
|
|
1560
1643
|
code,
|
|
1561
|
-
uses,
|
|
1562
1644
|
HELPERS_NAME,
|
|
1645
|
+
exportMode,
|
|
1646
|
+
detectWrappedDefault,
|
|
1563
1647
|
defaultIsModuleExports
|
|
1564
1648
|
);
|
|
1565
1649
|
|
|
1566
|
-
const importBlock = rewriteRequireExpressionsAndGetImportBlock(
|
|
1567
|
-
magicString,
|
|
1568
|
-
topLevelDeclarations,
|
|
1569
|
-
topLevelRequireDeclarators,
|
|
1570
|
-
reassignedNames,
|
|
1571
|
-
uses.commonjsHelpers && HELPERS_NAME,
|
|
1572
|
-
dynamicRegisterSources
|
|
1573
|
-
);
|
|
1574
|
-
|
|
1575
1650
|
if (shouldWrap) {
|
|
1576
|
-
wrapCode(magicString, uses, moduleName,
|
|
1651
|
+
wrapCode(magicString, uses, moduleName, exportsName);
|
|
1577
1652
|
}
|
|
1578
1653
|
|
|
1579
1654
|
magicString
|
|
@@ -1622,7 +1697,7 @@ function commonjs(options = {}) {
|
|
|
1622
1697
|
|
|
1623
1698
|
const esModulesWithDefaultExport = new Set();
|
|
1624
1699
|
const esModulesWithNamedExports = new Set();
|
|
1625
|
-
const
|
|
1700
|
+
const commonJsMetaPromises = new Map();
|
|
1626
1701
|
|
|
1627
1702
|
const ignoreRequire =
|
|
1628
1703
|
typeof options.ignore === 'function'
|
|
@@ -1675,13 +1750,11 @@ function commonjs(options = {}) {
|
|
|
1675
1750
|
return { meta: { commonjs: { isCommonJS: false } } };
|
|
1676
1751
|
}
|
|
1677
1752
|
|
|
1678
|
-
|
|
1679
|
-
|
|
1680
|
-
|
|
1681
|
-
if (isModuleRegisterProxy(id)) {
|
|
1682
|
-
disableWrap = true;
|
|
1753
|
+
// avoid wrapping as this is a commonjsRegister call
|
|
1754
|
+
const disableWrap = isWrappedId(id, DYNAMIC_REGISTER_SUFFIX);
|
|
1755
|
+
if (disableWrap) {
|
|
1683
1756
|
// eslint-disable-next-line no-param-reassign
|
|
1684
|
-
id =
|
|
1757
|
+
id = unwrapId(id, DYNAMIC_REGISTER_SUFFIX);
|
|
1685
1758
|
}
|
|
1686
1759
|
|
|
1687
1760
|
return transformCommonjs(
|
|
@@ -1726,6 +1799,39 @@ function commonjs(options = {}) {
|
|
|
1726
1799
|
return getSpecificHelperProxy(id);
|
|
1727
1800
|
}
|
|
1728
1801
|
|
|
1802
|
+
if (isWrappedId(id, MODULE_SUFFIX)) {
|
|
1803
|
+
const actualId = unwrapId(id, MODULE_SUFFIX);
|
|
1804
|
+
let name = getName(actualId);
|
|
1805
|
+
let code;
|
|
1806
|
+
if (isDynamicRequireModulesEnabled) {
|
|
1807
|
+
if (['modulePath', 'commonjsRequire', 'createModule'].includes(name)) {
|
|
1808
|
+
name = `${name}_`;
|
|
1809
|
+
}
|
|
1810
|
+
code =
|
|
1811
|
+
`import {commonjsRequire, createModule} from "${HELPERS_ID}";\n` +
|
|
1812
|
+
`var ${name} = createModule(${JSON.stringify(
|
|
1813
|
+
getVirtualPathForDynamicRequirePath(path.dirname(actualId), commonDir)
|
|
1814
|
+
)});\n` +
|
|
1815
|
+
`export {${name} as __module}`;
|
|
1816
|
+
} else {
|
|
1817
|
+
code = `var ${name} = {exports: {}}; export {${name} as __module}`;
|
|
1818
|
+
}
|
|
1819
|
+
return {
|
|
1820
|
+
code,
|
|
1821
|
+
syntheticNamedExports: '__module',
|
|
1822
|
+
meta: { commonjs: { isCommonJS: false } }
|
|
1823
|
+
};
|
|
1824
|
+
}
|
|
1825
|
+
|
|
1826
|
+
if (isWrappedId(id, EXPORTS_SUFFIX)) {
|
|
1827
|
+
const actualId = unwrapId(id, EXPORTS_SUFFIX);
|
|
1828
|
+
const name = getName(actualId);
|
|
1829
|
+
return {
|
|
1830
|
+
code: `var ${name} = {}; export {${name} as __exports}`,
|
|
1831
|
+
meta: { commonjs: { isCommonJS: false } }
|
|
1832
|
+
};
|
|
1833
|
+
}
|
|
1834
|
+
|
|
1729
1835
|
if (isWrappedId(id, EXTERNAL_SUFFIX)) {
|
|
1730
1836
|
const actualId = unwrapId(id, EXTERNAL_SUFFIX);
|
|
1731
1837
|
return getUnknownRequireProxy(
|
|
@@ -1746,9 +1852,9 @@ function commonjs(options = {}) {
|
|
|
1746
1852
|
return `export default require(${JSON.stringify(normalizePathSlashes(id))});`;
|
|
1747
1853
|
}
|
|
1748
1854
|
|
|
1749
|
-
if (
|
|
1855
|
+
if (isWrappedId(id, DYNAMIC_REGISTER_SUFFIX)) {
|
|
1750
1856
|
return getDynamicRequireProxy(
|
|
1751
|
-
normalizePathSlashes(
|
|
1857
|
+
normalizePathSlashes(unwrapId(id, DYNAMIC_REGISTER_SUFFIX)),
|
|
1752
1858
|
commonDir
|
|
1753
1859
|
);
|
|
1754
1860
|
}
|
|
@@ -1760,7 +1866,7 @@ function commonjs(options = {}) {
|
|
|
1760
1866
|
getRequireReturnsDefault(actualId),
|
|
1761
1867
|
esModulesWithDefaultExport,
|
|
1762
1868
|
esModulesWithNamedExports,
|
|
1763
|
-
|
|
1869
|
+
commonJsMetaPromises
|
|
1764
1870
|
);
|
|
1765
1871
|
}
|
|
1766
1872
|
|
|
@@ -1770,8 +1876,8 @@ function commonjs(options = {}) {
|
|
|
1770
1876
|
transform(code, rawId) {
|
|
1771
1877
|
let id = rawId;
|
|
1772
1878
|
|
|
1773
|
-
if (
|
|
1774
|
-
id =
|
|
1879
|
+
if (isWrappedId(id, DYNAMIC_REGISTER_SUFFIX)) {
|
|
1880
|
+
id = unwrapId(id, DYNAMIC_REGISTER_SUFFIX);
|
|
1775
1881
|
}
|
|
1776
1882
|
|
|
1777
1883
|
const extName = path.extname(id);
|
|
@@ -1791,16 +1897,12 @@ function commonjs(options = {}) {
|
|
|
1791
1897
|
}
|
|
1792
1898
|
},
|
|
1793
1899
|
|
|
1794
|
-
|
|
1795
|
-
|
|
1796
|
-
|
|
1797
|
-
|
|
1798
|
-
if (isCjs != null) {
|
|
1799
|
-
setIsCjsPromise(isCjsPromises, id, isCjs);
|
|
1800
|
-
return;
|
|
1801
|
-
}
|
|
1900
|
+
moduleParsed({ id, meta: { commonjs: commonjsMeta } }) {
|
|
1901
|
+
if (commonjsMeta && commonjsMeta.isCommonJS != null) {
|
|
1902
|
+
setCommonJSMetaPromise(commonJsMetaPromises, id, commonjsMeta);
|
|
1903
|
+
return;
|
|
1802
1904
|
}
|
|
1803
|
-
|
|
1905
|
+
setCommonJSMetaPromise(commonJsMetaPromises, id, null);
|
|
1804
1906
|
}
|
|
1805
1907
|
};
|
|
1806
1908
|
}
|