@rollup/plugin-commonjs 21.0.1 → 22.0.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/README.md +34 -6
- package/dist/index.es.js +878 -796
- package/dist/index.es.js.map +1 -1
- package/dist/index.js +876 -794
- package/dist/index.js.map +1 -1
- package/package.json +5 -5
- package/types/index.d.ts +52 -10
package/dist/index.es.js
CHANGED
|
@@ -1,15 +1,15 @@
|
|
|
1
|
-
import { basename, extname, dirname,
|
|
2
|
-
import { makeLegalIdentifier, attachScopes, extractAssignedNames
|
|
3
|
-
import getCommonDir from 'commondir';
|
|
1
|
+
import { basename, extname, dirname, relative, resolve, join, sep } from 'path';
|
|
2
|
+
import { makeLegalIdentifier, createFilter, attachScopes, extractAssignedNames } from '@rollup/pluginutils';
|
|
4
3
|
import { existsSync, readFileSync, statSync } from 'fs';
|
|
4
|
+
import getCommonDir from 'commondir';
|
|
5
5
|
import glob from 'glob';
|
|
6
6
|
import { walk } from 'estree-walker';
|
|
7
7
|
import MagicString from 'magic-string';
|
|
8
8
|
import isReference from 'is-reference';
|
|
9
|
-
import { sync } from 'resolve';
|
|
10
9
|
|
|
10
|
+
var version = "22.0.0-0";
|
|
11
11
|
var peerDependencies = {
|
|
12
|
-
rollup: "^2.
|
|
12
|
+
rollup: "^2.60.0"
|
|
13
13
|
};
|
|
14
14
|
|
|
15
15
|
function tryParse(parse, code, id) {
|
|
@@ -76,271 +76,6 @@ function analyzeTopLevelStatements(parse, code, id) {
|
|
|
76
76
|
return { isEsModule, hasDefaultExport, hasNamedExports, ast };
|
|
77
77
|
}
|
|
78
78
|
|
|
79
|
-
const isWrappedId = (id, suffix) => id.endsWith(suffix);
|
|
80
|
-
const wrapId = (id, suffix) => `\0${id}${suffix}`;
|
|
81
|
-
const unwrapId = (wrappedId, suffix) => wrappedId.slice(1, -suffix.length);
|
|
82
|
-
|
|
83
|
-
const PROXY_SUFFIX = '?commonjs-proxy';
|
|
84
|
-
const REQUIRE_SUFFIX = '?commonjs-require';
|
|
85
|
-
const EXTERNAL_SUFFIX = '?commonjs-external';
|
|
86
|
-
const EXPORTS_SUFFIX = '?commonjs-exports';
|
|
87
|
-
const MODULE_SUFFIX = '?commonjs-module';
|
|
88
|
-
|
|
89
|
-
const DYNAMIC_REGISTER_SUFFIX = '?commonjs-dynamic-register';
|
|
90
|
-
const DYNAMIC_JSON_PREFIX = '\0commonjs-dynamic-json:';
|
|
91
|
-
const DYNAMIC_PACKAGES_ID = '\0commonjs-dynamic-packages';
|
|
92
|
-
|
|
93
|
-
const HELPERS_ID = '\0commonjsHelpers.js';
|
|
94
|
-
|
|
95
|
-
// `x['default']` is used instead of `x.default` for backward compatibility with ES3 browsers.
|
|
96
|
-
// Minifiers like uglify will usually transpile it back if compatibility with ES3 is not enabled.
|
|
97
|
-
// This will no longer be necessary once Rollup switches to ES6 output, likely
|
|
98
|
-
// in Rollup 3
|
|
99
|
-
|
|
100
|
-
const HELPERS = `
|
|
101
|
-
export var commonjsGlobal = typeof globalThis !== 'undefined' ? globalThis : typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {};
|
|
102
|
-
|
|
103
|
-
export function getDefaultExportFromCjs (x) {
|
|
104
|
-
return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
export function getDefaultExportFromNamespaceIfPresent (n) {
|
|
108
|
-
return n && Object.prototype.hasOwnProperty.call(n, 'default') ? n['default'] : n;
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
export function getDefaultExportFromNamespaceIfNotNamed (n) {
|
|
112
|
-
return n && Object.prototype.hasOwnProperty.call(n, 'default') && Object.keys(n).length === 1 ? n['default'] : n;
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
export function getAugmentedNamespace(n) {
|
|
116
|
-
if (n.__esModule) return n;
|
|
117
|
-
var a = Object.defineProperty({}, '__esModule', {value: true});
|
|
118
|
-
Object.keys(n).forEach(function (k) {
|
|
119
|
-
var d = Object.getOwnPropertyDescriptor(n, k);
|
|
120
|
-
Object.defineProperty(a, k, d.get ? d : {
|
|
121
|
-
enumerable: true,
|
|
122
|
-
get: function () {
|
|
123
|
-
return n[k];
|
|
124
|
-
}
|
|
125
|
-
});
|
|
126
|
-
});
|
|
127
|
-
return a;
|
|
128
|
-
}
|
|
129
|
-
`;
|
|
130
|
-
|
|
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.');`;
|
|
132
|
-
|
|
133
|
-
const HELPER_NON_DYNAMIC = `
|
|
134
|
-
export function commonjsRequire (path) {
|
|
135
|
-
${FAILED_REQUIRE_ERROR}
|
|
136
|
-
}
|
|
137
|
-
`;
|
|
138
|
-
|
|
139
|
-
const getDynamicHelpers = (ignoreDynamicRequires) => `
|
|
140
|
-
export function createModule(modulePath) {
|
|
141
|
-
return {
|
|
142
|
-
path: modulePath,
|
|
143
|
-
exports: {},
|
|
144
|
-
require: function (path, base) {
|
|
145
|
-
return commonjsRequire(path, base == null ? modulePath : base);
|
|
146
|
-
}
|
|
147
|
-
};
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
export function commonjsRegister (path, loader) {
|
|
151
|
-
DYNAMIC_REQUIRE_LOADERS[path] = loader;
|
|
152
|
-
}
|
|
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
|
-
|
|
163
|
-
const DYNAMIC_REQUIRE_LOADERS = Object.create(null);
|
|
164
|
-
const DYNAMIC_REQUIRE_CACHE = Object.create(null);
|
|
165
|
-
const DYNAMIC_REQUIRE_SHORTS = Object.create(null);
|
|
166
|
-
const DEFAULT_PARENT_MODULE = {
|
|
167
|
-
id: '<' + 'rollup>', exports: {}, parent: undefined, filename: null, loaded: false, children: [], paths: []
|
|
168
|
-
};
|
|
169
|
-
const CHECKED_EXTENSIONS = ['', '.js', '.json'];
|
|
170
|
-
|
|
171
|
-
function normalize (path) {
|
|
172
|
-
path = path.replace(/\\\\/g, '/');
|
|
173
|
-
const parts = path.split('/');
|
|
174
|
-
const slashed = parts[0] === '';
|
|
175
|
-
for (let i = 1; i < parts.length; i++) {
|
|
176
|
-
if (parts[i] === '.' || parts[i] === '') {
|
|
177
|
-
parts.splice(i--, 1);
|
|
178
|
-
}
|
|
179
|
-
}
|
|
180
|
-
for (let i = 1; i < parts.length; i++) {
|
|
181
|
-
if (parts[i] !== '..') continue;
|
|
182
|
-
if (i > 0 && parts[i - 1] !== '..' && parts[i - 1] !== '.') {
|
|
183
|
-
parts.splice(--i, 2);
|
|
184
|
-
i--;
|
|
185
|
-
}
|
|
186
|
-
}
|
|
187
|
-
path = parts.join('/');
|
|
188
|
-
if (slashed && path[0] !== '/')
|
|
189
|
-
path = '/' + path;
|
|
190
|
-
else if (path.length === 0)
|
|
191
|
-
path = '.';
|
|
192
|
-
return path;
|
|
193
|
-
}
|
|
194
|
-
|
|
195
|
-
function join () {
|
|
196
|
-
if (arguments.length === 0)
|
|
197
|
-
return '.';
|
|
198
|
-
let joined;
|
|
199
|
-
for (let i = 0; i < arguments.length; ++i) {
|
|
200
|
-
let arg = arguments[i];
|
|
201
|
-
if (arg.length > 0) {
|
|
202
|
-
if (joined === undefined)
|
|
203
|
-
joined = arg;
|
|
204
|
-
else
|
|
205
|
-
joined += '/' + arg;
|
|
206
|
-
}
|
|
207
|
-
}
|
|
208
|
-
if (joined === undefined)
|
|
209
|
-
return '.';
|
|
210
|
-
|
|
211
|
-
return joined;
|
|
212
|
-
}
|
|
213
|
-
|
|
214
|
-
function isPossibleNodeModulesPath (modulePath) {
|
|
215
|
-
let c0 = modulePath[0];
|
|
216
|
-
if (c0 === '/' || c0 === '\\\\') return false;
|
|
217
|
-
let c1 = modulePath[1], c2 = modulePath[2];
|
|
218
|
-
if ((c0 === '.' && (!c1 || c1 === '/' || c1 === '\\\\')) ||
|
|
219
|
-
(c0 === '.' && c1 === '.' && (!c2 || c2 === '/' || c2 === '\\\\'))) return false;
|
|
220
|
-
if (c1 === ':' && (c2 === '/' || c2 === '\\\\'))
|
|
221
|
-
return false;
|
|
222
|
-
return true;
|
|
223
|
-
}
|
|
224
|
-
|
|
225
|
-
function dirname (path) {
|
|
226
|
-
if (path.length === 0)
|
|
227
|
-
return '.';
|
|
228
|
-
|
|
229
|
-
let i = path.length - 1;
|
|
230
|
-
while (i > 0) {
|
|
231
|
-
const c = path.charCodeAt(i);
|
|
232
|
-
if ((c === 47 || c === 92) && i !== path.length - 1)
|
|
233
|
-
break;
|
|
234
|
-
i--;
|
|
235
|
-
}
|
|
236
|
-
|
|
237
|
-
if (i > 0)
|
|
238
|
-
return path.substr(0, i);
|
|
239
|
-
|
|
240
|
-
if (path.chartCodeAt(0) === 47 || path.chartCodeAt(0) === 92)
|
|
241
|
-
return path.charAt(0);
|
|
242
|
-
|
|
243
|
-
return '.';
|
|
244
|
-
}
|
|
245
|
-
|
|
246
|
-
export function commonjsResolveImpl (path, originalModuleDir, testCache) {
|
|
247
|
-
const shouldTryNodeModules = isPossibleNodeModulesPath(path);
|
|
248
|
-
path = normalize(path);
|
|
249
|
-
let relPath;
|
|
250
|
-
if (path[0] === '/') {
|
|
251
|
-
originalModuleDir = '/';
|
|
252
|
-
}
|
|
253
|
-
while (true) {
|
|
254
|
-
if (!shouldTryNodeModules) {
|
|
255
|
-
relPath = originalModuleDir ? normalize(originalModuleDir + '/' + path) : path;
|
|
256
|
-
} else if (originalModuleDir) {
|
|
257
|
-
relPath = normalize(originalModuleDir + '/node_modules/' + path);
|
|
258
|
-
} else {
|
|
259
|
-
relPath = normalize(join('node_modules', path));
|
|
260
|
-
}
|
|
261
|
-
|
|
262
|
-
if (relPath.endsWith('/..')) {
|
|
263
|
-
break; // Travelled too far up, avoid infinite loop
|
|
264
|
-
}
|
|
265
|
-
|
|
266
|
-
for (let extensionIndex = 0; extensionIndex < CHECKED_EXTENSIONS.length; extensionIndex++) {
|
|
267
|
-
const resolvedPath = relPath + CHECKED_EXTENSIONS[extensionIndex];
|
|
268
|
-
if (DYNAMIC_REQUIRE_CACHE[resolvedPath]) {
|
|
269
|
-
return resolvedPath;
|
|
270
|
-
}
|
|
271
|
-
if (DYNAMIC_REQUIRE_SHORTS[resolvedPath]) {
|
|
272
|
-
return resolvedPath;
|
|
273
|
-
}
|
|
274
|
-
if (DYNAMIC_REQUIRE_LOADERS[resolvedPath]) {
|
|
275
|
-
return resolvedPath;
|
|
276
|
-
}
|
|
277
|
-
}
|
|
278
|
-
if (!shouldTryNodeModules) break;
|
|
279
|
-
const nextDir = normalize(originalModuleDir + '/..');
|
|
280
|
-
if (nextDir === originalModuleDir) break;
|
|
281
|
-
originalModuleDir = nextDir;
|
|
282
|
-
}
|
|
283
|
-
return null;
|
|
284
|
-
}
|
|
285
|
-
|
|
286
|
-
export function commonjsResolve (path, originalModuleDir) {
|
|
287
|
-
const resolvedPath = commonjsResolveImpl(path, originalModuleDir);
|
|
288
|
-
if (resolvedPath !== null) {
|
|
289
|
-
return resolvedPath;
|
|
290
|
-
}
|
|
291
|
-
return require.resolve(path);
|
|
292
|
-
}
|
|
293
|
-
|
|
294
|
-
export function commonjsRequire (path, originalModuleDir) {
|
|
295
|
-
let resolvedPath = commonjsResolveImpl(path, originalModuleDir, true);
|
|
296
|
-
if (resolvedPath !== null) {
|
|
297
|
-
let cachedModule = DYNAMIC_REQUIRE_CACHE[resolvedPath];
|
|
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
|
-
}
|
|
306
|
-
const loader = DYNAMIC_REQUIRE_LOADERS[resolvedPath];
|
|
307
|
-
if (loader) {
|
|
308
|
-
DYNAMIC_REQUIRE_CACHE[resolvedPath] = cachedModule = {
|
|
309
|
-
id: resolvedPath,
|
|
310
|
-
filename: resolvedPath,
|
|
311
|
-
path: dirname(resolvedPath),
|
|
312
|
-
exports: {},
|
|
313
|
-
parent: DEFAULT_PARENT_MODULE,
|
|
314
|
-
loaded: false,
|
|
315
|
-
children: [],
|
|
316
|
-
paths: [],
|
|
317
|
-
require: function (path, base) {
|
|
318
|
-
return commonjsRequire(path, (base === undefined || base === null) ? cachedModule.path : base);
|
|
319
|
-
}
|
|
320
|
-
};
|
|
321
|
-
try {
|
|
322
|
-
loader.call(commonjsGlobal, cachedModule, cachedModule.exports);
|
|
323
|
-
} catch (error) {
|
|
324
|
-
delete DYNAMIC_REQUIRE_CACHE[resolvedPath];
|
|
325
|
-
throw error;
|
|
326
|
-
}
|
|
327
|
-
cachedModule.loaded = true;
|
|
328
|
-
return cachedModule.exports;
|
|
329
|
-
};
|
|
330
|
-
}
|
|
331
|
-
${ignoreDynamicRequires ? 'return require(path);' : FAILED_REQUIRE_ERROR}
|
|
332
|
-
}
|
|
333
|
-
|
|
334
|
-
commonjsRequire.cache = DYNAMIC_REQUIRE_CACHE;
|
|
335
|
-
commonjsRequire.resolve = commonjsResolve;
|
|
336
|
-
`;
|
|
337
|
-
|
|
338
|
-
function getHelpersModule(isDynamicRequireModulesEnabled, ignoreDynamicRequires) {
|
|
339
|
-
return `${HELPERS}${
|
|
340
|
-
isDynamicRequireModulesEnabled ? getDynamicHelpers(ignoreDynamicRequires) : HELPER_NON_DYNAMIC
|
|
341
|
-
}`;
|
|
342
|
-
}
|
|
343
|
-
|
|
344
79
|
/* eslint-disable import/prefer-default-export */
|
|
345
80
|
|
|
346
81
|
function deconflict(scopes, globals, identifier) {
|
|
@@ -373,13 +108,35 @@ function normalizePathSlashes(path) {
|
|
|
373
108
|
return path.replace(/\\/g, '/');
|
|
374
109
|
}
|
|
375
110
|
|
|
376
|
-
const
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
111
|
+
const getVirtualPathForDynamicRequirePath = (path, commonDir) =>
|
|
112
|
+
`/${normalizePathSlashes(relative(commonDir, path))}`;
|
|
113
|
+
|
|
114
|
+
function capitalize(name) {
|
|
115
|
+
return name[0].toUpperCase() + name.slice(1);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
function getStrictRequiresFilter({ strictRequires }) {
|
|
119
|
+
switch (strictRequires) {
|
|
120
|
+
case true:
|
|
121
|
+
return { strictRequiresFilter: () => true, detectCyclesAndConditional: false };
|
|
122
|
+
// eslint-disable-next-line no-undefined
|
|
123
|
+
case undefined:
|
|
124
|
+
case 'auto':
|
|
125
|
+
case 'debug':
|
|
126
|
+
case null:
|
|
127
|
+
return { strictRequiresFilter: () => false, detectCyclesAndConditional: true };
|
|
128
|
+
case false:
|
|
129
|
+
return { strictRequiresFilter: () => false, detectCyclesAndConditional: false };
|
|
130
|
+
default:
|
|
131
|
+
if (typeof strictRequires === 'string' || Array.isArray(strictRequires)) {
|
|
132
|
+
return {
|
|
133
|
+
strictRequiresFilter: createFilter(strictRequires),
|
|
134
|
+
detectCyclesAndConditional: false
|
|
135
|
+
};
|
|
136
|
+
}
|
|
137
|
+
throw new Error('Unexpected value for "strictRequires" option.');
|
|
138
|
+
}
|
|
139
|
+
}
|
|
383
140
|
|
|
384
141
|
function getPackageEntryPoint(dirPath) {
|
|
385
142
|
let entryPoint = 'index.js';
|
|
@@ -397,41 +154,6 @@ function getPackageEntryPoint(dirPath) {
|
|
|
397
154
|
return entryPoint;
|
|
398
155
|
}
|
|
399
156
|
|
|
400
|
-
function getDynamicPackagesModule(dynamicRequireModuleDirPaths, commonDir) {
|
|
401
|
-
let code = `const commonjsRegisterOrShort = require('${HELPERS_ID}?commonjsRegisterOrShort');`;
|
|
402
|
-
for (const dir of dynamicRequireModuleDirPaths) {
|
|
403
|
-
const entryPoint = getPackageEntryPoint(dir);
|
|
404
|
-
|
|
405
|
-
code += `\ncommonjsRegisterOrShort(${JSON.stringify(
|
|
406
|
-
getVirtualPathForDynamicRequirePath(dir, commonDir)
|
|
407
|
-
)}, ${JSON.stringify(getVirtualPathForDynamicRequirePath(join(dir, entryPoint), commonDir))});`;
|
|
408
|
-
}
|
|
409
|
-
return code;
|
|
410
|
-
}
|
|
411
|
-
|
|
412
|
-
function getDynamicPackagesEntryIntro(
|
|
413
|
-
dynamicRequireModuleDirPaths,
|
|
414
|
-
dynamicRequireModuleSet
|
|
415
|
-
) {
|
|
416
|
-
let dynamicImports = Array.from(
|
|
417
|
-
dynamicRequireModuleSet,
|
|
418
|
-
(dynamicId) => `require(${JSON.stringify(wrapId(dynamicId, DYNAMIC_REGISTER_SUFFIX))});`
|
|
419
|
-
).join('\n');
|
|
420
|
-
|
|
421
|
-
if (dynamicRequireModuleDirPaths.length) {
|
|
422
|
-
dynamicImports += `require(${JSON.stringify(
|
|
423
|
-
wrapId(DYNAMIC_PACKAGES_ID, DYNAMIC_REGISTER_SUFFIX)
|
|
424
|
-
)});`;
|
|
425
|
-
}
|
|
426
|
-
|
|
427
|
-
return dynamicImports;
|
|
428
|
-
}
|
|
429
|
-
|
|
430
|
-
function isDynamicModuleImport(id, dynamicRequireModuleSet) {
|
|
431
|
-
const normalizedPath = normalizePathSlashes(id);
|
|
432
|
-
return dynamicRequireModuleSet.has(normalizedPath) && !normalizedPath.endsWith('.json');
|
|
433
|
-
}
|
|
434
|
-
|
|
435
157
|
function isDirectory(path) {
|
|
436
158
|
try {
|
|
437
159
|
if (statSync(path).isDirectory()) return true;
|
|
@@ -441,102 +163,250 @@ function isDirectory(path) {
|
|
|
441
163
|
return false;
|
|
442
164
|
}
|
|
443
165
|
|
|
444
|
-
function
|
|
445
|
-
const
|
|
166
|
+
function getDynamicRequireModules(patterns, dynamicRequireRoot) {
|
|
167
|
+
const dynamicRequireModules = new Map();
|
|
168
|
+
const dirNames = new Set();
|
|
446
169
|
for (const pattern of !patterns || Array.isArray(patterns) ? patterns || [] : [patterns]) {
|
|
447
170
|
const isNegated = pattern.startsWith('!');
|
|
448
|
-
const
|
|
171
|
+
const modifyMap = (targetPath, resolvedPath) =>
|
|
172
|
+
isNegated
|
|
173
|
+
? dynamicRequireModules.delete(targetPath)
|
|
174
|
+
: dynamicRequireModules.set(targetPath, resolvedPath);
|
|
449
175
|
for (const path of glob.sync(isNegated ? pattern.substr(1) : pattern)) {
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
176
|
+
const resolvedPath = resolve(path);
|
|
177
|
+
const requirePath = normalizePathSlashes(resolvedPath);
|
|
178
|
+
if (isDirectory(resolvedPath)) {
|
|
179
|
+
dirNames.add(resolvedPath);
|
|
180
|
+
const modulePath = resolve(join(resolvedPath, getPackageEntryPoint(path)));
|
|
181
|
+
modifyMap(requirePath, modulePath);
|
|
182
|
+
modifyMap(normalizePathSlashes(modulePath), modulePath);
|
|
183
|
+
} else {
|
|
184
|
+
dirNames.add(dirname(resolvedPath));
|
|
185
|
+
modifyMap(requirePath, resolvedPath);
|
|
453
186
|
}
|
|
454
187
|
}
|
|
455
188
|
}
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
189
|
+
return {
|
|
190
|
+
commonDir: dirNames.size ? getCommonDir([...dirNames, dynamicRequireRoot]) : null,
|
|
191
|
+
dynamicRequireModules
|
|
192
|
+
};
|
|
460
193
|
}
|
|
461
194
|
|
|
462
|
-
|
|
463
|
-
let commonJSMetaPromise = commonJSMetaPromises.get(id);
|
|
464
|
-
if (commonJSMetaPromise) return commonJSMetaPromise.promise;
|
|
195
|
+
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.');`;
|
|
465
196
|
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
197
|
+
function getDynamicModuleRegistry(
|
|
198
|
+
isDynamicRequireModulesEnabled,
|
|
199
|
+
dynamicRequireModules,
|
|
200
|
+
commonDir,
|
|
201
|
+
ignoreDynamicRequires
|
|
202
|
+
) {
|
|
203
|
+
if (!isDynamicRequireModulesEnabled) {
|
|
204
|
+
return `export function commonjsRequire(path) {
|
|
205
|
+
${FAILED_REQUIRE_ERROR}
|
|
206
|
+
}`;
|
|
207
|
+
}
|
|
208
|
+
const dynamicModuleImports = [...dynamicRequireModules.values()]
|
|
209
|
+
.map(
|
|
210
|
+
(id, index) =>
|
|
211
|
+
`import ${
|
|
212
|
+
id.endsWith('.json') ? `json${index}` : `{ __require as require${index} }`
|
|
213
|
+
} from ${JSON.stringify(id)};`
|
|
214
|
+
)
|
|
215
|
+
.join('\n');
|
|
216
|
+
const dynamicModuleProps = [...dynamicRequireModules.keys()]
|
|
217
|
+
.map(
|
|
218
|
+
(id, index) =>
|
|
219
|
+
`\t\t${JSON.stringify(getVirtualPathForDynamicRequirePath(id, commonDir))}: ${
|
|
220
|
+
id.endsWith('.json') ? `function () { return json${index}; }` : `require${index}`
|
|
221
|
+
}`
|
|
222
|
+
)
|
|
223
|
+
.join(',\n');
|
|
224
|
+
return `${dynamicModuleImports}
|
|
225
|
+
|
|
226
|
+
var dynamicModules;
|
|
227
|
+
|
|
228
|
+
function getDynamicModules() {
|
|
229
|
+
return dynamicModules || (dynamicModules = {
|
|
230
|
+
${dynamicModuleProps}
|
|
231
|
+
});
|
|
232
|
+
}
|
|
474
233
|
|
|
475
|
-
|
|
234
|
+
export function commonjsRequire(path, originalModuleDir) {
|
|
235
|
+
var resolvedPath = commonjsResolveImpl(path, originalModuleDir);
|
|
236
|
+
if (resolvedPath !== null) {
|
|
237
|
+
return getDynamicModules()[resolvedPath]();
|
|
238
|
+
}
|
|
239
|
+
${ignoreDynamicRequires ? 'return require(path);' : FAILED_REQUIRE_ERROR}
|
|
476
240
|
}
|
|
477
241
|
|
|
478
|
-
function
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
242
|
+
function commonjsResolve (path, originalModuleDir) {
|
|
243
|
+
const resolvedPath = commonjsResolveImpl(path, originalModuleDir);
|
|
244
|
+
if (resolvedPath !== null) {
|
|
245
|
+
return resolvedPath;
|
|
246
|
+
}
|
|
247
|
+
return require.resolve(path);
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
commonjsRequire.resolve = commonjsResolve;
|
|
251
|
+
|
|
252
|
+
function commonjsResolveImpl (path, originalModuleDir) {
|
|
253
|
+
var shouldTryNodeModules = isPossibleNodeModulesPath(path);
|
|
254
|
+
path = normalize(path);
|
|
255
|
+
var relPath;
|
|
256
|
+
if (path[0] === '/') {
|
|
257
|
+
originalModuleDir = '';
|
|
258
|
+
}
|
|
259
|
+
var modules = getDynamicModules();
|
|
260
|
+
var checkedExtensions = ['', '.js', '.json'];
|
|
261
|
+
while (true) {
|
|
262
|
+
if (!shouldTryNodeModules) {
|
|
263
|
+
relPath = normalize(originalModuleDir + '/' + path);
|
|
264
|
+
} else {
|
|
265
|
+
relPath = normalize(originalModuleDir + '/node_modules/' + path);
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
if (relPath.endsWith('/..')) {
|
|
269
|
+
break; // Travelled too far up, avoid infinite loop
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
for (var extensionIndex = 0; extensionIndex < checkedExtensions.length; extensionIndex++) {
|
|
273
|
+
var resolvedPath = relPath + checkedExtensions[extensionIndex];
|
|
274
|
+
if (modules[resolvedPath]) {
|
|
275
|
+
return resolvedPath;
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
if (!shouldTryNodeModules) break;
|
|
279
|
+
var nextDir = normalize(originalModuleDir + '/..');
|
|
280
|
+
if (nextDir === originalModuleDir) break;
|
|
281
|
+
originalModuleDir = nextDir;
|
|
282
|
+
}
|
|
283
|
+
return null;
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
function isPossibleNodeModulesPath (modulePath) {
|
|
287
|
+
var c0 = modulePath[0];
|
|
288
|
+
if (c0 === '/' || c0 === '\\\\') return false;
|
|
289
|
+
var c1 = modulePath[1], c2 = modulePath[2];
|
|
290
|
+
if ((c0 === '.' && (!c1 || c1 === '/' || c1 === '\\\\')) ||
|
|
291
|
+
(c0 === '.' && c1 === '.' && (!c2 || c2 === '/' || c2 === '\\\\'))) return false;
|
|
292
|
+
if (c1 === ':' && (c2 === '/' || c2 === '\\\\')) return false;
|
|
293
|
+
return true;
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
function normalize (path) {
|
|
297
|
+
path = path.replace(/\\\\/g, '/');
|
|
298
|
+
var parts = path.split('/');
|
|
299
|
+
var slashed = parts[0] === '';
|
|
300
|
+
for (var i = 1; i < parts.length; i++) {
|
|
301
|
+
if (parts[i] === '.' || parts[i] === '') {
|
|
302
|
+
parts.splice(i--, 1);
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
for (var i = 1; i < parts.length; i++) {
|
|
306
|
+
if (parts[i] !== '..') continue;
|
|
307
|
+
if (i > 0 && parts[i - 1] !== '..' && parts[i - 1] !== '.') {
|
|
308
|
+
parts.splice(--i, 2);
|
|
309
|
+
i--;
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
path = parts.join('/');
|
|
313
|
+
if (slashed && path[0] !== '/') path = '/' + path;
|
|
314
|
+
else if (path.length === 0) path = '.';
|
|
315
|
+
return path;
|
|
316
|
+
}`;
|
|
488
317
|
}
|
|
489
318
|
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
319
|
+
const isWrappedId = (id, suffix) => id.endsWith(suffix);
|
|
320
|
+
const wrapId = (id, suffix) => `\0${id}${suffix}`;
|
|
321
|
+
const unwrapId = (wrappedId, suffix) => wrappedId.slice(1, -suffix.length);
|
|
322
|
+
|
|
323
|
+
const PROXY_SUFFIX = '?commonjs-proxy';
|
|
324
|
+
const WRAPPED_SUFFIX = '?commonjs-wrapped';
|
|
325
|
+
const EXTERNAL_SUFFIX = '?commonjs-external';
|
|
326
|
+
const EXPORTS_SUFFIX = '?commonjs-exports';
|
|
327
|
+
const MODULE_SUFFIX = '?commonjs-module';
|
|
328
|
+
const ES_IMPORT_SUFFIX = '?es-import';
|
|
329
|
+
|
|
330
|
+
const DYNAMIC_MODULES_ID = '\0commonjs-dynamic-modules';
|
|
331
|
+
const HELPERS_ID = '\0commonjsHelpers.js';
|
|
332
|
+
|
|
333
|
+
const IS_WRAPPED_COMMONJS = 'withRequireFunction';
|
|
334
|
+
|
|
335
|
+
// `x['default']` is used instead of `x.default` for backward compatibility with ES3 browsers.
|
|
336
|
+
// Minifiers like uglify will usually transpile it back if compatibility with ES3 is not enabled.
|
|
337
|
+
// This could be improved by inspecting Rollup's "generatedCode" option
|
|
338
|
+
|
|
339
|
+
const HELPERS = `
|
|
340
|
+
export var commonjsGlobal = typeof globalThis !== 'undefined' ? globalThis : typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {};
|
|
341
|
+
|
|
342
|
+
export function getDefaultExportFromCjs (x) {
|
|
343
|
+
return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
export function getDefaultExportFromNamespaceIfPresent (n) {
|
|
347
|
+
return n && Object.prototype.hasOwnProperty.call(n, 'default') ? n['default'] : n;
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
export function getDefaultExportFromNamespaceIfNotNamed (n) {
|
|
351
|
+
return n && Object.prototype.hasOwnProperty.call(n, 'default') && Object.keys(n).length === 1 ? n['default'] : n;
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
export function getAugmentedNamespace(n) {
|
|
355
|
+
var f = n.default;
|
|
356
|
+
if (typeof f == "function") {
|
|
357
|
+
var a = function () {
|
|
358
|
+
return f.apply(this, arguments);
|
|
359
|
+
};
|
|
360
|
+
a.prototype = f.prototype;
|
|
361
|
+
} else a = {};
|
|
362
|
+
Object.defineProperty(a, '__esModule', {value: true});
|
|
363
|
+
Object.keys(n).forEach(function (k) {
|
|
364
|
+
var d = Object.getOwnPropertyDescriptor(n, k);
|
|
365
|
+
Object.defineProperty(a, k, d.get ? d : {
|
|
366
|
+
enumerable: true,
|
|
367
|
+
get: function () {
|
|
368
|
+
return n[k];
|
|
369
|
+
}
|
|
370
|
+
});
|
|
371
|
+
});
|
|
372
|
+
return a;
|
|
373
|
+
}
|
|
374
|
+
`;
|
|
375
|
+
|
|
376
|
+
function getHelpersModule() {
|
|
377
|
+
return HELPERS;
|
|
493
378
|
}
|
|
494
379
|
|
|
495
380
|
function getUnknownRequireProxy(id, requireReturnsDefault) {
|
|
496
381
|
if (requireReturnsDefault === true || id.endsWith('.json')) {
|
|
497
|
-
return `export {default} from ${JSON.stringify(id)};`;
|
|
382
|
+
return `export { default } from ${JSON.stringify(id)};`;
|
|
498
383
|
}
|
|
499
384
|
const name = getName(id);
|
|
500
385
|
const exported =
|
|
501
386
|
requireReturnsDefault === 'auto'
|
|
502
|
-
? `import {getDefaultExportFromNamespaceIfNotNamed} from "${HELPERS_ID}"; export default /*@__PURE__*/getDefaultExportFromNamespaceIfNotNamed(${name});`
|
|
387
|
+
? `import { getDefaultExportFromNamespaceIfNotNamed } from "${HELPERS_ID}"; export default /*@__PURE__*/getDefaultExportFromNamespaceIfNotNamed(${name});`
|
|
503
388
|
: requireReturnsDefault === 'preferred'
|
|
504
|
-
? `import {getDefaultExportFromNamespaceIfPresent} from "${HELPERS_ID}"; export default /*@__PURE__*/getDefaultExportFromNamespaceIfPresent(${name});`
|
|
389
|
+
? `import { getDefaultExportFromNamespaceIfPresent } from "${HELPERS_ID}"; export default /*@__PURE__*/getDefaultExportFromNamespaceIfPresent(${name});`
|
|
505
390
|
: !requireReturnsDefault
|
|
506
|
-
? `import {getAugmentedNamespace} from "${HELPERS_ID}"; export default /*@__PURE__*/getAugmentedNamespace(${name});`
|
|
391
|
+
? `import { getAugmentedNamespace } from "${HELPERS_ID}"; export default /*@__PURE__*/getAugmentedNamespace(${name});`
|
|
507
392
|
: `export default ${name};`;
|
|
508
393
|
return `import * as ${name} from ${JSON.stringify(id)}; ${exported}`;
|
|
509
394
|
}
|
|
510
395
|
|
|
511
|
-
function getDynamicJsonProxy(id, commonDir) {
|
|
512
|
-
const normalizedPath = normalizePathSlashes(id.slice(DYNAMIC_JSON_PREFIX.length));
|
|
513
|
-
return `const commonjsRegister = require('${HELPERS_ID}?commonjsRegister');\ncommonjsRegister(${JSON.stringify(
|
|
514
|
-
getVirtualPathForDynamicRequirePath(normalizedPath, commonDir)
|
|
515
|
-
)}, function (module, exports) {
|
|
516
|
-
module.exports = require(${JSON.stringify(normalizedPath)});
|
|
517
|
-
});`;
|
|
518
|
-
}
|
|
519
|
-
|
|
520
|
-
function getDynamicRequireProxy(normalizedPath, commonDir) {
|
|
521
|
-
return `const commonjsRegister = require('${HELPERS_ID}?commonjsRegister');\ncommonjsRegister(${JSON.stringify(
|
|
522
|
-
getVirtualPathForDynamicRequirePath(normalizedPath, commonDir)
|
|
523
|
-
)}, function (module, exports) {
|
|
524
|
-
${readFileSync(normalizedPath, { encoding: 'utf8' })}
|
|
525
|
-
});`;
|
|
526
|
-
}
|
|
527
|
-
|
|
528
396
|
async function getStaticRequireProxy(
|
|
529
397
|
id,
|
|
530
398
|
requireReturnsDefault,
|
|
531
399
|
esModulesWithDefaultExport,
|
|
532
400
|
esModulesWithNamedExports,
|
|
533
|
-
|
|
401
|
+
loadModule
|
|
534
402
|
) {
|
|
535
403
|
const name = getName(id);
|
|
536
|
-
const
|
|
404
|
+
const {
|
|
405
|
+
meta: { commonjs: commonjsMeta }
|
|
406
|
+
} = await loadModule({ id });
|
|
537
407
|
if (commonjsMeta && commonjsMeta.isCommonJS) {
|
|
538
408
|
return `export { __moduleExports as default } from ${JSON.stringify(id)};`;
|
|
539
|
-
} else if (commonjsMeta
|
|
409
|
+
} else if (!commonjsMeta) {
|
|
540
410
|
return getUnknownRequireProxy(id, requireReturnsDefault);
|
|
541
411
|
} else if (!requireReturnsDefault) {
|
|
542
412
|
return `import { getAugmentedNamespace } from "${HELPERS_ID}"; import * as ${name} from ${JSON.stringify(
|
|
@@ -553,6 +423,27 @@ async function getStaticRequireProxy(
|
|
|
553
423
|
return `export { default } from ${JSON.stringify(id)};`;
|
|
554
424
|
}
|
|
555
425
|
|
|
426
|
+
function getEsImportProxy(id, defaultIsModuleExports) {
|
|
427
|
+
const name = getName(id);
|
|
428
|
+
const exportsName = `${name}Exports`;
|
|
429
|
+
const requireModule = `require${capitalize(name)}`;
|
|
430
|
+
let code =
|
|
431
|
+
`import { getDefaultExportFromCjs } from "${HELPERS_ID}";\n` +
|
|
432
|
+
`import { __require as ${requireModule} } from ${JSON.stringify(id)};\n` +
|
|
433
|
+
`var ${exportsName} = ${requireModule}();\n` +
|
|
434
|
+
`export { ${exportsName} as __moduleExports };`;
|
|
435
|
+
if (defaultIsModuleExports) {
|
|
436
|
+
code += `\nexport { ${exportsName} as default };`;
|
|
437
|
+
} else {
|
|
438
|
+
code += `export default /*@__PURE__*/getDefaultExportFromCjs(${exportsName});`;
|
|
439
|
+
}
|
|
440
|
+
return {
|
|
441
|
+
code,
|
|
442
|
+
syntheticNamedExports: '__moduleExports',
|
|
443
|
+
meta: { commonjs: { isCommonJS: false } }
|
|
444
|
+
};
|
|
445
|
+
}
|
|
446
|
+
|
|
556
447
|
/* eslint-disable no-param-reassign, no-undefined */
|
|
557
448
|
|
|
558
449
|
function getCandidatesForExtension(resolved, extension) {
|
|
@@ -566,92 +457,189 @@ function getCandidates(resolved, extensions) {
|
|
|
566
457
|
);
|
|
567
458
|
}
|
|
568
459
|
|
|
569
|
-
function
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
if (importee[0] !== '.' || !importer) return undefined;
|
|
573
|
-
|
|
574
|
-
const resolved = resolve(dirname(importer), importee);
|
|
575
|
-
const candidates = getCandidates(resolved, extensions);
|
|
460
|
+
function resolveExtensions(importee, importer, extensions) {
|
|
461
|
+
// not our problem
|
|
462
|
+
if (importee[0] !== '.' || !importer) return undefined;
|
|
576
463
|
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
const stats = statSync(candidates[i]);
|
|
580
|
-
if (stats.isFile()) return { id: candidates[i] };
|
|
581
|
-
} catch (err) {
|
|
582
|
-
/* noop */
|
|
583
|
-
}
|
|
584
|
-
}
|
|
464
|
+
const resolved = resolve(dirname(importer), importee);
|
|
465
|
+
const candidates = getCandidates(resolved, extensions);
|
|
585
466
|
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
}
|
|
593
|
-
|
|
594
|
-
const importer =
|
|
595
|
-
rawImporter && isWrappedId(rawImporter, DYNAMIC_REGISTER_SUFFIX)
|
|
596
|
-
? unwrapId(rawImporter, DYNAMIC_REGISTER_SUFFIX)
|
|
597
|
-
: rawImporter;
|
|
598
|
-
|
|
599
|
-
// Except for exports, proxies are only importing resolved ids,
|
|
600
|
-
// no need to resolve again
|
|
601
|
-
if (importer && isWrappedId(importer, PROXY_SUFFIX)) {
|
|
602
|
-
return importee;
|
|
467
|
+
for (let i = 0; i < candidates.length; i += 1) {
|
|
468
|
+
try {
|
|
469
|
+
const stats = statSync(candidates[i]);
|
|
470
|
+
if (stats.isFile()) return { id: candidates[i] };
|
|
471
|
+
} catch (err) {
|
|
472
|
+
/* noop */
|
|
603
473
|
}
|
|
474
|
+
}
|
|
604
475
|
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
let isModuleRegistration = false;
|
|
608
|
-
|
|
609
|
-
if (isProxyModule) {
|
|
610
|
-
importee = unwrapId(importee, PROXY_SUFFIX);
|
|
611
|
-
} else if (isRequiredModule) {
|
|
612
|
-
importee = unwrapId(importee, REQUIRE_SUFFIX);
|
|
476
|
+
return undefined;
|
|
477
|
+
}
|
|
613
478
|
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
479
|
+
function getResolveId(extensions) {
|
|
480
|
+
return async function resolveId(importee, importer, resolveOptions) {
|
|
481
|
+
if (isWrappedId(importee, WRAPPED_SUFFIX)) {
|
|
482
|
+
return unwrapId(importee, WRAPPED_SUFFIX);
|
|
618
483
|
}
|
|
619
484
|
|
|
620
485
|
if (
|
|
486
|
+
isWrappedId(importee, MODULE_SUFFIX) ||
|
|
487
|
+
isWrappedId(importee, EXPORTS_SUFFIX) ||
|
|
488
|
+
isWrappedId(importee, PROXY_SUFFIX) ||
|
|
489
|
+
isWrappedId(importee, ES_IMPORT_SUFFIX) ||
|
|
490
|
+
isWrappedId(importee, EXTERNAL_SUFFIX) ||
|
|
621
491
|
importee.startsWith(HELPERS_ID) ||
|
|
622
|
-
importee ===
|
|
623
|
-
importee.startsWith(DYNAMIC_JSON_PREFIX)
|
|
492
|
+
importee === DYNAMIC_MODULES_ID
|
|
624
493
|
) {
|
|
625
494
|
return importee;
|
|
626
495
|
}
|
|
627
496
|
|
|
497
|
+
if (importer) {
|
|
498
|
+
if (
|
|
499
|
+
importer === DYNAMIC_MODULES_ID ||
|
|
500
|
+
// Except for exports, proxies are only importing resolved ids, no need to resolve again
|
|
501
|
+
isWrappedId(importer, PROXY_SUFFIX) ||
|
|
502
|
+
isWrappedId(importer, ES_IMPORT_SUFFIX)
|
|
503
|
+
) {
|
|
504
|
+
return importee;
|
|
505
|
+
}
|
|
506
|
+
if (isWrappedId(importer, EXTERNAL_SUFFIX)) {
|
|
507
|
+
// We need to return null for unresolved imports so that the proper warning is shown
|
|
508
|
+
if (!(await this.resolve(importee, importer, { skipSelf: true }))) {
|
|
509
|
+
return null;
|
|
510
|
+
}
|
|
511
|
+
// For other external imports, we need to make sure they are handled as external
|
|
512
|
+
return { id: importee, external: true };
|
|
513
|
+
}
|
|
514
|
+
}
|
|
515
|
+
|
|
628
516
|
if (importee.startsWith('\0')) {
|
|
629
517
|
return null;
|
|
630
518
|
}
|
|
631
519
|
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
if (resolved && isProxyModule) {
|
|
646
|
-
resolved.id = wrapId(resolved.id, resolved.external ? EXTERNAL_SUFFIX : PROXY_SUFFIX);
|
|
647
|
-
resolved.external = false;
|
|
648
|
-
} else if (resolved && isModuleRegistration) {
|
|
649
|
-
resolved.id = wrapId(resolved.id, DYNAMIC_REGISTER_SUFFIX);
|
|
650
|
-
} else if (!resolved && (isProxyModule || isRequiredModule)) {
|
|
651
|
-
return { id: wrapId(importee, EXTERNAL_SUFFIX), external: false };
|
|
652
|
-
}
|
|
520
|
+
// If this is an entry point or ESM import, we need to figure out if the importee is wrapped and
|
|
521
|
+
// if that is the case, we need to add a proxy.
|
|
522
|
+
const customOptions = resolveOptions.custom;
|
|
523
|
+
|
|
524
|
+
// If this is a require, we do not need a proxy
|
|
525
|
+
if (customOptions && customOptions['node-resolve'] && customOptions['node-resolve'].isRequire) {
|
|
526
|
+
return null;
|
|
527
|
+
}
|
|
528
|
+
|
|
529
|
+
const resolved =
|
|
530
|
+
(await this.resolve(importee, importer, Object.assign({ skipSelf: true }, resolveOptions))) ||
|
|
531
|
+
resolveExtensions(importee, importer, extensions);
|
|
532
|
+
if (!resolved || resolved.external) {
|
|
653
533
|
return resolved;
|
|
654
|
-
}
|
|
534
|
+
}
|
|
535
|
+
const {
|
|
536
|
+
meta: { commonjs: commonjsMeta }
|
|
537
|
+
} = await this.load(resolved);
|
|
538
|
+
if (commonjsMeta && commonjsMeta.isCommonJS === IS_WRAPPED_COMMONJS) {
|
|
539
|
+
return wrapId(resolved.id, ES_IMPORT_SUFFIX);
|
|
540
|
+
}
|
|
541
|
+
return resolved;
|
|
542
|
+
};
|
|
543
|
+
}
|
|
544
|
+
|
|
545
|
+
function getResolveRequireSourcesAndGetMeta(extensions, detectCyclesAndConditional) {
|
|
546
|
+
const knownCjsModuleTypes = Object.create(null);
|
|
547
|
+
const requiredIds = Object.create(null);
|
|
548
|
+
const unconditionallyRequiredIds = Object.create(null);
|
|
549
|
+
const dependentModules = Object.create(null);
|
|
550
|
+
const getDependentModules = (id) =>
|
|
551
|
+
dependentModules[id] || (dependentModules[id] = Object.create(null));
|
|
552
|
+
|
|
553
|
+
return {
|
|
554
|
+
getWrappedIds: () =>
|
|
555
|
+
Object.keys(knownCjsModuleTypes).filter(
|
|
556
|
+
(id) => knownCjsModuleTypes[id] === IS_WRAPPED_COMMONJS
|
|
557
|
+
),
|
|
558
|
+
isRequiredId: (id) => requiredIds[id],
|
|
559
|
+
resolveRequireSourcesAndGetMeta: (rollupContext) => async (
|
|
560
|
+
parentId,
|
|
561
|
+
isParentCommonJS,
|
|
562
|
+
sources
|
|
563
|
+
) => {
|
|
564
|
+
knownCjsModuleTypes[parentId] = knownCjsModuleTypes[parentId] || isParentCommonJS;
|
|
565
|
+
if (
|
|
566
|
+
knownCjsModuleTypes[parentId] &&
|
|
567
|
+
requiredIds[parentId] &&
|
|
568
|
+
!unconditionallyRequiredIds[parentId]
|
|
569
|
+
) {
|
|
570
|
+
knownCjsModuleTypes[parentId] = IS_WRAPPED_COMMONJS;
|
|
571
|
+
}
|
|
572
|
+
const requireTargets = await Promise.all(
|
|
573
|
+
sources.map(async ({ source, isConditional }) => {
|
|
574
|
+
// Never analyze or proxy internal modules
|
|
575
|
+
if (source.startsWith('\0')) {
|
|
576
|
+
return { id: source, allowProxy: false };
|
|
577
|
+
}
|
|
578
|
+
const resolved =
|
|
579
|
+
(await rollupContext.resolve(source, parentId, {
|
|
580
|
+
custom: {
|
|
581
|
+
'node-resolve': { isRequire: true }
|
|
582
|
+
}
|
|
583
|
+
})) || resolveExtensions(source, parentId, extensions);
|
|
584
|
+
if (!resolved) {
|
|
585
|
+
return { id: wrapId(source, EXTERNAL_SUFFIX), allowProxy: false };
|
|
586
|
+
}
|
|
587
|
+
const childId = resolved.id;
|
|
588
|
+
if (resolved.external) {
|
|
589
|
+
return { id: wrapId(childId, EXTERNAL_SUFFIX), allowProxy: false };
|
|
590
|
+
}
|
|
591
|
+
requiredIds[childId] = true;
|
|
592
|
+
if (
|
|
593
|
+
!(
|
|
594
|
+
detectCyclesAndConditional &&
|
|
595
|
+
(isConditional || knownCjsModuleTypes[parentId] === IS_WRAPPED_COMMONJS)
|
|
596
|
+
)
|
|
597
|
+
) {
|
|
598
|
+
unconditionallyRequiredIds[childId] = true;
|
|
599
|
+
}
|
|
600
|
+
const parentDependentModules = getDependentModules(parentId);
|
|
601
|
+
const childDependentModules = getDependentModules(childId);
|
|
602
|
+
childDependentModules[parentId] = true;
|
|
603
|
+
for (const dependentId of Object.keys(parentDependentModules)) {
|
|
604
|
+
childDependentModules[dependentId] = true;
|
|
605
|
+
}
|
|
606
|
+
if (parentDependentModules[childId]) {
|
|
607
|
+
// If we depend on one of our dependencies, we have a cycle. Then all modules that
|
|
608
|
+
// we depend on that also depend on the same module are part of a cycle as well.
|
|
609
|
+
if (detectCyclesAndConditional && isParentCommonJS) {
|
|
610
|
+
knownCjsModuleTypes[parentId] = IS_WRAPPED_COMMONJS;
|
|
611
|
+
knownCjsModuleTypes[childId] = IS_WRAPPED_COMMONJS;
|
|
612
|
+
for (const dependentId of Object.keys(parentDependentModules)) {
|
|
613
|
+
if (getDependentModules(dependentId)[childId]) {
|
|
614
|
+
knownCjsModuleTypes[dependentId] = IS_WRAPPED_COMMONJS;
|
|
615
|
+
}
|
|
616
|
+
}
|
|
617
|
+
}
|
|
618
|
+
} else {
|
|
619
|
+
// This makes sure the current transform handler waits for all direct dependencies to be
|
|
620
|
+
// loaded and transformed and therefore for all transitive CommonJS dependencies to be
|
|
621
|
+
// loaded as well so that all cycles have been found and knownCjsModuleTypes is reliable.
|
|
622
|
+
await rollupContext.load(resolved);
|
|
623
|
+
}
|
|
624
|
+
return { id: childId, allowProxy: true };
|
|
625
|
+
})
|
|
626
|
+
);
|
|
627
|
+
return {
|
|
628
|
+
requireTargets: requireTargets.map(({ id: dependencyId, allowProxy }, index) => {
|
|
629
|
+
const isCommonJS = knownCjsModuleTypes[dependencyId];
|
|
630
|
+
return {
|
|
631
|
+
source: sources[index].source,
|
|
632
|
+
id: allowProxy
|
|
633
|
+
? isCommonJS === IS_WRAPPED_COMMONJS
|
|
634
|
+
? wrapId(dependencyId, WRAPPED_SUFFIX)
|
|
635
|
+
: wrapId(dependencyId, PROXY_SUFFIX)
|
|
636
|
+
: dependencyId,
|
|
637
|
+
isCommonJS
|
|
638
|
+
};
|
|
639
|
+
}),
|
|
640
|
+
usesRequireWrapper: knownCjsModuleTypes[parentId] === IS_WRAPPED_COMMONJS
|
|
641
|
+
};
|
|
642
|
+
}
|
|
655
643
|
};
|
|
656
644
|
}
|
|
657
645
|
|
|
@@ -811,8 +799,9 @@ function wrapCode(magicString, uses, moduleName, exportsName) {
|
|
|
811
799
|
}
|
|
812
800
|
magicString
|
|
813
801
|
.trim()
|
|
802
|
+
.indent('\t')
|
|
814
803
|
.prepend(`(function (${args.join(', ')}) {\n`)
|
|
815
|
-
.append(`\n}(${passedArgs.join(', ')}));`);
|
|
804
|
+
.append(`\n} (${passedArgs.join(', ')}));`);
|
|
816
805
|
}
|
|
817
806
|
|
|
818
807
|
function rewriteExportsAndGetExportsBlock(
|
|
@@ -830,12 +819,27 @@ function rewriteExportsAndGetExportsBlock(
|
|
|
830
819
|
HELPERS_NAME,
|
|
831
820
|
exportMode,
|
|
832
821
|
detectWrappedDefault,
|
|
833
|
-
defaultIsModuleExports
|
|
822
|
+
defaultIsModuleExports,
|
|
823
|
+
usesRequireWrapper,
|
|
824
|
+
requireName
|
|
834
825
|
) {
|
|
835
826
|
const exports = [];
|
|
836
827
|
const exportDeclarations = [];
|
|
837
828
|
|
|
838
|
-
if (
|
|
829
|
+
if (usesRequireWrapper) {
|
|
830
|
+
getExportsWhenUsingRequireWrapper(
|
|
831
|
+
magicString,
|
|
832
|
+
wrapped,
|
|
833
|
+
exportMode,
|
|
834
|
+
exports,
|
|
835
|
+
moduleExportsAssignments,
|
|
836
|
+
exportsAssignmentsByName,
|
|
837
|
+
moduleName,
|
|
838
|
+
exportsName,
|
|
839
|
+
requireName,
|
|
840
|
+
defineCompiledEsmExpressions
|
|
841
|
+
);
|
|
842
|
+
} else if (exportMode === 'replace') {
|
|
839
843
|
getExportsForReplacedModuleExports(
|
|
840
844
|
magicString,
|
|
841
845
|
exports,
|
|
@@ -878,6 +882,49 @@ function rewriteExportsAndGetExportsBlock(
|
|
|
878
882
|
return `\n\n${exportDeclarations.join('\n')}`;
|
|
879
883
|
}
|
|
880
884
|
|
|
885
|
+
function getExportsWhenUsingRequireWrapper(
|
|
886
|
+
magicString,
|
|
887
|
+
wrapped,
|
|
888
|
+
exportMode,
|
|
889
|
+
exports,
|
|
890
|
+
moduleExportsAssignments,
|
|
891
|
+
exportsAssignmentsByName,
|
|
892
|
+
moduleName,
|
|
893
|
+
exportsName,
|
|
894
|
+
requireName,
|
|
895
|
+
defineCompiledEsmExpressions
|
|
896
|
+
) {
|
|
897
|
+
if (!wrapped) {
|
|
898
|
+
if (exportMode === 'replace') {
|
|
899
|
+
for (const { left } of moduleExportsAssignments) {
|
|
900
|
+
magicString.overwrite(left.start, left.end, exportsName);
|
|
901
|
+
}
|
|
902
|
+
} else {
|
|
903
|
+
// Collect and rewrite module.exports assignments
|
|
904
|
+
for (const { left } of moduleExportsAssignments) {
|
|
905
|
+
magicString.overwrite(left.start, left.end, `${moduleName}.exports`);
|
|
906
|
+
}
|
|
907
|
+
// Collect and rewrite named exports
|
|
908
|
+
for (const [exportName, { nodes }] of exportsAssignmentsByName) {
|
|
909
|
+
for (const node of nodes) {
|
|
910
|
+
magicString.overwrite(node.start, node.left.end, `${exportsName}.${exportName}`);
|
|
911
|
+
}
|
|
912
|
+
}
|
|
913
|
+
// Collect and rewrite exports.__esModule assignments
|
|
914
|
+
for (const expression of defineCompiledEsmExpressions) {
|
|
915
|
+
const moduleExportsExpression =
|
|
916
|
+
expression.type === 'CallExpression' ? expression.arguments[0] : expression.left.object;
|
|
917
|
+
magicString.overwrite(
|
|
918
|
+
moduleExportsExpression.start,
|
|
919
|
+
moduleExportsExpression.end,
|
|
920
|
+
exportsName
|
|
921
|
+
);
|
|
922
|
+
}
|
|
923
|
+
}
|
|
924
|
+
}
|
|
925
|
+
exports.push(`${requireName} as __require`);
|
|
926
|
+
}
|
|
927
|
+
|
|
881
928
|
function getExportsForReplacedModuleExports(
|
|
882
929
|
magicString,
|
|
883
930
|
exports,
|
|
@@ -965,7 +1012,7 @@ function getExports(
|
|
|
965
1012
|
}
|
|
966
1013
|
|
|
967
1014
|
if (!isRestorableCompiledEsm || defaultIsModuleExports === true) {
|
|
968
|
-
|
|
1015
|
+
exports.push(`${exportsName} as default`);
|
|
969
1016
|
} else if (moduleExportsAssignments.length === 0 || defaultIsModuleExports === false) {
|
|
970
1017
|
exports.push(`${deconflictedDefaultExportName || exportsName} as default`);
|
|
971
1018
|
} else {
|
|
@@ -975,7 +1022,7 @@ function getExports(
|
|
|
975
1022
|
}
|
|
976
1023
|
}
|
|
977
1024
|
|
|
978
|
-
function
|
|
1025
|
+
function isRequireExpression(node, scope) {
|
|
979
1026
|
if (!node) return false;
|
|
980
1027
|
if (node.type !== 'CallExpression') return false;
|
|
981
1028
|
|
|
@@ -1002,11 +1049,6 @@ function isModuleRequire({ object, property }, scope) {
|
|
|
1002
1049
|
);
|
|
1003
1050
|
}
|
|
1004
1051
|
|
|
1005
|
-
function isStaticRequireStatement(node, scope) {
|
|
1006
|
-
if (!isRequireStatement(node, scope)) return false;
|
|
1007
|
-
return !hasDynamicArguments(node);
|
|
1008
|
-
}
|
|
1009
|
-
|
|
1010
1052
|
function hasDynamicArguments(node) {
|
|
1011
1053
|
return (
|
|
1012
1054
|
node.arguments.length > 1 ||
|
|
@@ -1021,89 +1063,58 @@ function isNodeRequirePropertyAccess(parent) {
|
|
|
1021
1063
|
return parent && parent.property && reservedMethod[parent.property.name];
|
|
1022
1064
|
}
|
|
1023
1065
|
|
|
1024
|
-
function isIgnoredRequireStatement(requiredNode, ignoreRequire) {
|
|
1025
|
-
return ignoreRequire(requiredNode.arguments[0].value);
|
|
1026
|
-
}
|
|
1027
|
-
|
|
1028
1066
|
function getRequireStringArg(node) {
|
|
1029
1067
|
return node.arguments[0].type === 'Literal'
|
|
1030
1068
|
? node.arguments[0].value
|
|
1031
1069
|
: node.arguments[0].quasis[0].value.cooked;
|
|
1032
1070
|
}
|
|
1033
1071
|
|
|
1034
|
-
function hasDynamicModuleForPath(source, id, dynamicRequireModuleSet) {
|
|
1035
|
-
if (!/^(?:\.{0,2}[/\\]|[A-Za-z]:[/\\])/.test(source)) {
|
|
1036
|
-
try {
|
|
1037
|
-
const resolvedPath = normalizePathSlashes(sync(source, { basedir: dirname(id) }));
|
|
1038
|
-
if (dynamicRequireModuleSet.has(resolvedPath)) {
|
|
1039
|
-
return true;
|
|
1040
|
-
}
|
|
1041
|
-
} catch (ex) {
|
|
1042
|
-
// Probably a node.js internal module
|
|
1043
|
-
return false;
|
|
1044
|
-
}
|
|
1045
|
-
|
|
1046
|
-
return false;
|
|
1047
|
-
}
|
|
1048
|
-
|
|
1049
|
-
for (const attemptExt of ['', '.js', '.json']) {
|
|
1050
|
-
const resolvedPath = normalizePathSlashes(resolve(dirname(id), source + attemptExt));
|
|
1051
|
-
if (dynamicRequireModuleSet.has(resolvedPath)) {
|
|
1052
|
-
return true;
|
|
1053
|
-
}
|
|
1054
|
-
}
|
|
1055
|
-
|
|
1056
|
-
return false;
|
|
1057
|
-
}
|
|
1058
|
-
|
|
1059
1072
|
function getRequireHandlers() {
|
|
1060
|
-
const
|
|
1061
|
-
|
|
1062
|
-
|
|
1063
|
-
|
|
1064
|
-
|
|
1065
|
-
|
|
1066
|
-
|
|
1067
|
-
|
|
1068
|
-
|
|
1069
|
-
|
|
1070
|
-
|
|
1071
|
-
|
|
1072
|
-
|
|
1073
|
-
|
|
1074
|
-
|
|
1075
|
-
|
|
1076
|
-
|
|
1077
|
-
|
|
1078
|
-
|
|
1079
|
-
|
|
1080
|
-
name: null,
|
|
1081
|
-
nodesUsingRequired: []
|
|
1082
|
-
};
|
|
1083
|
-
}
|
|
1084
|
-
|
|
1085
|
-
return requiredBySource[sourceId];
|
|
1073
|
+
const requireExpressions = [];
|
|
1074
|
+
|
|
1075
|
+
function addRequireStatement(
|
|
1076
|
+
sourceId,
|
|
1077
|
+
node,
|
|
1078
|
+
scope,
|
|
1079
|
+
usesReturnValue,
|
|
1080
|
+
isInsideTryBlock,
|
|
1081
|
+
isInsideConditional,
|
|
1082
|
+
toBeRemoved
|
|
1083
|
+
) {
|
|
1084
|
+
requireExpressions.push({
|
|
1085
|
+
sourceId,
|
|
1086
|
+
node,
|
|
1087
|
+
scope,
|
|
1088
|
+
usesReturnValue,
|
|
1089
|
+
isInsideTryBlock,
|
|
1090
|
+
isInsideConditional,
|
|
1091
|
+
toBeRemoved
|
|
1092
|
+
});
|
|
1086
1093
|
}
|
|
1087
1094
|
|
|
1088
|
-
function rewriteRequireExpressionsAndGetImportBlock(
|
|
1095
|
+
async function rewriteRequireExpressionsAndGetImportBlock(
|
|
1089
1096
|
magicString,
|
|
1090
1097
|
topLevelDeclarations,
|
|
1091
|
-
topLevelRequireDeclarators,
|
|
1092
1098
|
reassignedNames,
|
|
1093
1099
|
helpersName,
|
|
1094
|
-
|
|
1100
|
+
dynamicRequireName,
|
|
1095
1101
|
moduleName,
|
|
1096
1102
|
exportsName,
|
|
1097
1103
|
id,
|
|
1098
|
-
exportMode
|
|
1104
|
+
exportMode,
|
|
1105
|
+
resolveRequireSourcesAndGetMeta,
|
|
1106
|
+
needsRequireWrapper,
|
|
1107
|
+
isEsModule,
|
|
1108
|
+
usesRequire,
|
|
1109
|
+
getIgnoreTryCatchRequireStatementMode
|
|
1099
1110
|
) {
|
|
1100
|
-
setRemainingImportNamesAndRewriteRequires(
|
|
1101
|
-
requireExpressionsWithUsedReturnValue,
|
|
1102
|
-
requiredByNode,
|
|
1103
|
-
magicString
|
|
1104
|
-
);
|
|
1105
1111
|
const imports = [];
|
|
1106
1112
|
imports.push(`import * as ${helpersName} from "${HELPERS_ID}";`);
|
|
1113
|
+
if (usesRequire) {
|
|
1114
|
+
imports.push(
|
|
1115
|
+
`import { commonjsRequire as ${dynamicRequireName} } from "${DYNAMIC_MODULES_ID}";`
|
|
1116
|
+
);
|
|
1117
|
+
}
|
|
1107
1118
|
if (exportMode === 'module') {
|
|
1108
1119
|
imports.push(
|
|
1109
1120
|
`import { __module as ${moduleName}, exports as ${exportsName} } from ${JSON.stringify(
|
|
@@ -1115,58 +1126,115 @@ function getRequireHandlers() {
|
|
|
1115
1126
|
`import { __exports as ${exportsName} } from ${JSON.stringify(wrapId(id, EXPORTS_SUFFIX))}`
|
|
1116
1127
|
);
|
|
1117
1128
|
}
|
|
1118
|
-
|
|
1119
|
-
|
|
1120
|
-
|
|
1121
|
-
|
|
1122
|
-
|
|
1123
|
-
|
|
1124
|
-
|
|
1125
|
-
|
|
1126
|
-
|
|
1127
|
-
|
|
1128
|
-
|
|
1129
|
-
|
|
1130
|
-
|
|
1131
|
-
|
|
1132
|
-
|
|
1129
|
+
const requiresBySource = collectSources(requireExpressions);
|
|
1130
|
+
const { requireTargets, usesRequireWrapper } = await resolveRequireSourcesAndGetMeta(
|
|
1131
|
+
id,
|
|
1132
|
+
needsRequireWrapper ? IS_WRAPPED_COMMONJS : !isEsModule,
|
|
1133
|
+
Object.keys(requiresBySource).map((source) => {
|
|
1134
|
+
return {
|
|
1135
|
+
source,
|
|
1136
|
+
isConditional: requiresBySource[source].every((require) => require.isInsideConditional)
|
|
1137
|
+
};
|
|
1138
|
+
})
|
|
1139
|
+
);
|
|
1140
|
+
processRequireExpressions(
|
|
1141
|
+
imports,
|
|
1142
|
+
requireTargets,
|
|
1143
|
+
requiresBySource,
|
|
1144
|
+
getIgnoreTryCatchRequireStatementMode,
|
|
1145
|
+
magicString
|
|
1146
|
+
);
|
|
1147
|
+
return {
|
|
1148
|
+
importBlock: imports.length ? `${imports.join('\n')}\n\n` : '',
|
|
1149
|
+
usesRequireWrapper
|
|
1150
|
+
};
|
|
1133
1151
|
}
|
|
1134
1152
|
|
|
1135
1153
|
return {
|
|
1136
1154
|
addRequireStatement,
|
|
1137
|
-
requiredSources,
|
|
1138
1155
|
rewriteRequireExpressionsAndGetImportBlock
|
|
1139
1156
|
};
|
|
1140
1157
|
}
|
|
1141
1158
|
|
|
1142
|
-
function
|
|
1143
|
-
|
|
1144
|
-
|
|
1159
|
+
function collectSources(requireExpressions) {
|
|
1160
|
+
const requiresBySource = Object.create(null);
|
|
1161
|
+
for (const requireExpression of requireExpressions) {
|
|
1162
|
+
const { sourceId } = requireExpression;
|
|
1163
|
+
if (!requiresBySource[sourceId]) {
|
|
1164
|
+
requiresBySource[sourceId] = [];
|
|
1165
|
+
}
|
|
1166
|
+
const requires = requiresBySource[sourceId];
|
|
1167
|
+
requires.push(requireExpression);
|
|
1168
|
+
}
|
|
1169
|
+
return requiresBySource;
|
|
1170
|
+
}
|
|
1171
|
+
|
|
1172
|
+
function processRequireExpressions(
|
|
1173
|
+
imports,
|
|
1174
|
+
requireTargets,
|
|
1175
|
+
requiresBySource,
|
|
1176
|
+
getIgnoreTryCatchRequireStatementMode,
|
|
1145
1177
|
magicString
|
|
1146
1178
|
) {
|
|
1147
|
-
|
|
1148
|
-
for (const
|
|
1149
|
-
const
|
|
1150
|
-
|
|
1151
|
-
|
|
1152
|
-
|
|
1153
|
-
|
|
1154
|
-
|
|
1155
|
-
|
|
1156
|
-
|
|
1157
|
-
|
|
1179
|
+
const generateRequireName = getGenerateRequireName();
|
|
1180
|
+
for (const { source, id: resolvedId, isCommonJS } of requireTargets) {
|
|
1181
|
+
const requires = requiresBySource[source];
|
|
1182
|
+
const name = generateRequireName(requires);
|
|
1183
|
+
let usesRequired = false;
|
|
1184
|
+
let needsImport = false;
|
|
1185
|
+
for (const { node, usesReturnValue, toBeRemoved, isInsideTryBlock } of requires) {
|
|
1186
|
+
const { canConvertRequire, shouldRemoveRequire } =
|
|
1187
|
+
isInsideTryBlock && isWrappedId(resolvedId, EXTERNAL_SUFFIX)
|
|
1188
|
+
? getIgnoreTryCatchRequireStatementMode(source)
|
|
1189
|
+
: { canConvertRequire: true, shouldRemoveRequire: false };
|
|
1190
|
+
if (shouldRemoveRequire) {
|
|
1191
|
+
if (usesReturnValue) {
|
|
1192
|
+
magicString.overwrite(node.start, node.end, 'undefined');
|
|
1193
|
+
} else {
|
|
1194
|
+
magicString.remove(toBeRemoved.start, toBeRemoved.end);
|
|
1195
|
+
}
|
|
1196
|
+
} else if (canConvertRequire) {
|
|
1197
|
+
needsImport = true;
|
|
1198
|
+
if (isCommonJS === IS_WRAPPED_COMMONJS) {
|
|
1199
|
+
magicString.overwrite(node.start, node.end, `${name}()`);
|
|
1200
|
+
} else if (usesReturnValue) {
|
|
1201
|
+
usesRequired = true;
|
|
1202
|
+
magicString.overwrite(node.start, node.end, name);
|
|
1203
|
+
} else {
|
|
1204
|
+
magicString.remove(toBeRemoved.start, toBeRemoved.end);
|
|
1205
|
+
}
|
|
1206
|
+
}
|
|
1207
|
+
}
|
|
1208
|
+
if (needsImport) {
|
|
1209
|
+
if (isCommonJS === IS_WRAPPED_COMMONJS) {
|
|
1210
|
+
imports.push(`import { __require as ${name} } from ${JSON.stringify(resolvedId)};`);
|
|
1211
|
+
} else {
|
|
1212
|
+
imports.push(`import ${usesRequired ? `${name} from ` : ''}${JSON.stringify(resolvedId)};`);
|
|
1213
|
+
}
|
|
1158
1214
|
}
|
|
1159
|
-
magicString.overwrite(requireExpression.start, requireExpression.end, required.name);
|
|
1160
1215
|
}
|
|
1161
1216
|
}
|
|
1162
1217
|
|
|
1218
|
+
function getGenerateRequireName() {
|
|
1219
|
+
let uid = 0;
|
|
1220
|
+
return (requires) => {
|
|
1221
|
+
let name;
|
|
1222
|
+
const hasNameConflict = ({ scope }) => scope.contains(name);
|
|
1223
|
+
do {
|
|
1224
|
+
name = `require$$${uid}`;
|
|
1225
|
+
uid += 1;
|
|
1226
|
+
} while (requires.some(hasNameConflict));
|
|
1227
|
+
return name;
|
|
1228
|
+
};
|
|
1229
|
+
}
|
|
1230
|
+
|
|
1163
1231
|
/* eslint-disable no-param-reassign, no-shadow, no-underscore-dangle, no-continue */
|
|
1164
1232
|
|
|
1165
1233
|
const exportsPattern = /^(?:module\.)?exports(?:\.([a-zA-Z_$][a-zA-Z_$0-9]*))?$/;
|
|
1166
1234
|
|
|
1167
1235
|
const functionType = /^(?:FunctionDeclaration|FunctionExpression|ArrowFunctionExpression)$/;
|
|
1168
1236
|
|
|
1169
|
-
function transformCommonjs(
|
|
1237
|
+
async function transformCommonjs(
|
|
1170
1238
|
parse,
|
|
1171
1239
|
code,
|
|
1172
1240
|
id,
|
|
@@ -1177,11 +1245,14 @@ function transformCommonjs(
|
|
|
1177
1245
|
getIgnoreTryCatchRequireStatementMode,
|
|
1178
1246
|
sourceMap,
|
|
1179
1247
|
isDynamicRequireModulesEnabled,
|
|
1180
|
-
|
|
1181
|
-
disableWrap,
|
|
1248
|
+
dynamicRequireModules,
|
|
1182
1249
|
commonDir,
|
|
1183
1250
|
astCache,
|
|
1184
|
-
defaultIsModuleExports
|
|
1251
|
+
defaultIsModuleExports,
|
|
1252
|
+
needsRequireWrapper,
|
|
1253
|
+
resolveRequireSourcesAndGetMeta,
|
|
1254
|
+
isRequired,
|
|
1255
|
+
checkDynamicRequire
|
|
1185
1256
|
) {
|
|
1186
1257
|
const ast = astCache || tryParse(parse, code, id);
|
|
1187
1258
|
const magicString = new MagicString(code);
|
|
@@ -1191,7 +1262,6 @@ function transformCommonjs(
|
|
|
1191
1262
|
global: false,
|
|
1192
1263
|
require: false
|
|
1193
1264
|
};
|
|
1194
|
-
let usesDynamicRequire = false;
|
|
1195
1265
|
const virtualDynamicRequirePath =
|
|
1196
1266
|
isDynamicRequireModulesEnabled && getVirtualPathForDynamicRequirePath(dirname(id), commonDir);
|
|
1197
1267
|
let scope = attachScopes(ast, 'scope');
|
|
@@ -1201,24 +1271,18 @@ function transformCommonjs(
|
|
|
1201
1271
|
let shouldWrap = false;
|
|
1202
1272
|
|
|
1203
1273
|
const globals = new Set();
|
|
1204
|
-
|
|
1205
|
-
//
|
|
1206
|
-
|
|
1207
|
-
|
|
1208
|
-
|
|
1209
|
-
|
|
1210
|
-
const {
|
|
1211
|
-
addRequireStatement,
|
|
1212
|
-
requiredSources,
|
|
1213
|
-
rewriteRequireExpressionsAndGetImportBlock
|
|
1214
|
-
} = getRequireHandlers();
|
|
1274
|
+
// A conditionalNode is a node for which execution is not guaranteed. If such a node is a require
|
|
1275
|
+
// or contains nested requires, those should be handled as function calls unless there is an
|
|
1276
|
+
// unconditional require elsewhere.
|
|
1277
|
+
let currentConditionalNodeEnd = null;
|
|
1278
|
+
const conditionalNodes = new Set();
|
|
1279
|
+
const { addRequireStatement, rewriteRequireExpressionsAndGetImportBlock } = getRequireHandlers();
|
|
1215
1280
|
|
|
1216
1281
|
// See which names are assigned to. This is necessary to prevent
|
|
1217
1282
|
// illegally replacing `var foo = require('foo')` with `import foo from 'foo'`,
|
|
1218
1283
|
// where `foo` is later reassigned. (This happens in the wild. CommonJS, sigh)
|
|
1219
1284
|
const reassignedNames = new Set();
|
|
1220
1285
|
const topLevelDeclarations = [];
|
|
1221
|
-
const topLevelRequireDeclarators = new Set();
|
|
1222
1286
|
const skippedNodes = new Set();
|
|
1223
1287
|
const moduleAccessScopes = new Set([scope]);
|
|
1224
1288
|
const exportsAccessScopes = new Set([scope]);
|
|
@@ -1227,6 +1291,8 @@ function transformCommonjs(
|
|
|
1227
1291
|
const exportsAssignmentsByName = new Map();
|
|
1228
1292
|
const topLevelAssignments = new Set();
|
|
1229
1293
|
const topLevelDefineCompiledEsmExpressions = [];
|
|
1294
|
+
const replacedGlobal = [];
|
|
1295
|
+
const replacedDynamicRequires = [];
|
|
1230
1296
|
|
|
1231
1297
|
walk(ast, {
|
|
1232
1298
|
enter(node, parent) {
|
|
@@ -1238,6 +1304,12 @@ function transformCommonjs(
|
|
|
1238
1304
|
if (currentTryBlockEnd !== null && node.start > currentTryBlockEnd) {
|
|
1239
1305
|
currentTryBlockEnd = null;
|
|
1240
1306
|
}
|
|
1307
|
+
if (currentConditionalNodeEnd !== null && node.start > currentConditionalNodeEnd) {
|
|
1308
|
+
currentConditionalNodeEnd = null;
|
|
1309
|
+
}
|
|
1310
|
+
if (currentConditionalNodeEnd === null && conditionalNodes.has(node)) {
|
|
1311
|
+
currentConditionalNodeEnd = node.end;
|
|
1312
|
+
}
|
|
1241
1313
|
|
|
1242
1314
|
programDepth += 1;
|
|
1243
1315
|
if (node.scope) ({ scope } = node);
|
|
@@ -1249,11 +1321,6 @@ function transformCommonjs(
|
|
|
1249
1321
|
|
|
1250
1322
|
// eslint-disable-next-line default-case
|
|
1251
1323
|
switch (node.type) {
|
|
1252
|
-
case 'TryStatement':
|
|
1253
|
-
if (currentTryBlockEnd === null) {
|
|
1254
|
-
currentTryBlockEnd = node.block.end;
|
|
1255
|
-
}
|
|
1256
|
-
return;
|
|
1257
1324
|
case 'AssignmentExpression':
|
|
1258
1325
|
if (node.left.type === 'MemberExpression') {
|
|
1259
1326
|
const flattened = getKeypath(node.left);
|
|
@@ -1324,12 +1391,15 @@ function transformCommonjs(
|
|
|
1324
1391
|
return;
|
|
1325
1392
|
}
|
|
1326
1393
|
|
|
1394
|
+
// Transform require.resolve
|
|
1327
1395
|
if (
|
|
1396
|
+
isDynamicRequireModulesEnabled &&
|
|
1328
1397
|
node.callee.object &&
|
|
1329
|
-
node.callee.object
|
|
1330
|
-
node.callee.property.name === 'resolve'
|
|
1331
|
-
hasDynamicModuleForPath(id, '/', dynamicRequireModuleSet)
|
|
1398
|
+
isRequire(node.callee.object, scope) &&
|
|
1399
|
+
node.callee.property.name === 'resolve'
|
|
1332
1400
|
) {
|
|
1401
|
+
checkDynamicRequire();
|
|
1402
|
+
uses.require = true;
|
|
1333
1403
|
const requireNode = node.callee.object;
|
|
1334
1404
|
magicString.appendLeft(
|
|
1335
1405
|
node.end - 1,
|
|
@@ -1337,98 +1407,45 @@ function transformCommonjs(
|
|
|
1337
1407
|
dirname(id) === '.' ? null /* default behavior */ : virtualDynamicRequirePath
|
|
1338
1408
|
)}`
|
|
1339
1409
|
);
|
|
1340
|
-
|
|
1341
|
-
requireNode.start,
|
|
1342
|
-
requireNode.end,
|
|
1343
|
-
`${HELPERS_NAME}.commonjsRequire`,
|
|
1344
|
-
{
|
|
1345
|
-
storeName: true
|
|
1346
|
-
}
|
|
1347
|
-
);
|
|
1410
|
+
replacedDynamicRequires.push(requireNode);
|
|
1348
1411
|
return;
|
|
1349
1412
|
}
|
|
1350
1413
|
|
|
1351
|
-
if (!
|
|
1352
|
-
|
|
1353
|
-
skippedNodes.add(node.callee);
|
|
1414
|
+
if (!isRequireExpression(node, scope)) {
|
|
1415
|
+
return;
|
|
1354
1416
|
}
|
|
1355
|
-
if (!isIgnoredRequireStatement(node, ignoreRequire)) {
|
|
1356
|
-
skippedNodes.add(node.callee);
|
|
1357
|
-
const usesReturnValue = parent.type !== 'ExpressionStatement';
|
|
1358
|
-
|
|
1359
|
-
let canConvertRequire = true;
|
|
1360
|
-
let shouldRemoveRequireStatement = false;
|
|
1361
1417
|
|
|
1362
|
-
|
|
1363
|
-
|
|
1364
|
-
|
|
1365
|
-
|
|
1366
|
-
|
|
1367
|
-
|
|
1368
|
-
|
|
1369
|
-
|
|
1370
|
-
|
|
1418
|
+
skippedNodes.add(node.callee);
|
|
1419
|
+
uses.require = true;
|
|
1420
|
+
|
|
1421
|
+
if (hasDynamicArguments(node)) {
|
|
1422
|
+
if (isDynamicRequireModulesEnabled) {
|
|
1423
|
+
magicString.appendLeft(
|
|
1424
|
+
node.end - 1,
|
|
1425
|
+
`, ${JSON.stringify(
|
|
1426
|
+
dirname(id) === '.' ? null /* default behavior */ : virtualDynamicRequirePath
|
|
1427
|
+
)}`
|
|
1428
|
+
);
|
|
1371
1429
|
}
|
|
1372
|
-
|
|
1373
|
-
|
|
1374
|
-
|
|
1375
|
-
if (isDynamicRegister) {
|
|
1376
|
-
sourceId = unwrapId(sourceId, DYNAMIC_REGISTER_SUFFIX);
|
|
1377
|
-
if (sourceId.endsWith('.json')) {
|
|
1378
|
-
sourceId = DYNAMIC_JSON_PREFIX + sourceId;
|
|
1379
|
-
}
|
|
1380
|
-
dynamicRegisterSources.add(wrapId(sourceId, DYNAMIC_REGISTER_SUFFIX));
|
|
1381
|
-
} else {
|
|
1382
|
-
if (
|
|
1383
|
-
!sourceId.endsWith('.json') &&
|
|
1384
|
-
hasDynamicModuleForPath(sourceId, id, dynamicRequireModuleSet)
|
|
1385
|
-
) {
|
|
1386
|
-
if (shouldRemoveRequireStatement) {
|
|
1387
|
-
magicString.overwrite(node.start, node.end, `undefined`);
|
|
1388
|
-
} else if (canConvertRequire) {
|
|
1389
|
-
magicString.overwrite(
|
|
1390
|
-
node.start,
|
|
1391
|
-
node.end,
|
|
1392
|
-
`${HELPERS_NAME}.commonjsRequire(${JSON.stringify(
|
|
1393
|
-
getVirtualPathForDynamicRequirePath(sourceId, commonDir)
|
|
1394
|
-
)}, ${JSON.stringify(
|
|
1395
|
-
dirname(id) === '.' ? null /* default behavior */ : virtualDynamicRequirePath
|
|
1396
|
-
)})`
|
|
1397
|
-
);
|
|
1398
|
-
usesDynamicRequire = true;
|
|
1399
|
-
}
|
|
1400
|
-
return;
|
|
1401
|
-
}
|
|
1402
|
-
|
|
1403
|
-
if (canConvertRequire) {
|
|
1404
|
-
addRequireStatement(sourceId, node, scope, usesReturnValue);
|
|
1405
|
-
}
|
|
1430
|
+
if (!ignoreDynamicRequires) {
|
|
1431
|
+
checkDynamicRequire();
|
|
1432
|
+
replacedDynamicRequires.push(node.callee);
|
|
1406
1433
|
}
|
|
1434
|
+
return;
|
|
1435
|
+
}
|
|
1407
1436
|
|
|
1408
|
-
|
|
1409
|
-
|
|
1410
|
-
|
|
1411
|
-
|
|
1412
|
-
|
|
1413
|
-
|
|
1414
|
-
|
|
1415
|
-
|
|
1416
|
-
|
|
1417
|
-
|
|
1418
|
-
|
|
1419
|
-
|
|
1420
|
-
// and does not conflict with variables in other places where this is imported
|
|
1421
|
-
topLevelRequireDeclarators.add(parent);
|
|
1422
|
-
}
|
|
1423
|
-
} else {
|
|
1424
|
-
// This is a bare import, e.g. `require('foo');`
|
|
1425
|
-
|
|
1426
|
-
if (!canConvertRequire && !shouldRemoveRequireStatement) {
|
|
1427
|
-
return;
|
|
1428
|
-
}
|
|
1429
|
-
|
|
1430
|
-
magicString.remove(parent.start, parent.end);
|
|
1431
|
-
}
|
|
1437
|
+
const requireStringArg = getRequireStringArg(node);
|
|
1438
|
+
if (!ignoreRequire(requireStringArg)) {
|
|
1439
|
+
const usesReturnValue = parent.type !== 'ExpressionStatement';
|
|
1440
|
+
addRequireStatement(
|
|
1441
|
+
requireStringArg,
|
|
1442
|
+
node,
|
|
1443
|
+
scope,
|
|
1444
|
+
usesReturnValue,
|
|
1445
|
+
currentTryBlockEnd !== null,
|
|
1446
|
+
currentConditionalNodeEnd !== null,
|
|
1447
|
+
parent.type === 'ExpressionStatement' ? parent : node
|
|
1448
|
+
);
|
|
1432
1449
|
}
|
|
1433
1450
|
return;
|
|
1434
1451
|
}
|
|
@@ -1437,45 +1454,44 @@ function transformCommonjs(
|
|
|
1437
1454
|
// skip dead branches
|
|
1438
1455
|
if (isFalsy(node.test)) {
|
|
1439
1456
|
skippedNodes.add(node.consequent);
|
|
1440
|
-
} else if (
|
|
1441
|
-
|
|
1457
|
+
} else if (isTruthy(node.test)) {
|
|
1458
|
+
if (node.alternate) {
|
|
1459
|
+
skippedNodes.add(node.alternate);
|
|
1460
|
+
}
|
|
1461
|
+
} else {
|
|
1462
|
+
conditionalNodes.add(node.consequent);
|
|
1463
|
+
if (node.alternate) {
|
|
1464
|
+
conditionalNodes.add(node.alternate);
|
|
1465
|
+
}
|
|
1466
|
+
}
|
|
1467
|
+
return;
|
|
1468
|
+
case 'ArrowFunctionExpression':
|
|
1469
|
+
case 'FunctionDeclaration':
|
|
1470
|
+
case 'FunctionExpression':
|
|
1471
|
+
// requires in functions should be conditional unless it is an IIFE
|
|
1472
|
+
if (
|
|
1473
|
+
currentConditionalNodeEnd === null &&
|
|
1474
|
+
!(parent.type === 'CallExpression' && parent.callee === node)
|
|
1475
|
+
) {
|
|
1476
|
+
currentConditionalNodeEnd = node.end;
|
|
1442
1477
|
}
|
|
1443
1478
|
return;
|
|
1444
1479
|
case 'Identifier': {
|
|
1445
1480
|
const { name } = node;
|
|
1446
|
-
if (!
|
|
1481
|
+
if (!isReference(node, parent) || scope.contains(name)) return;
|
|
1447
1482
|
switch (name) {
|
|
1448
1483
|
case 'require':
|
|
1484
|
+
uses.require = true;
|
|
1449
1485
|
if (isNodeRequirePropertyAccess(parent)) {
|
|
1450
|
-
if (hasDynamicModuleForPath(id, '/', dynamicRequireModuleSet)) {
|
|
1451
|
-
if (parent.property.name === 'cache') {
|
|
1452
|
-
magicString.overwrite(node.start, node.end, `${HELPERS_NAME}.commonjsRequire`, {
|
|
1453
|
-
storeName: true
|
|
1454
|
-
});
|
|
1455
|
-
}
|
|
1456
|
-
}
|
|
1457
|
-
|
|
1458
1486
|
return;
|
|
1459
1487
|
}
|
|
1460
|
-
|
|
1461
|
-
if (isDynamicRequireModulesEnabled && isRequireStatement(parent, scope)) {
|
|
1462
|
-
magicString.appendLeft(
|
|
1463
|
-
parent.end - 1,
|
|
1464
|
-
`,${JSON.stringify(
|
|
1465
|
-
dirname(id) === '.' ? null /* default behavior */ : virtualDynamicRequirePath
|
|
1466
|
-
)}`
|
|
1467
|
-
);
|
|
1468
|
-
}
|
|
1469
1488
|
if (!ignoreDynamicRequires) {
|
|
1489
|
+
checkDynamicRequire();
|
|
1470
1490
|
if (isShorthandProperty(parent)) {
|
|
1471
|
-
magicString.
|
|
1472
|
-
} else {
|
|
1473
|
-
magicString.overwrite(node.start, node.end, `${HELPERS_NAME}.commonjsRequire`, {
|
|
1474
|
-
storeName: true
|
|
1475
|
-
});
|
|
1491
|
+
magicString.prependRight(node.start, 'require: ');
|
|
1476
1492
|
}
|
|
1493
|
+
replacedDynamicRequires.push(node);
|
|
1477
1494
|
}
|
|
1478
|
-
usesDynamicRequire = true;
|
|
1479
1495
|
return;
|
|
1480
1496
|
case 'module':
|
|
1481
1497
|
case 'exports':
|
|
@@ -1485,9 +1501,7 @@ function transformCommonjs(
|
|
|
1485
1501
|
case 'global':
|
|
1486
1502
|
uses.global = true;
|
|
1487
1503
|
if (!ignoreGlobal) {
|
|
1488
|
-
|
|
1489
|
-
storeName: true
|
|
1490
|
-
});
|
|
1504
|
+
replacedGlobal.push(node);
|
|
1491
1505
|
}
|
|
1492
1506
|
return;
|
|
1493
1507
|
case 'define':
|
|
@@ -1500,11 +1514,26 @@ function transformCommonjs(
|
|
|
1500
1514
|
return;
|
|
1501
1515
|
}
|
|
1502
1516
|
}
|
|
1517
|
+
case 'LogicalExpression':
|
|
1518
|
+
// skip dead branches
|
|
1519
|
+
if (node.operator === '&&') {
|
|
1520
|
+
if (isFalsy(node.left)) {
|
|
1521
|
+
skippedNodes.add(node.right);
|
|
1522
|
+
} else if (!isTruthy(node.left)) {
|
|
1523
|
+
conditionalNodes.add(node.right);
|
|
1524
|
+
}
|
|
1525
|
+
} else if (node.operator === '||') {
|
|
1526
|
+
if (isTruthy(node.left)) {
|
|
1527
|
+
skippedNodes.add(node.right);
|
|
1528
|
+
} else if (!isFalsy(node.left)) {
|
|
1529
|
+
conditionalNodes.add(node.right);
|
|
1530
|
+
}
|
|
1531
|
+
}
|
|
1532
|
+
return;
|
|
1503
1533
|
case 'MemberExpression':
|
|
1504
1534
|
if (!isDynamicRequireModulesEnabled && isModuleRequire(node, scope)) {
|
|
1505
|
-
|
|
1506
|
-
|
|
1507
|
-
});
|
|
1535
|
+
uses.require = true;
|
|
1536
|
+
replacedDynamicRequires.push(node);
|
|
1508
1537
|
skippedNodes.add(node.object);
|
|
1509
1538
|
skippedNodes.add(node.property);
|
|
1510
1539
|
}
|
|
@@ -1520,12 +1549,18 @@ function transformCommonjs(
|
|
|
1520
1549
|
if (lexicalDepth === 0) {
|
|
1521
1550
|
uses.global = true;
|
|
1522
1551
|
if (!ignoreGlobal) {
|
|
1523
|
-
|
|
1524
|
-
storeName: true
|
|
1525
|
-
});
|
|
1552
|
+
replacedGlobal.push(node);
|
|
1526
1553
|
}
|
|
1527
1554
|
}
|
|
1528
1555
|
return;
|
|
1556
|
+
case 'TryStatement':
|
|
1557
|
+
if (currentTryBlockEnd === null) {
|
|
1558
|
+
currentTryBlockEnd = node.block.end;
|
|
1559
|
+
}
|
|
1560
|
+
if (currentConditionalNodeEnd === null) {
|
|
1561
|
+
currentConditionalNodeEnd = node.end;
|
|
1562
|
+
}
|
|
1563
|
+
return;
|
|
1529
1564
|
case 'UnaryExpression':
|
|
1530
1565
|
// rewrite `typeof module`, `typeof module.exports` and `typeof exports` (https://github.com/rollup/rollup-plugin-commonjs/issues/151)
|
|
1531
1566
|
if (node.operator === 'typeof') {
|
|
@@ -1562,34 +1597,45 @@ function transformCommonjs(
|
|
|
1562
1597
|
const nameBase = getName(id);
|
|
1563
1598
|
const exportsName = deconflict([...exportsAccessScopes], globals, nameBase);
|
|
1564
1599
|
const moduleName = deconflict([...moduleAccessScopes], globals, `${nameBase}Module`);
|
|
1600
|
+
const requireName = deconflict([scope], globals, `require${capitalize(nameBase)}`);
|
|
1601
|
+
const isRequiredName = deconflict([scope], globals, `hasRequired${capitalize(nameBase)}`);
|
|
1602
|
+
const helpersName = deconflict([scope], globals, 'commonjsHelpers');
|
|
1603
|
+
const dynamicRequireName = deconflict([scope], globals, 'commonjsRequire');
|
|
1565
1604
|
const deconflictedExportNames = Object.create(null);
|
|
1566
1605
|
for (const [exportName, { scopes }] of exportsAssignmentsByName) {
|
|
1567
1606
|
deconflictedExportNames[exportName] = deconflict([...scopes], globals, exportName);
|
|
1568
1607
|
}
|
|
1569
1608
|
|
|
1609
|
+
for (const node of replacedGlobal) {
|
|
1610
|
+
magicString.overwrite(node.start, node.end, `${helpersName}.commonjsGlobal`, {
|
|
1611
|
+
storeName: true
|
|
1612
|
+
});
|
|
1613
|
+
}
|
|
1614
|
+
for (const node of replacedDynamicRequires) {
|
|
1615
|
+
magicString.overwrite(node.start, node.end, dynamicRequireName, {
|
|
1616
|
+
contentOnly: true,
|
|
1617
|
+
storeName: true
|
|
1618
|
+
});
|
|
1619
|
+
}
|
|
1620
|
+
|
|
1570
1621
|
// We cannot wrap ES/mixed modules
|
|
1571
|
-
shouldWrap =
|
|
1572
|
-
!isEsModule &&
|
|
1573
|
-
!disableWrap &&
|
|
1574
|
-
(shouldWrap || (uses.exports && moduleExportsAssignments.length > 0));
|
|
1622
|
+
shouldWrap = !isEsModule && (shouldWrap || (uses.exports && moduleExportsAssignments.length > 0));
|
|
1575
1623
|
const detectWrappedDefault =
|
|
1576
1624
|
shouldWrap &&
|
|
1577
1625
|
(topLevelDefineCompiledEsmExpressions.length > 0 || code.indexOf('__esModule') >= 0);
|
|
1578
1626
|
|
|
1579
1627
|
if (
|
|
1580
1628
|
!(
|
|
1581
|
-
|
|
1582
|
-
|
|
1629
|
+
shouldWrap ||
|
|
1630
|
+
isRequired ||
|
|
1583
1631
|
uses.module ||
|
|
1584
1632
|
uses.exports ||
|
|
1585
1633
|
uses.require ||
|
|
1586
|
-
usesDynamicRequire ||
|
|
1587
|
-
hasRemovedRequire ||
|
|
1588
1634
|
topLevelDefineCompiledEsmExpressions.length > 0
|
|
1589
1635
|
) &&
|
|
1590
1636
|
(ignoreGlobal || !uses.global)
|
|
1591
1637
|
) {
|
|
1592
|
-
return { meta: { commonjs: { isCommonJS: false } } };
|
|
1638
|
+
return { meta: { commonjs: { isCommonJS: false, isMixedModule: false } } };
|
|
1593
1639
|
}
|
|
1594
1640
|
|
|
1595
1641
|
let leadingComment = '';
|
|
@@ -1611,19 +1657,22 @@ function transformCommonjs(
|
|
|
1611
1657
|
? 'exports'
|
|
1612
1658
|
: 'module';
|
|
1613
1659
|
|
|
1614
|
-
const importBlock = rewriteRequireExpressionsAndGetImportBlock(
|
|
1660
|
+
const { importBlock, usesRequireWrapper } = await rewriteRequireExpressionsAndGetImportBlock(
|
|
1615
1661
|
magicString,
|
|
1616
1662
|
topLevelDeclarations,
|
|
1617
|
-
topLevelRequireDeclarators,
|
|
1618
1663
|
reassignedNames,
|
|
1619
|
-
|
|
1620
|
-
|
|
1664
|
+
helpersName,
|
|
1665
|
+
dynamicRequireName,
|
|
1621
1666
|
moduleName,
|
|
1622
1667
|
exportsName,
|
|
1623
1668
|
id,
|
|
1624
|
-
exportMode
|
|
1669
|
+
exportMode,
|
|
1670
|
+
resolveRequireSourcesAndGetMeta,
|
|
1671
|
+
needsRequireWrapper,
|
|
1672
|
+
isEsModule,
|
|
1673
|
+
uses.require,
|
|
1674
|
+
getIgnoreTryCatchRequireStatementMode
|
|
1625
1675
|
);
|
|
1626
|
-
|
|
1627
1676
|
const exportBlock = isEsModule
|
|
1628
1677
|
? ''
|
|
1629
1678
|
: rewriteExportsAndGetExportsBlock(
|
|
@@ -1638,16 +1687,35 @@ function transformCommonjs(
|
|
|
1638
1687
|
topLevelDefineCompiledEsmExpressions,
|
|
1639
1688
|
deconflictedExportNames,
|
|
1640
1689
|
code,
|
|
1641
|
-
|
|
1690
|
+
helpersName,
|
|
1642
1691
|
exportMode,
|
|
1643
1692
|
detectWrappedDefault,
|
|
1644
|
-
defaultIsModuleExports
|
|
1693
|
+
defaultIsModuleExports,
|
|
1694
|
+
usesRequireWrapper,
|
|
1695
|
+
requireName
|
|
1645
1696
|
);
|
|
1646
1697
|
|
|
1647
1698
|
if (shouldWrap) {
|
|
1648
1699
|
wrapCode(magicString, uses, moduleName, exportsName);
|
|
1649
1700
|
}
|
|
1650
1701
|
|
|
1702
|
+
if (usesRequireWrapper) {
|
|
1703
|
+
magicString.trim().indent('\t');
|
|
1704
|
+
magicString.prepend(
|
|
1705
|
+
`var ${isRequiredName};
|
|
1706
|
+
|
|
1707
|
+
function ${requireName} () {
|
|
1708
|
+
\tif (${isRequiredName}) return ${exportsName};
|
|
1709
|
+
\t${isRequiredName} = 1;
|
|
1710
|
+
`
|
|
1711
|
+
).append(`
|
|
1712
|
+
\treturn ${exportsName};
|
|
1713
|
+
}`);
|
|
1714
|
+
if (exportMode === 'replace') {
|
|
1715
|
+
magicString.prepend(`var ${exportsName};\n`);
|
|
1716
|
+
}
|
|
1717
|
+
}
|
|
1718
|
+
|
|
1651
1719
|
magicString
|
|
1652
1720
|
.trim()
|
|
1653
1721
|
.prepend(leadingComment + importBlock)
|
|
@@ -1656,24 +1724,32 @@ function transformCommonjs(
|
|
|
1656
1724
|
return {
|
|
1657
1725
|
code: magicString.toString(),
|
|
1658
1726
|
map: sourceMap ? magicString.generateMap() : null,
|
|
1659
|
-
syntheticNamedExports: isEsModule ? false : '__moduleExports',
|
|
1660
|
-
meta: {
|
|
1727
|
+
syntheticNamedExports: isEsModule || usesRequireWrapper ? false : '__moduleExports',
|
|
1728
|
+
meta: {
|
|
1729
|
+
commonjs: {
|
|
1730
|
+
isCommonJS: !isEsModule && (usesRequireWrapper ? IS_WRAPPED_COMMONJS : true),
|
|
1731
|
+
isMixedModule: isEsModule
|
|
1732
|
+
}
|
|
1733
|
+
}
|
|
1661
1734
|
};
|
|
1662
1735
|
}
|
|
1663
1736
|
|
|
1664
1737
|
function commonjs(options = {}) {
|
|
1665
|
-
const extensions = options.extensions || ['.js'];
|
|
1666
|
-
const filter = createFilter(options.include, options.exclude);
|
|
1667
1738
|
const {
|
|
1668
1739
|
ignoreGlobal,
|
|
1669
1740
|
ignoreDynamicRequires,
|
|
1670
1741
|
requireReturnsDefault: requireReturnsDefaultOption,
|
|
1671
1742
|
esmExternals
|
|
1672
1743
|
} = options;
|
|
1744
|
+
const extensions = options.extensions || ['.js'];
|
|
1745
|
+
const filter = createFilter(options.include, options.exclude);
|
|
1746
|
+
const { strictRequiresFilter, detectCyclesAndConditional } = getStrictRequiresFilter(options);
|
|
1747
|
+
|
|
1673
1748
|
const getRequireReturnsDefault =
|
|
1674
1749
|
typeof requireReturnsDefaultOption === 'function'
|
|
1675
1750
|
? requireReturnsDefaultOption
|
|
1676
1751
|
: () => requireReturnsDefaultOption;
|
|
1752
|
+
|
|
1677
1753
|
let esmExternalIds;
|
|
1678
1754
|
const isEsmExternal =
|
|
1679
1755
|
typeof esmExternals === 'function'
|
|
@@ -1681,20 +1757,27 @@ function commonjs(options = {}) {
|
|
|
1681
1757
|
: Array.isArray(esmExternals)
|
|
1682
1758
|
? ((esmExternalIds = new Set(esmExternals)), (id) => esmExternalIds.has(id))
|
|
1683
1759
|
: () => esmExternals;
|
|
1760
|
+
|
|
1684
1761
|
const defaultIsModuleExports =
|
|
1685
1762
|
typeof options.defaultIsModuleExports === 'boolean' ? options.defaultIsModuleExports : 'auto';
|
|
1686
1763
|
|
|
1687
|
-
const {
|
|
1688
|
-
|
|
1764
|
+
const {
|
|
1765
|
+
resolveRequireSourcesAndGetMeta,
|
|
1766
|
+
getWrappedIds,
|
|
1767
|
+
isRequiredId
|
|
1768
|
+
} = getResolveRequireSourcesAndGetMeta(extensions, detectCyclesAndConditional);
|
|
1769
|
+
const dynamicRequireRoot =
|
|
1770
|
+
typeof options.dynamicRequireRoot === 'string'
|
|
1771
|
+
? resolve(options.dynamicRequireRoot)
|
|
1772
|
+
: process.cwd();
|
|
1773
|
+
const { commonDir, dynamicRequireModules } = getDynamicRequireModules(
|
|
1774
|
+
options.dynamicRequireTargets,
|
|
1775
|
+
dynamicRequireRoot
|
|
1689
1776
|
);
|
|
1690
|
-
const isDynamicRequireModulesEnabled =
|
|
1691
|
-
const commonDir = isDynamicRequireModulesEnabled
|
|
1692
|
-
? getCommonDir(null, Array.from(dynamicRequireModuleSet).concat(process.cwd()))
|
|
1693
|
-
: null;
|
|
1777
|
+
const isDynamicRequireModulesEnabled = dynamicRequireModules.size > 0;
|
|
1694
1778
|
|
|
1695
1779
|
const esModulesWithDefaultExport = new Set();
|
|
1696
1780
|
const esModulesWithNamedExports = new Set();
|
|
1697
|
-
const commonJsMetaPromises = new Map();
|
|
1698
1781
|
|
|
1699
1782
|
const ignoreRequire =
|
|
1700
1783
|
typeof options.ignore === 'function'
|
|
@@ -1715,7 +1798,7 @@ function commonjs(options = {}) {
|
|
|
1715
1798
|
|
|
1716
1799
|
return {
|
|
1717
1800
|
canConvertRequire: mode !== 'remove' && mode !== true,
|
|
1718
|
-
|
|
1801
|
+
shouldRemoveRequire: mode === 'remove'
|
|
1719
1802
|
};
|
|
1720
1803
|
};
|
|
1721
1804
|
|
|
@@ -1724,12 +1807,6 @@ function commonjs(options = {}) {
|
|
|
1724
1807
|
const sourceMap = options.sourceMap !== false;
|
|
1725
1808
|
|
|
1726
1809
|
function transformAndCheckExports(code, id) {
|
|
1727
|
-
if (isDynamicRequireModulesEnabled && this.getModuleInfo(id).isEntry) {
|
|
1728
|
-
// eslint-disable-next-line no-param-reassign
|
|
1729
|
-
code =
|
|
1730
|
-
getDynamicPackagesEntryIntro(dynamicRequireModuleDirPaths, dynamicRequireModuleSet) + code;
|
|
1731
|
-
}
|
|
1732
|
-
|
|
1733
1810
|
const { isEsModule, hasDefaultExport, hasNamedExports, ast } = analyzeTopLevelStatements(
|
|
1734
1811
|
this.parse,
|
|
1735
1812
|
code,
|
|
@@ -1743,18 +1820,29 @@ function commonjs(options = {}) {
|
|
|
1743
1820
|
}
|
|
1744
1821
|
|
|
1745
1822
|
if (
|
|
1746
|
-
!
|
|
1747
|
-
(!hasCjsKeywords(code, ignoreGlobal) || (
|
|
1823
|
+
!dynamicRequireModules.has(normalizePathSlashes(id)) &&
|
|
1824
|
+
(!(hasCjsKeywords(code, ignoreGlobal) || isRequiredId(id)) ||
|
|
1825
|
+
(isEsModule && !options.transformMixedEsModules))
|
|
1748
1826
|
) {
|
|
1749
1827
|
return { meta: { commonjs: { isCommonJS: false } } };
|
|
1750
1828
|
}
|
|
1751
1829
|
|
|
1752
|
-
|
|
1753
|
-
|
|
1754
|
-
|
|
1755
|
-
|
|
1756
|
-
|
|
1757
|
-
|
|
1830
|
+
const needsRequireWrapper =
|
|
1831
|
+
!isEsModule &&
|
|
1832
|
+
(dynamicRequireModules.has(normalizePathSlashes(id)) || strictRequiresFilter(id));
|
|
1833
|
+
|
|
1834
|
+
const checkDynamicRequire = () => {
|
|
1835
|
+
if (id.indexOf(dynamicRequireRoot) !== 0) {
|
|
1836
|
+
this.error({
|
|
1837
|
+
code: 'DYNAMIC_REQUIRE_OUTSIDE_ROOT',
|
|
1838
|
+
id,
|
|
1839
|
+
dynamicRequireRoot,
|
|
1840
|
+
message: `"${id}" contains dynamic require statements but it is not within the current dynamicRequireRoot "${dynamicRequireRoot}". You should set dynamicRequireRoot to "${dirname(
|
|
1841
|
+
id
|
|
1842
|
+
)}" or one of its parent directories.`
|
|
1843
|
+
});
|
|
1844
|
+
}
|
|
1845
|
+
};
|
|
1758
1846
|
|
|
1759
1847
|
return transformCommonjs(
|
|
1760
1848
|
this.parse,
|
|
@@ -1767,17 +1855,37 @@ function commonjs(options = {}) {
|
|
|
1767
1855
|
getIgnoreTryCatchRequireStatementMode,
|
|
1768
1856
|
sourceMap,
|
|
1769
1857
|
isDynamicRequireModulesEnabled,
|
|
1770
|
-
|
|
1771
|
-
disableWrap,
|
|
1858
|
+
dynamicRequireModules,
|
|
1772
1859
|
commonDir,
|
|
1773
1860
|
ast,
|
|
1774
|
-
defaultIsModuleExports
|
|
1861
|
+
defaultIsModuleExports,
|
|
1862
|
+
needsRequireWrapper,
|
|
1863
|
+
resolveRequireSourcesAndGetMeta(this),
|
|
1864
|
+
isRequiredId(id),
|
|
1865
|
+
checkDynamicRequire
|
|
1775
1866
|
);
|
|
1776
1867
|
}
|
|
1777
1868
|
|
|
1778
1869
|
return {
|
|
1779
1870
|
name: 'commonjs',
|
|
1780
1871
|
|
|
1872
|
+
version,
|
|
1873
|
+
|
|
1874
|
+
options(rawOptions) {
|
|
1875
|
+
// We inject the resolver in the beginning so that "catch-all-resolver" like node-resolver
|
|
1876
|
+
// do not prevent our plugin from resolving entry points ot proxies.
|
|
1877
|
+
const plugins = Array.isArray(rawOptions.plugins)
|
|
1878
|
+
? rawOptions.plugins
|
|
1879
|
+
: rawOptions.plugins
|
|
1880
|
+
? [rawOptions.plugins]
|
|
1881
|
+
: [];
|
|
1882
|
+
plugins.unshift({
|
|
1883
|
+
name: 'commonjs--resolver',
|
|
1884
|
+
resolveId
|
|
1885
|
+
});
|
|
1886
|
+
return { ...rawOptions, plugins };
|
|
1887
|
+
},
|
|
1888
|
+
|
|
1781
1889
|
buildStart() {
|
|
1782
1890
|
validateRollupVersion(this.meta.rollupVersion, peerDependencies.rollup);
|
|
1783
1891
|
if (options.namedExports != null) {
|
|
@@ -1787,44 +1895,43 @@ function commonjs(options = {}) {
|
|
|
1787
1895
|
}
|
|
1788
1896
|
},
|
|
1789
1897
|
|
|
1790
|
-
|
|
1898
|
+
buildEnd() {
|
|
1899
|
+
if (options.strictRequires === 'debug') {
|
|
1900
|
+
const wrappedIds = getWrappedIds();
|
|
1901
|
+
if (wrappedIds.length) {
|
|
1902
|
+
this.warn({
|
|
1903
|
+
code: 'WRAPPED_IDS',
|
|
1904
|
+
ids: wrappedIds,
|
|
1905
|
+
message: `The commonjs plugin automatically wrapped the following files:\n[\n${wrappedIds
|
|
1906
|
+
.map((id) => `\t${JSON.stringify(relative(process.cwd(), id))}`)
|
|
1907
|
+
.join(',\n')}\n]`
|
|
1908
|
+
});
|
|
1909
|
+
} else {
|
|
1910
|
+
this.warn({
|
|
1911
|
+
code: 'WRAPPED_IDS',
|
|
1912
|
+
ids: wrappedIds,
|
|
1913
|
+
message: 'The commonjs plugin did not wrap any files.'
|
|
1914
|
+
});
|
|
1915
|
+
}
|
|
1916
|
+
}
|
|
1917
|
+
},
|
|
1791
1918
|
|
|
1792
1919
|
load(id) {
|
|
1793
1920
|
if (id === HELPERS_ID) {
|
|
1794
|
-
return getHelpersModule(
|
|
1795
|
-
}
|
|
1796
|
-
|
|
1797
|
-
if (id.startsWith(HELPERS_ID)) {
|
|
1798
|
-
return getSpecificHelperProxy(id);
|
|
1921
|
+
return getHelpersModule();
|
|
1799
1922
|
}
|
|
1800
1923
|
|
|
1801
1924
|
if (isWrappedId(id, MODULE_SUFFIX)) {
|
|
1802
|
-
const
|
|
1803
|
-
let name = getName(actualId);
|
|
1804
|
-
let code;
|
|
1805
|
-
if (isDynamicRequireModulesEnabled) {
|
|
1806
|
-
if (['modulePath', 'commonjsRequire', 'createModule'].includes(name)) {
|
|
1807
|
-
name = `${name}_`;
|
|
1808
|
-
}
|
|
1809
|
-
code =
|
|
1810
|
-
`import {commonjsRequire, createModule} from "${HELPERS_ID}";\n` +
|
|
1811
|
-
`var ${name} = createModule(${JSON.stringify(
|
|
1812
|
-
getVirtualPathForDynamicRequirePath(dirname(actualId), commonDir)
|
|
1813
|
-
)});\n` +
|
|
1814
|
-
`export {${name} as __module}`;
|
|
1815
|
-
} else {
|
|
1816
|
-
code = `var ${name} = {exports: {}}; export {${name} as __module}`;
|
|
1817
|
-
}
|
|
1925
|
+
const name = getName(unwrapId(id, MODULE_SUFFIX));
|
|
1818
1926
|
return {
|
|
1819
|
-
code
|
|
1927
|
+
code: `var ${name} = {exports: {}}; export {${name} as __module}`,
|
|
1820
1928
|
syntheticNamedExports: '__module',
|
|
1821
1929
|
meta: { commonjs: { isCommonJS: false } }
|
|
1822
1930
|
};
|
|
1823
1931
|
}
|
|
1824
1932
|
|
|
1825
1933
|
if (isWrappedId(id, EXPORTS_SUFFIX)) {
|
|
1826
|
-
const
|
|
1827
|
-
const name = getName(actualId);
|
|
1934
|
+
const name = getName(unwrapId(id, EXPORTS_SUFFIX));
|
|
1828
1935
|
return {
|
|
1829
1936
|
code: `var ${name} = {}; export {${name} as __exports}`,
|
|
1830
1937
|
meta: { commonjs: { isCommonJS: false } }
|
|
@@ -1839,22 +1946,16 @@ function commonjs(options = {}) {
|
|
|
1839
1946
|
);
|
|
1840
1947
|
}
|
|
1841
1948
|
|
|
1842
|
-
if (id
|
|
1843
|
-
return
|
|
1844
|
-
}
|
|
1845
|
-
|
|
1846
|
-
if (id.startsWith(DYNAMIC_JSON_PREFIX)) {
|
|
1847
|
-
return getDynamicJsonProxy(id, commonDir);
|
|
1848
|
-
}
|
|
1849
|
-
|
|
1850
|
-
if (isDynamicModuleImport(id, dynamicRequireModuleSet)) {
|
|
1851
|
-
return `export default require(${JSON.stringify(normalizePathSlashes(id))});`;
|
|
1949
|
+
if (isWrappedId(id, ES_IMPORT_SUFFIX)) {
|
|
1950
|
+
return getEsImportProxy(unwrapId(id, ES_IMPORT_SUFFIX), defaultIsModuleExports);
|
|
1852
1951
|
}
|
|
1853
1952
|
|
|
1854
|
-
if (
|
|
1855
|
-
return
|
|
1856
|
-
|
|
1857
|
-
|
|
1953
|
+
if (id === DYNAMIC_MODULES_ID) {
|
|
1954
|
+
return getDynamicModuleRegistry(
|
|
1955
|
+
isDynamicRequireModulesEnabled,
|
|
1956
|
+
dynamicRequireModules,
|
|
1957
|
+
commonDir,
|
|
1958
|
+
ignoreDynamicRequires
|
|
1858
1959
|
);
|
|
1859
1960
|
}
|
|
1860
1961
|
|
|
@@ -1865,43 +1966,24 @@ function commonjs(options = {}) {
|
|
|
1865
1966
|
getRequireReturnsDefault(actualId),
|
|
1866
1967
|
esModulesWithDefaultExport,
|
|
1867
1968
|
esModulesWithNamedExports,
|
|
1868
|
-
|
|
1969
|
+
this.load
|
|
1869
1970
|
);
|
|
1870
1971
|
}
|
|
1871
1972
|
|
|
1872
1973
|
return null;
|
|
1873
1974
|
},
|
|
1874
1975
|
|
|
1875
|
-
transform(code,
|
|
1876
|
-
let id = rawId;
|
|
1877
|
-
|
|
1878
|
-
if (isWrappedId(id, DYNAMIC_REGISTER_SUFFIX)) {
|
|
1879
|
-
id = unwrapId(id, DYNAMIC_REGISTER_SUFFIX);
|
|
1880
|
-
}
|
|
1881
|
-
|
|
1976
|
+
transform(code, id) {
|
|
1882
1977
|
const extName = extname(id);
|
|
1883
|
-
if (
|
|
1884
|
-
extName !== '.cjs' &&
|
|
1885
|
-
id !== DYNAMIC_PACKAGES_ID &&
|
|
1886
|
-
!id.startsWith(DYNAMIC_JSON_PREFIX) &&
|
|
1887
|
-
(!filter(id) || !extensions.includes(extName))
|
|
1888
|
-
) {
|
|
1978
|
+
if (extName !== '.cjs' && (!filter(id) || !extensions.includes(extName))) {
|
|
1889
1979
|
return null;
|
|
1890
1980
|
}
|
|
1891
1981
|
|
|
1892
1982
|
try {
|
|
1893
|
-
return transformAndCheckExports.call(this, code,
|
|
1983
|
+
return transformAndCheckExports.call(this, code, id);
|
|
1894
1984
|
} catch (err) {
|
|
1895
1985
|
return this.error(err, err.loc);
|
|
1896
1986
|
}
|
|
1897
|
-
},
|
|
1898
|
-
|
|
1899
|
-
moduleParsed({ id, meta: { commonjs: commonjsMeta } }) {
|
|
1900
|
-
if (commonjsMeta && commonjsMeta.isCommonJS != null) {
|
|
1901
|
-
setCommonJSMetaPromise(commonJsMetaPromises, id, commonjsMeta);
|
|
1902
|
-
return;
|
|
1903
|
-
}
|
|
1904
|
-
setCommonJSMetaPromise(commonJsMetaPromises, id, null);
|
|
1905
1987
|
}
|
|
1906
1988
|
};
|
|
1907
1989
|
}
|