@rollup/plugin-commonjs 17.0.0 → 17.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +18 -0
- package/README.md +14 -0
- package/dist/index.es.js +273 -82
- package/dist/index.es.js.map +1 -1
- package/dist/index.js +273 -82
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/types/index.d.ts +20 -0
package/dist/index.js
CHANGED
|
@@ -93,7 +93,7 @@ const PROXY_SUFFIX = '?commonjs-proxy';
|
|
|
93
93
|
const REQUIRE_SUFFIX = '?commonjs-require';
|
|
94
94
|
const EXTERNAL_SUFFIX = '?commonjs-external';
|
|
95
95
|
|
|
96
|
-
const
|
|
96
|
+
const DYNAMIC_REGISTER_SUFFIX = '?commonjs-dynamic-register';
|
|
97
97
|
const DYNAMIC_JSON_PREFIX = '\0commonjs-dynamic-json:';
|
|
98
98
|
const DYNAMIC_PACKAGES_ID = '\0commonjs-dynamic-packages';
|
|
99
99
|
|
|
@@ -243,10 +243,13 @@ function dirname (path) {
|
|
|
243
243
|
return '.';
|
|
244
244
|
}
|
|
245
245
|
|
|
246
|
-
export function
|
|
246
|
+
export function commonjsResolveImpl (path, originalModuleDir, testCache) {
|
|
247
247
|
const shouldTryNodeModules = isPossibleNodeModulesPath(path);
|
|
248
248
|
path = normalize(path);
|
|
249
249
|
let relPath;
|
|
250
|
+
if (path[0] === '/') {
|
|
251
|
+
originalModuleDir = '/';
|
|
252
|
+
}
|
|
250
253
|
while (true) {
|
|
251
254
|
if (!shouldTryNodeModules) {
|
|
252
255
|
relPath = originalModuleDir ? normalize(originalModuleDir + '/' + path) : path;
|
|
@@ -255,33 +258,18 @@ export function commonjsRequire (path, originalModuleDir) {
|
|
|
255
258
|
} else {
|
|
256
259
|
relPath = normalize(join('node_modules', path));
|
|
257
260
|
}
|
|
261
|
+
|
|
262
|
+
if (relPath.endsWith('/..')) {
|
|
263
|
+
break; // Travelled too far up, avoid infinite loop
|
|
264
|
+
}
|
|
265
|
+
|
|
258
266
|
for (let extensionIndex = 0; extensionIndex < CHECKED_EXTENSIONS.length; extensionIndex++) {
|
|
259
267
|
const resolvedPath = relPath + CHECKED_EXTENSIONS[extensionIndex];
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
if (
|
|
264
|
-
|
|
265
|
-
id: resolvedPath,
|
|
266
|
-
filename: resolvedPath,
|
|
267
|
-
path: dirname(resolvedPath),
|
|
268
|
-
exports: {},
|
|
269
|
-
parent: DEFAULT_PARENT_MODULE,
|
|
270
|
-
loaded: false,
|
|
271
|
-
children: [],
|
|
272
|
-
paths: [],
|
|
273
|
-
require: function (path, base) {
|
|
274
|
-
return commonjsRequire(path, (base === undefined || base === null) ? cachedModule.path : base);
|
|
275
|
-
}
|
|
276
|
-
};
|
|
277
|
-
try {
|
|
278
|
-
loader.call(commonjsGlobal, cachedModule, cachedModule.exports);
|
|
279
|
-
} catch (error) {
|
|
280
|
-
delete DYNAMIC_REQUIRE_CACHE[resolvedPath];
|
|
281
|
-
throw error;
|
|
282
|
-
}
|
|
283
|
-
cachedModule.loaded = true;
|
|
284
|
-
return cachedModule.exports;
|
|
268
|
+
if (DYNAMIC_REQUIRE_CACHE[resolvedPath]) {
|
|
269
|
+
return resolvedPath;
|
|
270
|
+
};
|
|
271
|
+
if (DYNAMIC_REQUIRE_LOADERS[resolvedPath]) {
|
|
272
|
+
return resolvedPath;
|
|
285
273
|
};
|
|
286
274
|
}
|
|
287
275
|
if (!shouldTryNodeModules) break;
|
|
@@ -289,10 +277,52 @@ export function commonjsRequire (path, originalModuleDir) {
|
|
|
289
277
|
if (nextDir === originalModuleDir) break;
|
|
290
278
|
originalModuleDir = nextDir;
|
|
291
279
|
}
|
|
280
|
+
return null;
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
export function commonjsResolve (path, originalModuleDir) {
|
|
284
|
+
const resolvedPath = commonjsResolveImpl(path, originalModuleDir);
|
|
285
|
+
if (resolvedPath !== null) {
|
|
286
|
+
return resolvedPath;
|
|
287
|
+
}
|
|
288
|
+
return require.resolve(path);
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
export function commonjsRequire (path, originalModuleDir) {
|
|
292
|
+
const resolvedPath = commonjsResolveImpl(path, originalModuleDir, true);
|
|
293
|
+
if (resolvedPath !== null) {
|
|
294
|
+
let cachedModule = DYNAMIC_REQUIRE_CACHE[resolvedPath];
|
|
295
|
+
if (cachedModule) return cachedModule.exports;
|
|
296
|
+
const loader = DYNAMIC_REQUIRE_LOADERS[resolvedPath];
|
|
297
|
+
if (loader) {
|
|
298
|
+
DYNAMIC_REQUIRE_CACHE[resolvedPath] = cachedModule = {
|
|
299
|
+
id: resolvedPath,
|
|
300
|
+
filename: resolvedPath,
|
|
301
|
+
path: dirname(resolvedPath),
|
|
302
|
+
exports: {},
|
|
303
|
+
parent: DEFAULT_PARENT_MODULE,
|
|
304
|
+
loaded: false,
|
|
305
|
+
children: [],
|
|
306
|
+
paths: [],
|
|
307
|
+
require: function (path, base) {
|
|
308
|
+
return commonjsRequire(path, (base === undefined || base === null) ? cachedModule.path : base);
|
|
309
|
+
}
|
|
310
|
+
};
|
|
311
|
+
try {
|
|
312
|
+
loader.call(commonjsGlobal, cachedModule, cachedModule.exports);
|
|
313
|
+
} catch (error) {
|
|
314
|
+
delete DYNAMIC_REQUIRE_CACHE[resolvedPath];
|
|
315
|
+
throw error;
|
|
316
|
+
}
|
|
317
|
+
cachedModule.loaded = true;
|
|
318
|
+
return cachedModule.exports;
|
|
319
|
+
};
|
|
320
|
+
}
|
|
292
321
|
return require(path);
|
|
293
322
|
}
|
|
294
323
|
|
|
295
324
|
commonjsRequire.cache = DYNAMIC_REQUIRE_CACHE;
|
|
325
|
+
commonjsRequire.resolve = commonjsResolve;
|
|
296
326
|
`;
|
|
297
327
|
|
|
298
328
|
function getHelpersModule(isDynamicRequireModulesEnabled) {
|
|
@@ -336,20 +366,26 @@ const getVirtualPathForDynamicRequirePath = (path, commonDir) => {
|
|
|
336
366
|
: normalizedPath;
|
|
337
367
|
};
|
|
338
368
|
|
|
369
|
+
function getPackageEntryPoint(dirPath) {
|
|
370
|
+
let entryPoint = 'index.js';
|
|
371
|
+
|
|
372
|
+
try {
|
|
373
|
+
if (fs.existsSync(path.join(dirPath, 'package.json'))) {
|
|
374
|
+
entryPoint =
|
|
375
|
+
JSON.parse(fs.readFileSync(path.join(dirPath, 'package.json'), { encoding: 'utf8' })).main ||
|
|
376
|
+
entryPoint;
|
|
377
|
+
}
|
|
378
|
+
} catch (ignored) {
|
|
379
|
+
// ignored
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
return entryPoint;
|
|
383
|
+
}
|
|
384
|
+
|
|
339
385
|
function getDynamicPackagesModule(dynamicRequireModuleDirPaths, commonDir) {
|
|
340
386
|
let code = `const commonjsRegister = require('${HELPERS_ID}?commonjsRegister');`;
|
|
341
387
|
for (const dir of dynamicRequireModuleDirPaths) {
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
try {
|
|
345
|
-
if (fs.existsSync(path.join(dir, 'package.json'))) {
|
|
346
|
-
entryPoint =
|
|
347
|
-
JSON.parse(fs.readFileSync(path.join(dir, 'package.json'), { encoding: 'utf8' })).main ||
|
|
348
|
-
entryPoint;
|
|
349
|
-
}
|
|
350
|
-
} catch (ignored) {
|
|
351
|
-
// ignored
|
|
352
|
-
}
|
|
388
|
+
const entryPoint = getPackageEntryPoint(dir);
|
|
353
389
|
|
|
354
390
|
code += `\ncommonjsRegister(${JSON.stringify(
|
|
355
391
|
getVirtualPathForDynamicRequirePath(dir, commonDir)
|
|
@@ -366,21 +402,42 @@ function getDynamicPackagesEntryIntro(
|
|
|
366
402
|
) {
|
|
367
403
|
let dynamicImports = Array.from(
|
|
368
404
|
dynamicRequireModuleSet,
|
|
369
|
-
(dynamicId) => `require(${JSON.stringify(
|
|
405
|
+
(dynamicId) => `require(${JSON.stringify(wrapModuleRegisterProxy(dynamicId))});`
|
|
370
406
|
).join('\n');
|
|
371
407
|
|
|
372
408
|
if (dynamicRequireModuleDirPaths.length) {
|
|
373
|
-
dynamicImports += `require(${JSON.stringify(
|
|
409
|
+
dynamicImports += `require(${JSON.stringify(wrapModuleRegisterProxy(DYNAMIC_PACKAGES_ID))});`;
|
|
374
410
|
}
|
|
375
411
|
|
|
376
412
|
return dynamicImports;
|
|
377
413
|
}
|
|
378
414
|
|
|
379
|
-
function
|
|
415
|
+
function wrapModuleRegisterProxy(id) {
|
|
416
|
+
return wrapId(id, DYNAMIC_REGISTER_SUFFIX);
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
function unwrapModuleRegisterProxy(id) {
|
|
420
|
+
return unwrapId(id, DYNAMIC_REGISTER_SUFFIX);
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
function isModuleRegisterProxy(id) {
|
|
424
|
+
return isWrappedId(id, DYNAMIC_REGISTER_SUFFIX);
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
function isDynamicModuleImport(id, dynamicRequireModuleSet) {
|
|
380
428
|
const normalizedPath = normalizePathSlashes(id);
|
|
381
429
|
return dynamicRequireModuleSet.has(normalizedPath) && !normalizedPath.endsWith('.json');
|
|
382
430
|
}
|
|
383
431
|
|
|
432
|
+
function isDirectory(path) {
|
|
433
|
+
try {
|
|
434
|
+
if (fs.statSync(path).isDirectory()) return true;
|
|
435
|
+
} catch (ignored) {
|
|
436
|
+
// Nothing to do here
|
|
437
|
+
}
|
|
438
|
+
return false;
|
|
439
|
+
}
|
|
440
|
+
|
|
384
441
|
function getDynamicRequirePaths(patterns) {
|
|
385
442
|
const dynamicRequireModuleSet = new Set();
|
|
386
443
|
for (const pattern of !patterns || Array.isArray(patterns) ? patterns || [] : [patterns]) {
|
|
@@ -388,17 +445,13 @@ function getDynamicRequirePaths(patterns) {
|
|
|
388
445
|
const modifySet = Set.prototype[isNegated ? 'delete' : 'add'].bind(dynamicRequireModuleSet);
|
|
389
446
|
for (const path$1 of glob__default['default'].sync(isNegated ? pattern.substr(1) : pattern)) {
|
|
390
447
|
modifySet(normalizePathSlashes(path.resolve(path$1)));
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
const dynamicRequireModuleDirPaths = Array.from(dynamicRequireModuleSet.values()).filter(
|
|
394
|
-
(path) => {
|
|
395
|
-
try {
|
|
396
|
-
if (fs.statSync(path).isDirectory()) return true;
|
|
397
|
-
} catch (ignored) {
|
|
398
|
-
// Nothing to do here
|
|
448
|
+
if (isDirectory(path$1)) {
|
|
449
|
+
modifySet(normalizePathSlashes(path.resolve(path.join(path$1, getPackageEntryPoint(path$1)))));
|
|
399
450
|
}
|
|
400
|
-
return false;
|
|
401
451
|
}
|
|
452
|
+
}
|
|
453
|
+
const dynamicRequireModuleDirPaths = Array.from(dynamicRequireModuleSet.values()).filter((path) =>
|
|
454
|
+
isDirectory(path)
|
|
402
455
|
);
|
|
403
456
|
return { dynamicRequireModuleSet, dynamicRequireModuleDirPaths };
|
|
404
457
|
}
|
|
@@ -531,7 +584,12 @@ function getResolveId(extensions) {
|
|
|
531
584
|
return undefined;
|
|
532
585
|
}
|
|
533
586
|
|
|
534
|
-
return function resolveId(importee,
|
|
587
|
+
return function resolveId(importee, rawImporter) {
|
|
588
|
+
const importer =
|
|
589
|
+
rawImporter && isModuleRegisterProxy(rawImporter)
|
|
590
|
+
? unwrapModuleRegisterProxy(rawImporter)
|
|
591
|
+
: rawImporter;
|
|
592
|
+
|
|
535
593
|
// Proxies are only importing resolved ids, no need to resolve again
|
|
536
594
|
if (importer && isWrappedId(importer, PROXY_SUFFIX)) {
|
|
537
595
|
return importee;
|
|
@@ -539,19 +597,28 @@ function getResolveId(extensions) {
|
|
|
539
597
|
|
|
540
598
|
const isProxyModule = isWrappedId(importee, PROXY_SUFFIX);
|
|
541
599
|
const isRequiredModule = isWrappedId(importee, REQUIRE_SUFFIX);
|
|
600
|
+
let isModuleRegistration = false;
|
|
601
|
+
|
|
542
602
|
if (isProxyModule) {
|
|
543
603
|
importee = unwrapId(importee, PROXY_SUFFIX);
|
|
544
604
|
} else if (isRequiredModule) {
|
|
545
605
|
importee = unwrapId(importee, REQUIRE_SUFFIX);
|
|
606
|
+
|
|
607
|
+
isModuleRegistration = isModuleRegisterProxy(importee);
|
|
608
|
+
if (isModuleRegistration) {
|
|
609
|
+
importee = unwrapModuleRegisterProxy(importee);
|
|
610
|
+
}
|
|
611
|
+
}
|
|
612
|
+
|
|
613
|
+
if (
|
|
614
|
+
importee.startsWith(HELPERS_ID) ||
|
|
615
|
+
importee === DYNAMIC_PACKAGES_ID ||
|
|
616
|
+
importee.startsWith(DYNAMIC_JSON_PREFIX)
|
|
617
|
+
) {
|
|
618
|
+
return importee;
|
|
546
619
|
}
|
|
620
|
+
|
|
547
621
|
if (importee.startsWith('\0')) {
|
|
548
|
-
if (
|
|
549
|
-
importee.startsWith(HELPERS_ID) ||
|
|
550
|
-
importee === DYNAMIC_PACKAGES_ID ||
|
|
551
|
-
importee.startsWith(DYNAMIC_JSON_PREFIX)
|
|
552
|
-
) {
|
|
553
|
-
return importee;
|
|
554
|
-
}
|
|
555
622
|
return null;
|
|
556
623
|
}
|
|
557
624
|
|
|
@@ -565,6 +632,8 @@ function getResolveId(extensions) {
|
|
|
565
632
|
if (resolved && isProxyModule) {
|
|
566
633
|
resolved.id = wrapId(resolved.id, resolved.external ? EXTERNAL_SUFFIX : PROXY_SUFFIX);
|
|
567
634
|
resolved.external = false;
|
|
635
|
+
} else if (resolved && isModuleRegistration) {
|
|
636
|
+
resolved.id = wrapModuleRegisterProxy(resolved.id);
|
|
568
637
|
} else if (!resolved && (isProxyModule || isRequiredModule)) {
|
|
569
638
|
return { id: wrapId(importee, EXTERNAL_SUFFIX), external: false };
|
|
570
639
|
}
|
|
@@ -710,6 +779,10 @@ function isLocallyShadowed(name, scope) {
|
|
|
710
779
|
return false;
|
|
711
780
|
}
|
|
712
781
|
|
|
782
|
+
function isShorthandProperty(parent) {
|
|
783
|
+
return parent && parent.type === 'Property' && parent.shorthand;
|
|
784
|
+
}
|
|
785
|
+
|
|
713
786
|
function wrapCode(magicString, uses, moduleName, HELPERS_NAME, virtualDynamicRequirePath) {
|
|
714
787
|
const args = `module${uses.exports ? ', exports' : ''}`;
|
|
715
788
|
|
|
@@ -1047,6 +1120,7 @@ function transformCommonjs(
|
|
|
1047
1120
|
isEsModule,
|
|
1048
1121
|
ignoreGlobal,
|
|
1049
1122
|
ignoreRequire,
|
|
1123
|
+
getIgnoreTryCatchRequireStatementMode,
|
|
1050
1124
|
sourceMap,
|
|
1051
1125
|
isDynamicRequireModulesEnabled,
|
|
1052
1126
|
dynamicRequireModuleSet,
|
|
@@ -1068,6 +1142,7 @@ function transformCommonjs(
|
|
|
1068
1142
|
let scope = pluginutils.attachScopes(ast, 'scope');
|
|
1069
1143
|
let lexicalDepth = 0;
|
|
1070
1144
|
let programDepth = 0;
|
|
1145
|
+
let currentTryBlockEnd = null;
|
|
1071
1146
|
let shouldWrap = false;
|
|
1072
1147
|
const defineCompiledEsmExpressions = [];
|
|
1073
1148
|
|
|
@@ -1077,6 +1152,7 @@ function transformCommonjs(
|
|
|
1077
1152
|
const HELPERS_NAME = deconflict(scope, globals, 'commonjsHelpers');
|
|
1078
1153
|
const namedExports = {};
|
|
1079
1154
|
const dynamicRegisterSources = new Set();
|
|
1155
|
+
let hasRemovedRequire = false;
|
|
1080
1156
|
|
|
1081
1157
|
const {
|
|
1082
1158
|
addRequireStatement,
|
|
@@ -1101,6 +1177,10 @@ function transformCommonjs(
|
|
|
1101
1177
|
return;
|
|
1102
1178
|
}
|
|
1103
1179
|
|
|
1180
|
+
if (currentTryBlockEnd !== null && node.start > currentTryBlockEnd) {
|
|
1181
|
+
currentTryBlockEnd = null;
|
|
1182
|
+
}
|
|
1183
|
+
|
|
1104
1184
|
programDepth += 1;
|
|
1105
1185
|
if (node.scope) ({ scope } = node);
|
|
1106
1186
|
if (functionType.test(node.type)) lexicalDepth += 1;
|
|
@@ -1111,6 +1191,11 @@ function transformCommonjs(
|
|
|
1111
1191
|
|
|
1112
1192
|
// eslint-disable-next-line default-case
|
|
1113
1193
|
switch (node.type) {
|
|
1194
|
+
case 'TryStatement':
|
|
1195
|
+
if (currentTryBlockEnd === null) {
|
|
1196
|
+
currentTryBlockEnd = node.block.end;
|
|
1197
|
+
}
|
|
1198
|
+
return;
|
|
1114
1199
|
case 'AssignmentExpression':
|
|
1115
1200
|
if (node.left.type === 'MemberExpression') {
|
|
1116
1201
|
const flattened = getKeypath(node.left);
|
|
@@ -1164,6 +1249,32 @@ function transformCommonjs(
|
|
|
1164
1249
|
}
|
|
1165
1250
|
return;
|
|
1166
1251
|
}
|
|
1252
|
+
|
|
1253
|
+
if (
|
|
1254
|
+
node.callee.object &&
|
|
1255
|
+
node.callee.object.name === 'require' &&
|
|
1256
|
+
node.callee.property.name === 'resolve' &&
|
|
1257
|
+
hasDynamicModuleForPath(id, '/', dynamicRequireModuleSet)
|
|
1258
|
+
) {
|
|
1259
|
+
const requireNode = node.callee.object;
|
|
1260
|
+
magicString.appendLeft(
|
|
1261
|
+
node.end - 1,
|
|
1262
|
+
`,${JSON.stringify(
|
|
1263
|
+
path.dirname(id) === '.' ? null /* default behavior */ : virtualDynamicRequirePath
|
|
1264
|
+
)}`
|
|
1265
|
+
);
|
|
1266
|
+
magicString.overwrite(
|
|
1267
|
+
requireNode.start,
|
|
1268
|
+
requireNode.end,
|
|
1269
|
+
`${HELPERS_NAME}.commonjsRequire`,
|
|
1270
|
+
{
|
|
1271
|
+
storeName: true
|
|
1272
|
+
}
|
|
1273
|
+
);
|
|
1274
|
+
uses.commonjsHelpers = true;
|
|
1275
|
+
return;
|
|
1276
|
+
}
|
|
1277
|
+
|
|
1167
1278
|
if (!isStaticRequireStatement(node, scope)) return;
|
|
1168
1279
|
if (!isDynamicRequireModulesEnabled) {
|
|
1169
1280
|
skippedNodes.add(node.callee);
|
|
@@ -1172,35 +1283,61 @@ function transformCommonjs(
|
|
|
1172
1283
|
skippedNodes.add(node.callee);
|
|
1173
1284
|
const usesReturnValue = parent.type !== 'ExpressionStatement';
|
|
1174
1285
|
|
|
1286
|
+
let canConvertRequire = true;
|
|
1287
|
+
let shouldRemoveRequireStatement = false;
|
|
1288
|
+
|
|
1289
|
+
if (currentTryBlockEnd !== null) {
|
|
1290
|
+
({
|
|
1291
|
+
canConvertRequire,
|
|
1292
|
+
shouldRemoveRequireStatement
|
|
1293
|
+
} = getIgnoreTryCatchRequireStatementMode(node.arguments[0].value));
|
|
1294
|
+
|
|
1295
|
+
if (shouldRemoveRequireStatement) {
|
|
1296
|
+
hasRemovedRequire = true;
|
|
1297
|
+
}
|
|
1298
|
+
}
|
|
1299
|
+
|
|
1175
1300
|
let sourceId = getRequireStringArg(node);
|
|
1176
|
-
const isDynamicRegister = sourceId
|
|
1301
|
+
const isDynamicRegister = isModuleRegisterProxy(sourceId);
|
|
1177
1302
|
if (isDynamicRegister) {
|
|
1178
|
-
sourceId = sourceId
|
|
1303
|
+
sourceId = unwrapModuleRegisterProxy(sourceId);
|
|
1179
1304
|
if (sourceId.endsWith('.json')) {
|
|
1180
1305
|
sourceId = DYNAMIC_JSON_PREFIX + sourceId;
|
|
1181
1306
|
}
|
|
1182
|
-
dynamicRegisterSources.add(sourceId);
|
|
1307
|
+
dynamicRegisterSources.add(wrapModuleRegisterProxy(sourceId));
|
|
1183
1308
|
} else {
|
|
1184
1309
|
if (
|
|
1185
1310
|
!sourceId.endsWith('.json') &&
|
|
1186
1311
|
hasDynamicModuleForPath(sourceId, id, dynamicRequireModuleSet)
|
|
1187
1312
|
) {
|
|
1188
|
-
|
|
1189
|
-
node.start,
|
|
1190
|
-
|
|
1191
|
-
|
|
1192
|
-
|
|
1193
|
-
|
|
1194
|
-
|
|
1195
|
-
|
|
1196
|
-
|
|
1197
|
-
|
|
1313
|
+
if (shouldRemoveRequireStatement) {
|
|
1314
|
+
magicString.overwrite(node.start, node.end, `undefined`);
|
|
1315
|
+
} else if (canConvertRequire) {
|
|
1316
|
+
magicString.overwrite(
|
|
1317
|
+
node.start,
|
|
1318
|
+
node.end,
|
|
1319
|
+
`${HELPERS_NAME}.commonjsRequire(${JSON.stringify(
|
|
1320
|
+
getVirtualPathForDynamicRequirePath(sourceId, commonDir)
|
|
1321
|
+
)}, ${JSON.stringify(
|
|
1322
|
+
path.dirname(id) === '.' ? null /* default behavior */ : virtualDynamicRequirePath
|
|
1323
|
+
)})`
|
|
1324
|
+
);
|
|
1325
|
+
uses.commonjsHelpers = true;
|
|
1326
|
+
}
|
|
1198
1327
|
return;
|
|
1199
1328
|
}
|
|
1200
|
-
|
|
1329
|
+
|
|
1330
|
+
if (canConvertRequire) {
|
|
1331
|
+
addRequireStatement(sourceId, node, scope, usesReturnValue);
|
|
1332
|
+
}
|
|
1201
1333
|
}
|
|
1202
1334
|
|
|
1203
1335
|
if (usesReturnValue) {
|
|
1336
|
+
if (shouldRemoveRequireStatement) {
|
|
1337
|
+
magicString.overwrite(node.start, node.end, `undefined`);
|
|
1338
|
+
return;
|
|
1339
|
+
}
|
|
1340
|
+
|
|
1204
1341
|
if (
|
|
1205
1342
|
parent.type === 'VariableDeclarator' &&
|
|
1206
1343
|
!scope.parent &&
|
|
@@ -1212,6 +1349,11 @@ function transformCommonjs(
|
|
|
1212
1349
|
}
|
|
1213
1350
|
} else {
|
|
1214
1351
|
// This is a bare import, e.g. `require('foo');`
|
|
1352
|
+
|
|
1353
|
+
if (!canConvertRequire && !shouldRemoveRequireStatement) {
|
|
1354
|
+
return;
|
|
1355
|
+
}
|
|
1356
|
+
|
|
1215
1357
|
magicString.remove(parent.start, parent.end);
|
|
1216
1358
|
}
|
|
1217
1359
|
}
|
|
@@ -1231,7 +1373,18 @@ function transformCommonjs(
|
|
|
1231
1373
|
if (!(isReference__default['default'](node, parent) && !scope.contains(name))) return;
|
|
1232
1374
|
switch (name) {
|
|
1233
1375
|
case 'require':
|
|
1234
|
-
if (isNodeRequirePropertyAccess(parent))
|
|
1376
|
+
if (isNodeRequirePropertyAccess(parent)) {
|
|
1377
|
+
if (hasDynamicModuleForPath(id, '/', dynamicRequireModuleSet)) {
|
|
1378
|
+
if (parent.property.name === 'cache') {
|
|
1379
|
+
magicString.overwrite(node.start, node.end, `${HELPERS_NAME}.commonjsRequire`, {
|
|
1380
|
+
storeName: true
|
|
1381
|
+
});
|
|
1382
|
+
uses.commonjsHelpers = true;
|
|
1383
|
+
}
|
|
1384
|
+
}
|
|
1385
|
+
|
|
1386
|
+
return;
|
|
1387
|
+
}
|
|
1235
1388
|
|
|
1236
1389
|
if (isDynamicRequireModulesEnabled && isRequireStatement(parent, scope)) {
|
|
1237
1390
|
magicString.appendLeft(
|
|
@@ -1241,10 +1394,14 @@ function transformCommonjs(
|
|
|
1241
1394
|
)}`
|
|
1242
1395
|
);
|
|
1243
1396
|
}
|
|
1397
|
+
if (isShorthandProperty(parent)) {
|
|
1398
|
+
magicString.appendRight(node.end, `: ${HELPERS_NAME}.commonjsRequire`);
|
|
1399
|
+
} else {
|
|
1400
|
+
magicString.overwrite(node.start, node.end, `${HELPERS_NAME}.commonjsRequire`, {
|
|
1401
|
+
storeName: true
|
|
1402
|
+
});
|
|
1403
|
+
}
|
|
1244
1404
|
|
|
1245
|
-
magicString.overwrite(node.start, node.end, `${HELPERS_NAME}.commonjsRequire`, {
|
|
1246
|
-
storeName: true
|
|
1247
|
-
});
|
|
1248
1405
|
uses.commonjsHelpers = true;
|
|
1249
1406
|
return;
|
|
1250
1407
|
case 'module':
|
|
@@ -1353,7 +1510,8 @@ function transformCommonjs(
|
|
|
1353
1510
|
uses.module ||
|
|
1354
1511
|
uses.exports ||
|
|
1355
1512
|
uses.require ||
|
|
1356
|
-
uses.commonjsHelpers
|
|
1513
|
+
uses.commonjsHelpers ||
|
|
1514
|
+
hasRemovedRequire
|
|
1357
1515
|
) &&
|
|
1358
1516
|
(ignoreGlobal || !uses.global)
|
|
1359
1517
|
) {
|
|
@@ -1449,6 +1607,20 @@ function commonjs(options = {}) {
|
|
|
1449
1607
|
? (id) => options.ignore.includes(id)
|
|
1450
1608
|
: () => false;
|
|
1451
1609
|
|
|
1610
|
+
const getIgnoreTryCatchRequireStatementMode = (id) => {
|
|
1611
|
+
const mode =
|
|
1612
|
+
typeof options.ignoreTryCatch === 'function'
|
|
1613
|
+
? options.ignoreTryCatch(id)
|
|
1614
|
+
: Array.isArray(options.ignoreTryCatch)
|
|
1615
|
+
? options.ignoreTryCatch.includes(id)
|
|
1616
|
+
: options.ignoreTryCatch || false;
|
|
1617
|
+
|
|
1618
|
+
return {
|
|
1619
|
+
canConvertRequire: mode !== 'remove' && mode !== true,
|
|
1620
|
+
shouldRemoveRequireStatement: mode === 'remove'
|
|
1621
|
+
};
|
|
1622
|
+
};
|
|
1623
|
+
|
|
1452
1624
|
const resolveId = getResolveId(extensions);
|
|
1453
1625
|
|
|
1454
1626
|
const sourceMap = options.sourceMap !== false;
|
|
@@ -1478,8 +1650,13 @@ function commonjs(options = {}) {
|
|
|
1478
1650
|
return { meta: { commonjs: { isCommonJS: false } } };
|
|
1479
1651
|
}
|
|
1480
1652
|
|
|
1653
|
+
let disableWrap = false;
|
|
1654
|
+
|
|
1481
1655
|
// avoid wrapping in createCommonjsModule, as this is a commonjsRegister call
|
|
1482
|
-
|
|
1656
|
+
if (isModuleRegisterProxy(id)) {
|
|
1657
|
+
disableWrap = true;
|
|
1658
|
+
id = unwrapModuleRegisterProxy(id);
|
|
1659
|
+
}
|
|
1483
1660
|
|
|
1484
1661
|
return transformCommonjs(
|
|
1485
1662
|
this.parse,
|
|
@@ -1488,6 +1665,7 @@ function commonjs(options = {}) {
|
|
|
1488
1665
|
isEsModule,
|
|
1489
1666
|
ignoreGlobal || isEsModule,
|
|
1490
1667
|
ignoreRequire,
|
|
1668
|
+
getIgnoreTryCatchRequireStatementMode,
|
|
1491
1669
|
sourceMap,
|
|
1492
1670
|
isDynamicRequireModulesEnabled,
|
|
1493
1671
|
dynamicRequireModuleSet,
|
|
@@ -1536,8 +1714,15 @@ function commonjs(options = {}) {
|
|
|
1536
1714
|
return getDynamicJsonProxy(id, commonDir);
|
|
1537
1715
|
}
|
|
1538
1716
|
|
|
1539
|
-
if (
|
|
1540
|
-
return
|
|
1717
|
+
if (isDynamicModuleImport(id, dynamicRequireModuleSet)) {
|
|
1718
|
+
return `export default require(${JSON.stringify(normalizePathSlashes(id))});`;
|
|
1719
|
+
}
|
|
1720
|
+
|
|
1721
|
+
if (isModuleRegisterProxy(id)) {
|
|
1722
|
+
return getDynamicRequireProxy(
|
|
1723
|
+
normalizePathSlashes(unwrapModuleRegisterProxy(id)),
|
|
1724
|
+
commonDir
|
|
1725
|
+
);
|
|
1541
1726
|
}
|
|
1542
1727
|
|
|
1543
1728
|
if (isWrappedId(id, PROXY_SUFFIX)) {
|
|
@@ -1553,7 +1738,13 @@ function commonjs(options = {}) {
|
|
|
1553
1738
|
return null;
|
|
1554
1739
|
},
|
|
1555
1740
|
|
|
1556
|
-
transform(code,
|
|
1741
|
+
transform(code, rawId) {
|
|
1742
|
+
let id = rawId;
|
|
1743
|
+
|
|
1744
|
+
if (isModuleRegisterProxy(id)) {
|
|
1745
|
+
id = unwrapModuleRegisterProxy(id);
|
|
1746
|
+
}
|
|
1747
|
+
|
|
1557
1748
|
const extName = path.extname(id);
|
|
1558
1749
|
if (
|
|
1559
1750
|
extName !== '.cjs' &&
|
|
@@ -1565,7 +1756,7 @@ function commonjs(options = {}) {
|
|
|
1565
1756
|
}
|
|
1566
1757
|
|
|
1567
1758
|
try {
|
|
1568
|
-
return transformAndCheckExports.call(this, code,
|
|
1759
|
+
return transformAndCheckExports.call(this, code, rawId);
|
|
1569
1760
|
} catch (err) {
|
|
1570
1761
|
return this.error(err, err.loc);
|
|
1571
1762
|
}
|