@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/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,23 @@
|
|
|
1
1
|
# @rollup/plugin-commonjs ChangeLog
|
|
2
2
|
|
|
3
|
+
## v17.1.0
|
|
4
|
+
|
|
5
|
+
_2021-01-29_
|
|
6
|
+
|
|
7
|
+
### Bugfixes
|
|
8
|
+
|
|
9
|
+
- fix: correctly replace shorthand `require` (#764)
|
|
10
|
+
|
|
11
|
+
### Features
|
|
12
|
+
|
|
13
|
+
- feature: load dynamic commonjs modules from es `import` (#766)
|
|
14
|
+
- feature: support cache/resolve access inside dynamic modules (#728)
|
|
15
|
+
- feature: allow keeping `require` calls inside try-catch (#729)
|
|
16
|
+
|
|
17
|
+
### Updates
|
|
18
|
+
|
|
19
|
+
- chore: fix lint error (#719)
|
|
20
|
+
|
|
3
21
|
## v17.0.0
|
|
4
22
|
|
|
5
23
|
_2020-11-30_
|
package/README.md
CHANGED
|
@@ -120,6 +120,20 @@ Default: `[]`
|
|
|
120
120
|
|
|
121
121
|
Sometimes you have to leave require statements unconverted. Pass an array containing the IDs or an `id => boolean` function.
|
|
122
122
|
|
|
123
|
+
### `ignoreTryCatch`
|
|
124
|
+
|
|
125
|
+
Type: `boolean | 'remove' | string[] | ((id: string) => boolean)`<br>
|
|
126
|
+
Default: `false`
|
|
127
|
+
|
|
128
|
+
In most cases, where `require` calls are inside a `try-catch` clause, they should be left unconverted as it requires an optional dependency that may or may not be installed beside the rolled up package.
|
|
129
|
+
Due to the conversion of `require` to a static `import` - the call is hoisted to the top of the file, outside of the `try-catch` clause.
|
|
130
|
+
|
|
131
|
+
- `true`: All `require` calls inside a `try` will be left unconverted.
|
|
132
|
+
- `false`: All `require` calls inside a `try` will be converted as if the `try-catch` clause is not there.
|
|
133
|
+
- `remove`: Remove all `require` calls from inside any `try` block.
|
|
134
|
+
- `string[]`: Pass an array containing the IDs to left unconverted.
|
|
135
|
+
- `((id: string) => boolean|'remove')`: Pass a function that control individual IDs.
|
|
136
|
+
|
|
123
137
|
### `esmExternals`
|
|
124
138
|
|
|
125
139
|
Type: `boolean | string[] | ((id: string) => boolean)`
|
package/dist/index.es.js
CHANGED
|
@@ -84,7 +84,7 @@ const PROXY_SUFFIX = '?commonjs-proxy';
|
|
|
84
84
|
const REQUIRE_SUFFIX = '?commonjs-require';
|
|
85
85
|
const EXTERNAL_SUFFIX = '?commonjs-external';
|
|
86
86
|
|
|
87
|
-
const
|
|
87
|
+
const DYNAMIC_REGISTER_SUFFIX = '?commonjs-dynamic-register';
|
|
88
88
|
const DYNAMIC_JSON_PREFIX = '\0commonjs-dynamic-json:';
|
|
89
89
|
const DYNAMIC_PACKAGES_ID = '\0commonjs-dynamic-packages';
|
|
90
90
|
|
|
@@ -234,10 +234,13 @@ function dirname (path) {
|
|
|
234
234
|
return '.';
|
|
235
235
|
}
|
|
236
236
|
|
|
237
|
-
export function
|
|
237
|
+
export function commonjsResolveImpl (path, originalModuleDir, testCache) {
|
|
238
238
|
const shouldTryNodeModules = isPossibleNodeModulesPath(path);
|
|
239
239
|
path = normalize(path);
|
|
240
240
|
let relPath;
|
|
241
|
+
if (path[0] === '/') {
|
|
242
|
+
originalModuleDir = '/';
|
|
243
|
+
}
|
|
241
244
|
while (true) {
|
|
242
245
|
if (!shouldTryNodeModules) {
|
|
243
246
|
relPath = originalModuleDir ? normalize(originalModuleDir + '/' + path) : path;
|
|
@@ -246,33 +249,18 @@ export function commonjsRequire (path, originalModuleDir) {
|
|
|
246
249
|
} else {
|
|
247
250
|
relPath = normalize(join('node_modules', path));
|
|
248
251
|
}
|
|
252
|
+
|
|
253
|
+
if (relPath.endsWith('/..')) {
|
|
254
|
+
break; // Travelled too far up, avoid infinite loop
|
|
255
|
+
}
|
|
256
|
+
|
|
249
257
|
for (let extensionIndex = 0; extensionIndex < CHECKED_EXTENSIONS.length; extensionIndex++) {
|
|
250
258
|
const resolvedPath = relPath + CHECKED_EXTENSIONS[extensionIndex];
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
if (
|
|
255
|
-
|
|
256
|
-
id: resolvedPath,
|
|
257
|
-
filename: resolvedPath,
|
|
258
|
-
path: dirname(resolvedPath),
|
|
259
|
-
exports: {},
|
|
260
|
-
parent: DEFAULT_PARENT_MODULE,
|
|
261
|
-
loaded: false,
|
|
262
|
-
children: [],
|
|
263
|
-
paths: [],
|
|
264
|
-
require: function (path, base) {
|
|
265
|
-
return commonjsRequire(path, (base === undefined || base === null) ? cachedModule.path : base);
|
|
266
|
-
}
|
|
267
|
-
};
|
|
268
|
-
try {
|
|
269
|
-
loader.call(commonjsGlobal, cachedModule, cachedModule.exports);
|
|
270
|
-
} catch (error) {
|
|
271
|
-
delete DYNAMIC_REQUIRE_CACHE[resolvedPath];
|
|
272
|
-
throw error;
|
|
273
|
-
}
|
|
274
|
-
cachedModule.loaded = true;
|
|
275
|
-
return cachedModule.exports;
|
|
259
|
+
if (DYNAMIC_REQUIRE_CACHE[resolvedPath]) {
|
|
260
|
+
return resolvedPath;
|
|
261
|
+
};
|
|
262
|
+
if (DYNAMIC_REQUIRE_LOADERS[resolvedPath]) {
|
|
263
|
+
return resolvedPath;
|
|
276
264
|
};
|
|
277
265
|
}
|
|
278
266
|
if (!shouldTryNodeModules) break;
|
|
@@ -280,10 +268,52 @@ export function commonjsRequire (path, originalModuleDir) {
|
|
|
280
268
|
if (nextDir === originalModuleDir) break;
|
|
281
269
|
originalModuleDir = nextDir;
|
|
282
270
|
}
|
|
271
|
+
return null;
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
export function commonjsResolve (path, originalModuleDir) {
|
|
275
|
+
const resolvedPath = commonjsResolveImpl(path, originalModuleDir);
|
|
276
|
+
if (resolvedPath !== null) {
|
|
277
|
+
return resolvedPath;
|
|
278
|
+
}
|
|
279
|
+
return require.resolve(path);
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
export function commonjsRequire (path, originalModuleDir) {
|
|
283
|
+
const resolvedPath = commonjsResolveImpl(path, originalModuleDir, true);
|
|
284
|
+
if (resolvedPath !== null) {
|
|
285
|
+
let cachedModule = DYNAMIC_REQUIRE_CACHE[resolvedPath];
|
|
286
|
+
if (cachedModule) return cachedModule.exports;
|
|
287
|
+
const loader = DYNAMIC_REQUIRE_LOADERS[resolvedPath];
|
|
288
|
+
if (loader) {
|
|
289
|
+
DYNAMIC_REQUIRE_CACHE[resolvedPath] = cachedModule = {
|
|
290
|
+
id: resolvedPath,
|
|
291
|
+
filename: resolvedPath,
|
|
292
|
+
path: dirname(resolvedPath),
|
|
293
|
+
exports: {},
|
|
294
|
+
parent: DEFAULT_PARENT_MODULE,
|
|
295
|
+
loaded: false,
|
|
296
|
+
children: [],
|
|
297
|
+
paths: [],
|
|
298
|
+
require: function (path, base) {
|
|
299
|
+
return commonjsRequire(path, (base === undefined || base === null) ? cachedModule.path : base);
|
|
300
|
+
}
|
|
301
|
+
};
|
|
302
|
+
try {
|
|
303
|
+
loader.call(commonjsGlobal, cachedModule, cachedModule.exports);
|
|
304
|
+
} catch (error) {
|
|
305
|
+
delete DYNAMIC_REQUIRE_CACHE[resolvedPath];
|
|
306
|
+
throw error;
|
|
307
|
+
}
|
|
308
|
+
cachedModule.loaded = true;
|
|
309
|
+
return cachedModule.exports;
|
|
310
|
+
};
|
|
311
|
+
}
|
|
283
312
|
return require(path);
|
|
284
313
|
}
|
|
285
314
|
|
|
286
315
|
commonjsRequire.cache = DYNAMIC_REQUIRE_CACHE;
|
|
316
|
+
commonjsRequire.resolve = commonjsResolve;
|
|
287
317
|
`;
|
|
288
318
|
|
|
289
319
|
function getHelpersModule(isDynamicRequireModulesEnabled) {
|
|
@@ -327,20 +357,26 @@ const getVirtualPathForDynamicRequirePath = (path, commonDir) => {
|
|
|
327
357
|
: normalizedPath;
|
|
328
358
|
};
|
|
329
359
|
|
|
360
|
+
function getPackageEntryPoint(dirPath) {
|
|
361
|
+
let entryPoint = 'index.js';
|
|
362
|
+
|
|
363
|
+
try {
|
|
364
|
+
if (existsSync(join(dirPath, 'package.json'))) {
|
|
365
|
+
entryPoint =
|
|
366
|
+
JSON.parse(readFileSync(join(dirPath, 'package.json'), { encoding: 'utf8' })).main ||
|
|
367
|
+
entryPoint;
|
|
368
|
+
}
|
|
369
|
+
} catch (ignored) {
|
|
370
|
+
// ignored
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
return entryPoint;
|
|
374
|
+
}
|
|
375
|
+
|
|
330
376
|
function getDynamicPackagesModule(dynamicRequireModuleDirPaths, commonDir) {
|
|
331
377
|
let code = `const commonjsRegister = require('${HELPERS_ID}?commonjsRegister');`;
|
|
332
378
|
for (const dir of dynamicRequireModuleDirPaths) {
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
try {
|
|
336
|
-
if (existsSync(join(dir, 'package.json'))) {
|
|
337
|
-
entryPoint =
|
|
338
|
-
JSON.parse(readFileSync(join(dir, 'package.json'), { encoding: 'utf8' })).main ||
|
|
339
|
-
entryPoint;
|
|
340
|
-
}
|
|
341
|
-
} catch (ignored) {
|
|
342
|
-
// ignored
|
|
343
|
-
}
|
|
379
|
+
const entryPoint = getPackageEntryPoint(dir);
|
|
344
380
|
|
|
345
381
|
code += `\ncommonjsRegister(${JSON.stringify(
|
|
346
382
|
getVirtualPathForDynamicRequirePath(dir, commonDir)
|
|
@@ -357,21 +393,42 @@ function getDynamicPackagesEntryIntro(
|
|
|
357
393
|
) {
|
|
358
394
|
let dynamicImports = Array.from(
|
|
359
395
|
dynamicRequireModuleSet,
|
|
360
|
-
(dynamicId) => `require(${JSON.stringify(
|
|
396
|
+
(dynamicId) => `require(${JSON.stringify(wrapModuleRegisterProxy(dynamicId))});`
|
|
361
397
|
).join('\n');
|
|
362
398
|
|
|
363
399
|
if (dynamicRequireModuleDirPaths.length) {
|
|
364
|
-
dynamicImports += `require(${JSON.stringify(
|
|
400
|
+
dynamicImports += `require(${JSON.stringify(wrapModuleRegisterProxy(DYNAMIC_PACKAGES_ID))});`;
|
|
365
401
|
}
|
|
366
402
|
|
|
367
403
|
return dynamicImports;
|
|
368
404
|
}
|
|
369
405
|
|
|
370
|
-
function
|
|
406
|
+
function wrapModuleRegisterProxy(id) {
|
|
407
|
+
return wrapId(id, DYNAMIC_REGISTER_SUFFIX);
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
function unwrapModuleRegisterProxy(id) {
|
|
411
|
+
return unwrapId(id, DYNAMIC_REGISTER_SUFFIX);
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
function isModuleRegisterProxy(id) {
|
|
415
|
+
return isWrappedId(id, DYNAMIC_REGISTER_SUFFIX);
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
function isDynamicModuleImport(id, dynamicRequireModuleSet) {
|
|
371
419
|
const normalizedPath = normalizePathSlashes(id);
|
|
372
420
|
return dynamicRequireModuleSet.has(normalizedPath) && !normalizedPath.endsWith('.json');
|
|
373
421
|
}
|
|
374
422
|
|
|
423
|
+
function isDirectory(path) {
|
|
424
|
+
try {
|
|
425
|
+
if (statSync(path).isDirectory()) return true;
|
|
426
|
+
} catch (ignored) {
|
|
427
|
+
// Nothing to do here
|
|
428
|
+
}
|
|
429
|
+
return false;
|
|
430
|
+
}
|
|
431
|
+
|
|
375
432
|
function getDynamicRequirePaths(patterns) {
|
|
376
433
|
const dynamicRequireModuleSet = new Set();
|
|
377
434
|
for (const pattern of !patterns || Array.isArray(patterns) ? patterns || [] : [patterns]) {
|
|
@@ -379,17 +436,13 @@ function getDynamicRequirePaths(patterns) {
|
|
|
379
436
|
const modifySet = Set.prototype[isNegated ? 'delete' : 'add'].bind(dynamicRequireModuleSet);
|
|
380
437
|
for (const path of glob.sync(isNegated ? pattern.substr(1) : pattern)) {
|
|
381
438
|
modifySet(normalizePathSlashes(resolve(path)));
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
const dynamicRequireModuleDirPaths = Array.from(dynamicRequireModuleSet.values()).filter(
|
|
385
|
-
(path) => {
|
|
386
|
-
try {
|
|
387
|
-
if (statSync(path).isDirectory()) return true;
|
|
388
|
-
} catch (ignored) {
|
|
389
|
-
// Nothing to do here
|
|
439
|
+
if (isDirectory(path)) {
|
|
440
|
+
modifySet(normalizePathSlashes(resolve(join(path, getPackageEntryPoint(path)))));
|
|
390
441
|
}
|
|
391
|
-
return false;
|
|
392
442
|
}
|
|
443
|
+
}
|
|
444
|
+
const dynamicRequireModuleDirPaths = Array.from(dynamicRequireModuleSet.values()).filter((path) =>
|
|
445
|
+
isDirectory(path)
|
|
393
446
|
);
|
|
394
447
|
return { dynamicRequireModuleSet, dynamicRequireModuleDirPaths };
|
|
395
448
|
}
|
|
@@ -522,7 +575,12 @@ function getResolveId(extensions) {
|
|
|
522
575
|
return undefined;
|
|
523
576
|
}
|
|
524
577
|
|
|
525
|
-
return function resolveId(importee,
|
|
578
|
+
return function resolveId(importee, rawImporter) {
|
|
579
|
+
const importer =
|
|
580
|
+
rawImporter && isModuleRegisterProxy(rawImporter)
|
|
581
|
+
? unwrapModuleRegisterProxy(rawImporter)
|
|
582
|
+
: rawImporter;
|
|
583
|
+
|
|
526
584
|
// Proxies are only importing resolved ids, no need to resolve again
|
|
527
585
|
if (importer && isWrappedId(importer, PROXY_SUFFIX)) {
|
|
528
586
|
return importee;
|
|
@@ -530,19 +588,28 @@ function getResolveId(extensions) {
|
|
|
530
588
|
|
|
531
589
|
const isProxyModule = isWrappedId(importee, PROXY_SUFFIX);
|
|
532
590
|
const isRequiredModule = isWrappedId(importee, REQUIRE_SUFFIX);
|
|
591
|
+
let isModuleRegistration = false;
|
|
592
|
+
|
|
533
593
|
if (isProxyModule) {
|
|
534
594
|
importee = unwrapId(importee, PROXY_SUFFIX);
|
|
535
595
|
} else if (isRequiredModule) {
|
|
536
596
|
importee = unwrapId(importee, REQUIRE_SUFFIX);
|
|
597
|
+
|
|
598
|
+
isModuleRegistration = isModuleRegisterProxy(importee);
|
|
599
|
+
if (isModuleRegistration) {
|
|
600
|
+
importee = unwrapModuleRegisterProxy(importee);
|
|
601
|
+
}
|
|
602
|
+
}
|
|
603
|
+
|
|
604
|
+
if (
|
|
605
|
+
importee.startsWith(HELPERS_ID) ||
|
|
606
|
+
importee === DYNAMIC_PACKAGES_ID ||
|
|
607
|
+
importee.startsWith(DYNAMIC_JSON_PREFIX)
|
|
608
|
+
) {
|
|
609
|
+
return importee;
|
|
537
610
|
}
|
|
611
|
+
|
|
538
612
|
if (importee.startsWith('\0')) {
|
|
539
|
-
if (
|
|
540
|
-
importee.startsWith(HELPERS_ID) ||
|
|
541
|
-
importee === DYNAMIC_PACKAGES_ID ||
|
|
542
|
-
importee.startsWith(DYNAMIC_JSON_PREFIX)
|
|
543
|
-
) {
|
|
544
|
-
return importee;
|
|
545
|
-
}
|
|
546
613
|
return null;
|
|
547
614
|
}
|
|
548
615
|
|
|
@@ -556,6 +623,8 @@ function getResolveId(extensions) {
|
|
|
556
623
|
if (resolved && isProxyModule) {
|
|
557
624
|
resolved.id = wrapId(resolved.id, resolved.external ? EXTERNAL_SUFFIX : PROXY_SUFFIX);
|
|
558
625
|
resolved.external = false;
|
|
626
|
+
} else if (resolved && isModuleRegistration) {
|
|
627
|
+
resolved.id = wrapModuleRegisterProxy(resolved.id);
|
|
559
628
|
} else if (!resolved && (isProxyModule || isRequiredModule)) {
|
|
560
629
|
return { id: wrapId(importee, EXTERNAL_SUFFIX), external: false };
|
|
561
630
|
}
|
|
@@ -701,6 +770,10 @@ function isLocallyShadowed(name, scope) {
|
|
|
701
770
|
return false;
|
|
702
771
|
}
|
|
703
772
|
|
|
773
|
+
function isShorthandProperty(parent) {
|
|
774
|
+
return parent && parent.type === 'Property' && parent.shorthand;
|
|
775
|
+
}
|
|
776
|
+
|
|
704
777
|
function wrapCode(magicString, uses, moduleName, HELPERS_NAME, virtualDynamicRequirePath) {
|
|
705
778
|
const args = `module${uses.exports ? ', exports' : ''}`;
|
|
706
779
|
|
|
@@ -1038,6 +1111,7 @@ function transformCommonjs(
|
|
|
1038
1111
|
isEsModule,
|
|
1039
1112
|
ignoreGlobal,
|
|
1040
1113
|
ignoreRequire,
|
|
1114
|
+
getIgnoreTryCatchRequireStatementMode,
|
|
1041
1115
|
sourceMap,
|
|
1042
1116
|
isDynamicRequireModulesEnabled,
|
|
1043
1117
|
dynamicRequireModuleSet,
|
|
@@ -1059,6 +1133,7 @@ function transformCommonjs(
|
|
|
1059
1133
|
let scope = attachScopes(ast, 'scope');
|
|
1060
1134
|
let lexicalDepth = 0;
|
|
1061
1135
|
let programDepth = 0;
|
|
1136
|
+
let currentTryBlockEnd = null;
|
|
1062
1137
|
let shouldWrap = false;
|
|
1063
1138
|
const defineCompiledEsmExpressions = [];
|
|
1064
1139
|
|
|
@@ -1068,6 +1143,7 @@ function transformCommonjs(
|
|
|
1068
1143
|
const HELPERS_NAME = deconflict(scope, globals, 'commonjsHelpers');
|
|
1069
1144
|
const namedExports = {};
|
|
1070
1145
|
const dynamicRegisterSources = new Set();
|
|
1146
|
+
let hasRemovedRequire = false;
|
|
1071
1147
|
|
|
1072
1148
|
const {
|
|
1073
1149
|
addRequireStatement,
|
|
@@ -1092,6 +1168,10 @@ function transformCommonjs(
|
|
|
1092
1168
|
return;
|
|
1093
1169
|
}
|
|
1094
1170
|
|
|
1171
|
+
if (currentTryBlockEnd !== null && node.start > currentTryBlockEnd) {
|
|
1172
|
+
currentTryBlockEnd = null;
|
|
1173
|
+
}
|
|
1174
|
+
|
|
1095
1175
|
programDepth += 1;
|
|
1096
1176
|
if (node.scope) ({ scope } = node);
|
|
1097
1177
|
if (functionType.test(node.type)) lexicalDepth += 1;
|
|
@@ -1102,6 +1182,11 @@ function transformCommonjs(
|
|
|
1102
1182
|
|
|
1103
1183
|
// eslint-disable-next-line default-case
|
|
1104
1184
|
switch (node.type) {
|
|
1185
|
+
case 'TryStatement':
|
|
1186
|
+
if (currentTryBlockEnd === null) {
|
|
1187
|
+
currentTryBlockEnd = node.block.end;
|
|
1188
|
+
}
|
|
1189
|
+
return;
|
|
1105
1190
|
case 'AssignmentExpression':
|
|
1106
1191
|
if (node.left.type === 'MemberExpression') {
|
|
1107
1192
|
const flattened = getKeypath(node.left);
|
|
@@ -1155,6 +1240,32 @@ function transformCommonjs(
|
|
|
1155
1240
|
}
|
|
1156
1241
|
return;
|
|
1157
1242
|
}
|
|
1243
|
+
|
|
1244
|
+
if (
|
|
1245
|
+
node.callee.object &&
|
|
1246
|
+
node.callee.object.name === 'require' &&
|
|
1247
|
+
node.callee.property.name === 'resolve' &&
|
|
1248
|
+
hasDynamicModuleForPath(id, '/', dynamicRequireModuleSet)
|
|
1249
|
+
) {
|
|
1250
|
+
const requireNode = node.callee.object;
|
|
1251
|
+
magicString.appendLeft(
|
|
1252
|
+
node.end - 1,
|
|
1253
|
+
`,${JSON.stringify(
|
|
1254
|
+
dirname(id) === '.' ? null /* default behavior */ : virtualDynamicRequirePath
|
|
1255
|
+
)}`
|
|
1256
|
+
);
|
|
1257
|
+
magicString.overwrite(
|
|
1258
|
+
requireNode.start,
|
|
1259
|
+
requireNode.end,
|
|
1260
|
+
`${HELPERS_NAME}.commonjsRequire`,
|
|
1261
|
+
{
|
|
1262
|
+
storeName: true
|
|
1263
|
+
}
|
|
1264
|
+
);
|
|
1265
|
+
uses.commonjsHelpers = true;
|
|
1266
|
+
return;
|
|
1267
|
+
}
|
|
1268
|
+
|
|
1158
1269
|
if (!isStaticRequireStatement(node, scope)) return;
|
|
1159
1270
|
if (!isDynamicRequireModulesEnabled) {
|
|
1160
1271
|
skippedNodes.add(node.callee);
|
|
@@ -1163,35 +1274,61 @@ function transformCommonjs(
|
|
|
1163
1274
|
skippedNodes.add(node.callee);
|
|
1164
1275
|
const usesReturnValue = parent.type !== 'ExpressionStatement';
|
|
1165
1276
|
|
|
1277
|
+
let canConvertRequire = true;
|
|
1278
|
+
let shouldRemoveRequireStatement = false;
|
|
1279
|
+
|
|
1280
|
+
if (currentTryBlockEnd !== null) {
|
|
1281
|
+
({
|
|
1282
|
+
canConvertRequire,
|
|
1283
|
+
shouldRemoveRequireStatement
|
|
1284
|
+
} = getIgnoreTryCatchRequireStatementMode(node.arguments[0].value));
|
|
1285
|
+
|
|
1286
|
+
if (shouldRemoveRequireStatement) {
|
|
1287
|
+
hasRemovedRequire = true;
|
|
1288
|
+
}
|
|
1289
|
+
}
|
|
1290
|
+
|
|
1166
1291
|
let sourceId = getRequireStringArg(node);
|
|
1167
|
-
const isDynamicRegister = sourceId
|
|
1292
|
+
const isDynamicRegister = isModuleRegisterProxy(sourceId);
|
|
1168
1293
|
if (isDynamicRegister) {
|
|
1169
|
-
sourceId = sourceId
|
|
1294
|
+
sourceId = unwrapModuleRegisterProxy(sourceId);
|
|
1170
1295
|
if (sourceId.endsWith('.json')) {
|
|
1171
1296
|
sourceId = DYNAMIC_JSON_PREFIX + sourceId;
|
|
1172
1297
|
}
|
|
1173
|
-
dynamicRegisterSources.add(sourceId);
|
|
1298
|
+
dynamicRegisterSources.add(wrapModuleRegisterProxy(sourceId));
|
|
1174
1299
|
} else {
|
|
1175
1300
|
if (
|
|
1176
1301
|
!sourceId.endsWith('.json') &&
|
|
1177
1302
|
hasDynamicModuleForPath(sourceId, id, dynamicRequireModuleSet)
|
|
1178
1303
|
) {
|
|
1179
|
-
|
|
1180
|
-
node.start,
|
|
1181
|
-
|
|
1182
|
-
|
|
1183
|
-
|
|
1184
|
-
|
|
1185
|
-
|
|
1186
|
-
|
|
1187
|
-
|
|
1188
|
-
|
|
1304
|
+
if (shouldRemoveRequireStatement) {
|
|
1305
|
+
magicString.overwrite(node.start, node.end, `undefined`);
|
|
1306
|
+
} else if (canConvertRequire) {
|
|
1307
|
+
magicString.overwrite(
|
|
1308
|
+
node.start,
|
|
1309
|
+
node.end,
|
|
1310
|
+
`${HELPERS_NAME}.commonjsRequire(${JSON.stringify(
|
|
1311
|
+
getVirtualPathForDynamicRequirePath(sourceId, commonDir)
|
|
1312
|
+
)}, ${JSON.stringify(
|
|
1313
|
+
dirname(id) === '.' ? null /* default behavior */ : virtualDynamicRequirePath
|
|
1314
|
+
)})`
|
|
1315
|
+
);
|
|
1316
|
+
uses.commonjsHelpers = true;
|
|
1317
|
+
}
|
|
1189
1318
|
return;
|
|
1190
1319
|
}
|
|
1191
|
-
|
|
1320
|
+
|
|
1321
|
+
if (canConvertRequire) {
|
|
1322
|
+
addRequireStatement(sourceId, node, scope, usesReturnValue);
|
|
1323
|
+
}
|
|
1192
1324
|
}
|
|
1193
1325
|
|
|
1194
1326
|
if (usesReturnValue) {
|
|
1327
|
+
if (shouldRemoveRequireStatement) {
|
|
1328
|
+
magicString.overwrite(node.start, node.end, `undefined`);
|
|
1329
|
+
return;
|
|
1330
|
+
}
|
|
1331
|
+
|
|
1195
1332
|
if (
|
|
1196
1333
|
parent.type === 'VariableDeclarator' &&
|
|
1197
1334
|
!scope.parent &&
|
|
@@ -1203,6 +1340,11 @@ function transformCommonjs(
|
|
|
1203
1340
|
}
|
|
1204
1341
|
} else {
|
|
1205
1342
|
// This is a bare import, e.g. `require('foo');`
|
|
1343
|
+
|
|
1344
|
+
if (!canConvertRequire && !shouldRemoveRequireStatement) {
|
|
1345
|
+
return;
|
|
1346
|
+
}
|
|
1347
|
+
|
|
1206
1348
|
magicString.remove(parent.start, parent.end);
|
|
1207
1349
|
}
|
|
1208
1350
|
}
|
|
@@ -1222,7 +1364,18 @@ function transformCommonjs(
|
|
|
1222
1364
|
if (!(isReference(node, parent) && !scope.contains(name))) return;
|
|
1223
1365
|
switch (name) {
|
|
1224
1366
|
case 'require':
|
|
1225
|
-
if (isNodeRequirePropertyAccess(parent))
|
|
1367
|
+
if (isNodeRequirePropertyAccess(parent)) {
|
|
1368
|
+
if (hasDynamicModuleForPath(id, '/', dynamicRequireModuleSet)) {
|
|
1369
|
+
if (parent.property.name === 'cache') {
|
|
1370
|
+
magicString.overwrite(node.start, node.end, `${HELPERS_NAME}.commonjsRequire`, {
|
|
1371
|
+
storeName: true
|
|
1372
|
+
});
|
|
1373
|
+
uses.commonjsHelpers = true;
|
|
1374
|
+
}
|
|
1375
|
+
}
|
|
1376
|
+
|
|
1377
|
+
return;
|
|
1378
|
+
}
|
|
1226
1379
|
|
|
1227
1380
|
if (isDynamicRequireModulesEnabled && isRequireStatement(parent, scope)) {
|
|
1228
1381
|
magicString.appendLeft(
|
|
@@ -1232,10 +1385,14 @@ function transformCommonjs(
|
|
|
1232
1385
|
)}`
|
|
1233
1386
|
);
|
|
1234
1387
|
}
|
|
1388
|
+
if (isShorthandProperty(parent)) {
|
|
1389
|
+
magicString.appendRight(node.end, `: ${HELPERS_NAME}.commonjsRequire`);
|
|
1390
|
+
} else {
|
|
1391
|
+
magicString.overwrite(node.start, node.end, `${HELPERS_NAME}.commonjsRequire`, {
|
|
1392
|
+
storeName: true
|
|
1393
|
+
});
|
|
1394
|
+
}
|
|
1235
1395
|
|
|
1236
|
-
magicString.overwrite(node.start, node.end, `${HELPERS_NAME}.commonjsRequire`, {
|
|
1237
|
-
storeName: true
|
|
1238
|
-
});
|
|
1239
1396
|
uses.commonjsHelpers = true;
|
|
1240
1397
|
return;
|
|
1241
1398
|
case 'module':
|
|
@@ -1344,7 +1501,8 @@ function transformCommonjs(
|
|
|
1344
1501
|
uses.module ||
|
|
1345
1502
|
uses.exports ||
|
|
1346
1503
|
uses.require ||
|
|
1347
|
-
uses.commonjsHelpers
|
|
1504
|
+
uses.commonjsHelpers ||
|
|
1505
|
+
hasRemovedRequire
|
|
1348
1506
|
) &&
|
|
1349
1507
|
(ignoreGlobal || !uses.global)
|
|
1350
1508
|
) {
|
|
@@ -1440,6 +1598,20 @@ function commonjs(options = {}) {
|
|
|
1440
1598
|
? (id) => options.ignore.includes(id)
|
|
1441
1599
|
: () => false;
|
|
1442
1600
|
|
|
1601
|
+
const getIgnoreTryCatchRequireStatementMode = (id) => {
|
|
1602
|
+
const mode =
|
|
1603
|
+
typeof options.ignoreTryCatch === 'function'
|
|
1604
|
+
? options.ignoreTryCatch(id)
|
|
1605
|
+
: Array.isArray(options.ignoreTryCatch)
|
|
1606
|
+
? options.ignoreTryCatch.includes(id)
|
|
1607
|
+
: options.ignoreTryCatch || false;
|
|
1608
|
+
|
|
1609
|
+
return {
|
|
1610
|
+
canConvertRequire: mode !== 'remove' && mode !== true,
|
|
1611
|
+
shouldRemoveRequireStatement: mode === 'remove'
|
|
1612
|
+
};
|
|
1613
|
+
};
|
|
1614
|
+
|
|
1443
1615
|
const resolveId = getResolveId(extensions);
|
|
1444
1616
|
|
|
1445
1617
|
const sourceMap = options.sourceMap !== false;
|
|
@@ -1469,8 +1641,13 @@ function commonjs(options = {}) {
|
|
|
1469
1641
|
return { meta: { commonjs: { isCommonJS: false } } };
|
|
1470
1642
|
}
|
|
1471
1643
|
|
|
1644
|
+
let disableWrap = false;
|
|
1645
|
+
|
|
1472
1646
|
// avoid wrapping in createCommonjsModule, as this is a commonjsRegister call
|
|
1473
|
-
|
|
1647
|
+
if (isModuleRegisterProxy(id)) {
|
|
1648
|
+
disableWrap = true;
|
|
1649
|
+
id = unwrapModuleRegisterProxy(id);
|
|
1650
|
+
}
|
|
1474
1651
|
|
|
1475
1652
|
return transformCommonjs(
|
|
1476
1653
|
this.parse,
|
|
@@ -1479,6 +1656,7 @@ function commonjs(options = {}) {
|
|
|
1479
1656
|
isEsModule,
|
|
1480
1657
|
ignoreGlobal || isEsModule,
|
|
1481
1658
|
ignoreRequire,
|
|
1659
|
+
getIgnoreTryCatchRequireStatementMode,
|
|
1482
1660
|
sourceMap,
|
|
1483
1661
|
isDynamicRequireModulesEnabled,
|
|
1484
1662
|
dynamicRequireModuleSet,
|
|
@@ -1527,8 +1705,15 @@ function commonjs(options = {}) {
|
|
|
1527
1705
|
return getDynamicJsonProxy(id, commonDir);
|
|
1528
1706
|
}
|
|
1529
1707
|
|
|
1530
|
-
if (
|
|
1531
|
-
return
|
|
1708
|
+
if (isDynamicModuleImport(id, dynamicRequireModuleSet)) {
|
|
1709
|
+
return `export default require(${JSON.stringify(normalizePathSlashes(id))});`;
|
|
1710
|
+
}
|
|
1711
|
+
|
|
1712
|
+
if (isModuleRegisterProxy(id)) {
|
|
1713
|
+
return getDynamicRequireProxy(
|
|
1714
|
+
normalizePathSlashes(unwrapModuleRegisterProxy(id)),
|
|
1715
|
+
commonDir
|
|
1716
|
+
);
|
|
1532
1717
|
}
|
|
1533
1718
|
|
|
1534
1719
|
if (isWrappedId(id, PROXY_SUFFIX)) {
|
|
@@ -1544,7 +1729,13 @@ function commonjs(options = {}) {
|
|
|
1544
1729
|
return null;
|
|
1545
1730
|
},
|
|
1546
1731
|
|
|
1547
|
-
transform(code,
|
|
1732
|
+
transform(code, rawId) {
|
|
1733
|
+
let id = rawId;
|
|
1734
|
+
|
|
1735
|
+
if (isModuleRegisterProxy(id)) {
|
|
1736
|
+
id = unwrapModuleRegisterProxy(id);
|
|
1737
|
+
}
|
|
1738
|
+
|
|
1548
1739
|
const extName = extname(id);
|
|
1549
1740
|
if (
|
|
1550
1741
|
extName !== '.cjs' &&
|
|
@@ -1556,7 +1747,7 @@ function commonjs(options = {}) {
|
|
|
1556
1747
|
}
|
|
1557
1748
|
|
|
1558
1749
|
try {
|
|
1559
|
-
return transformAndCheckExports.call(this, code,
|
|
1750
|
+
return transformAndCheckExports.call(this, code, rawId);
|
|
1560
1751
|
} catch (err) {
|
|
1561
1752
|
return this.error(err, err.loc);
|
|
1562
1753
|
}
|