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