@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.es.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { basename, extname, dirname,
|
|
1
|
+
import { basename, extname, dirname, join, resolve, sep } from 'path';
|
|
2
2
|
import { makeLegalIdentifier, attachScopes, extractAssignedNames, createFilter } from '@rollup/pluginutils';
|
|
3
3
|
import getCommonDir from 'commondir';
|
|
4
4
|
import { existsSync, readFileSync, statSync } from 'fs';
|
|
@@ -9,7 +9,7 @@ import isReference from 'is-reference';
|
|
|
9
9
|
import { sync } from 'resolve';
|
|
10
10
|
|
|
11
11
|
var peerDependencies = {
|
|
12
|
-
rollup: "^2.
|
|
12
|
+
rollup: "^2.38.3"
|
|
13
13
|
};
|
|
14
14
|
|
|
15
15
|
function tryParse(parse, code, id) {
|
|
@@ -83,6 +83,8 @@ const unwrapId = (wrappedId, suffix) => wrappedId.slice(1, -suffix.length);
|
|
|
83
83
|
const PROXY_SUFFIX = '?commonjs-proxy';
|
|
84
84
|
const REQUIRE_SUFFIX = '?commonjs-require';
|
|
85
85
|
const EXTERNAL_SUFFIX = '?commonjs-external';
|
|
86
|
+
const EXPORTS_SUFFIX = '?commonjs-exports';
|
|
87
|
+
const MODULE_SUFFIX = '?commonjs-module';
|
|
86
88
|
|
|
87
89
|
const DYNAMIC_REGISTER_SUFFIX = '?commonjs-dynamic-register';
|
|
88
90
|
const DYNAMIC_JSON_PREFIX = '\0commonjs-dynamic-json:';
|
|
@@ -129,33 +131,38 @@ export function getAugmentedNamespace(n) {
|
|
|
129
131
|
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.');`;
|
|
130
132
|
|
|
131
133
|
const HELPER_NON_DYNAMIC = `
|
|
132
|
-
export function createCommonjsModule(fn) {
|
|
133
|
-
var module = { exports: {} }
|
|
134
|
-
return fn(module, module.exports), module.exports;
|
|
135
|
-
}
|
|
136
|
-
|
|
137
134
|
export function commonjsRequire (path) {
|
|
138
135
|
${FAILED_REQUIRE_ERROR}
|
|
139
136
|
}
|
|
140
137
|
`;
|
|
141
138
|
|
|
142
139
|
const getDynamicHelpers = (ignoreDynamicRequires) => `
|
|
143
|
-
export function
|
|
144
|
-
return
|
|
145
|
-
path:
|
|
140
|
+
export function createModule(modulePath) {
|
|
141
|
+
return {
|
|
142
|
+
path: modulePath,
|
|
146
143
|
exports: {},
|
|
147
144
|
require: function (path, base) {
|
|
148
|
-
return commonjsRequire(path,
|
|
145
|
+
return commonjsRequire(path, base == null ? modulePath : base);
|
|
149
146
|
}
|
|
150
|
-
}
|
|
147
|
+
};
|
|
151
148
|
}
|
|
152
149
|
|
|
153
150
|
export function commonjsRegister (path, loader) {
|
|
154
151
|
DYNAMIC_REQUIRE_LOADERS[path] = loader;
|
|
155
152
|
}
|
|
156
153
|
|
|
154
|
+
export function commonjsRegisterOrShort (path, to) {
|
|
155
|
+
const resolvedPath = commonjsResolveImpl(path, null, true);
|
|
156
|
+
if (resolvedPath !== null && DYNAMIC_REQUIRE_CACHE[resolvedPath]) {
|
|
157
|
+
DYNAMIC_REQUIRE_CACHE[path] = DYNAMIC_REQUIRE_CACHE[resolvedPath];
|
|
158
|
+
} else {
|
|
159
|
+
DYNAMIC_REQUIRE_SHORTS[path] = to;
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
|
|
157
163
|
const DYNAMIC_REQUIRE_LOADERS = Object.create(null);
|
|
158
164
|
const DYNAMIC_REQUIRE_CACHE = Object.create(null);
|
|
165
|
+
const DYNAMIC_REQUIRE_SHORTS = Object.create(null);
|
|
159
166
|
const DEFAULT_PARENT_MODULE = {
|
|
160
167
|
id: '<' + 'rollup>', exports: {}, parent: undefined, filename: null, loaded: false, children: [], paths: []
|
|
161
168
|
};
|
|
@@ -260,10 +267,13 @@ export function commonjsResolveImpl (path, originalModuleDir, testCache) {
|
|
|
260
267
|
const resolvedPath = relPath + CHECKED_EXTENSIONS[extensionIndex];
|
|
261
268
|
if (DYNAMIC_REQUIRE_CACHE[resolvedPath]) {
|
|
262
269
|
return resolvedPath;
|
|
263
|
-
}
|
|
270
|
+
}
|
|
271
|
+
if (DYNAMIC_REQUIRE_SHORTS[resolvedPath]) {
|
|
272
|
+
return resolvedPath;
|
|
273
|
+
}
|
|
264
274
|
if (DYNAMIC_REQUIRE_LOADERS[resolvedPath]) {
|
|
265
275
|
return resolvedPath;
|
|
266
|
-
}
|
|
276
|
+
}
|
|
267
277
|
}
|
|
268
278
|
if (!shouldTryNodeModules) break;
|
|
269
279
|
const nextDir = normalize(originalModuleDir + '/..');
|
|
@@ -282,10 +292,17 @@ export function commonjsResolve (path, originalModuleDir) {
|
|
|
282
292
|
}
|
|
283
293
|
|
|
284
294
|
export function commonjsRequire (path, originalModuleDir) {
|
|
285
|
-
|
|
295
|
+
let resolvedPath = commonjsResolveImpl(path, originalModuleDir, true);
|
|
286
296
|
if (resolvedPath !== null) {
|
|
287
297
|
let cachedModule = DYNAMIC_REQUIRE_CACHE[resolvedPath];
|
|
288
298
|
if (cachedModule) return cachedModule.exports;
|
|
299
|
+
let shortTo = DYNAMIC_REQUIRE_SHORTS[resolvedPath];
|
|
300
|
+
if (shortTo) {
|
|
301
|
+
cachedModule = DYNAMIC_REQUIRE_CACHE[shortTo];
|
|
302
|
+
if (cachedModule)
|
|
303
|
+
return cachedModule.exports;
|
|
304
|
+
resolvedPath = commonjsResolveImpl(shortTo, null, true);
|
|
305
|
+
}
|
|
289
306
|
const loader = DYNAMIC_REQUIRE_LOADERS[resolvedPath];
|
|
290
307
|
if (loader) {
|
|
291
308
|
DYNAMIC_REQUIRE_CACHE[resolvedPath] = cachedModule = {
|
|
@@ -326,16 +343,20 @@ function getHelpersModule(isDynamicRequireModulesEnabled, ignoreDynamicRequires)
|
|
|
326
343
|
|
|
327
344
|
/* eslint-disable import/prefer-default-export */
|
|
328
345
|
|
|
329
|
-
function deconflict(
|
|
346
|
+
function deconflict(scopes, globals, identifier) {
|
|
330
347
|
let i = 1;
|
|
331
348
|
let deconflicted = makeLegalIdentifier(identifier);
|
|
349
|
+
const hasConflicts = () =>
|
|
350
|
+
scopes.some((scope) => scope.contains(deconflicted)) || globals.has(deconflicted);
|
|
332
351
|
|
|
333
|
-
while (
|
|
352
|
+
while (hasConflicts()) {
|
|
334
353
|
deconflicted = makeLegalIdentifier(`${identifier}_${i}`);
|
|
335
354
|
i += 1;
|
|
336
355
|
}
|
|
337
|
-
|
|
338
|
-
scope
|
|
356
|
+
|
|
357
|
+
for (const scope of scopes) {
|
|
358
|
+
scope.declarations[deconflicted] = true;
|
|
359
|
+
}
|
|
339
360
|
|
|
340
361
|
return deconflicted;
|
|
341
362
|
}
|
|
@@ -345,8 +366,7 @@ function getName(id) {
|
|
|
345
366
|
if (name !== 'index') {
|
|
346
367
|
return name;
|
|
347
368
|
}
|
|
348
|
-
|
|
349
|
-
return makeLegalIdentifier(segments[segments.length - 1]);
|
|
369
|
+
return makeLegalIdentifier(basename(dirname(id)));
|
|
350
370
|
}
|
|
351
371
|
|
|
352
372
|
function normalizePathSlashes(path) {
|
|
@@ -378,15 +398,13 @@ function getPackageEntryPoint(dirPath) {
|
|
|
378
398
|
}
|
|
379
399
|
|
|
380
400
|
function getDynamicPackagesModule(dynamicRequireModuleDirPaths, commonDir) {
|
|
381
|
-
let code = `const
|
|
401
|
+
let code = `const commonjsRegisterOrShort = require('${HELPERS_ID}?commonjsRegisterOrShort');`;
|
|
382
402
|
for (const dir of dynamicRequireModuleDirPaths) {
|
|
383
403
|
const entryPoint = getPackageEntryPoint(dir);
|
|
384
404
|
|
|
385
|
-
code += `\
|
|
405
|
+
code += `\ncommonjsRegisterOrShort(${JSON.stringify(
|
|
386
406
|
getVirtualPathForDynamicRequirePath(dir, commonDir)
|
|
387
|
-
)},
|
|
388
|
-
module.exports = require(${JSON.stringify(normalizePathSlashes(join(dir, entryPoint)))});
|
|
389
|
-
});`;
|
|
407
|
+
)}, ${JSON.stringify(getVirtualPathForDynamicRequirePath(join(dir, entryPoint), commonDir))});`;
|
|
390
408
|
}
|
|
391
409
|
return code;
|
|
392
410
|
}
|
|
@@ -397,28 +415,18 @@ function getDynamicPackagesEntryIntro(
|
|
|
397
415
|
) {
|
|
398
416
|
let dynamicImports = Array.from(
|
|
399
417
|
dynamicRequireModuleSet,
|
|
400
|
-
(dynamicId) => `require(${JSON.stringify(
|
|
418
|
+
(dynamicId) => `require(${JSON.stringify(wrapId(dynamicId, DYNAMIC_REGISTER_SUFFIX))});`
|
|
401
419
|
).join('\n');
|
|
402
420
|
|
|
403
421
|
if (dynamicRequireModuleDirPaths.length) {
|
|
404
|
-
dynamicImports += `require(${JSON.stringify(
|
|
422
|
+
dynamicImports += `require(${JSON.stringify(
|
|
423
|
+
wrapId(DYNAMIC_PACKAGES_ID, DYNAMIC_REGISTER_SUFFIX)
|
|
424
|
+
)});`;
|
|
405
425
|
}
|
|
406
426
|
|
|
407
427
|
return dynamicImports;
|
|
408
428
|
}
|
|
409
429
|
|
|
410
|
-
function wrapModuleRegisterProxy(id) {
|
|
411
|
-
return wrapId(id, DYNAMIC_REGISTER_SUFFIX);
|
|
412
|
-
}
|
|
413
|
-
|
|
414
|
-
function unwrapModuleRegisterProxy(id) {
|
|
415
|
-
return unwrapId(id, DYNAMIC_REGISTER_SUFFIX);
|
|
416
|
-
}
|
|
417
|
-
|
|
418
|
-
function isModuleRegisterProxy(id) {
|
|
419
|
-
return isWrappedId(id, DYNAMIC_REGISTER_SUFFIX);
|
|
420
|
-
}
|
|
421
|
-
|
|
422
430
|
function isDynamicModuleImport(id, dynamicRequireModuleSet) {
|
|
423
431
|
const normalizedPath = normalizePathSlashes(id);
|
|
424
432
|
return dynamicRequireModuleSet.has(normalizedPath) && !normalizedPath.endsWith('.json');
|
|
@@ -451,37 +459,37 @@ function getDynamicRequirePaths(patterns) {
|
|
|
451
459
|
return { dynamicRequireModuleSet, dynamicRequireModuleDirPaths };
|
|
452
460
|
}
|
|
453
461
|
|
|
454
|
-
function
|
|
455
|
-
let
|
|
456
|
-
if (
|
|
462
|
+
function getCommonJSMetaPromise(commonJSMetaPromises, id) {
|
|
463
|
+
let commonJSMetaPromise = commonJSMetaPromises.get(id);
|
|
464
|
+
if (commonJSMetaPromise) return commonJSMetaPromise.promise;
|
|
457
465
|
|
|
458
466
|
const promise = new Promise((resolve) => {
|
|
459
|
-
|
|
467
|
+
commonJSMetaPromise = {
|
|
460
468
|
resolve,
|
|
461
469
|
promise: null
|
|
462
470
|
};
|
|
463
|
-
|
|
471
|
+
commonJSMetaPromises.set(id, commonJSMetaPromise);
|
|
464
472
|
});
|
|
465
|
-
|
|
473
|
+
commonJSMetaPromise.promise = promise;
|
|
466
474
|
|
|
467
475
|
return promise;
|
|
468
476
|
}
|
|
469
477
|
|
|
470
|
-
function
|
|
471
|
-
const
|
|
472
|
-
if (
|
|
473
|
-
if (
|
|
474
|
-
|
|
475
|
-
|
|
478
|
+
function setCommonJSMetaPromise(commonJSMetaPromises, id, commonjsMeta) {
|
|
479
|
+
const commonJSMetaPromise = commonJSMetaPromises.get(id);
|
|
480
|
+
if (commonJSMetaPromise) {
|
|
481
|
+
if (commonJSMetaPromise.resolve) {
|
|
482
|
+
commonJSMetaPromise.resolve(commonjsMeta);
|
|
483
|
+
commonJSMetaPromise.resolve = null;
|
|
476
484
|
}
|
|
477
485
|
} else {
|
|
478
|
-
|
|
486
|
+
commonJSMetaPromises.set(id, { promise: Promise.resolve(commonjsMeta), resolve: null });
|
|
479
487
|
}
|
|
480
488
|
}
|
|
481
489
|
|
|
482
490
|
// e.g. id === "commonjsHelpers?commonjsRegister"
|
|
483
491
|
function getSpecificHelperProxy(id) {
|
|
484
|
-
return `export {${id.split('?')[1]} as default} from
|
|
492
|
+
return `export {${id.split('?')[1]} as default} from "${HELPERS_ID}";`;
|
|
485
493
|
}
|
|
486
494
|
|
|
487
495
|
function getUnknownRequireProxy(id, requireReturnsDefault) {
|
|
@@ -522,16 +530,16 @@ async function getStaticRequireProxy(
|
|
|
522
530
|
requireReturnsDefault,
|
|
523
531
|
esModulesWithDefaultExport,
|
|
524
532
|
esModulesWithNamedExports,
|
|
525
|
-
|
|
533
|
+
commonJsMetaPromises
|
|
526
534
|
) {
|
|
527
535
|
const name = getName(id);
|
|
528
|
-
const
|
|
529
|
-
if (
|
|
530
|
-
return `
|
|
531
|
-
} else if (
|
|
536
|
+
const commonjsMeta = await getCommonJSMetaPromise(commonJsMetaPromises, id);
|
|
537
|
+
if (commonjsMeta && commonjsMeta.isCommonJS) {
|
|
538
|
+
return `export { __moduleExports as default } from ${JSON.stringify(id)};`;
|
|
539
|
+
} else if (commonjsMeta === null) {
|
|
532
540
|
return getUnknownRequireProxy(id, requireReturnsDefault);
|
|
533
541
|
} else if (!requireReturnsDefault) {
|
|
534
|
-
return `import {getAugmentedNamespace} from "${HELPERS_ID}"; import * as ${name} from ${JSON.stringify(
|
|
542
|
+
return `import { getAugmentedNamespace } from "${HELPERS_ID}"; import * as ${name} from ${JSON.stringify(
|
|
535
543
|
id
|
|
536
544
|
)}; export default /*@__PURE__*/getAugmentedNamespace(${name});`;
|
|
537
545
|
} else if (
|
|
@@ -542,7 +550,7 @@ async function getStaticRequireProxy(
|
|
|
542
550
|
) {
|
|
543
551
|
return `import * as ${name} from ${JSON.stringify(id)}; export default ${name};`;
|
|
544
552
|
}
|
|
545
|
-
return `export {default} from ${JSON.stringify(id)};`;
|
|
553
|
+
return `export { default } from ${JSON.stringify(id)};`;
|
|
546
554
|
}
|
|
547
555
|
|
|
548
556
|
/* eslint-disable no-param-reassign, no-undefined */
|
|
@@ -579,12 +587,17 @@ function getResolveId(extensions) {
|
|
|
579
587
|
}
|
|
580
588
|
|
|
581
589
|
return function resolveId(importee, rawImporter) {
|
|
590
|
+
if (isWrappedId(importee, MODULE_SUFFIX) || isWrappedId(importee, EXPORTS_SUFFIX)) {
|
|
591
|
+
return importee;
|
|
592
|
+
}
|
|
593
|
+
|
|
582
594
|
const importer =
|
|
583
|
-
rawImporter &&
|
|
584
|
-
?
|
|
595
|
+
rawImporter && isWrappedId(rawImporter, DYNAMIC_REGISTER_SUFFIX)
|
|
596
|
+
? unwrapId(rawImporter, DYNAMIC_REGISTER_SUFFIX)
|
|
585
597
|
: rawImporter;
|
|
586
598
|
|
|
587
|
-
//
|
|
599
|
+
// Except for exports, proxies are only importing resolved ids,
|
|
600
|
+
// no need to resolve again
|
|
588
601
|
if (importer && isWrappedId(importer, PROXY_SUFFIX)) {
|
|
589
602
|
return importee;
|
|
590
603
|
}
|
|
@@ -598,9 +611,9 @@ function getResolveId(extensions) {
|
|
|
598
611
|
} else if (isRequiredModule) {
|
|
599
612
|
importee = unwrapId(importee, REQUIRE_SUFFIX);
|
|
600
613
|
|
|
601
|
-
isModuleRegistration =
|
|
614
|
+
isModuleRegistration = isWrappedId(importee, DYNAMIC_REGISTER_SUFFIX);
|
|
602
615
|
if (isModuleRegistration) {
|
|
603
|
-
importee =
|
|
616
|
+
importee = unwrapId(importee, DYNAMIC_REGISTER_SUFFIX);
|
|
604
617
|
}
|
|
605
618
|
}
|
|
606
619
|
|
|
@@ -627,7 +640,7 @@ function getResolveId(extensions) {
|
|
|
627
640
|
resolved.id = wrapId(resolved.id, resolved.external ? EXTERNAL_SUFFIX : PROXY_SUFFIX);
|
|
628
641
|
resolved.external = false;
|
|
629
642
|
} else if (resolved && isModuleRegistration) {
|
|
630
|
-
resolved.id =
|
|
643
|
+
resolved.id = wrapId(resolved.id, DYNAMIC_REGISTER_SUFFIX);
|
|
631
644
|
} else if (!resolved && (isProxyModule || isRequiredModule)) {
|
|
632
645
|
return { id: wrapId(importee, EXTERNAL_SUFFIX), external: false };
|
|
633
646
|
}
|
|
@@ -727,8 +740,6 @@ function isDefineCompiledEsm(node) {
|
|
|
727
740
|
}
|
|
728
741
|
|
|
729
742
|
function getDefinePropertyCallName(node, targetName) {
|
|
730
|
-
const targetNames = targetName.split('.');
|
|
731
|
-
|
|
732
743
|
const {
|
|
733
744
|
callee: { object, property }
|
|
734
745
|
} = node;
|
|
@@ -736,6 +747,7 @@ function getDefinePropertyCallName(node, targetName) {
|
|
|
736
747
|
if (!property || property.type !== 'Identifier' || property.name !== 'defineProperty') return;
|
|
737
748
|
if (node.arguments.length !== 3) return;
|
|
738
749
|
|
|
750
|
+
const targetNames = targetName.split('.');
|
|
739
751
|
const [target, key, value] = node.arguments;
|
|
740
752
|
if (targetNames.length === 1) {
|
|
741
753
|
if (target.type !== 'Identifier' || target.name !== targetNames[0]) {
|
|
@@ -762,128 +774,199 @@ function getDefinePropertyCallName(node, targetName) {
|
|
|
762
774
|
return { key: key.value, value: valueProperty.value };
|
|
763
775
|
}
|
|
764
776
|
|
|
765
|
-
function isLocallyShadowed(name, scope) {
|
|
766
|
-
while (scope.parent) {
|
|
767
|
-
if (scope.declarations[name]) {
|
|
768
|
-
return true;
|
|
769
|
-
}
|
|
770
|
-
// eslint-disable-next-line no-param-reassign
|
|
771
|
-
scope = scope.parent;
|
|
772
|
-
}
|
|
773
|
-
return false;
|
|
774
|
-
}
|
|
775
|
-
|
|
776
777
|
function isShorthandProperty(parent) {
|
|
777
778
|
return parent && parent.type === 'Property' && parent.shorthand;
|
|
778
779
|
}
|
|
779
780
|
|
|
780
|
-
function
|
|
781
|
-
|
|
781
|
+
function hasDefineEsmProperty(node) {
|
|
782
|
+
return node.properties.some((property) => {
|
|
783
|
+
if (
|
|
784
|
+
property.type === 'Property' &&
|
|
785
|
+
property.key.type === 'Identifier' &&
|
|
786
|
+
property.key.name === '__esModule' &&
|
|
787
|
+
isTruthy(property.value)
|
|
788
|
+
) {
|
|
789
|
+
return true;
|
|
790
|
+
}
|
|
791
|
+
return false;
|
|
792
|
+
});
|
|
793
|
+
}
|
|
782
794
|
|
|
795
|
+
function wrapCode(magicString, uses, moduleName, exportsName) {
|
|
796
|
+
const args = [];
|
|
797
|
+
const passedArgs = [];
|
|
798
|
+
if (uses.module) {
|
|
799
|
+
args.push('module');
|
|
800
|
+
passedArgs.push(moduleName);
|
|
801
|
+
}
|
|
802
|
+
if (uses.exports) {
|
|
803
|
+
args.push('exports');
|
|
804
|
+
passedArgs.push(exportsName);
|
|
805
|
+
}
|
|
783
806
|
magicString
|
|
784
807
|
.trim()
|
|
785
|
-
.prepend(`
|
|
786
|
-
.append(
|
|
787
|
-
`\n}${virtualDynamicRequirePath ? `, ${JSON.stringify(virtualDynamicRequirePath)}` : ''});`
|
|
788
|
-
);
|
|
808
|
+
.prepend(`(function (${args.join(', ')}) {\n`)
|
|
809
|
+
.append(`\n}(${passedArgs.join(', ')}));`);
|
|
789
810
|
}
|
|
790
811
|
|
|
791
812
|
function rewriteExportsAndGetExportsBlock(
|
|
792
813
|
magicString,
|
|
793
814
|
moduleName,
|
|
815
|
+
exportsName,
|
|
794
816
|
wrapped,
|
|
795
|
-
|
|
796
|
-
|
|
817
|
+
moduleExportsAssignments,
|
|
818
|
+
firstTopLevelModuleExportsAssignment,
|
|
819
|
+
exportsAssignmentsByName,
|
|
820
|
+
topLevelAssignments,
|
|
797
821
|
defineCompiledEsmExpressions,
|
|
798
|
-
|
|
799
|
-
isRestorableCompiledEsm,
|
|
822
|
+
deconflictedExportNames,
|
|
800
823
|
code,
|
|
801
|
-
uses,
|
|
802
824
|
HELPERS_NAME,
|
|
825
|
+
exportMode,
|
|
826
|
+
detectWrappedDefault,
|
|
803
827
|
defaultIsModuleExports
|
|
804
828
|
) {
|
|
805
|
-
const
|
|
806
|
-
const
|
|
807
|
-
|
|
829
|
+
const exports = [];
|
|
830
|
+
const exportDeclarations = [];
|
|
831
|
+
|
|
832
|
+
if (exportMode === 'replace') {
|
|
833
|
+
getExportsForReplacedModuleExports(
|
|
834
|
+
magicString,
|
|
835
|
+
exports,
|
|
836
|
+
exportDeclarations,
|
|
837
|
+
moduleExportsAssignments,
|
|
838
|
+
firstTopLevelModuleExportsAssignment,
|
|
839
|
+
exportsName
|
|
840
|
+
);
|
|
841
|
+
} else {
|
|
842
|
+
exports.push(`${exportsName} as __moduleExports`);
|
|
843
|
+
if (wrapped) {
|
|
844
|
+
getExportsWhenWrapping(
|
|
845
|
+
exportDeclarations,
|
|
846
|
+
exportsName,
|
|
847
|
+
detectWrappedDefault,
|
|
848
|
+
HELPERS_NAME,
|
|
849
|
+
defaultIsModuleExports
|
|
850
|
+
);
|
|
851
|
+
} else {
|
|
852
|
+
getExports(
|
|
853
|
+
magicString,
|
|
854
|
+
exports,
|
|
855
|
+
exportDeclarations,
|
|
856
|
+
moduleExportsAssignments,
|
|
857
|
+
exportsAssignmentsByName,
|
|
858
|
+
deconflictedExportNames,
|
|
859
|
+
topLevelAssignments,
|
|
860
|
+
moduleName,
|
|
861
|
+
exportsName,
|
|
862
|
+
defineCompiledEsmExpressions,
|
|
863
|
+
HELPERS_NAME,
|
|
864
|
+
defaultIsModuleExports
|
|
865
|
+
);
|
|
866
|
+
}
|
|
867
|
+
}
|
|
868
|
+
if (exports.length) {
|
|
869
|
+
exportDeclarations.push(`export { ${exports.join(', ')} };`);
|
|
870
|
+
}
|
|
808
871
|
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
const namedExportProperties = [];
|
|
872
|
+
return `\n\n${exportDeclarations.join('\n')}`;
|
|
873
|
+
}
|
|
812
874
|
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
875
|
+
function getExportsForReplacedModuleExports(
|
|
876
|
+
magicString,
|
|
877
|
+
exports,
|
|
878
|
+
exportDeclarations,
|
|
879
|
+
moduleExportsAssignments,
|
|
880
|
+
firstTopLevelModuleExportsAssignment,
|
|
881
|
+
exportsName
|
|
882
|
+
) {
|
|
883
|
+
for (const { left } of moduleExportsAssignments) {
|
|
884
|
+
magicString.overwrite(left.start, left.end, exportsName);
|
|
885
|
+
}
|
|
886
|
+
magicString.prependRight(firstTopLevelModuleExportsAssignment.left.start, 'var ');
|
|
887
|
+
exports.push(`${exportsName} as __moduleExports`);
|
|
888
|
+
exportDeclarations.push(`export default ${exportsName};`);
|
|
889
|
+
}
|
|
818
890
|
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
891
|
+
function getExportsWhenWrapping(
|
|
892
|
+
exportDeclarations,
|
|
893
|
+
exportsName,
|
|
894
|
+
detectWrappedDefault,
|
|
895
|
+
HELPERS_NAME,
|
|
896
|
+
defaultIsModuleExports
|
|
897
|
+
) {
|
|
898
|
+
exportDeclarations.push(
|
|
899
|
+
`export default ${
|
|
900
|
+
detectWrappedDefault && defaultIsModuleExports === 'auto'
|
|
901
|
+
? `/*@__PURE__*/${HELPERS_NAME}.getDefaultExportFromCjs(${exportsName})`
|
|
902
|
+
: defaultIsModuleExports === false
|
|
903
|
+
? `${exportsName}.default`
|
|
904
|
+
: exportsName
|
|
905
|
+
};`
|
|
906
|
+
);
|
|
907
|
+
}
|
|
833
908
|
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
909
|
+
function getExports(
|
|
910
|
+
magicString,
|
|
911
|
+
exports,
|
|
912
|
+
exportDeclarations,
|
|
913
|
+
moduleExportsAssignments,
|
|
914
|
+
exportsAssignmentsByName,
|
|
915
|
+
deconflictedExportNames,
|
|
916
|
+
topLevelAssignments,
|
|
917
|
+
moduleName,
|
|
918
|
+
exportsName,
|
|
919
|
+
defineCompiledEsmExpressions,
|
|
920
|
+
HELPERS_NAME,
|
|
921
|
+
defaultIsModuleExports
|
|
922
|
+
) {
|
|
923
|
+
let deconflictedDefaultExportName;
|
|
924
|
+
// Collect and rewrite module.exports assignments
|
|
925
|
+
for (const { left } of moduleExportsAssignments) {
|
|
926
|
+
magicString.overwrite(left.start, left.end, `${moduleName}.exports`);
|
|
927
|
+
}
|
|
928
|
+
|
|
929
|
+
// Collect and rewrite named exports
|
|
930
|
+
for (const [exportName, { nodes }] of exportsAssignmentsByName) {
|
|
931
|
+
const deconflicted = deconflictedExportNames[exportName];
|
|
932
|
+
let needsDeclaration = true;
|
|
933
|
+
for (const node of nodes) {
|
|
934
|
+
let replacement = `${deconflicted} = ${exportsName}.${exportName}`;
|
|
935
|
+
if (needsDeclaration && topLevelAssignments.has(node)) {
|
|
936
|
+
replacement = `var ${replacement}`;
|
|
937
|
+
needsDeclaration = false;
|
|
838
938
|
}
|
|
939
|
+
magicString.overwrite(node.start, node.left.end, replacement);
|
|
839
940
|
}
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
if (!hasModuleExportsAssignment) {
|
|
843
|
-
const moduleExports = `{\n${namedExportProperties.join(',\n')}\n}`;
|
|
844
|
-
magicString
|
|
845
|
-
.trim()
|
|
846
|
-
.append(
|
|
847
|
-
`\n\nvar ${moduleName} = ${
|
|
848
|
-
isRestorableCompiledEsm
|
|
849
|
-
? `/*#__PURE__*/Object.defineProperty(${moduleExports}, '__esModule', {value: true})`
|
|
850
|
-
: moduleExports
|
|
851
|
-
};`
|
|
852
|
-
);
|
|
941
|
+
if (needsDeclaration) {
|
|
942
|
+
magicString.prepend(`var ${deconflicted};\n`);
|
|
853
943
|
}
|
|
854
|
-
}
|
|
855
944
|
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
if (defaultIsModuleExports === 'auto') {
|
|
859
|
-
if (isRestorableCompiledEsm) {
|
|
860
|
-
defaultExport.push(`export default ${deconflictedDefaultExportName || moduleName};`);
|
|
861
|
-
} else if (
|
|
862
|
-
(wrapped || deconflictedDefaultExportName) &&
|
|
863
|
-
(defineCompiledEsmExpressions.length > 0 || code.includes('__esModule'))
|
|
864
|
-
) {
|
|
865
|
-
// eslint-disable-next-line no-param-reassign
|
|
866
|
-
uses.commonjsHelpers = true;
|
|
867
|
-
defaultExport.push(
|
|
868
|
-
`export default /*@__PURE__*/${HELPERS_NAME}.getDefaultExportFromCjs(${moduleName});`
|
|
869
|
-
);
|
|
870
|
-
} else {
|
|
871
|
-
defaultExport.push(`export default ${moduleName};`);
|
|
872
|
-
}
|
|
873
|
-
} else if (defaultIsModuleExports === true) {
|
|
874
|
-
defaultExport.push(`export default ${moduleName};`);
|
|
875
|
-
} else if (defaultIsModuleExports === false) {
|
|
876
|
-
if (deconflictedDefaultExportName) {
|
|
877
|
-
defaultExport.push(`export default ${deconflictedDefaultExportName};`);
|
|
945
|
+
if (exportName === 'default') {
|
|
946
|
+
deconflictedDefaultExportName = deconflicted;
|
|
878
947
|
} else {
|
|
879
|
-
|
|
948
|
+
exports.push(exportName === deconflicted ? exportName : `${deconflicted} as ${exportName}`);
|
|
880
949
|
}
|
|
881
950
|
}
|
|
882
951
|
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
952
|
+
// Collect and rewrite exports.__esModule assignments
|
|
953
|
+
let isRestorableCompiledEsm = false;
|
|
954
|
+
for (const expression of defineCompiledEsmExpressions) {
|
|
955
|
+
isRestorableCompiledEsm = true;
|
|
956
|
+
const moduleExportsExpression =
|
|
957
|
+
expression.type === 'CallExpression' ? expression.arguments[0] : expression.left.object;
|
|
958
|
+
magicString.overwrite(moduleExportsExpression.start, moduleExportsExpression.end, exportsName);
|
|
959
|
+
}
|
|
960
|
+
|
|
961
|
+
if (!isRestorableCompiledEsm || defaultIsModuleExports === true) {
|
|
962
|
+
exportDeclarations.push(`export default ${exportsName};`);
|
|
963
|
+
} else if (moduleExportsAssignments.length === 0 || defaultIsModuleExports === false) {
|
|
964
|
+
exports.push(`${deconflictedDefaultExportName || exportsName} as default`);
|
|
965
|
+
} else {
|
|
966
|
+
exportDeclarations.push(
|
|
967
|
+
`export default /*@__PURE__*/${HELPERS_NAME}.getDefaultExportFromCjs(${exportsName});`
|
|
968
|
+
);
|
|
969
|
+
}
|
|
887
970
|
}
|
|
888
971
|
|
|
889
972
|
function isRequireStatement(node, scope) {
|
|
@@ -1001,44 +1084,46 @@ function getRequireHandlers() {
|
|
|
1001
1084
|
topLevelDeclarations,
|
|
1002
1085
|
topLevelRequireDeclarators,
|
|
1003
1086
|
reassignedNames,
|
|
1004
|
-
|
|
1005
|
-
dynamicRegisterSources
|
|
1087
|
+
helpersName,
|
|
1088
|
+
dynamicRegisterSources,
|
|
1089
|
+
moduleName,
|
|
1090
|
+
exportsName,
|
|
1091
|
+
id,
|
|
1092
|
+
exportMode
|
|
1006
1093
|
) {
|
|
1007
|
-
const removedDeclarators = getDeclaratorsReplacedByImportsAndSetImportNames(
|
|
1008
|
-
topLevelRequireDeclarators,
|
|
1009
|
-
requiredByNode,
|
|
1010
|
-
reassignedNames
|
|
1011
|
-
);
|
|
1012
1094
|
setRemainingImportNamesAndRewriteRequires(
|
|
1013
1095
|
requireExpressionsWithUsedReturnValue,
|
|
1014
1096
|
requiredByNode,
|
|
1015
1097
|
magicString
|
|
1016
1098
|
);
|
|
1017
|
-
|
|
1018
|
-
|
|
1019
|
-
|
|
1020
|
-
|
|
1021
|
-
|
|
1022
|
-
|
|
1023
|
-
|
|
1024
|
-
|
|
1025
|
-
|
|
1026
|
-
|
|
1027
|
-
|
|
1028
|
-
|
|
1029
|
-
|
|
1030
|
-
|
|
1031
|
-
|
|
1032
|
-
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
|
|
1040
|
-
|
|
1041
|
-
|
|
1099
|
+
const imports = [];
|
|
1100
|
+
imports.push(`import * as ${helpersName} from "${HELPERS_ID}";`);
|
|
1101
|
+
if (exportMode === 'module') {
|
|
1102
|
+
imports.push(
|
|
1103
|
+
`import { __module as ${moduleName}, exports as ${exportsName} } from ${JSON.stringify(
|
|
1104
|
+
wrapId(id, MODULE_SUFFIX)
|
|
1105
|
+
)}`
|
|
1106
|
+
);
|
|
1107
|
+
} else if (exportMode === 'exports') {
|
|
1108
|
+
imports.push(
|
|
1109
|
+
`import { __exports as ${exportsName} } from ${JSON.stringify(wrapId(id, EXPORTS_SUFFIX))}`
|
|
1110
|
+
);
|
|
1111
|
+
}
|
|
1112
|
+
for (const source of dynamicRegisterSources) {
|
|
1113
|
+
imports.push(`import ${JSON.stringify(wrapId(source, REQUIRE_SUFFIX))};`);
|
|
1114
|
+
}
|
|
1115
|
+
for (const source of requiredSources) {
|
|
1116
|
+
if (!source.startsWith('\0')) {
|
|
1117
|
+
imports.push(`import ${JSON.stringify(wrapId(source, REQUIRE_SUFFIX))};`);
|
|
1118
|
+
}
|
|
1119
|
+
const { name, nodesUsingRequired } = requiredBySource[source];
|
|
1120
|
+
imports.push(
|
|
1121
|
+
`import ${nodesUsingRequired.length ? `${name} from ` : ''}${JSON.stringify(
|
|
1122
|
+
source.startsWith('\0') ? source : wrapId(source, PROXY_SUFFIX)
|
|
1123
|
+
)};`
|
|
1124
|
+
);
|
|
1125
|
+
}
|
|
1126
|
+
return imports.length ? `${imports.join('\n')}\n\n` : '';
|
|
1042
1127
|
}
|
|
1043
1128
|
|
|
1044
1129
|
return {
|
|
@@ -1048,30 +1133,6 @@ function getRequireHandlers() {
|
|
|
1048
1133
|
};
|
|
1049
1134
|
}
|
|
1050
1135
|
|
|
1051
|
-
function getDeclaratorsReplacedByImportsAndSetImportNames(
|
|
1052
|
-
topLevelRequireDeclarators,
|
|
1053
|
-
requiredByNode,
|
|
1054
|
-
reassignedNames
|
|
1055
|
-
) {
|
|
1056
|
-
const removedDeclarators = new Set();
|
|
1057
|
-
for (const declarator of topLevelRequireDeclarators) {
|
|
1058
|
-
const { required } = requiredByNode.get(declarator.init);
|
|
1059
|
-
if (!required.name) {
|
|
1060
|
-
const potentialName = declarator.id.name;
|
|
1061
|
-
if (
|
|
1062
|
-
!reassignedNames.has(potentialName) &&
|
|
1063
|
-
!required.nodesUsingRequired.some((node) =>
|
|
1064
|
-
isLocallyShadowed(potentialName, requiredByNode.get(node).scope)
|
|
1065
|
-
)
|
|
1066
|
-
) {
|
|
1067
|
-
required.name = potentialName;
|
|
1068
|
-
removedDeclarators.add(declarator);
|
|
1069
|
-
}
|
|
1070
|
-
}
|
|
1071
|
-
}
|
|
1072
|
-
return removedDeclarators;
|
|
1073
|
-
}
|
|
1074
|
-
|
|
1075
1136
|
function setRemainingImportNamesAndRewriteRequires(
|
|
1076
1137
|
requireExpressionsWithUsedReturnValue,
|
|
1077
1138
|
requiredByNode,
|
|
@@ -1093,25 +1154,6 @@ function setRemainingImportNamesAndRewriteRequires(
|
|
|
1093
1154
|
}
|
|
1094
1155
|
}
|
|
1095
1156
|
|
|
1096
|
-
function removeDeclaratorsFromDeclarations(topLevelDeclarations, removedDeclarators, magicString) {
|
|
1097
|
-
for (const declaration of topLevelDeclarations) {
|
|
1098
|
-
let keepDeclaration = false;
|
|
1099
|
-
let [{ start }] = declaration.declarations;
|
|
1100
|
-
for (const declarator of declaration.declarations) {
|
|
1101
|
-
if (removedDeclarators.has(declarator)) {
|
|
1102
|
-
magicString.remove(start, declarator.end);
|
|
1103
|
-
} else if (!keepDeclaration) {
|
|
1104
|
-
magicString.remove(start, declarator.start);
|
|
1105
|
-
keepDeclaration = true;
|
|
1106
|
-
}
|
|
1107
|
-
start = declarator.end;
|
|
1108
|
-
}
|
|
1109
|
-
if (!keepDeclaration) {
|
|
1110
|
-
magicString.remove(declaration.start, declaration.end);
|
|
1111
|
-
}
|
|
1112
|
-
}
|
|
1113
|
-
}
|
|
1114
|
-
|
|
1115
1157
|
/* eslint-disable no-param-reassign, no-shadow, no-underscore-dangle, no-continue */
|
|
1116
1158
|
|
|
1117
1159
|
const exportsPattern = /^(?:module\.)?exports(?:\.([a-zA-Z_$][a-zA-Z_$0-9]*))?$/;
|
|
@@ -1141,9 +1183,9 @@ function transformCommonjs(
|
|
|
1141
1183
|
module: false,
|
|
1142
1184
|
exports: false,
|
|
1143
1185
|
global: false,
|
|
1144
|
-
require: false
|
|
1145
|
-
commonjsHelpers: false
|
|
1186
|
+
require: false
|
|
1146
1187
|
};
|
|
1188
|
+
let usesDynamicRequire = false;
|
|
1147
1189
|
const virtualDynamicRequirePath =
|
|
1148
1190
|
isDynamicRequireModulesEnabled && getVirtualPathForDynamicRequirePath(dirname(id), commonDir);
|
|
1149
1191
|
let scope = attachScopes(ast, 'scope');
|
|
@@ -1151,13 +1193,11 @@ function transformCommonjs(
|
|
|
1151
1193
|
let programDepth = 0;
|
|
1152
1194
|
let currentTryBlockEnd = null;
|
|
1153
1195
|
let shouldWrap = false;
|
|
1154
|
-
const defineCompiledEsmExpressions = [];
|
|
1155
1196
|
|
|
1156
1197
|
const globals = new Set();
|
|
1157
1198
|
|
|
1158
1199
|
// TODO technically wrong since globals isn't populated yet, but ¯\_(ツ)_/¯
|
|
1159
|
-
const HELPERS_NAME = deconflict(scope, globals, 'commonjsHelpers');
|
|
1160
|
-
const namedExports = {};
|
|
1200
|
+
const HELPERS_NAME = deconflict([scope], globals, 'commonjsHelpers');
|
|
1161
1201
|
const dynamicRegisterSources = new Set();
|
|
1162
1202
|
let hasRemovedRequire = false;
|
|
1163
1203
|
|
|
@@ -1174,8 +1214,13 @@ function transformCommonjs(
|
|
|
1174
1214
|
const topLevelDeclarations = [];
|
|
1175
1215
|
const topLevelRequireDeclarators = new Set();
|
|
1176
1216
|
const skippedNodes = new Set();
|
|
1177
|
-
const
|
|
1178
|
-
const
|
|
1217
|
+
const moduleAccessScopes = new Set([scope]);
|
|
1218
|
+
const exportsAccessScopes = new Set([scope]);
|
|
1219
|
+
const moduleExportsAssignments = [];
|
|
1220
|
+
let firstTopLevelModuleExportsAssignment = null;
|
|
1221
|
+
const exportsAssignmentsByName = new Map();
|
|
1222
|
+
const topLevelAssignments = new Set();
|
|
1223
|
+
const topLevelDefineCompiledEsmExpressions = [];
|
|
1179
1224
|
|
|
1180
1225
|
walk(ast, {
|
|
1181
1226
|
enter(node, parent) {
|
|
@@ -1215,30 +1260,46 @@ function transformCommonjs(
|
|
|
1215
1260
|
uses[flattened.name] = true;
|
|
1216
1261
|
|
|
1217
1262
|
// we're dealing with `module.exports = ...` or `[module.]exports.foo = ...` –
|
|
1218
|
-
if (
|
|
1219
|
-
|
|
1263
|
+
if (flattened.keypath === 'module.exports') {
|
|
1264
|
+
moduleExportsAssignments.push(node);
|
|
1265
|
+
if (programDepth > 3) {
|
|
1266
|
+
moduleAccessScopes.add(scope);
|
|
1267
|
+
} else if (!firstTopLevelModuleExportsAssignment) {
|
|
1268
|
+
firstTopLevelModuleExportsAssignment = node;
|
|
1269
|
+
}
|
|
1270
|
+
|
|
1271
|
+
if (defaultIsModuleExports === false) {
|
|
1272
|
+
shouldWrap = true;
|
|
1273
|
+
} else if (defaultIsModuleExports === 'auto') {
|
|
1274
|
+
if (node.right.type === 'ObjectExpression') {
|
|
1275
|
+
if (hasDefineEsmProperty(node.right)) {
|
|
1276
|
+
shouldWrap = true;
|
|
1277
|
+
}
|
|
1278
|
+
} else if (defaultIsModuleExports === false) {
|
|
1279
|
+
shouldWrap = true;
|
|
1280
|
+
}
|
|
1281
|
+
}
|
|
1220
1282
|
} else if (exportName === KEY_COMPILED_ESM) {
|
|
1221
|
-
|
|
1222
|
-
|
|
1223
|
-
|
|
1224
|
-
|
|
1225
|
-
|
|
1283
|
+
if (programDepth > 3) {
|
|
1284
|
+
shouldWrap = true;
|
|
1285
|
+
} else {
|
|
1286
|
+
topLevelDefineCompiledEsmExpressions.push(node);
|
|
1287
|
+
}
|
|
1226
1288
|
} else {
|
|
1227
|
-
|
|
1289
|
+
const exportsAssignments = exportsAssignmentsByName.get(exportName) || {
|
|
1290
|
+
nodes: [],
|
|
1291
|
+
scopes: new Set()
|
|
1292
|
+
};
|
|
1293
|
+
exportsAssignments.nodes.push(node);
|
|
1294
|
+
exportsAssignments.scopes.add(scope);
|
|
1295
|
+
exportsAccessScopes.add(scope);
|
|
1296
|
+
exportsAssignmentsByName.set(exportName, exportsAssignments);
|
|
1297
|
+
if (programDepth <= 3) {
|
|
1298
|
+
topLevelAssignments.add(node);
|
|
1299
|
+
}
|
|
1228
1300
|
}
|
|
1229
1301
|
|
|
1230
1302
|
skippedNodes.add(node.left);
|
|
1231
|
-
|
|
1232
|
-
if (flattened.keypath === 'module.exports' && node.right.type === 'ObjectExpression') {
|
|
1233
|
-
node.right.properties.forEach((prop) => {
|
|
1234
|
-
if (prop.computed || !('key' in prop) || prop.key.type !== 'Identifier') return;
|
|
1235
|
-
const { name } = prop.key;
|
|
1236
|
-
if (name === makeLegalIdentifier(name)) namedExports[name] = true;
|
|
1237
|
-
});
|
|
1238
|
-
return;
|
|
1239
|
-
}
|
|
1240
|
-
|
|
1241
|
-
if (exportsPatternMatch[1]) namedExports[exportsPatternMatch[1]] = true;
|
|
1242
1303
|
} else {
|
|
1243
1304
|
for (const name of extractAssignedNames(node.left)) {
|
|
1244
1305
|
reassignedNames.add(name);
|
|
@@ -1250,7 +1311,7 @@ function transformCommonjs(
|
|
|
1250
1311
|
if (programDepth === 3 && parent.type === 'ExpressionStatement') {
|
|
1251
1312
|
// skip special handling for [module.]exports until we know we render this
|
|
1252
1313
|
skippedNodes.add(node.arguments[0]);
|
|
1253
|
-
|
|
1314
|
+
topLevelDefineCompiledEsmExpressions.push(node);
|
|
1254
1315
|
} else {
|
|
1255
1316
|
shouldWrap = true;
|
|
1256
1317
|
}
|
|
@@ -1278,7 +1339,6 @@ function transformCommonjs(
|
|
|
1278
1339
|
storeName: true
|
|
1279
1340
|
}
|
|
1280
1341
|
);
|
|
1281
|
-
uses.commonjsHelpers = true;
|
|
1282
1342
|
return;
|
|
1283
1343
|
}
|
|
1284
1344
|
|
|
@@ -1305,13 +1365,13 @@ function transformCommonjs(
|
|
|
1305
1365
|
}
|
|
1306
1366
|
|
|
1307
1367
|
let sourceId = getRequireStringArg(node);
|
|
1308
|
-
const isDynamicRegister =
|
|
1368
|
+
const isDynamicRegister = isWrappedId(sourceId, DYNAMIC_REGISTER_SUFFIX);
|
|
1309
1369
|
if (isDynamicRegister) {
|
|
1310
|
-
sourceId =
|
|
1370
|
+
sourceId = unwrapId(sourceId, DYNAMIC_REGISTER_SUFFIX);
|
|
1311
1371
|
if (sourceId.endsWith('.json')) {
|
|
1312
1372
|
sourceId = DYNAMIC_JSON_PREFIX + sourceId;
|
|
1313
1373
|
}
|
|
1314
|
-
dynamicRegisterSources.add(
|
|
1374
|
+
dynamicRegisterSources.add(wrapId(sourceId, DYNAMIC_REGISTER_SUFFIX));
|
|
1315
1375
|
} else {
|
|
1316
1376
|
if (
|
|
1317
1377
|
!sourceId.endsWith('.json') &&
|
|
@@ -1329,7 +1389,7 @@ function transformCommonjs(
|
|
|
1329
1389
|
dirname(id) === '.' ? null /* default behavior */ : virtualDynamicRequirePath
|
|
1330
1390
|
)})`
|
|
1331
1391
|
);
|
|
1332
|
-
|
|
1392
|
+
usesDynamicRequire = true;
|
|
1333
1393
|
}
|
|
1334
1394
|
return;
|
|
1335
1395
|
}
|
|
@@ -1386,7 +1446,6 @@ function transformCommonjs(
|
|
|
1386
1446
|
magicString.overwrite(node.start, node.end, `${HELPERS_NAME}.commonjsRequire`, {
|
|
1387
1447
|
storeName: true
|
|
1388
1448
|
});
|
|
1389
|
-
uses.commonjsHelpers = true;
|
|
1390
1449
|
}
|
|
1391
1450
|
}
|
|
1392
1451
|
|
|
@@ -1410,8 +1469,7 @@ function transformCommonjs(
|
|
|
1410
1469
|
});
|
|
1411
1470
|
}
|
|
1412
1471
|
}
|
|
1413
|
-
|
|
1414
|
-
uses.commonjsHelpers = true;
|
|
1472
|
+
usesDynamicRequire = true;
|
|
1415
1473
|
return;
|
|
1416
1474
|
case 'module':
|
|
1417
1475
|
case 'exports':
|
|
@@ -1424,11 +1482,12 @@ function transformCommonjs(
|
|
|
1424
1482
|
magicString.overwrite(node.start, node.end, `${HELPERS_NAME}.commonjsGlobal`, {
|
|
1425
1483
|
storeName: true
|
|
1426
1484
|
});
|
|
1427
|
-
uses.commonjsHelpers = true;
|
|
1428
1485
|
}
|
|
1429
1486
|
return;
|
|
1430
1487
|
case 'define':
|
|
1431
|
-
magicString.overwrite(node.start, node.end, 'undefined', {
|
|
1488
|
+
magicString.overwrite(node.start, node.end, 'undefined', {
|
|
1489
|
+
storeName: true
|
|
1490
|
+
});
|
|
1432
1491
|
return;
|
|
1433
1492
|
default:
|
|
1434
1493
|
globals.add(name);
|
|
@@ -1440,7 +1499,6 @@ function transformCommonjs(
|
|
|
1440
1499
|
magicString.overwrite(node.start, node.end, `${HELPERS_NAME}.commonjsRequire`, {
|
|
1441
1500
|
storeName: true
|
|
1442
1501
|
});
|
|
1443
|
-
uses.commonjsHelpers = true;
|
|
1444
1502
|
skippedNodes.add(node.object);
|
|
1445
1503
|
skippedNodes.add(node.property);
|
|
1446
1504
|
}
|
|
@@ -1459,7 +1517,6 @@ function transformCommonjs(
|
|
|
1459
1517
|
magicString.overwrite(node.start, node.end, `${HELPERS_NAME}.commonjsGlobal`, {
|
|
1460
1518
|
storeName: true
|
|
1461
1519
|
});
|
|
1462
|
-
uses.commonjsHelpers = true;
|
|
1463
1520
|
}
|
|
1464
1521
|
}
|
|
1465
1522
|
return;
|
|
@@ -1476,7 +1533,9 @@ function transformCommonjs(
|
|
|
1476
1533
|
flattened.keypath === 'module' ||
|
|
1477
1534
|
flattened.keypath === 'exports'
|
|
1478
1535
|
) {
|
|
1479
|
-
magicString.overwrite(node.start, node.end, `'object'`, {
|
|
1536
|
+
magicString.overwrite(node.start, node.end, `'object'`, {
|
|
1537
|
+
storeName: false
|
|
1538
|
+
});
|
|
1480
1539
|
}
|
|
1481
1540
|
}
|
|
1482
1541
|
return;
|
|
@@ -1494,23 +1553,22 @@ function transformCommonjs(
|
|
|
1494
1553
|
}
|
|
1495
1554
|
});
|
|
1496
1555
|
|
|
1497
|
-
|
|
1498
|
-
|
|
1499
|
-
|
|
1500
|
-
|
|
1501
|
-
|
|
1502
|
-
|
|
1503
|
-
defineCompiledEsmExpressions[0].end
|
|
1504
|
-
);
|
|
1505
|
-
} else {
|
|
1506
|
-
shouldWrap = true;
|
|
1507
|
-
uses.exports = true;
|
|
1508
|
-
}
|
|
1556
|
+
const nameBase = getName(id);
|
|
1557
|
+
const exportsName = deconflict([...exportsAccessScopes], globals, nameBase);
|
|
1558
|
+
const moduleName = deconflict([...moduleAccessScopes], globals, `${nameBase}Module`);
|
|
1559
|
+
const deconflictedExportNames = Object.create(null);
|
|
1560
|
+
for (const [exportName, { scopes }] of exportsAssignmentsByName) {
|
|
1561
|
+
deconflictedExportNames[exportName] = deconflict([...scopes], globals, exportName);
|
|
1509
1562
|
}
|
|
1510
1563
|
|
|
1511
1564
|
// We cannot wrap ES/mixed modules
|
|
1512
|
-
shouldWrap =
|
|
1513
|
-
|
|
1565
|
+
shouldWrap =
|
|
1566
|
+
!isEsModule &&
|
|
1567
|
+
!disableWrap &&
|
|
1568
|
+
(shouldWrap || (uses.exports && moduleExportsAssignments.length > 0));
|
|
1569
|
+
const detectWrappedDefault =
|
|
1570
|
+
shouldWrap &&
|
|
1571
|
+
(topLevelDefineCompiledEsmExpressions.length > 0 || code.indexOf('__esModule') >= 0);
|
|
1514
1572
|
|
|
1515
1573
|
if (
|
|
1516
1574
|
!(
|
|
@@ -1519,17 +1577,15 @@ function transformCommonjs(
|
|
|
1519
1577
|
uses.module ||
|
|
1520
1578
|
uses.exports ||
|
|
1521
1579
|
uses.require ||
|
|
1522
|
-
|
|
1580
|
+
usesDynamicRequire ||
|
|
1523
1581
|
hasRemovedRequire ||
|
|
1524
|
-
|
|
1582
|
+
topLevelDefineCompiledEsmExpressions.length > 0
|
|
1525
1583
|
) &&
|
|
1526
1584
|
(ignoreGlobal || !uses.global)
|
|
1527
1585
|
) {
|
|
1528
1586
|
return { meta: { commonjs: { isCommonJS: false } } };
|
|
1529
1587
|
}
|
|
1530
1588
|
|
|
1531
|
-
const moduleName = deconflict(scope, globals, getName(id));
|
|
1532
|
-
|
|
1533
1589
|
let leadingComment = '';
|
|
1534
1590
|
if (code.startsWith('/*')) {
|
|
1535
1591
|
const commentEnd = code.indexOf('*/', 2) + 2;
|
|
@@ -1537,34 +1593,53 @@ function transformCommonjs(
|
|
|
1537
1593
|
magicString.remove(0, commentEnd).trim();
|
|
1538
1594
|
}
|
|
1539
1595
|
|
|
1596
|
+
const exportMode = shouldWrap
|
|
1597
|
+
? uses.module
|
|
1598
|
+
? 'module'
|
|
1599
|
+
: 'exports'
|
|
1600
|
+
: firstTopLevelModuleExportsAssignment
|
|
1601
|
+
? exportsAssignmentsByName.size === 0 && topLevelDefineCompiledEsmExpressions.length === 0
|
|
1602
|
+
? 'replace'
|
|
1603
|
+
: 'module'
|
|
1604
|
+
: moduleExportsAssignments.length === 0
|
|
1605
|
+
? 'exports'
|
|
1606
|
+
: 'module';
|
|
1607
|
+
|
|
1608
|
+
const importBlock = rewriteRequireExpressionsAndGetImportBlock(
|
|
1609
|
+
magicString,
|
|
1610
|
+
topLevelDeclarations,
|
|
1611
|
+
topLevelRequireDeclarators,
|
|
1612
|
+
reassignedNames,
|
|
1613
|
+
HELPERS_NAME,
|
|
1614
|
+
dynamicRegisterSources,
|
|
1615
|
+
moduleName,
|
|
1616
|
+
exportsName,
|
|
1617
|
+
id,
|
|
1618
|
+
exportMode
|
|
1619
|
+
);
|
|
1620
|
+
|
|
1540
1621
|
const exportBlock = isEsModule
|
|
1541
1622
|
? ''
|
|
1542
1623
|
: rewriteExportsAndGetExportsBlock(
|
|
1543
1624
|
magicString,
|
|
1544
1625
|
moduleName,
|
|
1626
|
+
exportsName,
|
|
1545
1627
|
shouldWrap,
|
|
1546
|
-
|
|
1547
|
-
|
|
1548
|
-
|
|
1549
|
-
|
|
1550
|
-
|
|
1628
|
+
moduleExportsAssignments,
|
|
1629
|
+
firstTopLevelModuleExportsAssignment,
|
|
1630
|
+
exportsAssignmentsByName,
|
|
1631
|
+
topLevelAssignments,
|
|
1632
|
+
topLevelDefineCompiledEsmExpressions,
|
|
1633
|
+
deconflictedExportNames,
|
|
1551
1634
|
code,
|
|
1552
|
-
uses,
|
|
1553
1635
|
HELPERS_NAME,
|
|
1636
|
+
exportMode,
|
|
1637
|
+
detectWrappedDefault,
|
|
1554
1638
|
defaultIsModuleExports
|
|
1555
1639
|
);
|
|
1556
1640
|
|
|
1557
|
-
const importBlock = rewriteRequireExpressionsAndGetImportBlock(
|
|
1558
|
-
magicString,
|
|
1559
|
-
topLevelDeclarations,
|
|
1560
|
-
topLevelRequireDeclarators,
|
|
1561
|
-
reassignedNames,
|
|
1562
|
-
uses.commonjsHelpers && HELPERS_NAME,
|
|
1563
|
-
dynamicRegisterSources
|
|
1564
|
-
);
|
|
1565
|
-
|
|
1566
1641
|
if (shouldWrap) {
|
|
1567
|
-
wrapCode(magicString, uses, moduleName,
|
|
1642
|
+
wrapCode(magicString, uses, moduleName, exportsName);
|
|
1568
1643
|
}
|
|
1569
1644
|
|
|
1570
1645
|
magicString
|
|
@@ -1613,7 +1688,7 @@ function commonjs(options = {}) {
|
|
|
1613
1688
|
|
|
1614
1689
|
const esModulesWithDefaultExport = new Set();
|
|
1615
1690
|
const esModulesWithNamedExports = new Set();
|
|
1616
|
-
const
|
|
1691
|
+
const commonJsMetaPromises = new Map();
|
|
1617
1692
|
|
|
1618
1693
|
const ignoreRequire =
|
|
1619
1694
|
typeof options.ignore === 'function'
|
|
@@ -1666,13 +1741,11 @@ function commonjs(options = {}) {
|
|
|
1666
1741
|
return { meta: { commonjs: { isCommonJS: false } } };
|
|
1667
1742
|
}
|
|
1668
1743
|
|
|
1669
|
-
|
|
1670
|
-
|
|
1671
|
-
|
|
1672
|
-
if (isModuleRegisterProxy(id)) {
|
|
1673
|
-
disableWrap = true;
|
|
1744
|
+
// avoid wrapping as this is a commonjsRegister call
|
|
1745
|
+
const disableWrap = isWrappedId(id, DYNAMIC_REGISTER_SUFFIX);
|
|
1746
|
+
if (disableWrap) {
|
|
1674
1747
|
// eslint-disable-next-line no-param-reassign
|
|
1675
|
-
id =
|
|
1748
|
+
id = unwrapId(id, DYNAMIC_REGISTER_SUFFIX);
|
|
1676
1749
|
}
|
|
1677
1750
|
|
|
1678
1751
|
return transformCommonjs(
|
|
@@ -1717,6 +1790,39 @@ function commonjs(options = {}) {
|
|
|
1717
1790
|
return getSpecificHelperProxy(id);
|
|
1718
1791
|
}
|
|
1719
1792
|
|
|
1793
|
+
if (isWrappedId(id, MODULE_SUFFIX)) {
|
|
1794
|
+
const actualId = unwrapId(id, MODULE_SUFFIX);
|
|
1795
|
+
let name = getName(actualId);
|
|
1796
|
+
let code;
|
|
1797
|
+
if (isDynamicRequireModulesEnabled) {
|
|
1798
|
+
if (['modulePath', 'commonjsRequire', 'createModule'].includes(name)) {
|
|
1799
|
+
name = `${name}_`;
|
|
1800
|
+
}
|
|
1801
|
+
code =
|
|
1802
|
+
`import {commonjsRequire, createModule} from "${HELPERS_ID}";\n` +
|
|
1803
|
+
`var ${name} = createModule(${JSON.stringify(
|
|
1804
|
+
getVirtualPathForDynamicRequirePath(dirname(actualId), commonDir)
|
|
1805
|
+
)});\n` +
|
|
1806
|
+
`export {${name} as __module}`;
|
|
1807
|
+
} else {
|
|
1808
|
+
code = `var ${name} = {exports: {}}; export {${name} as __module}`;
|
|
1809
|
+
}
|
|
1810
|
+
return {
|
|
1811
|
+
code,
|
|
1812
|
+
syntheticNamedExports: '__module',
|
|
1813
|
+
meta: { commonjs: { isCommonJS: false } }
|
|
1814
|
+
};
|
|
1815
|
+
}
|
|
1816
|
+
|
|
1817
|
+
if (isWrappedId(id, EXPORTS_SUFFIX)) {
|
|
1818
|
+
const actualId = unwrapId(id, EXPORTS_SUFFIX);
|
|
1819
|
+
const name = getName(actualId);
|
|
1820
|
+
return {
|
|
1821
|
+
code: `var ${name} = {}; export {${name} as __exports}`,
|
|
1822
|
+
meta: { commonjs: { isCommonJS: false } }
|
|
1823
|
+
};
|
|
1824
|
+
}
|
|
1825
|
+
|
|
1720
1826
|
if (isWrappedId(id, EXTERNAL_SUFFIX)) {
|
|
1721
1827
|
const actualId = unwrapId(id, EXTERNAL_SUFFIX);
|
|
1722
1828
|
return getUnknownRequireProxy(
|
|
@@ -1737,9 +1843,9 @@ function commonjs(options = {}) {
|
|
|
1737
1843
|
return `export default require(${JSON.stringify(normalizePathSlashes(id))});`;
|
|
1738
1844
|
}
|
|
1739
1845
|
|
|
1740
|
-
if (
|
|
1846
|
+
if (isWrappedId(id, DYNAMIC_REGISTER_SUFFIX)) {
|
|
1741
1847
|
return getDynamicRequireProxy(
|
|
1742
|
-
normalizePathSlashes(
|
|
1848
|
+
normalizePathSlashes(unwrapId(id, DYNAMIC_REGISTER_SUFFIX)),
|
|
1743
1849
|
commonDir
|
|
1744
1850
|
);
|
|
1745
1851
|
}
|
|
@@ -1751,7 +1857,7 @@ function commonjs(options = {}) {
|
|
|
1751
1857
|
getRequireReturnsDefault(actualId),
|
|
1752
1858
|
esModulesWithDefaultExport,
|
|
1753
1859
|
esModulesWithNamedExports,
|
|
1754
|
-
|
|
1860
|
+
commonJsMetaPromises
|
|
1755
1861
|
);
|
|
1756
1862
|
}
|
|
1757
1863
|
|
|
@@ -1761,8 +1867,8 @@ function commonjs(options = {}) {
|
|
|
1761
1867
|
transform(code, rawId) {
|
|
1762
1868
|
let id = rawId;
|
|
1763
1869
|
|
|
1764
|
-
if (
|
|
1765
|
-
id =
|
|
1870
|
+
if (isWrappedId(id, DYNAMIC_REGISTER_SUFFIX)) {
|
|
1871
|
+
id = unwrapId(id, DYNAMIC_REGISTER_SUFFIX);
|
|
1766
1872
|
}
|
|
1767
1873
|
|
|
1768
1874
|
const extName = extname(id);
|
|
@@ -1782,16 +1888,12 @@ function commonjs(options = {}) {
|
|
|
1782
1888
|
}
|
|
1783
1889
|
},
|
|
1784
1890
|
|
|
1785
|
-
|
|
1786
|
-
|
|
1787
|
-
|
|
1788
|
-
|
|
1789
|
-
if (isCjs != null) {
|
|
1790
|
-
setIsCjsPromise(isCjsPromises, id, isCjs);
|
|
1791
|
-
return;
|
|
1792
|
-
}
|
|
1891
|
+
moduleParsed({ id, meta: { commonjs: commonjsMeta } }) {
|
|
1892
|
+
if (commonjsMeta && commonjsMeta.isCommonJS != null) {
|
|
1893
|
+
setCommonJSMetaPromise(commonJsMetaPromises, id, commonjsMeta);
|
|
1894
|
+
return;
|
|
1793
1895
|
}
|
|
1794
|
-
|
|
1896
|
+
setCommonJSMetaPromise(commonJsMetaPromises, id, null);
|
|
1795
1897
|
}
|
|
1796
1898
|
};
|
|
1797
1899
|
}
|