@rollup/plugin-commonjs 21.0.1 → 22.0.0-3
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 +898 -795
- package/dist/index.es.js.map +1 -1
- package/dist/index.js +896 -793
- 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 = "21.0.1";
|
|
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
|
|
180
|
+
const modifyMap = (targetPath, resolvedPath) =>
|
|
181
|
+
isNegated
|
|
182
|
+
? dynamicRequireModules.delete(targetPath)
|
|
183
|
+
: dynamicRequireModules.set(targetPath, resolvedPath);
|
|
458
184
|
for (const path$1 of glob__default["default"].sync(isNegated ? pattern.substr(1) : pattern)) {
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
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
|
+
}`;
|
|
326
|
+
}
|
|
327
|
+
|
|
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;
|
|
497
382
|
}
|
|
383
|
+
`;
|
|
498
384
|
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
return `export {${id.split('?')[1]} as default} from "${HELPERS_ID}";`;
|
|
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,92 +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, resolveOptions) {
|
|
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
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
return { id: wrapId(importee, EXTERNAL_SUFFIX), external: false };
|
|
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;
|
|
661
619
|
}
|
|
662
|
-
|
|
663
|
-
|
|
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
|
+
}
|
|
664
670
|
};
|
|
665
671
|
}
|
|
666
672
|
|
|
@@ -820,8 +826,9 @@ function wrapCode(magicString, uses, moduleName, exportsName) {
|
|
|
820
826
|
}
|
|
821
827
|
magicString
|
|
822
828
|
.trim()
|
|
829
|
+
.indent('\t')
|
|
823
830
|
.prepend(`(function (${args.join(', ')}) {\n`)
|
|
824
|
-
.append(`\n}(${passedArgs.join(', ')}));`);
|
|
831
|
+
.append(`\n} (${passedArgs.join(', ')}));`);
|
|
825
832
|
}
|
|
826
833
|
|
|
827
834
|
function rewriteExportsAndGetExportsBlock(
|
|
@@ -839,12 +846,27 @@ function rewriteExportsAndGetExportsBlock(
|
|
|
839
846
|
HELPERS_NAME,
|
|
840
847
|
exportMode,
|
|
841
848
|
detectWrappedDefault,
|
|
842
|
-
defaultIsModuleExports
|
|
849
|
+
defaultIsModuleExports,
|
|
850
|
+
usesRequireWrapper,
|
|
851
|
+
requireName
|
|
843
852
|
) {
|
|
844
853
|
const exports = [];
|
|
845
854
|
const exportDeclarations = [];
|
|
846
855
|
|
|
847
|
-
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') {
|
|
848
870
|
getExportsForReplacedModuleExports(
|
|
849
871
|
magicString,
|
|
850
872
|
exports,
|
|
@@ -887,6 +909,49 @@ function rewriteExportsAndGetExportsBlock(
|
|
|
887
909
|
return `\n\n${exportDeclarations.join('\n')}`;
|
|
888
910
|
}
|
|
889
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
|
+
|
|
890
955
|
function getExportsForReplacedModuleExports(
|
|
891
956
|
magicString,
|
|
892
957
|
exports,
|
|
@@ -974,7 +1039,7 @@ function getExports(
|
|
|
974
1039
|
}
|
|
975
1040
|
|
|
976
1041
|
if (!isRestorableCompiledEsm || defaultIsModuleExports === true) {
|
|
977
|
-
|
|
1042
|
+
exports.push(`${exportsName} as default`);
|
|
978
1043
|
} else if (moduleExportsAssignments.length === 0 || defaultIsModuleExports === false) {
|
|
979
1044
|
exports.push(`${deconflictedDefaultExportName || exportsName} as default`);
|
|
980
1045
|
} else {
|
|
@@ -984,7 +1049,7 @@ function getExports(
|
|
|
984
1049
|
}
|
|
985
1050
|
}
|
|
986
1051
|
|
|
987
|
-
function
|
|
1052
|
+
function isRequireExpression(node, scope) {
|
|
988
1053
|
if (!node) return false;
|
|
989
1054
|
if (node.type !== 'CallExpression') return false;
|
|
990
1055
|
|
|
@@ -1011,11 +1076,6 @@ function isModuleRequire({ object, property }, scope) {
|
|
|
1011
1076
|
);
|
|
1012
1077
|
}
|
|
1013
1078
|
|
|
1014
|
-
function isStaticRequireStatement(node, scope) {
|
|
1015
|
-
if (!isRequireStatement(node, scope)) return false;
|
|
1016
|
-
return !hasDynamicArguments(node);
|
|
1017
|
-
}
|
|
1018
|
-
|
|
1019
1079
|
function hasDynamicArguments(node) {
|
|
1020
1080
|
return (
|
|
1021
1081
|
node.arguments.length > 1 ||
|
|
@@ -1030,89 +1090,58 @@ function isNodeRequirePropertyAccess(parent) {
|
|
|
1030
1090
|
return parent && parent.property && reservedMethod[parent.property.name];
|
|
1031
1091
|
}
|
|
1032
1092
|
|
|
1033
|
-
function isIgnoredRequireStatement(requiredNode, ignoreRequire) {
|
|
1034
|
-
return ignoreRequire(requiredNode.arguments[0].value);
|
|
1035
|
-
}
|
|
1036
|
-
|
|
1037
1093
|
function getRequireStringArg(node) {
|
|
1038
1094
|
return node.arguments[0].type === 'Literal'
|
|
1039
1095
|
? node.arguments[0].value
|
|
1040
1096
|
: node.arguments[0].quasis[0].value.cooked;
|
|
1041
1097
|
}
|
|
1042
1098
|
|
|
1043
|
-
function hasDynamicModuleForPath(source, id, dynamicRequireModuleSet) {
|
|
1044
|
-
if (!/^(?:\.{0,2}[/\\]|[A-Za-z]:[/\\])/.test(source)) {
|
|
1045
|
-
try {
|
|
1046
|
-
const resolvedPath = normalizePathSlashes(resolve.sync(source, { basedir: path.dirname(id) }));
|
|
1047
|
-
if (dynamicRequireModuleSet.has(resolvedPath)) {
|
|
1048
|
-
return true;
|
|
1049
|
-
}
|
|
1050
|
-
} catch (ex) {
|
|
1051
|
-
// Probably a node.js internal module
|
|
1052
|
-
return false;
|
|
1053
|
-
}
|
|
1054
|
-
|
|
1055
|
-
return false;
|
|
1056
|
-
}
|
|
1057
|
-
|
|
1058
|
-
for (const attemptExt of ['', '.js', '.json']) {
|
|
1059
|
-
const resolvedPath = normalizePathSlashes(path.resolve(path.dirname(id), source + attemptExt));
|
|
1060
|
-
if (dynamicRequireModuleSet.has(resolvedPath)) {
|
|
1061
|
-
return true;
|
|
1062
|
-
}
|
|
1063
|
-
}
|
|
1064
|
-
|
|
1065
|
-
return false;
|
|
1066
|
-
}
|
|
1067
|
-
|
|
1068
1099
|
function getRequireHandlers() {
|
|
1069
|
-
const
|
|
1070
|
-
|
|
1071
|
-
|
|
1072
|
-
|
|
1073
|
-
|
|
1074
|
-
|
|
1075
|
-
|
|
1076
|
-
|
|
1077
|
-
|
|
1078
|
-
|
|
1079
|
-
|
|
1080
|
-
|
|
1081
|
-
|
|
1082
|
-
|
|
1083
|
-
|
|
1084
|
-
|
|
1085
|
-
|
|
1086
|
-
|
|
1087
|
-
|
|
1088
|
-
|
|
1089
|
-
name: null,
|
|
1090
|
-
nodesUsingRequired: []
|
|
1091
|
-
};
|
|
1092
|
-
}
|
|
1093
|
-
|
|
1094
|
-
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
|
+
});
|
|
1095
1120
|
}
|
|
1096
1121
|
|
|
1097
|
-
function rewriteRequireExpressionsAndGetImportBlock(
|
|
1122
|
+
async function rewriteRequireExpressionsAndGetImportBlock(
|
|
1098
1123
|
magicString,
|
|
1099
1124
|
topLevelDeclarations,
|
|
1100
|
-
topLevelRequireDeclarators,
|
|
1101
1125
|
reassignedNames,
|
|
1102
1126
|
helpersName,
|
|
1103
|
-
|
|
1127
|
+
dynamicRequireName,
|
|
1104
1128
|
moduleName,
|
|
1105
1129
|
exportsName,
|
|
1106
1130
|
id,
|
|
1107
|
-
exportMode
|
|
1131
|
+
exportMode,
|
|
1132
|
+
resolveRequireSourcesAndGetMeta,
|
|
1133
|
+
needsRequireWrapper,
|
|
1134
|
+
isEsModule,
|
|
1135
|
+
usesRequire,
|
|
1136
|
+
getIgnoreTryCatchRequireStatementMode
|
|
1108
1137
|
) {
|
|
1109
|
-
setRemainingImportNamesAndRewriteRequires(
|
|
1110
|
-
requireExpressionsWithUsedReturnValue,
|
|
1111
|
-
requiredByNode,
|
|
1112
|
-
magicString
|
|
1113
|
-
);
|
|
1114
1138
|
const imports = [];
|
|
1115
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
|
+
}
|
|
1116
1145
|
if (exportMode === 'module') {
|
|
1117
1146
|
imports.push(
|
|
1118
1147
|
`import { __module as ${moduleName}, exports as ${exportsName} } from ${JSON.stringify(
|
|
@@ -1124,58 +1153,115 @@ function getRequireHandlers() {
|
|
|
1124
1153
|
`import { __exports as ${exportsName} } from ${JSON.stringify(wrapId(id, EXPORTS_SUFFIX))}`
|
|
1125
1154
|
);
|
|
1126
1155
|
}
|
|
1127
|
-
|
|
1128
|
-
|
|
1129
|
-
|
|
1130
|
-
|
|
1131
|
-
|
|
1132
|
-
|
|
1133
|
-
|
|
1134
|
-
|
|
1135
|
-
|
|
1136
|
-
|
|
1137
|
-
|
|
1138
|
-
|
|
1139
|
-
|
|
1140
|
-
|
|
1141
|
-
|
|
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
|
+
};
|
|
1142
1178
|
}
|
|
1143
1179
|
|
|
1144
1180
|
return {
|
|
1145
1181
|
addRequireStatement,
|
|
1146
|
-
requiredSources,
|
|
1147
1182
|
rewriteRequireExpressionsAndGetImportBlock
|
|
1148
1183
|
};
|
|
1149
1184
|
}
|
|
1150
1185
|
|
|
1151
|
-
function
|
|
1152
|
-
|
|
1153
|
-
|
|
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,
|
|
1154
1204
|
magicString
|
|
1155
1205
|
) {
|
|
1156
|
-
|
|
1157
|
-
for (const
|
|
1158
|
-
const
|
|
1159
|
-
|
|
1160
|
-
|
|
1161
|
-
|
|
1162
|
-
|
|
1163
|
-
|
|
1164
|
-
|
|
1165
|
-
|
|
1166
|
-
|
|
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
|
+
}
|
|
1167
1241
|
}
|
|
1168
|
-
magicString.overwrite(requireExpression.start, requireExpression.end, required.name);
|
|
1169
1242
|
}
|
|
1170
1243
|
}
|
|
1171
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
|
+
|
|
1172
1258
|
/* eslint-disable no-param-reassign, no-shadow, no-underscore-dangle, no-continue */
|
|
1173
1259
|
|
|
1174
1260
|
const exportsPattern = /^(?:module\.)?exports(?:\.([a-zA-Z_$][a-zA-Z_$0-9]*))?$/;
|
|
1175
1261
|
|
|
1176
1262
|
const functionType = /^(?:FunctionDeclaration|FunctionExpression|ArrowFunctionExpression)$/;
|
|
1177
1263
|
|
|
1178
|
-
function transformCommonjs(
|
|
1264
|
+
async function transformCommonjs(
|
|
1179
1265
|
parse,
|
|
1180
1266
|
code,
|
|
1181
1267
|
id,
|
|
@@ -1186,11 +1272,14 @@ function transformCommonjs(
|
|
|
1186
1272
|
getIgnoreTryCatchRequireStatementMode,
|
|
1187
1273
|
sourceMap,
|
|
1188
1274
|
isDynamicRequireModulesEnabled,
|
|
1189
|
-
|
|
1190
|
-
disableWrap,
|
|
1275
|
+
dynamicRequireModules,
|
|
1191
1276
|
commonDir,
|
|
1192
1277
|
astCache,
|
|
1193
|
-
defaultIsModuleExports
|
|
1278
|
+
defaultIsModuleExports,
|
|
1279
|
+
needsRequireWrapper,
|
|
1280
|
+
resolveRequireSourcesAndGetMeta,
|
|
1281
|
+
isRequired,
|
|
1282
|
+
checkDynamicRequire
|
|
1194
1283
|
) {
|
|
1195
1284
|
const ast = astCache || tryParse(parse, code, id);
|
|
1196
1285
|
const magicString = new MagicString__default["default"](code);
|
|
@@ -1200,7 +1289,6 @@ function transformCommonjs(
|
|
|
1200
1289
|
global: false,
|
|
1201
1290
|
require: false
|
|
1202
1291
|
};
|
|
1203
|
-
let usesDynamicRequire = false;
|
|
1204
1292
|
const virtualDynamicRequirePath =
|
|
1205
1293
|
isDynamicRequireModulesEnabled && getVirtualPathForDynamicRequirePath(path.dirname(id), commonDir);
|
|
1206
1294
|
let scope = pluginutils.attachScopes(ast, 'scope');
|
|
@@ -1210,24 +1298,18 @@ function transformCommonjs(
|
|
|
1210
1298
|
let shouldWrap = false;
|
|
1211
1299
|
|
|
1212
1300
|
const globals = new Set();
|
|
1213
|
-
|
|
1214
|
-
//
|
|
1215
|
-
|
|
1216
|
-
|
|
1217
|
-
|
|
1218
|
-
|
|
1219
|
-
const {
|
|
1220
|
-
addRequireStatement,
|
|
1221
|
-
requiredSources,
|
|
1222
|
-
rewriteRequireExpressionsAndGetImportBlock
|
|
1223
|
-
} = 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();
|
|
1224
1307
|
|
|
1225
1308
|
// See which names are assigned to. This is necessary to prevent
|
|
1226
1309
|
// illegally replacing `var foo = require('foo')` with `import foo from 'foo'`,
|
|
1227
1310
|
// where `foo` is later reassigned. (This happens in the wild. CommonJS, sigh)
|
|
1228
1311
|
const reassignedNames = new Set();
|
|
1229
1312
|
const topLevelDeclarations = [];
|
|
1230
|
-
const topLevelRequireDeclarators = new Set();
|
|
1231
1313
|
const skippedNodes = new Set();
|
|
1232
1314
|
const moduleAccessScopes = new Set([scope]);
|
|
1233
1315
|
const exportsAccessScopes = new Set([scope]);
|
|
@@ -1236,6 +1318,8 @@ function transformCommonjs(
|
|
|
1236
1318
|
const exportsAssignmentsByName = new Map();
|
|
1237
1319
|
const topLevelAssignments = new Set();
|
|
1238
1320
|
const topLevelDefineCompiledEsmExpressions = [];
|
|
1321
|
+
const replacedGlobal = [];
|
|
1322
|
+
const replacedDynamicRequires = [];
|
|
1239
1323
|
|
|
1240
1324
|
estreeWalker.walk(ast, {
|
|
1241
1325
|
enter(node, parent) {
|
|
@@ -1247,6 +1331,12 @@ function transformCommonjs(
|
|
|
1247
1331
|
if (currentTryBlockEnd !== null && node.start > currentTryBlockEnd) {
|
|
1248
1332
|
currentTryBlockEnd = null;
|
|
1249
1333
|
}
|
|
1334
|
+
if (currentConditionalNodeEnd !== null && node.start > currentConditionalNodeEnd) {
|
|
1335
|
+
currentConditionalNodeEnd = null;
|
|
1336
|
+
}
|
|
1337
|
+
if (currentConditionalNodeEnd === null && conditionalNodes.has(node)) {
|
|
1338
|
+
currentConditionalNodeEnd = node.end;
|
|
1339
|
+
}
|
|
1250
1340
|
|
|
1251
1341
|
programDepth += 1;
|
|
1252
1342
|
if (node.scope) ({ scope } = node);
|
|
@@ -1258,11 +1348,6 @@ function transformCommonjs(
|
|
|
1258
1348
|
|
|
1259
1349
|
// eslint-disable-next-line default-case
|
|
1260
1350
|
switch (node.type) {
|
|
1261
|
-
case 'TryStatement':
|
|
1262
|
-
if (currentTryBlockEnd === null) {
|
|
1263
|
-
currentTryBlockEnd = node.block.end;
|
|
1264
|
-
}
|
|
1265
|
-
return;
|
|
1266
1351
|
case 'AssignmentExpression':
|
|
1267
1352
|
if (node.left.type === 'MemberExpression') {
|
|
1268
1353
|
const flattened = getKeypath(node.left);
|
|
@@ -1333,12 +1418,15 @@ function transformCommonjs(
|
|
|
1333
1418
|
return;
|
|
1334
1419
|
}
|
|
1335
1420
|
|
|
1421
|
+
// Transform require.resolve
|
|
1336
1422
|
if (
|
|
1423
|
+
isDynamicRequireModulesEnabled &&
|
|
1337
1424
|
node.callee.object &&
|
|
1338
|
-
node.callee.object
|
|
1339
|
-
node.callee.property.name === 'resolve'
|
|
1340
|
-
hasDynamicModuleForPath(id, '/', dynamicRequireModuleSet)
|
|
1425
|
+
isRequire(node.callee.object, scope) &&
|
|
1426
|
+
node.callee.property.name === 'resolve'
|
|
1341
1427
|
) {
|
|
1428
|
+
checkDynamicRequire(node.start);
|
|
1429
|
+
uses.require = true;
|
|
1342
1430
|
const requireNode = node.callee.object;
|
|
1343
1431
|
magicString.appendLeft(
|
|
1344
1432
|
node.end - 1,
|
|
@@ -1346,98 +1434,45 @@ function transformCommonjs(
|
|
|
1346
1434
|
path.dirname(id) === '.' ? null /* default behavior */ : virtualDynamicRequirePath
|
|
1347
1435
|
)}`
|
|
1348
1436
|
);
|
|
1349
|
-
|
|
1350
|
-
requireNode.start,
|
|
1351
|
-
requireNode.end,
|
|
1352
|
-
`${HELPERS_NAME}.commonjsRequire`,
|
|
1353
|
-
{
|
|
1354
|
-
storeName: true
|
|
1355
|
-
}
|
|
1356
|
-
);
|
|
1437
|
+
replacedDynamicRequires.push(requireNode);
|
|
1357
1438
|
return;
|
|
1358
1439
|
}
|
|
1359
1440
|
|
|
1360
|
-
if (!
|
|
1361
|
-
|
|
1362
|
-
skippedNodes.add(node.callee);
|
|
1441
|
+
if (!isRequireExpression(node, scope)) {
|
|
1442
|
+
return;
|
|
1363
1443
|
}
|
|
1364
|
-
if (!isIgnoredRequireStatement(node, ignoreRequire)) {
|
|
1365
|
-
skippedNodes.add(node.callee);
|
|
1366
|
-
const usesReturnValue = parent.type !== 'ExpressionStatement';
|
|
1367
|
-
|
|
1368
|
-
let canConvertRequire = true;
|
|
1369
|
-
let shouldRemoveRequireStatement = false;
|
|
1370
|
-
|
|
1371
|
-
if (currentTryBlockEnd !== null) {
|
|
1372
|
-
({
|
|
1373
|
-
canConvertRequire,
|
|
1374
|
-
shouldRemoveRequireStatement
|
|
1375
|
-
} = getIgnoreTryCatchRequireStatementMode(node.arguments[0].value));
|
|
1376
1444
|
|
|
1377
|
-
|
|
1378
|
-
|
|
1379
|
-
|
|
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
|
+
);
|
|
1380
1457
|
}
|
|
1381
|
-
|
|
1382
|
-
|
|
1383
|
-
const isDynamicRegister = isWrappedId(sourceId, DYNAMIC_REGISTER_SUFFIX);
|
|
1384
|
-
if (isDynamicRegister) {
|
|
1385
|
-
sourceId = unwrapId(sourceId, DYNAMIC_REGISTER_SUFFIX);
|
|
1386
|
-
if (sourceId.endsWith('.json')) {
|
|
1387
|
-
sourceId = DYNAMIC_JSON_PREFIX + sourceId;
|
|
1388
|
-
}
|
|
1389
|
-
dynamicRegisterSources.add(wrapId(sourceId, DYNAMIC_REGISTER_SUFFIX));
|
|
1390
|
-
} else {
|
|
1391
|
-
if (
|
|
1392
|
-
!sourceId.endsWith('.json') &&
|
|
1393
|
-
hasDynamicModuleForPath(sourceId, id, dynamicRequireModuleSet)
|
|
1394
|
-
) {
|
|
1395
|
-
if (shouldRemoveRequireStatement) {
|
|
1396
|
-
magicString.overwrite(node.start, node.end, `undefined`);
|
|
1397
|
-
} else if (canConvertRequire) {
|
|
1398
|
-
magicString.overwrite(
|
|
1399
|
-
node.start,
|
|
1400
|
-
node.end,
|
|
1401
|
-
`${HELPERS_NAME}.commonjsRequire(${JSON.stringify(
|
|
1402
|
-
getVirtualPathForDynamicRequirePath(sourceId, commonDir)
|
|
1403
|
-
)}, ${JSON.stringify(
|
|
1404
|
-
path.dirname(id) === '.' ? null /* default behavior */ : virtualDynamicRequirePath
|
|
1405
|
-
)})`
|
|
1406
|
-
);
|
|
1407
|
-
usesDynamicRequire = true;
|
|
1408
|
-
}
|
|
1409
|
-
return;
|
|
1410
|
-
}
|
|
1411
|
-
|
|
1412
|
-
if (canConvertRequire) {
|
|
1413
|
-
addRequireStatement(sourceId, node, scope, usesReturnValue);
|
|
1414
|
-
}
|
|
1458
|
+
if (!ignoreDynamicRequires) {
|
|
1459
|
+
replacedDynamicRequires.push(node.callee);
|
|
1415
1460
|
}
|
|
1461
|
+
return;
|
|
1462
|
+
}
|
|
1416
1463
|
|
|
1417
|
-
|
|
1418
|
-
|
|
1419
|
-
|
|
1420
|
-
|
|
1421
|
-
|
|
1422
|
-
|
|
1423
|
-
|
|
1424
|
-
|
|
1425
|
-
|
|
1426
|
-
|
|
1427
|
-
|
|
1428
|
-
|
|
1429
|
-
// and does not conflict with variables in other places where this is imported
|
|
1430
|
-
topLevelRequireDeclarators.add(parent);
|
|
1431
|
-
}
|
|
1432
|
-
} else {
|
|
1433
|
-
// This is a bare import, e.g. `require('foo');`
|
|
1434
|
-
|
|
1435
|
-
if (!canConvertRequire && !shouldRemoveRequireStatement) {
|
|
1436
|
-
return;
|
|
1437
|
-
}
|
|
1438
|
-
|
|
1439
|
-
magicString.remove(parent.start, parent.end);
|
|
1440
|
-
}
|
|
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
|
+
);
|
|
1441
1476
|
}
|
|
1442
1477
|
return;
|
|
1443
1478
|
}
|
|
@@ -1446,45 +1481,43 @@ function transformCommonjs(
|
|
|
1446
1481
|
// skip dead branches
|
|
1447
1482
|
if (isFalsy(node.test)) {
|
|
1448
1483
|
skippedNodes.add(node.consequent);
|
|
1449
|
-
} else if (
|
|
1450
|
-
|
|
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;
|
|
1451
1504
|
}
|
|
1452
1505
|
return;
|
|
1453
1506
|
case 'Identifier': {
|
|
1454
1507
|
const { name } = node;
|
|
1455
|
-
if (!
|
|
1508
|
+
if (!isReference__default["default"](node, parent) || scope.contains(name)) return;
|
|
1456
1509
|
switch (name) {
|
|
1457
1510
|
case 'require':
|
|
1511
|
+
uses.require = true;
|
|
1458
1512
|
if (isNodeRequirePropertyAccess(parent)) {
|
|
1459
|
-
if (hasDynamicModuleForPath(id, '/', dynamicRequireModuleSet)) {
|
|
1460
|
-
if (parent.property.name === 'cache') {
|
|
1461
|
-
magicString.overwrite(node.start, node.end, `${HELPERS_NAME}.commonjsRequire`, {
|
|
1462
|
-
storeName: true
|
|
1463
|
-
});
|
|
1464
|
-
}
|
|
1465
|
-
}
|
|
1466
|
-
|
|
1467
1513
|
return;
|
|
1468
1514
|
}
|
|
1469
|
-
|
|
1470
|
-
if (isDynamicRequireModulesEnabled && isRequireStatement(parent, scope)) {
|
|
1471
|
-
magicString.appendLeft(
|
|
1472
|
-
parent.end - 1,
|
|
1473
|
-
`,${JSON.stringify(
|
|
1474
|
-
path.dirname(id) === '.' ? null /* default behavior */ : virtualDynamicRequirePath
|
|
1475
|
-
)}`
|
|
1476
|
-
);
|
|
1477
|
-
}
|
|
1478
1515
|
if (!ignoreDynamicRequires) {
|
|
1479
1516
|
if (isShorthandProperty(parent)) {
|
|
1480
|
-
magicString.
|
|
1481
|
-
} else {
|
|
1482
|
-
magicString.overwrite(node.start, node.end, `${HELPERS_NAME}.commonjsRequire`, {
|
|
1483
|
-
storeName: true
|
|
1484
|
-
});
|
|
1517
|
+
magicString.prependRight(node.start, 'require: ');
|
|
1485
1518
|
}
|
|
1519
|
+
replacedDynamicRequires.push(node);
|
|
1486
1520
|
}
|
|
1487
|
-
usesDynamicRequire = true;
|
|
1488
1521
|
return;
|
|
1489
1522
|
case 'module':
|
|
1490
1523
|
case 'exports':
|
|
@@ -1494,9 +1527,7 @@ function transformCommonjs(
|
|
|
1494
1527
|
case 'global':
|
|
1495
1528
|
uses.global = true;
|
|
1496
1529
|
if (!ignoreGlobal) {
|
|
1497
|
-
|
|
1498
|
-
storeName: true
|
|
1499
|
-
});
|
|
1530
|
+
replacedGlobal.push(node);
|
|
1500
1531
|
}
|
|
1501
1532
|
return;
|
|
1502
1533
|
case 'define':
|
|
@@ -1509,11 +1540,26 @@ function transformCommonjs(
|
|
|
1509
1540
|
return;
|
|
1510
1541
|
}
|
|
1511
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;
|
|
1512
1559
|
case 'MemberExpression':
|
|
1513
1560
|
if (!isDynamicRequireModulesEnabled && isModuleRequire(node, scope)) {
|
|
1514
|
-
|
|
1515
|
-
|
|
1516
|
-
});
|
|
1561
|
+
uses.require = true;
|
|
1562
|
+
replacedDynamicRequires.push(node);
|
|
1517
1563
|
skippedNodes.add(node.object);
|
|
1518
1564
|
skippedNodes.add(node.property);
|
|
1519
1565
|
}
|
|
@@ -1529,12 +1575,18 @@ function transformCommonjs(
|
|
|
1529
1575
|
if (lexicalDepth === 0) {
|
|
1530
1576
|
uses.global = true;
|
|
1531
1577
|
if (!ignoreGlobal) {
|
|
1532
|
-
|
|
1533
|
-
storeName: true
|
|
1534
|
-
});
|
|
1578
|
+
replacedGlobal.push(node);
|
|
1535
1579
|
}
|
|
1536
1580
|
}
|
|
1537
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;
|
|
1538
1590
|
case 'UnaryExpression':
|
|
1539
1591
|
// rewrite `typeof module`, `typeof module.exports` and `typeof exports` (https://github.com/rollup/rollup-plugin-commonjs/issues/151)
|
|
1540
1592
|
if (node.operator === 'typeof') {
|
|
@@ -1544,9 +1596,10 @@ function transformCommonjs(
|
|
|
1544
1596
|
if (scope.contains(flattened.name)) return;
|
|
1545
1597
|
|
|
1546
1598
|
if (
|
|
1547
|
-
|
|
1548
|
-
flattened.keypath === 'module' ||
|
|
1549
|
-
|
|
1599
|
+
!isEsModule &&
|
|
1600
|
+
(flattened.keypath === 'module.exports' ||
|
|
1601
|
+
flattened.keypath === 'module' ||
|
|
1602
|
+
flattened.keypath === 'exports')
|
|
1550
1603
|
) {
|
|
1551
1604
|
magicString.overwrite(node.start, node.end, `'object'`, {
|
|
1552
1605
|
storeName: false
|
|
@@ -1571,34 +1624,45 @@ function transformCommonjs(
|
|
|
1571
1624
|
const nameBase = getName(id);
|
|
1572
1625
|
const exportsName = deconflict([...exportsAccessScopes], globals, nameBase);
|
|
1573
1626
|
const moduleName = deconflict([...moduleAccessScopes], globals, `${nameBase}Module`);
|
|
1627
|
+
const requireName = deconflict([scope], globals, `require${capitalize(nameBase)}`);
|
|
1628
|
+
const isRequiredName = deconflict([scope], globals, `hasRequired${capitalize(nameBase)}`);
|
|
1629
|
+
const helpersName = deconflict([scope], globals, 'commonjsHelpers');
|
|
1630
|
+
const dynamicRequireName = deconflict([scope], globals, 'commonjsRequire');
|
|
1574
1631
|
const deconflictedExportNames = Object.create(null);
|
|
1575
1632
|
for (const [exportName, { scopes }] of exportsAssignmentsByName) {
|
|
1576
1633
|
deconflictedExportNames[exportName] = deconflict([...scopes], globals, exportName);
|
|
1577
1634
|
}
|
|
1578
1635
|
|
|
1636
|
+
for (const node of replacedGlobal) {
|
|
1637
|
+
magicString.overwrite(node.start, node.end, `${helpersName}.commonjsGlobal`, {
|
|
1638
|
+
storeName: true
|
|
1639
|
+
});
|
|
1640
|
+
}
|
|
1641
|
+
for (const node of replacedDynamicRequires) {
|
|
1642
|
+
magicString.overwrite(node.start, node.end, dynamicRequireName, {
|
|
1643
|
+
contentOnly: true,
|
|
1644
|
+
storeName: true
|
|
1645
|
+
});
|
|
1646
|
+
}
|
|
1647
|
+
|
|
1579
1648
|
// We cannot wrap ES/mixed modules
|
|
1580
|
-
shouldWrap =
|
|
1581
|
-
!isEsModule &&
|
|
1582
|
-
!disableWrap &&
|
|
1583
|
-
(shouldWrap || (uses.exports && moduleExportsAssignments.length > 0));
|
|
1649
|
+
shouldWrap = !isEsModule && (shouldWrap || (uses.exports && moduleExportsAssignments.length > 0));
|
|
1584
1650
|
const detectWrappedDefault =
|
|
1585
1651
|
shouldWrap &&
|
|
1586
1652
|
(topLevelDefineCompiledEsmExpressions.length > 0 || code.indexOf('__esModule') >= 0);
|
|
1587
1653
|
|
|
1588
1654
|
if (
|
|
1589
1655
|
!(
|
|
1590
|
-
|
|
1591
|
-
|
|
1656
|
+
shouldWrap ||
|
|
1657
|
+
isRequired ||
|
|
1592
1658
|
uses.module ||
|
|
1593
1659
|
uses.exports ||
|
|
1594
1660
|
uses.require ||
|
|
1595
|
-
usesDynamicRequire ||
|
|
1596
|
-
hasRemovedRequire ||
|
|
1597
1661
|
topLevelDefineCompiledEsmExpressions.length > 0
|
|
1598
1662
|
) &&
|
|
1599
1663
|
(ignoreGlobal || !uses.global)
|
|
1600
1664
|
) {
|
|
1601
|
-
return { meta: { commonjs: { isCommonJS: false } } };
|
|
1665
|
+
return { meta: { commonjs: { isCommonJS: false, isMixedModule: false } } };
|
|
1602
1666
|
}
|
|
1603
1667
|
|
|
1604
1668
|
let leadingComment = '';
|
|
@@ -1620,19 +1684,22 @@ function transformCommonjs(
|
|
|
1620
1684
|
? 'exports'
|
|
1621
1685
|
: 'module';
|
|
1622
1686
|
|
|
1623
|
-
const importBlock = rewriteRequireExpressionsAndGetImportBlock(
|
|
1687
|
+
const { importBlock, usesRequireWrapper } = await rewriteRequireExpressionsAndGetImportBlock(
|
|
1624
1688
|
magicString,
|
|
1625
1689
|
topLevelDeclarations,
|
|
1626
|
-
topLevelRequireDeclarators,
|
|
1627
1690
|
reassignedNames,
|
|
1628
|
-
|
|
1629
|
-
|
|
1691
|
+
helpersName,
|
|
1692
|
+
dynamicRequireName,
|
|
1630
1693
|
moduleName,
|
|
1631
1694
|
exportsName,
|
|
1632
1695
|
id,
|
|
1633
|
-
exportMode
|
|
1696
|
+
exportMode,
|
|
1697
|
+
resolveRequireSourcesAndGetMeta,
|
|
1698
|
+
needsRequireWrapper,
|
|
1699
|
+
isEsModule,
|
|
1700
|
+
uses.require,
|
|
1701
|
+
getIgnoreTryCatchRequireStatementMode
|
|
1634
1702
|
);
|
|
1635
|
-
|
|
1636
1703
|
const exportBlock = isEsModule
|
|
1637
1704
|
? ''
|
|
1638
1705
|
: rewriteExportsAndGetExportsBlock(
|
|
@@ -1647,16 +1714,35 @@ function transformCommonjs(
|
|
|
1647
1714
|
topLevelDefineCompiledEsmExpressions,
|
|
1648
1715
|
deconflictedExportNames,
|
|
1649
1716
|
code,
|
|
1650
|
-
|
|
1717
|
+
helpersName,
|
|
1651
1718
|
exportMode,
|
|
1652
1719
|
detectWrappedDefault,
|
|
1653
|
-
defaultIsModuleExports
|
|
1720
|
+
defaultIsModuleExports,
|
|
1721
|
+
usesRequireWrapper,
|
|
1722
|
+
requireName
|
|
1654
1723
|
);
|
|
1655
1724
|
|
|
1656
1725
|
if (shouldWrap) {
|
|
1657
1726
|
wrapCode(magicString, uses, moduleName, exportsName);
|
|
1658
1727
|
}
|
|
1659
1728
|
|
|
1729
|
+
if (usesRequireWrapper) {
|
|
1730
|
+
magicString.trim().indent('\t');
|
|
1731
|
+
magicString.prepend(
|
|
1732
|
+
`var ${isRequiredName};
|
|
1733
|
+
|
|
1734
|
+
function ${requireName} () {
|
|
1735
|
+
\tif (${isRequiredName}) return ${exportsName};
|
|
1736
|
+
\t${isRequiredName} = 1;
|
|
1737
|
+
`
|
|
1738
|
+
).append(`
|
|
1739
|
+
\treturn ${exportsName};
|
|
1740
|
+
}`);
|
|
1741
|
+
if (exportMode === 'replace') {
|
|
1742
|
+
magicString.prepend(`var ${exportsName};\n`);
|
|
1743
|
+
}
|
|
1744
|
+
}
|
|
1745
|
+
|
|
1660
1746
|
magicString
|
|
1661
1747
|
.trim()
|
|
1662
1748
|
.prepend(leadingComment + importBlock)
|
|
@@ -1665,24 +1751,32 @@ function transformCommonjs(
|
|
|
1665
1751
|
return {
|
|
1666
1752
|
code: magicString.toString(),
|
|
1667
1753
|
map: sourceMap ? magicString.generateMap() : null,
|
|
1668
|
-
syntheticNamedExports: isEsModule ? false : '__moduleExports',
|
|
1669
|
-
meta: {
|
|
1754
|
+
syntheticNamedExports: isEsModule || usesRequireWrapper ? false : '__moduleExports',
|
|
1755
|
+
meta: {
|
|
1756
|
+
commonjs: {
|
|
1757
|
+
isCommonJS: !isEsModule && (usesRequireWrapper ? IS_WRAPPED_COMMONJS : true),
|
|
1758
|
+
isMixedModule: isEsModule
|
|
1759
|
+
}
|
|
1760
|
+
}
|
|
1670
1761
|
};
|
|
1671
1762
|
}
|
|
1672
1763
|
|
|
1673
1764
|
function commonjs(options = {}) {
|
|
1674
|
-
const extensions = options.extensions || ['.js'];
|
|
1675
|
-
const filter = pluginutils.createFilter(options.include, options.exclude);
|
|
1676
1765
|
const {
|
|
1677
1766
|
ignoreGlobal,
|
|
1678
1767
|
ignoreDynamicRequires,
|
|
1679
1768
|
requireReturnsDefault: requireReturnsDefaultOption,
|
|
1680
1769
|
esmExternals
|
|
1681
1770
|
} = options;
|
|
1771
|
+
const extensions = options.extensions || ['.js'];
|
|
1772
|
+
const filter = pluginutils.createFilter(options.include, options.exclude);
|
|
1773
|
+
const { strictRequiresFilter, detectCyclesAndConditional } = getStrictRequiresFilter(options);
|
|
1774
|
+
|
|
1682
1775
|
const getRequireReturnsDefault =
|
|
1683
1776
|
typeof requireReturnsDefaultOption === 'function'
|
|
1684
1777
|
? requireReturnsDefaultOption
|
|
1685
1778
|
: () => requireReturnsDefaultOption;
|
|
1779
|
+
|
|
1686
1780
|
let esmExternalIds;
|
|
1687
1781
|
const isEsmExternal =
|
|
1688
1782
|
typeof esmExternals === 'function'
|
|
@@ -1690,20 +1784,27 @@ function commonjs(options = {}) {
|
|
|
1690
1784
|
: Array.isArray(esmExternals)
|
|
1691
1785
|
? ((esmExternalIds = new Set(esmExternals)), (id) => esmExternalIds.has(id))
|
|
1692
1786
|
: () => esmExternals;
|
|
1787
|
+
|
|
1693
1788
|
const defaultIsModuleExports =
|
|
1694
1789
|
typeof options.defaultIsModuleExports === 'boolean' ? options.defaultIsModuleExports : 'auto';
|
|
1695
1790
|
|
|
1696
|
-
const {
|
|
1697
|
-
|
|
1791
|
+
const {
|
|
1792
|
+
resolveRequireSourcesAndGetMeta,
|
|
1793
|
+
getWrappedIds,
|
|
1794
|
+
isRequiredId
|
|
1795
|
+
} = getResolveRequireSourcesAndGetMeta(extensions, detectCyclesAndConditional);
|
|
1796
|
+
const dynamicRequireRoot =
|
|
1797
|
+
typeof options.dynamicRequireRoot === 'string'
|
|
1798
|
+
? path.resolve(options.dynamicRequireRoot)
|
|
1799
|
+
: process.cwd();
|
|
1800
|
+
const { commonDir, dynamicRequireModules } = getDynamicRequireModules(
|
|
1801
|
+
options.dynamicRequireTargets,
|
|
1802
|
+
dynamicRequireRoot
|
|
1698
1803
|
);
|
|
1699
|
-
const isDynamicRequireModulesEnabled =
|
|
1700
|
-
const commonDir = isDynamicRequireModulesEnabled
|
|
1701
|
-
? getCommonDir__default["default"](null, Array.from(dynamicRequireModuleSet).concat(process.cwd()))
|
|
1702
|
-
: null;
|
|
1804
|
+
const isDynamicRequireModulesEnabled = dynamicRequireModules.size > 0;
|
|
1703
1805
|
|
|
1704
1806
|
const esModulesWithDefaultExport = new Set();
|
|
1705
1807
|
const esModulesWithNamedExports = new Set();
|
|
1706
|
-
const commonJsMetaPromises = new Map();
|
|
1707
1808
|
|
|
1708
1809
|
const ignoreRequire =
|
|
1709
1810
|
typeof options.ignore === 'function'
|
|
@@ -1724,7 +1825,7 @@ function commonjs(options = {}) {
|
|
|
1724
1825
|
|
|
1725
1826
|
return {
|
|
1726
1827
|
canConvertRequire: mode !== 'remove' && mode !== true,
|
|
1727
|
-
|
|
1828
|
+
shouldRemoveRequire: mode === 'remove'
|
|
1728
1829
|
};
|
|
1729
1830
|
};
|
|
1730
1831
|
|
|
@@ -1733,12 +1834,6 @@ function commonjs(options = {}) {
|
|
|
1733
1834
|
const sourceMap = options.sourceMap !== false;
|
|
1734
1835
|
|
|
1735
1836
|
function transformAndCheckExports(code, id) {
|
|
1736
|
-
if (isDynamicRequireModulesEnabled && this.getModuleInfo(id).isEntry) {
|
|
1737
|
-
// eslint-disable-next-line no-param-reassign
|
|
1738
|
-
code =
|
|
1739
|
-
getDynamicPackagesEntryIntro(dynamicRequireModuleDirPaths, dynamicRequireModuleSet) + code;
|
|
1740
|
-
}
|
|
1741
|
-
|
|
1742
1837
|
const { isEsModule, hasDefaultExport, hasNamedExports, ast } = analyzeTopLevelStatements(
|
|
1743
1838
|
this.parse,
|
|
1744
1839
|
code,
|
|
@@ -1752,18 +1847,32 @@ function commonjs(options = {}) {
|
|
|
1752
1847
|
}
|
|
1753
1848
|
|
|
1754
1849
|
if (
|
|
1755
|
-
!
|
|
1756
|
-
(!hasCjsKeywords(code, ignoreGlobal) || (
|
|
1850
|
+
!dynamicRequireModules.has(normalizePathSlashes(id)) &&
|
|
1851
|
+
(!(hasCjsKeywords(code, ignoreGlobal) || isRequiredId(id)) ||
|
|
1852
|
+
(isEsModule && !options.transformMixedEsModules))
|
|
1757
1853
|
) {
|
|
1758
1854
|
return { meta: { commonjs: { isCommonJS: false } } };
|
|
1759
1855
|
}
|
|
1760
1856
|
|
|
1761
|
-
|
|
1762
|
-
|
|
1763
|
-
|
|
1764
|
-
|
|
1765
|
-
|
|
1766
|
-
|
|
1857
|
+
const needsRequireWrapper =
|
|
1858
|
+
!isEsModule &&
|
|
1859
|
+
(dynamicRequireModules.has(normalizePathSlashes(id)) || strictRequiresFilter(id));
|
|
1860
|
+
|
|
1861
|
+
const checkDynamicRequire = (position) => {
|
|
1862
|
+
if (id.indexOf(dynamicRequireRoot) !== 0) {
|
|
1863
|
+
this.error(
|
|
1864
|
+
{
|
|
1865
|
+
code: 'DYNAMIC_REQUIRE_OUTSIDE_ROOT',
|
|
1866
|
+
id,
|
|
1867
|
+
dynamicRequireRoot,
|
|
1868
|
+
message: `"${id}" contains dynamic require statements but it is not within the current dynamicRequireRoot "${dynamicRequireRoot}". You should set dynamicRequireRoot to "${path.dirname(
|
|
1869
|
+
id
|
|
1870
|
+
)}" or one of its parent directories.`
|
|
1871
|
+
},
|
|
1872
|
+
position
|
|
1873
|
+
);
|
|
1874
|
+
}
|
|
1875
|
+
};
|
|
1767
1876
|
|
|
1768
1877
|
return transformCommonjs(
|
|
1769
1878
|
this.parse,
|
|
@@ -1776,17 +1885,37 @@ function commonjs(options = {}) {
|
|
|
1776
1885
|
getIgnoreTryCatchRequireStatementMode,
|
|
1777
1886
|
sourceMap,
|
|
1778
1887
|
isDynamicRequireModulesEnabled,
|
|
1779
|
-
|
|
1780
|
-
disableWrap,
|
|
1888
|
+
dynamicRequireModules,
|
|
1781
1889
|
commonDir,
|
|
1782
1890
|
ast,
|
|
1783
|
-
defaultIsModuleExports
|
|
1891
|
+
defaultIsModuleExports,
|
|
1892
|
+
needsRequireWrapper,
|
|
1893
|
+
resolveRequireSourcesAndGetMeta(this),
|
|
1894
|
+
isRequiredId(id),
|
|
1895
|
+
checkDynamicRequire
|
|
1784
1896
|
);
|
|
1785
1897
|
}
|
|
1786
1898
|
|
|
1787
1899
|
return {
|
|
1788
1900
|
name: 'commonjs',
|
|
1789
1901
|
|
|
1902
|
+
version,
|
|
1903
|
+
|
|
1904
|
+
options(rawOptions) {
|
|
1905
|
+
// We inject the resolver in the beginning so that "catch-all-resolver" like node-resolver
|
|
1906
|
+
// do not prevent our plugin from resolving entry points ot proxies.
|
|
1907
|
+
const plugins = Array.isArray(rawOptions.plugins)
|
|
1908
|
+
? rawOptions.plugins
|
|
1909
|
+
: rawOptions.plugins
|
|
1910
|
+
? [rawOptions.plugins]
|
|
1911
|
+
: [];
|
|
1912
|
+
plugins.unshift({
|
|
1913
|
+
name: 'commonjs--resolver',
|
|
1914
|
+
resolveId
|
|
1915
|
+
});
|
|
1916
|
+
return { ...rawOptions, plugins };
|
|
1917
|
+
},
|
|
1918
|
+
|
|
1790
1919
|
buildStart() {
|
|
1791
1920
|
validateRollupVersion(this.meta.rollupVersion, peerDependencies.rollup);
|
|
1792
1921
|
if (options.namedExports != null) {
|
|
@@ -1796,44 +1925,43 @@ function commonjs(options = {}) {
|
|
|
1796
1925
|
}
|
|
1797
1926
|
},
|
|
1798
1927
|
|
|
1799
|
-
|
|
1928
|
+
buildEnd() {
|
|
1929
|
+
if (options.strictRequires === 'debug') {
|
|
1930
|
+
const wrappedIds = getWrappedIds();
|
|
1931
|
+
if (wrappedIds.length) {
|
|
1932
|
+
this.warn({
|
|
1933
|
+
code: 'WRAPPED_IDS',
|
|
1934
|
+
ids: wrappedIds,
|
|
1935
|
+
message: `The commonjs plugin automatically wrapped the following files:\n[\n${wrappedIds
|
|
1936
|
+
.map((id) => `\t${JSON.stringify(path.relative(process.cwd(), id))}`)
|
|
1937
|
+
.join(',\n')}\n]`
|
|
1938
|
+
});
|
|
1939
|
+
} else {
|
|
1940
|
+
this.warn({
|
|
1941
|
+
code: 'WRAPPED_IDS',
|
|
1942
|
+
ids: wrappedIds,
|
|
1943
|
+
message: 'The commonjs plugin did not wrap any files.'
|
|
1944
|
+
});
|
|
1945
|
+
}
|
|
1946
|
+
}
|
|
1947
|
+
},
|
|
1800
1948
|
|
|
1801
1949
|
load(id) {
|
|
1802
1950
|
if (id === HELPERS_ID) {
|
|
1803
|
-
return getHelpersModule(
|
|
1804
|
-
}
|
|
1805
|
-
|
|
1806
|
-
if (id.startsWith(HELPERS_ID)) {
|
|
1807
|
-
return getSpecificHelperProxy(id);
|
|
1951
|
+
return getHelpersModule();
|
|
1808
1952
|
}
|
|
1809
1953
|
|
|
1810
1954
|
if (isWrappedId(id, MODULE_SUFFIX)) {
|
|
1811
|
-
const
|
|
1812
|
-
let name = getName(actualId);
|
|
1813
|
-
let code;
|
|
1814
|
-
if (isDynamicRequireModulesEnabled) {
|
|
1815
|
-
if (['modulePath', 'commonjsRequire', 'createModule'].includes(name)) {
|
|
1816
|
-
name = `${name}_`;
|
|
1817
|
-
}
|
|
1818
|
-
code =
|
|
1819
|
-
`import {commonjsRequire, createModule} from "${HELPERS_ID}";\n` +
|
|
1820
|
-
`var ${name} = createModule(${JSON.stringify(
|
|
1821
|
-
getVirtualPathForDynamicRequirePath(path.dirname(actualId), commonDir)
|
|
1822
|
-
)});\n` +
|
|
1823
|
-
`export {${name} as __module}`;
|
|
1824
|
-
} else {
|
|
1825
|
-
code = `var ${name} = {exports: {}}; export {${name} as __module}`;
|
|
1826
|
-
}
|
|
1955
|
+
const name = getName(unwrapId(id, MODULE_SUFFIX));
|
|
1827
1956
|
return {
|
|
1828
|
-
code
|
|
1957
|
+
code: `var ${name} = {exports: {}}; export {${name} as __module}`,
|
|
1829
1958
|
syntheticNamedExports: '__module',
|
|
1830
1959
|
meta: { commonjs: { isCommonJS: false } }
|
|
1831
1960
|
};
|
|
1832
1961
|
}
|
|
1833
1962
|
|
|
1834
1963
|
if (isWrappedId(id, EXPORTS_SUFFIX)) {
|
|
1835
|
-
const
|
|
1836
|
-
const name = getName(actualId);
|
|
1964
|
+
const name = getName(unwrapId(id, EXPORTS_SUFFIX));
|
|
1837
1965
|
return {
|
|
1838
1966
|
code: `var ${name} = {}; export {${name} as __exports}`,
|
|
1839
1967
|
meta: { commonjs: { isCommonJS: false } }
|
|
@@ -1848,22 +1976,16 @@ function commonjs(options = {}) {
|
|
|
1848
1976
|
);
|
|
1849
1977
|
}
|
|
1850
1978
|
|
|
1851
|
-
if (id
|
|
1852
|
-
return
|
|
1853
|
-
}
|
|
1854
|
-
|
|
1855
|
-
if (id.startsWith(DYNAMIC_JSON_PREFIX)) {
|
|
1856
|
-
return getDynamicJsonProxy(id, commonDir);
|
|
1857
|
-
}
|
|
1858
|
-
|
|
1859
|
-
if (isDynamicModuleImport(id, dynamicRequireModuleSet)) {
|
|
1860
|
-
return `export default require(${JSON.stringify(normalizePathSlashes(id))});`;
|
|
1979
|
+
if (isWrappedId(id, ES_IMPORT_SUFFIX)) {
|
|
1980
|
+
return getEsImportProxy(unwrapId(id, ES_IMPORT_SUFFIX), defaultIsModuleExports);
|
|
1861
1981
|
}
|
|
1862
1982
|
|
|
1863
|
-
if (
|
|
1864
|
-
return
|
|
1865
|
-
|
|
1866
|
-
|
|
1983
|
+
if (id === DYNAMIC_MODULES_ID) {
|
|
1984
|
+
return getDynamicModuleRegistry(
|
|
1985
|
+
isDynamicRequireModulesEnabled,
|
|
1986
|
+
dynamicRequireModules,
|
|
1987
|
+
commonDir,
|
|
1988
|
+
ignoreDynamicRequires
|
|
1867
1989
|
);
|
|
1868
1990
|
}
|
|
1869
1991
|
|
|
@@ -1874,43 +1996,24 @@ function commonjs(options = {}) {
|
|
|
1874
1996
|
getRequireReturnsDefault(actualId),
|
|
1875
1997
|
esModulesWithDefaultExport,
|
|
1876
1998
|
esModulesWithNamedExports,
|
|
1877
|
-
|
|
1999
|
+
this.load
|
|
1878
2000
|
);
|
|
1879
2001
|
}
|
|
1880
2002
|
|
|
1881
2003
|
return null;
|
|
1882
2004
|
},
|
|
1883
2005
|
|
|
1884
|
-
transform(code,
|
|
1885
|
-
let id = rawId;
|
|
1886
|
-
|
|
1887
|
-
if (isWrappedId(id, DYNAMIC_REGISTER_SUFFIX)) {
|
|
1888
|
-
id = unwrapId(id, DYNAMIC_REGISTER_SUFFIX);
|
|
1889
|
-
}
|
|
1890
|
-
|
|
2006
|
+
transform(code, id) {
|
|
1891
2007
|
const extName = path.extname(id);
|
|
1892
|
-
if (
|
|
1893
|
-
extName !== '.cjs' &&
|
|
1894
|
-
id !== DYNAMIC_PACKAGES_ID &&
|
|
1895
|
-
!id.startsWith(DYNAMIC_JSON_PREFIX) &&
|
|
1896
|
-
(!filter(id) || !extensions.includes(extName))
|
|
1897
|
-
) {
|
|
2008
|
+
if (extName !== '.cjs' && (!filter(id) || !extensions.includes(extName))) {
|
|
1898
2009
|
return null;
|
|
1899
2010
|
}
|
|
1900
2011
|
|
|
1901
2012
|
try {
|
|
1902
|
-
return transformAndCheckExports.call(this, code,
|
|
2013
|
+
return transformAndCheckExports.call(this, code, id);
|
|
1903
2014
|
} catch (err) {
|
|
1904
2015
|
return this.error(err, err.loc);
|
|
1905
2016
|
}
|
|
1906
|
-
},
|
|
1907
|
-
|
|
1908
|
-
moduleParsed({ id, meta: { commonjs: commonjsMeta } }) {
|
|
1909
|
-
if (commonjsMeta && commonjsMeta.isCommonJS != null) {
|
|
1910
|
-
setCommonJSMetaPromise(commonJsMetaPromises, id, commonjsMeta);
|
|
1911
|
-
return;
|
|
1912
|
-
}
|
|
1913
|
-
setCommonJSMetaPromise(commonJsMetaPromises, id, null);
|
|
1914
2017
|
}
|
|
1915
2018
|
};
|
|
1916
2019
|
}
|