@rollup/plugin-commonjs 18.0.0 → 19.0.2
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 +36 -0
- package/README.md +46 -1
- package/dist/index.es.js +443 -323
- package/dist/index.es.js.map +1 -1
- package/dist/index.js +443 -323
- package/dist/index.js.map +1 -1
- package/package.json +14 -14
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
|
}
|
|
@@ -387,15 +408,13 @@ function getPackageEntryPoint(dirPath) {
|
|
|
387
408
|
}
|
|
388
409
|
|
|
389
410
|
function getDynamicPackagesModule(dynamicRequireModuleDirPaths, commonDir) {
|
|
390
|
-
let code = `const
|
|
411
|
+
let code = `const commonjsRegisterOrShort = require('${HELPERS_ID}?commonjsRegisterOrShort');`;
|
|
391
412
|
for (const dir of dynamicRequireModuleDirPaths) {
|
|
392
413
|
const entryPoint = getPackageEntryPoint(dir);
|
|
393
414
|
|
|
394
|
-
code += `\
|
|
415
|
+
code += `\ncommonjsRegisterOrShort(${JSON.stringify(
|
|
395
416
|
getVirtualPathForDynamicRequirePath(dir, commonDir)
|
|
396
|
-
)},
|
|
397
|
-
module.exports = require(${JSON.stringify(normalizePathSlashes(path.join(dir, entryPoint)))});
|
|
398
|
-
});`;
|
|
417
|
+
)}, ${JSON.stringify(getVirtualPathForDynamicRequirePath(path.join(dir, entryPoint), commonDir))});`;
|
|
399
418
|
}
|
|
400
419
|
return code;
|
|
401
420
|
}
|
|
@@ -406,28 +425,18 @@ function getDynamicPackagesEntryIntro(
|
|
|
406
425
|
) {
|
|
407
426
|
let dynamicImports = Array.from(
|
|
408
427
|
dynamicRequireModuleSet,
|
|
409
|
-
(dynamicId) => `require(${JSON.stringify(
|
|
428
|
+
(dynamicId) => `require(${JSON.stringify(wrapId(dynamicId, DYNAMIC_REGISTER_SUFFIX))});`
|
|
410
429
|
).join('\n');
|
|
411
430
|
|
|
412
431
|
if (dynamicRequireModuleDirPaths.length) {
|
|
413
|
-
dynamicImports += `require(${JSON.stringify(
|
|
432
|
+
dynamicImports += `require(${JSON.stringify(
|
|
433
|
+
wrapId(DYNAMIC_PACKAGES_ID, DYNAMIC_REGISTER_SUFFIX)
|
|
434
|
+
)});`;
|
|
414
435
|
}
|
|
415
436
|
|
|
416
437
|
return dynamicImports;
|
|
417
438
|
}
|
|
418
439
|
|
|
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
440
|
function isDynamicModuleImport(id, dynamicRequireModuleSet) {
|
|
432
441
|
const normalizedPath = normalizePathSlashes(id);
|
|
433
442
|
return dynamicRequireModuleSet.has(normalizedPath) && !normalizedPath.endsWith('.json');
|
|
@@ -460,39 +469,37 @@ function getDynamicRequirePaths(patterns) {
|
|
|
460
469
|
return { dynamicRequireModuleSet, dynamicRequireModuleDirPaths };
|
|
461
470
|
}
|
|
462
471
|
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
let isCjsPromise = isCjsPromises.get(id);
|
|
467
|
-
if (isCjsPromise) return isCjsPromise.promise;
|
|
472
|
+
function getCommonJSMetaPromise(commonJSMetaPromises, id) {
|
|
473
|
+
let commonJSMetaPromise = commonJSMetaPromises.get(id);
|
|
474
|
+
if (commonJSMetaPromise) return commonJSMetaPromise.promise;
|
|
468
475
|
|
|
469
476
|
const promise = new Promise((resolve) => {
|
|
470
|
-
|
|
477
|
+
commonJSMetaPromise = {
|
|
471
478
|
resolve,
|
|
472
479
|
promise: null
|
|
473
480
|
};
|
|
474
|
-
|
|
481
|
+
commonJSMetaPromises.set(id, commonJSMetaPromise);
|
|
475
482
|
});
|
|
476
|
-
|
|
483
|
+
commonJSMetaPromise.promise = promise;
|
|
477
484
|
|
|
478
485
|
return promise;
|
|
479
486
|
}
|
|
480
487
|
|
|
481
|
-
function
|
|
482
|
-
const
|
|
483
|
-
if (
|
|
484
|
-
if (
|
|
485
|
-
|
|
486
|
-
|
|
488
|
+
function setCommonJSMetaPromise(commonJSMetaPromises, id, commonjsMeta) {
|
|
489
|
+
const commonJSMetaPromise = commonJSMetaPromises.get(id);
|
|
490
|
+
if (commonJSMetaPromise) {
|
|
491
|
+
if (commonJSMetaPromise.resolve) {
|
|
492
|
+
commonJSMetaPromise.resolve(commonjsMeta);
|
|
493
|
+
commonJSMetaPromise.resolve = null;
|
|
487
494
|
}
|
|
488
495
|
} else {
|
|
489
|
-
|
|
496
|
+
commonJSMetaPromises.set(id, { promise: Promise.resolve(commonjsMeta), resolve: null });
|
|
490
497
|
}
|
|
491
498
|
}
|
|
492
499
|
|
|
493
500
|
// e.g. id === "commonjsHelpers?commonjsRegister"
|
|
494
501
|
function getSpecificHelperProxy(id) {
|
|
495
|
-
return `export {${id.split('?')[1]} as default} from
|
|
502
|
+
return `export {${id.split('?')[1]} as default} from "${HELPERS_ID}";`;
|
|
496
503
|
}
|
|
497
504
|
|
|
498
505
|
function getUnknownRequireProxy(id, requireReturnsDefault) {
|
|
@@ -532,16 +539,17 @@ async function getStaticRequireProxy(
|
|
|
532
539
|
id,
|
|
533
540
|
requireReturnsDefault,
|
|
534
541
|
esModulesWithDefaultExport,
|
|
535
|
-
esModulesWithNamedExports
|
|
542
|
+
esModulesWithNamedExports,
|
|
543
|
+
commonJsMetaPromises
|
|
536
544
|
) {
|
|
537
545
|
const name = getName(id);
|
|
538
|
-
const
|
|
539
|
-
if (
|
|
540
|
-
return `
|
|
541
|
-
} else if (
|
|
546
|
+
const commonjsMeta = await getCommonJSMetaPromise(commonJsMetaPromises, id);
|
|
547
|
+
if (commonjsMeta && commonjsMeta.isCommonJS) {
|
|
548
|
+
return `export { __moduleExports as default } from ${JSON.stringify(id)};`;
|
|
549
|
+
} else if (commonjsMeta === null) {
|
|
542
550
|
return getUnknownRequireProxy(id, requireReturnsDefault);
|
|
543
551
|
} else if (!requireReturnsDefault) {
|
|
544
|
-
return `import {getAugmentedNamespace} from "${HELPERS_ID}"; import * as ${name} from ${JSON.stringify(
|
|
552
|
+
return `import { getAugmentedNamespace } from "${HELPERS_ID}"; import * as ${name} from ${JSON.stringify(
|
|
545
553
|
id
|
|
546
554
|
)}; export default /*@__PURE__*/getAugmentedNamespace(${name});`;
|
|
547
555
|
} else if (
|
|
@@ -552,7 +560,7 @@ async function getStaticRequireProxy(
|
|
|
552
560
|
) {
|
|
553
561
|
return `import * as ${name} from ${JSON.stringify(id)}; export default ${name};`;
|
|
554
562
|
}
|
|
555
|
-
return `export {default} from ${JSON.stringify(id)};`;
|
|
563
|
+
return `export { default } from ${JSON.stringify(id)};`;
|
|
556
564
|
}
|
|
557
565
|
|
|
558
566
|
/* eslint-disable no-param-reassign, no-undefined */
|
|
@@ -589,12 +597,17 @@ function getResolveId(extensions) {
|
|
|
589
597
|
}
|
|
590
598
|
|
|
591
599
|
return function resolveId(importee, rawImporter) {
|
|
600
|
+
if (isWrappedId(importee, MODULE_SUFFIX) || isWrappedId(importee, EXPORTS_SUFFIX)) {
|
|
601
|
+
return importee;
|
|
602
|
+
}
|
|
603
|
+
|
|
592
604
|
const importer =
|
|
593
|
-
rawImporter &&
|
|
594
|
-
?
|
|
605
|
+
rawImporter && isWrappedId(rawImporter, DYNAMIC_REGISTER_SUFFIX)
|
|
606
|
+
? unwrapId(rawImporter, DYNAMIC_REGISTER_SUFFIX)
|
|
595
607
|
: rawImporter;
|
|
596
608
|
|
|
597
|
-
//
|
|
609
|
+
// Except for exports, proxies are only importing resolved ids,
|
|
610
|
+
// no need to resolve again
|
|
598
611
|
if (importer && isWrappedId(importer, PROXY_SUFFIX)) {
|
|
599
612
|
return importee;
|
|
600
613
|
}
|
|
@@ -608,9 +621,9 @@ function getResolveId(extensions) {
|
|
|
608
621
|
} else if (isRequiredModule) {
|
|
609
622
|
importee = unwrapId(importee, REQUIRE_SUFFIX);
|
|
610
623
|
|
|
611
|
-
isModuleRegistration =
|
|
624
|
+
isModuleRegistration = isWrappedId(importee, DYNAMIC_REGISTER_SUFFIX);
|
|
612
625
|
if (isModuleRegistration) {
|
|
613
|
-
importee =
|
|
626
|
+
importee = unwrapId(importee, DYNAMIC_REGISTER_SUFFIX);
|
|
614
627
|
}
|
|
615
628
|
}
|
|
616
629
|
|
|
@@ -637,7 +650,7 @@ function getResolveId(extensions) {
|
|
|
637
650
|
resolved.id = wrapId(resolved.id, resolved.external ? EXTERNAL_SUFFIX : PROXY_SUFFIX);
|
|
638
651
|
resolved.external = false;
|
|
639
652
|
} else if (resolved && isModuleRegistration) {
|
|
640
|
-
resolved.id =
|
|
653
|
+
resolved.id = wrapId(resolved.id, DYNAMIC_REGISTER_SUFFIX);
|
|
641
654
|
} else if (!resolved && (isProxyModule || isRequiredModule)) {
|
|
642
655
|
return { id: wrapId(importee, EXTERNAL_SUFFIX), external: false };
|
|
643
656
|
}
|
|
@@ -737,8 +750,6 @@ function isDefineCompiledEsm(node) {
|
|
|
737
750
|
}
|
|
738
751
|
|
|
739
752
|
function getDefinePropertyCallName(node, targetName) {
|
|
740
|
-
const targetNames = targetName.split('.');
|
|
741
|
-
|
|
742
753
|
const {
|
|
743
754
|
callee: { object, property }
|
|
744
755
|
} = node;
|
|
@@ -746,6 +757,7 @@ function getDefinePropertyCallName(node, targetName) {
|
|
|
746
757
|
if (!property || property.type !== 'Identifier' || property.name !== 'defineProperty') return;
|
|
747
758
|
if (node.arguments.length !== 3) return;
|
|
748
759
|
|
|
760
|
+
const targetNames = targetName.split('.');
|
|
749
761
|
const [target, key, value] = node.arguments;
|
|
750
762
|
if (targetNames.length === 1) {
|
|
751
763
|
if (target.type !== 'Identifier' || target.name !== targetNames[0]) {
|
|
@@ -772,117 +784,199 @@ function getDefinePropertyCallName(node, targetName) {
|
|
|
772
784
|
return { key: key.value, value: valueProperty.value };
|
|
773
785
|
}
|
|
774
786
|
|
|
775
|
-
function isLocallyShadowed(name, scope) {
|
|
776
|
-
while (scope.parent) {
|
|
777
|
-
if (scope.declarations[name]) {
|
|
778
|
-
return true;
|
|
779
|
-
}
|
|
780
|
-
// eslint-disable-next-line no-param-reassign
|
|
781
|
-
scope = scope.parent;
|
|
782
|
-
}
|
|
783
|
-
return false;
|
|
784
|
-
}
|
|
785
|
-
|
|
786
787
|
function isShorthandProperty(parent) {
|
|
787
788
|
return parent && parent.type === 'Property' && parent.shorthand;
|
|
788
789
|
}
|
|
789
790
|
|
|
790
|
-
function
|
|
791
|
-
|
|
791
|
+
function hasDefineEsmProperty(node) {
|
|
792
|
+
return node.properties.some((property) => {
|
|
793
|
+
if (
|
|
794
|
+
property.type === 'Property' &&
|
|
795
|
+
property.key.type === 'Identifier' &&
|
|
796
|
+
property.key.name === '__esModule' &&
|
|
797
|
+
isTruthy(property.value)
|
|
798
|
+
) {
|
|
799
|
+
return true;
|
|
800
|
+
}
|
|
801
|
+
return false;
|
|
802
|
+
});
|
|
803
|
+
}
|
|
792
804
|
|
|
805
|
+
function wrapCode(magicString, uses, moduleName, exportsName) {
|
|
806
|
+
const args = [];
|
|
807
|
+
const passedArgs = [];
|
|
808
|
+
if (uses.module) {
|
|
809
|
+
args.push('module');
|
|
810
|
+
passedArgs.push(moduleName);
|
|
811
|
+
}
|
|
812
|
+
if (uses.exports) {
|
|
813
|
+
args.push('exports');
|
|
814
|
+
passedArgs.push(exportsName);
|
|
815
|
+
}
|
|
793
816
|
magicString
|
|
794
817
|
.trim()
|
|
795
|
-
.prepend(`
|
|
796
|
-
.append(
|
|
797
|
-
`\n}${virtualDynamicRequirePath ? `, ${JSON.stringify(virtualDynamicRequirePath)}` : ''});`
|
|
798
|
-
);
|
|
818
|
+
.prepend(`(function (${args.join(', ')}) {\n`)
|
|
819
|
+
.append(`\n}(${passedArgs.join(', ')}));`);
|
|
799
820
|
}
|
|
800
821
|
|
|
801
822
|
function rewriteExportsAndGetExportsBlock(
|
|
802
823
|
magicString,
|
|
803
824
|
moduleName,
|
|
825
|
+
exportsName,
|
|
804
826
|
wrapped,
|
|
805
|
-
|
|
806
|
-
|
|
827
|
+
moduleExportsAssignments,
|
|
828
|
+
firstTopLevelModuleExportsAssignment,
|
|
829
|
+
exportsAssignmentsByName,
|
|
830
|
+
topLevelAssignments,
|
|
807
831
|
defineCompiledEsmExpressions,
|
|
808
|
-
|
|
809
|
-
isRestorableCompiledEsm,
|
|
832
|
+
deconflictedExportNames,
|
|
810
833
|
code,
|
|
811
|
-
|
|
812
|
-
|
|
834
|
+
HELPERS_NAME,
|
|
835
|
+
exportMode,
|
|
836
|
+
detectWrappedDefault,
|
|
837
|
+
defaultIsModuleExports
|
|
813
838
|
) {
|
|
814
|
-
const
|
|
815
|
-
const
|
|
816
|
-
|
|
839
|
+
const exports = [];
|
|
840
|
+
const exportDeclarations = [];
|
|
841
|
+
|
|
842
|
+
if (exportMode === 'replace') {
|
|
843
|
+
getExportsForReplacedModuleExports(
|
|
844
|
+
magicString,
|
|
845
|
+
exports,
|
|
846
|
+
exportDeclarations,
|
|
847
|
+
moduleExportsAssignments,
|
|
848
|
+
firstTopLevelModuleExportsAssignment,
|
|
849
|
+
exportsName
|
|
850
|
+
);
|
|
851
|
+
} else {
|
|
852
|
+
exports.push(`${exportsName} as __moduleExports`);
|
|
853
|
+
if (wrapped) {
|
|
854
|
+
getExportsWhenWrapping(
|
|
855
|
+
exportDeclarations,
|
|
856
|
+
exportsName,
|
|
857
|
+
detectWrappedDefault,
|
|
858
|
+
HELPERS_NAME,
|
|
859
|
+
defaultIsModuleExports
|
|
860
|
+
);
|
|
861
|
+
} else {
|
|
862
|
+
getExports(
|
|
863
|
+
magicString,
|
|
864
|
+
exports,
|
|
865
|
+
exportDeclarations,
|
|
866
|
+
moduleExportsAssignments,
|
|
867
|
+
exportsAssignmentsByName,
|
|
868
|
+
deconflictedExportNames,
|
|
869
|
+
topLevelAssignments,
|
|
870
|
+
moduleName,
|
|
871
|
+
exportsName,
|
|
872
|
+
defineCompiledEsmExpressions,
|
|
873
|
+
HELPERS_NAME,
|
|
874
|
+
defaultIsModuleExports
|
|
875
|
+
);
|
|
876
|
+
}
|
|
877
|
+
}
|
|
878
|
+
if (exports.length) {
|
|
879
|
+
exportDeclarations.push(`export { ${exports.join(', ')} };`);
|
|
880
|
+
}
|
|
817
881
|
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
const namedExportProperties = [];
|
|
882
|
+
return `\n\n${exportDeclarations.join('\n')}`;
|
|
883
|
+
}
|
|
821
884
|
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
885
|
+
function getExportsForReplacedModuleExports(
|
|
886
|
+
magicString,
|
|
887
|
+
exports,
|
|
888
|
+
exportDeclarations,
|
|
889
|
+
moduleExportsAssignments,
|
|
890
|
+
firstTopLevelModuleExportsAssignment,
|
|
891
|
+
exportsName
|
|
892
|
+
) {
|
|
893
|
+
for (const { left } of moduleExportsAssignments) {
|
|
894
|
+
magicString.overwrite(left.start, left.end, exportsName);
|
|
895
|
+
}
|
|
896
|
+
magicString.prependRight(firstTopLevelModuleExportsAssignment.left.start, 'var ');
|
|
897
|
+
exports.push(`${exportsName} as __moduleExports`);
|
|
898
|
+
exportDeclarations.push(`export default ${exportsName};`);
|
|
899
|
+
}
|
|
827
900
|
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
901
|
+
function getExportsWhenWrapping(
|
|
902
|
+
exportDeclarations,
|
|
903
|
+
exportsName,
|
|
904
|
+
detectWrappedDefault,
|
|
905
|
+
HELPERS_NAME,
|
|
906
|
+
defaultIsModuleExports
|
|
907
|
+
) {
|
|
908
|
+
exportDeclarations.push(
|
|
909
|
+
`export default ${
|
|
910
|
+
detectWrappedDefault && defaultIsModuleExports === 'auto'
|
|
911
|
+
? `/*@__PURE__*/${HELPERS_NAME}.getDefaultExportFromCjs(${exportsName})`
|
|
912
|
+
: defaultIsModuleExports === false
|
|
913
|
+
? `${exportsName}.default`
|
|
914
|
+
: exportsName
|
|
915
|
+
};`
|
|
916
|
+
);
|
|
917
|
+
}
|
|
918
|
+
|
|
919
|
+
function getExports(
|
|
920
|
+
magicString,
|
|
921
|
+
exports,
|
|
922
|
+
exportDeclarations,
|
|
923
|
+
moduleExportsAssignments,
|
|
924
|
+
exportsAssignmentsByName,
|
|
925
|
+
deconflictedExportNames,
|
|
926
|
+
topLevelAssignments,
|
|
927
|
+
moduleName,
|
|
928
|
+
exportsName,
|
|
929
|
+
defineCompiledEsmExpressions,
|
|
930
|
+
HELPERS_NAME,
|
|
931
|
+
defaultIsModuleExports
|
|
932
|
+
) {
|
|
933
|
+
let deconflictedDefaultExportName;
|
|
934
|
+
// Collect and rewrite module.exports assignments
|
|
935
|
+
for (const { left } of moduleExportsAssignments) {
|
|
936
|
+
magicString.overwrite(left.start, left.end, `${moduleName}.exports`);
|
|
937
|
+
}
|
|
842
938
|
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
939
|
+
// Collect and rewrite named exports
|
|
940
|
+
for (const [exportName, { nodes }] of exportsAssignmentsByName) {
|
|
941
|
+
const deconflicted = deconflictedExportNames[exportName];
|
|
942
|
+
let needsDeclaration = true;
|
|
943
|
+
for (const node of nodes) {
|
|
944
|
+
let replacement = `${deconflicted} = ${exportsName}.${exportName}`;
|
|
945
|
+
if (needsDeclaration && topLevelAssignments.has(node)) {
|
|
946
|
+
replacement = `var ${replacement}`;
|
|
947
|
+
needsDeclaration = false;
|
|
847
948
|
}
|
|
949
|
+
magicString.overwrite(node.start, node.left.end, replacement);
|
|
950
|
+
}
|
|
951
|
+
if (needsDeclaration) {
|
|
952
|
+
magicString.prepend(`var ${deconflicted};\n`);
|
|
848
953
|
}
|
|
849
954
|
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
.trim()
|
|
855
|
-
.append(
|
|
856
|
-
`\n\nvar ${moduleName} = ${
|
|
857
|
-
isRestorableCompiledEsm
|
|
858
|
-
? `/*#__PURE__*/Object.defineProperty(${moduleExports}, '__esModule', {value: true})`
|
|
859
|
-
: moduleExports
|
|
860
|
-
};`
|
|
861
|
-
);
|
|
955
|
+
if (exportName === 'default') {
|
|
956
|
+
deconflictedDefaultExportName = deconflicted;
|
|
957
|
+
} else {
|
|
958
|
+
exports.push(exportName === deconflicted ? exportName : `${deconflicted} as ${exportName}`);
|
|
862
959
|
}
|
|
863
960
|
}
|
|
864
961
|
|
|
865
|
-
//
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
(
|
|
872
|
-
) {
|
|
873
|
-
// eslint-disable-next-line no-param-reassign
|
|
874
|
-
uses.commonjsHelpers = true;
|
|
875
|
-
defaultExport.push(
|
|
876
|
-
`export default /*@__PURE__*/${HELPERS_NAME}.getDefaultExportFromCjs(${moduleName});`
|
|
877
|
-
);
|
|
878
|
-
} else {
|
|
879
|
-
defaultExport.push(`export default ${moduleName};`);
|
|
962
|
+
// Collect and rewrite exports.__esModule assignments
|
|
963
|
+
let isRestorableCompiledEsm = false;
|
|
964
|
+
for (const expression of defineCompiledEsmExpressions) {
|
|
965
|
+
isRestorableCompiledEsm = true;
|
|
966
|
+
const moduleExportsExpression =
|
|
967
|
+
expression.type === 'CallExpression' ? expression.arguments[0] : expression.left.object;
|
|
968
|
+
magicString.overwrite(moduleExportsExpression.start, moduleExportsExpression.end, exportsName);
|
|
880
969
|
}
|
|
881
970
|
|
|
882
|
-
|
|
883
|
-
.
|
|
884
|
-
|
|
885
|
-
.
|
|
971
|
+
if (!isRestorableCompiledEsm || defaultIsModuleExports === true) {
|
|
972
|
+
exportDeclarations.push(`export default ${exportsName};`);
|
|
973
|
+
} else if (moduleExportsAssignments.length === 0 || defaultIsModuleExports === false) {
|
|
974
|
+
exports.push(`${deconflictedDefaultExportName || exportsName} as default`);
|
|
975
|
+
} else {
|
|
976
|
+
exportDeclarations.push(
|
|
977
|
+
`export default /*@__PURE__*/${HELPERS_NAME}.getDefaultExportFromCjs(${exportsName});`
|
|
978
|
+
);
|
|
979
|
+
}
|
|
886
980
|
}
|
|
887
981
|
|
|
888
982
|
function isRequireStatement(node, scope) {
|
|
@@ -1000,44 +1094,46 @@ function getRequireHandlers() {
|
|
|
1000
1094
|
topLevelDeclarations,
|
|
1001
1095
|
topLevelRequireDeclarators,
|
|
1002
1096
|
reassignedNames,
|
|
1003
|
-
|
|
1004
|
-
dynamicRegisterSources
|
|
1097
|
+
helpersName,
|
|
1098
|
+
dynamicRegisterSources,
|
|
1099
|
+
moduleName,
|
|
1100
|
+
exportsName,
|
|
1101
|
+
id,
|
|
1102
|
+
exportMode
|
|
1005
1103
|
) {
|
|
1006
|
-
const removedDeclarators = getDeclaratorsReplacedByImportsAndSetImportNames(
|
|
1007
|
-
topLevelRequireDeclarators,
|
|
1008
|
-
requiredByNode,
|
|
1009
|
-
reassignedNames
|
|
1010
|
-
);
|
|
1011
1104
|
setRemainingImportNamesAndRewriteRequires(
|
|
1012
1105
|
requireExpressionsWithUsedReturnValue,
|
|
1013
1106
|
requiredByNode,
|
|
1014
1107
|
magicString
|
|
1015
1108
|
);
|
|
1016
|
-
|
|
1017
|
-
|
|
1018
|
-
|
|
1019
|
-
|
|
1020
|
-
|
|
1021
|
-
|
|
1022
|
-
|
|
1023
|
-
|
|
1024
|
-
|
|
1025
|
-
|
|
1026
|
-
|
|
1027
|
-
|
|
1028
|
-
|
|
1029
|
-
|
|
1030
|
-
|
|
1031
|
-
|
|
1032
|
-
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
|
|
1040
|
-
|
|
1109
|
+
const imports = [];
|
|
1110
|
+
imports.push(`import * as ${helpersName} from "${HELPERS_ID}";`);
|
|
1111
|
+
if (exportMode === 'module') {
|
|
1112
|
+
imports.push(
|
|
1113
|
+
`import { __module as ${moduleName}, exports as ${exportsName} } from ${JSON.stringify(
|
|
1114
|
+
wrapId(id, MODULE_SUFFIX)
|
|
1115
|
+
)}`
|
|
1116
|
+
);
|
|
1117
|
+
} else if (exportMode === 'exports') {
|
|
1118
|
+
imports.push(
|
|
1119
|
+
`import { __exports as ${exportsName} } from ${JSON.stringify(wrapId(id, EXPORTS_SUFFIX))}`
|
|
1120
|
+
);
|
|
1121
|
+
}
|
|
1122
|
+
for (const source of dynamicRegisterSources) {
|
|
1123
|
+
imports.push(`import ${JSON.stringify(wrapId(source, REQUIRE_SUFFIX))};`);
|
|
1124
|
+
}
|
|
1125
|
+
for (const source of requiredSources) {
|
|
1126
|
+
if (!source.startsWith('\0')) {
|
|
1127
|
+
imports.push(`import ${JSON.stringify(wrapId(source, REQUIRE_SUFFIX))};`);
|
|
1128
|
+
}
|
|
1129
|
+
const { name, nodesUsingRequired } = requiredBySource[source];
|
|
1130
|
+
imports.push(
|
|
1131
|
+
`import ${nodesUsingRequired.length ? `${name} from ` : ''}${JSON.stringify(
|
|
1132
|
+
source.startsWith('\0') ? source : wrapId(source, PROXY_SUFFIX)
|
|
1133
|
+
)};`
|
|
1134
|
+
);
|
|
1135
|
+
}
|
|
1136
|
+
return imports.length ? `${imports.join('\n')}\n\n` : '';
|
|
1041
1137
|
}
|
|
1042
1138
|
|
|
1043
1139
|
return {
|
|
@@ -1047,30 +1143,6 @@ function getRequireHandlers() {
|
|
|
1047
1143
|
};
|
|
1048
1144
|
}
|
|
1049
1145
|
|
|
1050
|
-
function getDeclaratorsReplacedByImportsAndSetImportNames(
|
|
1051
|
-
topLevelRequireDeclarators,
|
|
1052
|
-
requiredByNode,
|
|
1053
|
-
reassignedNames
|
|
1054
|
-
) {
|
|
1055
|
-
const removedDeclarators = new Set();
|
|
1056
|
-
for (const declarator of topLevelRequireDeclarators) {
|
|
1057
|
-
const { required } = requiredByNode.get(declarator.init);
|
|
1058
|
-
if (!required.name) {
|
|
1059
|
-
const potentialName = declarator.id.name;
|
|
1060
|
-
if (
|
|
1061
|
-
!reassignedNames.has(potentialName) &&
|
|
1062
|
-
!required.nodesUsingRequired.some((node) =>
|
|
1063
|
-
isLocallyShadowed(potentialName, requiredByNode.get(node).scope)
|
|
1064
|
-
)
|
|
1065
|
-
) {
|
|
1066
|
-
required.name = potentialName;
|
|
1067
|
-
removedDeclarators.add(declarator);
|
|
1068
|
-
}
|
|
1069
|
-
}
|
|
1070
|
-
}
|
|
1071
|
-
return removedDeclarators;
|
|
1072
|
-
}
|
|
1073
|
-
|
|
1074
1146
|
function setRemainingImportNamesAndRewriteRequires(
|
|
1075
1147
|
requireExpressionsWithUsedReturnValue,
|
|
1076
1148
|
requiredByNode,
|
|
@@ -1092,25 +1164,6 @@ function setRemainingImportNamesAndRewriteRequires(
|
|
|
1092
1164
|
}
|
|
1093
1165
|
}
|
|
1094
1166
|
|
|
1095
|
-
function removeDeclaratorsFromDeclarations(topLevelDeclarations, removedDeclarators, magicString) {
|
|
1096
|
-
for (const declaration of topLevelDeclarations) {
|
|
1097
|
-
let keepDeclaration = false;
|
|
1098
|
-
let [{ start }] = declaration.declarations;
|
|
1099
|
-
for (const declarator of declaration.declarations) {
|
|
1100
|
-
if (removedDeclarators.has(declarator)) {
|
|
1101
|
-
magicString.remove(start, declarator.end);
|
|
1102
|
-
} else if (!keepDeclaration) {
|
|
1103
|
-
magicString.remove(start, declarator.start);
|
|
1104
|
-
keepDeclaration = true;
|
|
1105
|
-
}
|
|
1106
|
-
start = declarator.end;
|
|
1107
|
-
}
|
|
1108
|
-
if (!keepDeclaration) {
|
|
1109
|
-
magicString.remove(declaration.start, declaration.end);
|
|
1110
|
-
}
|
|
1111
|
-
}
|
|
1112
|
-
}
|
|
1113
|
-
|
|
1114
1167
|
/* eslint-disable no-param-reassign, no-shadow, no-underscore-dangle, no-continue */
|
|
1115
1168
|
|
|
1116
1169
|
const exportsPattern = /^(?:module\.)?exports(?:\.([a-zA-Z_$][a-zA-Z_$0-9]*))?$/;
|
|
@@ -1131,7 +1184,8 @@ function transformCommonjs(
|
|
|
1131
1184
|
dynamicRequireModuleSet,
|
|
1132
1185
|
disableWrap,
|
|
1133
1186
|
commonDir,
|
|
1134
|
-
astCache
|
|
1187
|
+
astCache,
|
|
1188
|
+
defaultIsModuleExports
|
|
1135
1189
|
) {
|
|
1136
1190
|
const ast = astCache || tryParse(parse, code, id);
|
|
1137
1191
|
const magicString = new MagicString__default['default'](code);
|
|
@@ -1139,9 +1193,9 @@ function transformCommonjs(
|
|
|
1139
1193
|
module: false,
|
|
1140
1194
|
exports: false,
|
|
1141
1195
|
global: false,
|
|
1142
|
-
require: false
|
|
1143
|
-
commonjsHelpers: false
|
|
1196
|
+
require: false
|
|
1144
1197
|
};
|
|
1198
|
+
let usesDynamicRequire = false;
|
|
1145
1199
|
const virtualDynamicRequirePath =
|
|
1146
1200
|
isDynamicRequireModulesEnabled && getVirtualPathForDynamicRequirePath(path.dirname(id), commonDir);
|
|
1147
1201
|
let scope = pluginutils.attachScopes(ast, 'scope');
|
|
@@ -1149,13 +1203,11 @@ function transformCommonjs(
|
|
|
1149
1203
|
let programDepth = 0;
|
|
1150
1204
|
let currentTryBlockEnd = null;
|
|
1151
1205
|
let shouldWrap = false;
|
|
1152
|
-
const defineCompiledEsmExpressions = [];
|
|
1153
1206
|
|
|
1154
1207
|
const globals = new Set();
|
|
1155
1208
|
|
|
1156
1209
|
// TODO technically wrong since globals isn't populated yet, but ¯\_(ツ)_/¯
|
|
1157
|
-
const HELPERS_NAME = deconflict(scope, globals, 'commonjsHelpers');
|
|
1158
|
-
const namedExports = {};
|
|
1210
|
+
const HELPERS_NAME = deconflict([scope], globals, 'commonjsHelpers');
|
|
1159
1211
|
const dynamicRegisterSources = new Set();
|
|
1160
1212
|
let hasRemovedRequire = false;
|
|
1161
1213
|
|
|
@@ -1172,8 +1224,13 @@ function transformCommonjs(
|
|
|
1172
1224
|
const topLevelDeclarations = [];
|
|
1173
1225
|
const topLevelRequireDeclarators = new Set();
|
|
1174
1226
|
const skippedNodes = new Set();
|
|
1175
|
-
const
|
|
1176
|
-
const
|
|
1227
|
+
const moduleAccessScopes = new Set([scope]);
|
|
1228
|
+
const exportsAccessScopes = new Set([scope]);
|
|
1229
|
+
const moduleExportsAssignments = [];
|
|
1230
|
+
let firstTopLevelModuleExportsAssignment = null;
|
|
1231
|
+
const exportsAssignmentsByName = new Map();
|
|
1232
|
+
const topLevelAssignments = new Set();
|
|
1233
|
+
const topLevelDefineCompiledEsmExpressions = [];
|
|
1177
1234
|
|
|
1178
1235
|
estreeWalker.walk(ast, {
|
|
1179
1236
|
enter(node, parent) {
|
|
@@ -1213,30 +1270,46 @@ function transformCommonjs(
|
|
|
1213
1270
|
uses[flattened.name] = true;
|
|
1214
1271
|
|
|
1215
1272
|
// we're dealing with `module.exports = ...` or `[module.]exports.foo = ...` –
|
|
1216
|
-
if (
|
|
1217
|
-
|
|
1273
|
+
if (flattened.keypath === 'module.exports') {
|
|
1274
|
+
moduleExportsAssignments.push(node);
|
|
1275
|
+
if (programDepth > 3) {
|
|
1276
|
+
moduleAccessScopes.add(scope);
|
|
1277
|
+
} else if (!firstTopLevelModuleExportsAssignment) {
|
|
1278
|
+
firstTopLevelModuleExportsAssignment = node;
|
|
1279
|
+
}
|
|
1280
|
+
|
|
1281
|
+
if (defaultIsModuleExports === false) {
|
|
1282
|
+
shouldWrap = true;
|
|
1283
|
+
} else if (defaultIsModuleExports === 'auto') {
|
|
1284
|
+
if (node.right.type === 'ObjectExpression') {
|
|
1285
|
+
if (hasDefineEsmProperty(node.right)) {
|
|
1286
|
+
shouldWrap = true;
|
|
1287
|
+
}
|
|
1288
|
+
} else if (defaultIsModuleExports === false) {
|
|
1289
|
+
shouldWrap = true;
|
|
1290
|
+
}
|
|
1291
|
+
}
|
|
1218
1292
|
} else if (exportName === KEY_COMPILED_ESM) {
|
|
1219
|
-
|
|
1220
|
-
|
|
1221
|
-
|
|
1222
|
-
|
|
1223
|
-
|
|
1293
|
+
if (programDepth > 3) {
|
|
1294
|
+
shouldWrap = true;
|
|
1295
|
+
} else {
|
|
1296
|
+
topLevelDefineCompiledEsmExpressions.push(node);
|
|
1297
|
+
}
|
|
1224
1298
|
} else {
|
|
1225
|
-
|
|
1299
|
+
const exportsAssignments = exportsAssignmentsByName.get(exportName) || {
|
|
1300
|
+
nodes: [],
|
|
1301
|
+
scopes: new Set()
|
|
1302
|
+
};
|
|
1303
|
+
exportsAssignments.nodes.push(node);
|
|
1304
|
+
exportsAssignments.scopes.add(scope);
|
|
1305
|
+
exportsAccessScopes.add(scope);
|
|
1306
|
+
exportsAssignmentsByName.set(exportName, exportsAssignments);
|
|
1307
|
+
if (programDepth <= 3) {
|
|
1308
|
+
topLevelAssignments.add(node);
|
|
1309
|
+
}
|
|
1226
1310
|
}
|
|
1227
1311
|
|
|
1228
1312
|
skippedNodes.add(node.left);
|
|
1229
|
-
|
|
1230
|
-
if (flattened.keypath === 'module.exports' && node.right.type === 'ObjectExpression') {
|
|
1231
|
-
node.right.properties.forEach((prop) => {
|
|
1232
|
-
if (prop.computed || !('key' in prop) || prop.key.type !== 'Identifier') return;
|
|
1233
|
-
const { name } = prop.key;
|
|
1234
|
-
if (name === pluginutils.makeLegalIdentifier(name)) namedExports[name] = true;
|
|
1235
|
-
});
|
|
1236
|
-
return;
|
|
1237
|
-
}
|
|
1238
|
-
|
|
1239
|
-
if (exportsPatternMatch[1]) namedExports[exportsPatternMatch[1]] = true;
|
|
1240
1313
|
} else {
|
|
1241
1314
|
for (const name of pluginutils.extractAssignedNames(node.left)) {
|
|
1242
1315
|
reassignedNames.add(name);
|
|
@@ -1248,7 +1321,7 @@ function transformCommonjs(
|
|
|
1248
1321
|
if (programDepth === 3 && parent.type === 'ExpressionStatement') {
|
|
1249
1322
|
// skip special handling for [module.]exports until we know we render this
|
|
1250
1323
|
skippedNodes.add(node.arguments[0]);
|
|
1251
|
-
|
|
1324
|
+
topLevelDefineCompiledEsmExpressions.push(node);
|
|
1252
1325
|
} else {
|
|
1253
1326
|
shouldWrap = true;
|
|
1254
1327
|
}
|
|
@@ -1276,7 +1349,6 @@ function transformCommonjs(
|
|
|
1276
1349
|
storeName: true
|
|
1277
1350
|
}
|
|
1278
1351
|
);
|
|
1279
|
-
uses.commonjsHelpers = true;
|
|
1280
1352
|
return;
|
|
1281
1353
|
}
|
|
1282
1354
|
|
|
@@ -1303,13 +1375,13 @@ function transformCommonjs(
|
|
|
1303
1375
|
}
|
|
1304
1376
|
|
|
1305
1377
|
let sourceId = getRequireStringArg(node);
|
|
1306
|
-
const isDynamicRegister =
|
|
1378
|
+
const isDynamicRegister = isWrappedId(sourceId, DYNAMIC_REGISTER_SUFFIX);
|
|
1307
1379
|
if (isDynamicRegister) {
|
|
1308
|
-
sourceId =
|
|
1380
|
+
sourceId = unwrapId(sourceId, DYNAMIC_REGISTER_SUFFIX);
|
|
1309
1381
|
if (sourceId.endsWith('.json')) {
|
|
1310
1382
|
sourceId = DYNAMIC_JSON_PREFIX + sourceId;
|
|
1311
1383
|
}
|
|
1312
|
-
dynamicRegisterSources.add(
|
|
1384
|
+
dynamicRegisterSources.add(wrapId(sourceId, DYNAMIC_REGISTER_SUFFIX));
|
|
1313
1385
|
} else {
|
|
1314
1386
|
if (
|
|
1315
1387
|
!sourceId.endsWith('.json') &&
|
|
@@ -1327,7 +1399,7 @@ function transformCommonjs(
|
|
|
1327
1399
|
path.dirname(id) === '.' ? null /* default behavior */ : virtualDynamicRequirePath
|
|
1328
1400
|
)})`
|
|
1329
1401
|
);
|
|
1330
|
-
|
|
1402
|
+
usesDynamicRequire = true;
|
|
1331
1403
|
}
|
|
1332
1404
|
return;
|
|
1333
1405
|
}
|
|
@@ -1384,7 +1456,6 @@ function transformCommonjs(
|
|
|
1384
1456
|
magicString.overwrite(node.start, node.end, `${HELPERS_NAME}.commonjsRequire`, {
|
|
1385
1457
|
storeName: true
|
|
1386
1458
|
});
|
|
1387
|
-
uses.commonjsHelpers = true;
|
|
1388
1459
|
}
|
|
1389
1460
|
}
|
|
1390
1461
|
|
|
@@ -1408,8 +1479,7 @@ function transformCommonjs(
|
|
|
1408
1479
|
});
|
|
1409
1480
|
}
|
|
1410
1481
|
}
|
|
1411
|
-
|
|
1412
|
-
uses.commonjsHelpers = true;
|
|
1482
|
+
usesDynamicRequire = true;
|
|
1413
1483
|
return;
|
|
1414
1484
|
case 'module':
|
|
1415
1485
|
case 'exports':
|
|
@@ -1422,11 +1492,12 @@ function transformCommonjs(
|
|
|
1422
1492
|
magicString.overwrite(node.start, node.end, `${HELPERS_NAME}.commonjsGlobal`, {
|
|
1423
1493
|
storeName: true
|
|
1424
1494
|
});
|
|
1425
|
-
uses.commonjsHelpers = true;
|
|
1426
1495
|
}
|
|
1427
1496
|
return;
|
|
1428
1497
|
case 'define':
|
|
1429
|
-
magicString.overwrite(node.start, node.end, 'undefined', {
|
|
1498
|
+
magicString.overwrite(node.start, node.end, 'undefined', {
|
|
1499
|
+
storeName: true
|
|
1500
|
+
});
|
|
1430
1501
|
return;
|
|
1431
1502
|
default:
|
|
1432
1503
|
globals.add(name);
|
|
@@ -1438,7 +1509,6 @@ function transformCommonjs(
|
|
|
1438
1509
|
magicString.overwrite(node.start, node.end, `${HELPERS_NAME}.commonjsRequire`, {
|
|
1439
1510
|
storeName: true
|
|
1440
1511
|
});
|
|
1441
|
-
uses.commonjsHelpers = true;
|
|
1442
1512
|
skippedNodes.add(node.object);
|
|
1443
1513
|
skippedNodes.add(node.property);
|
|
1444
1514
|
}
|
|
@@ -1457,7 +1527,6 @@ function transformCommonjs(
|
|
|
1457
1527
|
magicString.overwrite(node.start, node.end, `${HELPERS_NAME}.commonjsGlobal`, {
|
|
1458
1528
|
storeName: true
|
|
1459
1529
|
});
|
|
1460
|
-
uses.commonjsHelpers = true;
|
|
1461
1530
|
}
|
|
1462
1531
|
}
|
|
1463
1532
|
return;
|
|
@@ -1474,7 +1543,9 @@ function transformCommonjs(
|
|
|
1474
1543
|
flattened.keypath === 'module' ||
|
|
1475
1544
|
flattened.keypath === 'exports'
|
|
1476
1545
|
) {
|
|
1477
|
-
magicString.overwrite(node.start, node.end, `'object'`, {
|
|
1546
|
+
magicString.overwrite(node.start, node.end, `'object'`, {
|
|
1547
|
+
storeName: false
|
|
1548
|
+
});
|
|
1478
1549
|
}
|
|
1479
1550
|
}
|
|
1480
1551
|
return;
|
|
@@ -1492,23 +1563,22 @@ function transformCommonjs(
|
|
|
1492
1563
|
}
|
|
1493
1564
|
});
|
|
1494
1565
|
|
|
1495
|
-
|
|
1496
|
-
|
|
1497
|
-
|
|
1498
|
-
|
|
1499
|
-
|
|
1500
|
-
|
|
1501
|
-
defineCompiledEsmExpressions[0].end
|
|
1502
|
-
);
|
|
1503
|
-
} else {
|
|
1504
|
-
shouldWrap = true;
|
|
1505
|
-
uses.exports = true;
|
|
1506
|
-
}
|
|
1566
|
+
const nameBase = getName(id);
|
|
1567
|
+
const exportsName = deconflict([...exportsAccessScopes], globals, nameBase);
|
|
1568
|
+
const moduleName = deconflict([...moduleAccessScopes], globals, `${nameBase}Module`);
|
|
1569
|
+
const deconflictedExportNames = Object.create(null);
|
|
1570
|
+
for (const [exportName, { scopes }] of exportsAssignmentsByName) {
|
|
1571
|
+
deconflictedExportNames[exportName] = deconflict([...scopes], globals, exportName);
|
|
1507
1572
|
}
|
|
1508
1573
|
|
|
1509
1574
|
// We cannot wrap ES/mixed modules
|
|
1510
|
-
shouldWrap =
|
|
1511
|
-
|
|
1575
|
+
shouldWrap =
|
|
1576
|
+
!isEsModule &&
|
|
1577
|
+
!disableWrap &&
|
|
1578
|
+
(shouldWrap || (uses.exports && moduleExportsAssignments.length > 0));
|
|
1579
|
+
const detectWrappedDefault =
|
|
1580
|
+
shouldWrap &&
|
|
1581
|
+
(topLevelDefineCompiledEsmExpressions.length > 0 || code.indexOf('__esModule') >= 0);
|
|
1512
1582
|
|
|
1513
1583
|
if (
|
|
1514
1584
|
!(
|
|
@@ -1517,17 +1587,15 @@ function transformCommonjs(
|
|
|
1517
1587
|
uses.module ||
|
|
1518
1588
|
uses.exports ||
|
|
1519
1589
|
uses.require ||
|
|
1520
|
-
|
|
1590
|
+
usesDynamicRequire ||
|
|
1521
1591
|
hasRemovedRequire ||
|
|
1522
|
-
|
|
1592
|
+
topLevelDefineCompiledEsmExpressions.length > 0
|
|
1523
1593
|
) &&
|
|
1524
1594
|
(ignoreGlobal || !uses.global)
|
|
1525
1595
|
) {
|
|
1526
1596
|
return { meta: { commonjs: { isCommonJS: false } } };
|
|
1527
1597
|
}
|
|
1528
1598
|
|
|
1529
|
-
const moduleName = deconflict(scope, globals, getName(id));
|
|
1530
|
-
|
|
1531
1599
|
let leadingComment = '';
|
|
1532
1600
|
if (code.startsWith('/*')) {
|
|
1533
1601
|
const commentEnd = code.indexOf('*/', 2) + 2;
|
|
@@ -1535,33 +1603,53 @@ function transformCommonjs(
|
|
|
1535
1603
|
magicString.remove(0, commentEnd).trim();
|
|
1536
1604
|
}
|
|
1537
1605
|
|
|
1606
|
+
const exportMode = shouldWrap
|
|
1607
|
+
? uses.module
|
|
1608
|
+
? 'module'
|
|
1609
|
+
: 'exports'
|
|
1610
|
+
: firstTopLevelModuleExportsAssignment
|
|
1611
|
+
? exportsAssignmentsByName.size === 0 && topLevelDefineCompiledEsmExpressions.length === 0
|
|
1612
|
+
? 'replace'
|
|
1613
|
+
: 'module'
|
|
1614
|
+
: moduleExportsAssignments.length === 0
|
|
1615
|
+
? 'exports'
|
|
1616
|
+
: 'module';
|
|
1617
|
+
|
|
1618
|
+
const importBlock = rewriteRequireExpressionsAndGetImportBlock(
|
|
1619
|
+
magicString,
|
|
1620
|
+
topLevelDeclarations,
|
|
1621
|
+
topLevelRequireDeclarators,
|
|
1622
|
+
reassignedNames,
|
|
1623
|
+
HELPERS_NAME,
|
|
1624
|
+
dynamicRegisterSources,
|
|
1625
|
+
moduleName,
|
|
1626
|
+
exportsName,
|
|
1627
|
+
id,
|
|
1628
|
+
exportMode
|
|
1629
|
+
);
|
|
1630
|
+
|
|
1538
1631
|
const exportBlock = isEsModule
|
|
1539
1632
|
? ''
|
|
1540
1633
|
: rewriteExportsAndGetExportsBlock(
|
|
1541
1634
|
magicString,
|
|
1542
1635
|
moduleName,
|
|
1636
|
+
exportsName,
|
|
1543
1637
|
shouldWrap,
|
|
1544
|
-
|
|
1545
|
-
|
|
1546
|
-
|
|
1547
|
-
|
|
1548
|
-
|
|
1638
|
+
moduleExportsAssignments,
|
|
1639
|
+
firstTopLevelModuleExportsAssignment,
|
|
1640
|
+
exportsAssignmentsByName,
|
|
1641
|
+
topLevelAssignments,
|
|
1642
|
+
topLevelDefineCompiledEsmExpressions,
|
|
1643
|
+
deconflictedExportNames,
|
|
1549
1644
|
code,
|
|
1550
|
-
|
|
1551
|
-
|
|
1645
|
+
HELPERS_NAME,
|
|
1646
|
+
exportMode,
|
|
1647
|
+
detectWrappedDefault,
|
|
1648
|
+
defaultIsModuleExports
|
|
1552
1649
|
);
|
|
1553
1650
|
|
|
1554
|
-
const importBlock = rewriteRequireExpressionsAndGetImportBlock(
|
|
1555
|
-
magicString,
|
|
1556
|
-
topLevelDeclarations,
|
|
1557
|
-
topLevelRequireDeclarators,
|
|
1558
|
-
reassignedNames,
|
|
1559
|
-
uses.commonjsHelpers && HELPERS_NAME,
|
|
1560
|
-
dynamicRegisterSources
|
|
1561
|
-
);
|
|
1562
|
-
|
|
1563
1651
|
if (shouldWrap) {
|
|
1564
|
-
wrapCode(magicString, uses, moduleName,
|
|
1652
|
+
wrapCode(magicString, uses, moduleName, exportsName);
|
|
1565
1653
|
}
|
|
1566
1654
|
|
|
1567
1655
|
magicString
|
|
@@ -1597,6 +1685,8 @@ function commonjs(options = {}) {
|
|
|
1597
1685
|
: Array.isArray(esmExternals)
|
|
1598
1686
|
? ((esmExternalIds = new Set(esmExternals)), (id) => esmExternalIds.has(id))
|
|
1599
1687
|
: () => esmExternals;
|
|
1688
|
+
const defaultIsModuleExports =
|
|
1689
|
+
typeof options.defaultIsModuleExports === 'boolean' ? options.defaultIsModuleExports : 'auto';
|
|
1600
1690
|
|
|
1601
1691
|
const { dynamicRequireModuleSet, dynamicRequireModuleDirPaths } = getDynamicRequirePaths(
|
|
1602
1692
|
options.dynamicRequireTargets
|
|
@@ -1608,6 +1698,7 @@ function commonjs(options = {}) {
|
|
|
1608
1698
|
|
|
1609
1699
|
const esModulesWithDefaultExport = new Set();
|
|
1610
1700
|
const esModulesWithNamedExports = new Set();
|
|
1701
|
+
const commonJsMetaPromises = new Map();
|
|
1611
1702
|
|
|
1612
1703
|
const ignoreRequire =
|
|
1613
1704
|
typeof options.ignore === 'function'
|
|
@@ -1660,13 +1751,11 @@ function commonjs(options = {}) {
|
|
|
1660
1751
|
return { meta: { commonjs: { isCommonJS: false } } };
|
|
1661
1752
|
}
|
|
1662
1753
|
|
|
1663
|
-
|
|
1664
|
-
|
|
1665
|
-
|
|
1666
|
-
if (isModuleRegisterProxy(id)) {
|
|
1667
|
-
disableWrap = true;
|
|
1754
|
+
// avoid wrapping as this is a commonjsRegister call
|
|
1755
|
+
const disableWrap = isWrappedId(id, DYNAMIC_REGISTER_SUFFIX);
|
|
1756
|
+
if (disableWrap) {
|
|
1668
1757
|
// eslint-disable-next-line no-param-reassign
|
|
1669
|
-
id =
|
|
1758
|
+
id = unwrapId(id, DYNAMIC_REGISTER_SUFFIX);
|
|
1670
1759
|
}
|
|
1671
1760
|
|
|
1672
1761
|
return transformCommonjs(
|
|
@@ -1683,7 +1772,8 @@ function commonjs(options = {}) {
|
|
|
1683
1772
|
dynamicRequireModuleSet,
|
|
1684
1773
|
disableWrap,
|
|
1685
1774
|
commonDir,
|
|
1686
|
-
ast
|
|
1775
|
+
ast,
|
|
1776
|
+
defaultIsModuleExports
|
|
1687
1777
|
);
|
|
1688
1778
|
}
|
|
1689
1779
|
|
|
@@ -1710,6 +1800,39 @@ function commonjs(options = {}) {
|
|
|
1710
1800
|
return getSpecificHelperProxy(id);
|
|
1711
1801
|
}
|
|
1712
1802
|
|
|
1803
|
+
if (isWrappedId(id, MODULE_SUFFIX)) {
|
|
1804
|
+
const actualId = unwrapId(id, MODULE_SUFFIX);
|
|
1805
|
+
let name = getName(actualId);
|
|
1806
|
+
let code;
|
|
1807
|
+
if (isDynamicRequireModulesEnabled) {
|
|
1808
|
+
if (['modulePath', 'commonjsRequire', 'createModule'].includes(name)) {
|
|
1809
|
+
name = `${name}_`;
|
|
1810
|
+
}
|
|
1811
|
+
code =
|
|
1812
|
+
`import {commonjsRequire, createModule} from "${HELPERS_ID}";\n` +
|
|
1813
|
+
`var ${name} = createModule(${JSON.stringify(
|
|
1814
|
+
getVirtualPathForDynamicRequirePath(path.dirname(actualId), commonDir)
|
|
1815
|
+
)});\n` +
|
|
1816
|
+
`export {${name} as __module}`;
|
|
1817
|
+
} else {
|
|
1818
|
+
code = `var ${name} = {exports: {}}; export {${name} as __module}`;
|
|
1819
|
+
}
|
|
1820
|
+
return {
|
|
1821
|
+
code,
|
|
1822
|
+
syntheticNamedExports: '__module',
|
|
1823
|
+
meta: { commonjs: { isCommonJS: false } }
|
|
1824
|
+
};
|
|
1825
|
+
}
|
|
1826
|
+
|
|
1827
|
+
if (isWrappedId(id, EXPORTS_SUFFIX)) {
|
|
1828
|
+
const actualId = unwrapId(id, EXPORTS_SUFFIX);
|
|
1829
|
+
const name = getName(actualId);
|
|
1830
|
+
return {
|
|
1831
|
+
code: `var ${name} = {}; export {${name} as __exports}`,
|
|
1832
|
+
meta: { commonjs: { isCommonJS: false } }
|
|
1833
|
+
};
|
|
1834
|
+
}
|
|
1835
|
+
|
|
1713
1836
|
if (isWrappedId(id, EXTERNAL_SUFFIX)) {
|
|
1714
1837
|
const actualId = unwrapId(id, EXTERNAL_SUFFIX);
|
|
1715
1838
|
return getUnknownRequireProxy(
|
|
@@ -1730,9 +1853,9 @@ function commonjs(options = {}) {
|
|
|
1730
1853
|
return `export default require(${JSON.stringify(normalizePathSlashes(id))});`;
|
|
1731
1854
|
}
|
|
1732
1855
|
|
|
1733
|
-
if (
|
|
1856
|
+
if (isWrappedId(id, DYNAMIC_REGISTER_SUFFIX)) {
|
|
1734
1857
|
return getDynamicRequireProxy(
|
|
1735
|
-
normalizePathSlashes(
|
|
1858
|
+
normalizePathSlashes(unwrapId(id, DYNAMIC_REGISTER_SUFFIX)),
|
|
1736
1859
|
commonDir
|
|
1737
1860
|
);
|
|
1738
1861
|
}
|
|
@@ -1743,7 +1866,8 @@ function commonjs(options = {}) {
|
|
|
1743
1866
|
actualId,
|
|
1744
1867
|
getRequireReturnsDefault(actualId),
|
|
1745
1868
|
esModulesWithDefaultExport,
|
|
1746
|
-
esModulesWithNamedExports
|
|
1869
|
+
esModulesWithNamedExports,
|
|
1870
|
+
commonJsMetaPromises
|
|
1747
1871
|
);
|
|
1748
1872
|
}
|
|
1749
1873
|
|
|
@@ -1753,8 +1877,8 @@ function commonjs(options = {}) {
|
|
|
1753
1877
|
transform(code, rawId) {
|
|
1754
1878
|
let id = rawId;
|
|
1755
1879
|
|
|
1756
|
-
if (
|
|
1757
|
-
id =
|
|
1880
|
+
if (isWrappedId(id, DYNAMIC_REGISTER_SUFFIX)) {
|
|
1881
|
+
id = unwrapId(id, DYNAMIC_REGISTER_SUFFIX);
|
|
1758
1882
|
}
|
|
1759
1883
|
|
|
1760
1884
|
const extName = path.extname(id);
|
|
@@ -1774,16 +1898,12 @@ function commonjs(options = {}) {
|
|
|
1774
1898
|
}
|
|
1775
1899
|
},
|
|
1776
1900
|
|
|
1777
|
-
|
|
1778
|
-
|
|
1779
|
-
|
|
1780
|
-
|
|
1781
|
-
if (isCjs != null) {
|
|
1782
|
-
setIsCjsPromise(id, isCjs);
|
|
1783
|
-
return;
|
|
1784
|
-
}
|
|
1901
|
+
moduleParsed({ id, meta: { commonjs: commonjsMeta } }) {
|
|
1902
|
+
if (commonjsMeta && commonjsMeta.isCommonJS != null) {
|
|
1903
|
+
setCommonJSMetaPromise(commonJsMetaPromises, id, commonjsMeta);
|
|
1904
|
+
return;
|
|
1785
1905
|
}
|
|
1786
|
-
|
|
1906
|
+
setCommonJSMetaPromise(commonJsMetaPromises, id, null);
|
|
1787
1907
|
}
|
|
1788
1908
|
};
|
|
1789
1909
|
}
|