@rollup/plugin-commonjs 21.0.1 → 22.0.0-0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +34 -6
- package/dist/index.es.js +878 -796
- package/dist/index.es.js.map +1 -1
- package/dist/index.js +876 -794
- package/dist/index.js.map +1 -1
- package/package.json +5 -5
- package/types/index.d.ts +52 -10
package/dist/index.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-0";
|
|
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
|
+
}`;
|
|
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,92 +466,189 @@ function getCandidates(resolved, extensions) {
|
|
|
575
466
|
);
|
|
576
467
|
}
|
|
577
468
|
|
|
578
|
-
function
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
if (importee[0] !== '.' || !importer) return undefined;
|
|
582
|
-
|
|
583
|
-
const resolved = path.resolve(path.dirname(importer), importee);
|
|
584
|
-
const candidates = getCandidates(resolved, extensions);
|
|
469
|
+
function resolveExtensions(importee, importer, extensions) {
|
|
470
|
+
// not our problem
|
|
471
|
+
if (importee[0] !== '.' || !importer) return undefined;
|
|
585
472
|
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
const stats = fs.statSync(candidates[i]);
|
|
589
|
-
if (stats.isFile()) return { id: candidates[i] };
|
|
590
|
-
} catch (err) {
|
|
591
|
-
/* noop */
|
|
592
|
-
}
|
|
593
|
-
}
|
|
473
|
+
const resolved = path.resolve(path.dirname(importer), importee);
|
|
474
|
+
const candidates = getCandidates(resolved, extensions);
|
|
594
475
|
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
}
|
|
602
|
-
|
|
603
|
-
const importer =
|
|
604
|
-
rawImporter && isWrappedId(rawImporter, DYNAMIC_REGISTER_SUFFIX)
|
|
605
|
-
? unwrapId(rawImporter, DYNAMIC_REGISTER_SUFFIX)
|
|
606
|
-
: rawImporter;
|
|
607
|
-
|
|
608
|
-
// Except for exports, proxies are only importing resolved ids,
|
|
609
|
-
// no need to resolve again
|
|
610
|
-
if (importer && isWrappedId(importer, PROXY_SUFFIX)) {
|
|
611
|
-
return importee;
|
|
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 */
|
|
612
482
|
}
|
|
483
|
+
}
|
|
613
484
|
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
let isModuleRegistration = false;
|
|
617
|
-
|
|
618
|
-
if (isProxyModule) {
|
|
619
|
-
importee = unwrapId(importee, PROXY_SUFFIX);
|
|
620
|
-
} else if (isRequiredModule) {
|
|
621
|
-
importee = unwrapId(importee, REQUIRE_SUFFIX);
|
|
485
|
+
return undefined;
|
|
486
|
+
}
|
|
622
487
|
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
488
|
+
function getResolveId(extensions) {
|
|
489
|
+
return async function resolveId(importee, importer, resolveOptions) {
|
|
490
|
+
if (isWrappedId(importee, WRAPPED_SUFFIX)) {
|
|
491
|
+
return unwrapId(importee, WRAPPED_SUFFIX);
|
|
627
492
|
}
|
|
628
493
|
|
|
629
494
|
if (
|
|
495
|
+
isWrappedId(importee, MODULE_SUFFIX) ||
|
|
496
|
+
isWrappedId(importee, EXPORTS_SUFFIX) ||
|
|
497
|
+
isWrappedId(importee, PROXY_SUFFIX) ||
|
|
498
|
+
isWrappedId(importee, ES_IMPORT_SUFFIX) ||
|
|
499
|
+
isWrappedId(importee, EXTERNAL_SUFFIX) ||
|
|
630
500
|
importee.startsWith(HELPERS_ID) ||
|
|
631
|
-
importee ===
|
|
632
|
-
importee.startsWith(DYNAMIC_JSON_PREFIX)
|
|
501
|
+
importee === DYNAMIC_MODULES_ID
|
|
633
502
|
) {
|
|
634
503
|
return importee;
|
|
635
504
|
}
|
|
636
505
|
|
|
506
|
+
if (importer) {
|
|
507
|
+
if (
|
|
508
|
+
importer === DYNAMIC_MODULES_ID ||
|
|
509
|
+
// Except for exports, proxies are only importing resolved ids, no need to resolve again
|
|
510
|
+
isWrappedId(importer, PROXY_SUFFIX) ||
|
|
511
|
+
isWrappedId(importer, ES_IMPORT_SUFFIX)
|
|
512
|
+
) {
|
|
513
|
+
return importee;
|
|
514
|
+
}
|
|
515
|
+
if (isWrappedId(importer, EXTERNAL_SUFFIX)) {
|
|
516
|
+
// We need to return null for unresolved imports so that the proper warning is shown
|
|
517
|
+
if (!(await this.resolve(importee, importer, { skipSelf: true }))) {
|
|
518
|
+
return null;
|
|
519
|
+
}
|
|
520
|
+
// For other external imports, we need to make sure they are handled as external
|
|
521
|
+
return { id: importee, external: true };
|
|
522
|
+
}
|
|
523
|
+
}
|
|
524
|
+
|
|
637
525
|
if (importee.startsWith('\0')) {
|
|
638
526
|
return null;
|
|
639
527
|
}
|
|
640
528
|
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
if (resolved && isProxyModule) {
|
|
655
|
-
resolved.id = wrapId(resolved.id, resolved.external ? EXTERNAL_SUFFIX : PROXY_SUFFIX);
|
|
656
|
-
resolved.external = false;
|
|
657
|
-
} else if (resolved && isModuleRegistration) {
|
|
658
|
-
resolved.id = wrapId(resolved.id, DYNAMIC_REGISTER_SUFFIX);
|
|
659
|
-
} else if (!resolved && (isProxyModule || isRequiredModule)) {
|
|
660
|
-
return { id: wrapId(importee, EXTERNAL_SUFFIX), external: false };
|
|
661
|
-
}
|
|
529
|
+
// If this is an entry point or ESM import, we need to figure out if the importee is wrapped and
|
|
530
|
+
// if that is the case, we need to add a proxy.
|
|
531
|
+
const customOptions = resolveOptions.custom;
|
|
532
|
+
|
|
533
|
+
// If this is a require, we do not need a proxy
|
|
534
|
+
if (customOptions && customOptions['node-resolve'] && customOptions['node-resolve'].isRequire) {
|
|
535
|
+
return null;
|
|
536
|
+
}
|
|
537
|
+
|
|
538
|
+
const resolved =
|
|
539
|
+
(await this.resolve(importee, importer, Object.assign({ skipSelf: true }, resolveOptions))) ||
|
|
540
|
+
resolveExtensions(importee, importer, extensions);
|
|
541
|
+
if (!resolved || resolved.external) {
|
|
662
542
|
return resolved;
|
|
663
|
-
}
|
|
543
|
+
}
|
|
544
|
+
const {
|
|
545
|
+
meta: { commonjs: commonjsMeta }
|
|
546
|
+
} = await this.load(resolved);
|
|
547
|
+
if (commonjsMeta && commonjsMeta.isCommonJS === IS_WRAPPED_COMMONJS) {
|
|
548
|
+
return wrapId(resolved.id, ES_IMPORT_SUFFIX);
|
|
549
|
+
}
|
|
550
|
+
return resolved;
|
|
551
|
+
};
|
|
552
|
+
}
|
|
553
|
+
|
|
554
|
+
function getResolveRequireSourcesAndGetMeta(extensions, detectCyclesAndConditional) {
|
|
555
|
+
const knownCjsModuleTypes = Object.create(null);
|
|
556
|
+
const requiredIds = Object.create(null);
|
|
557
|
+
const unconditionallyRequiredIds = Object.create(null);
|
|
558
|
+
const dependentModules = Object.create(null);
|
|
559
|
+
const getDependentModules = (id) =>
|
|
560
|
+
dependentModules[id] || (dependentModules[id] = Object.create(null));
|
|
561
|
+
|
|
562
|
+
return {
|
|
563
|
+
getWrappedIds: () =>
|
|
564
|
+
Object.keys(knownCjsModuleTypes).filter(
|
|
565
|
+
(id) => knownCjsModuleTypes[id] === IS_WRAPPED_COMMONJS
|
|
566
|
+
),
|
|
567
|
+
isRequiredId: (id) => requiredIds[id],
|
|
568
|
+
resolveRequireSourcesAndGetMeta: (rollupContext) => async (
|
|
569
|
+
parentId,
|
|
570
|
+
isParentCommonJS,
|
|
571
|
+
sources
|
|
572
|
+
) => {
|
|
573
|
+
knownCjsModuleTypes[parentId] = knownCjsModuleTypes[parentId] || isParentCommonJS;
|
|
574
|
+
if (
|
|
575
|
+
knownCjsModuleTypes[parentId] &&
|
|
576
|
+
requiredIds[parentId] &&
|
|
577
|
+
!unconditionallyRequiredIds[parentId]
|
|
578
|
+
) {
|
|
579
|
+
knownCjsModuleTypes[parentId] = IS_WRAPPED_COMMONJS;
|
|
580
|
+
}
|
|
581
|
+
const requireTargets = await Promise.all(
|
|
582
|
+
sources.map(async ({ source, isConditional }) => {
|
|
583
|
+
// Never analyze or proxy internal modules
|
|
584
|
+
if (source.startsWith('\0')) {
|
|
585
|
+
return { id: source, allowProxy: false };
|
|
586
|
+
}
|
|
587
|
+
const resolved =
|
|
588
|
+
(await rollupContext.resolve(source, parentId, {
|
|
589
|
+
custom: {
|
|
590
|
+
'node-resolve': { isRequire: true }
|
|
591
|
+
}
|
|
592
|
+
})) || resolveExtensions(source, parentId, extensions);
|
|
593
|
+
if (!resolved) {
|
|
594
|
+
return { id: wrapId(source, EXTERNAL_SUFFIX), allowProxy: false };
|
|
595
|
+
}
|
|
596
|
+
const childId = resolved.id;
|
|
597
|
+
if (resolved.external) {
|
|
598
|
+
return { id: wrapId(childId, EXTERNAL_SUFFIX), allowProxy: false };
|
|
599
|
+
}
|
|
600
|
+
requiredIds[childId] = true;
|
|
601
|
+
if (
|
|
602
|
+
!(
|
|
603
|
+
detectCyclesAndConditional &&
|
|
604
|
+
(isConditional || knownCjsModuleTypes[parentId] === IS_WRAPPED_COMMONJS)
|
|
605
|
+
)
|
|
606
|
+
) {
|
|
607
|
+
unconditionallyRequiredIds[childId] = true;
|
|
608
|
+
}
|
|
609
|
+
const parentDependentModules = getDependentModules(parentId);
|
|
610
|
+
const childDependentModules = getDependentModules(childId);
|
|
611
|
+
childDependentModules[parentId] = true;
|
|
612
|
+
for (const dependentId of Object.keys(parentDependentModules)) {
|
|
613
|
+
childDependentModules[dependentId] = true;
|
|
614
|
+
}
|
|
615
|
+
if (parentDependentModules[childId]) {
|
|
616
|
+
// If we depend on one of our dependencies, we have a cycle. Then all modules that
|
|
617
|
+
// we depend on that also depend on the same module are part of a cycle as well.
|
|
618
|
+
if (detectCyclesAndConditional && isParentCommonJS) {
|
|
619
|
+
knownCjsModuleTypes[parentId] = IS_WRAPPED_COMMONJS;
|
|
620
|
+
knownCjsModuleTypes[childId] = IS_WRAPPED_COMMONJS;
|
|
621
|
+
for (const dependentId of Object.keys(parentDependentModules)) {
|
|
622
|
+
if (getDependentModules(dependentId)[childId]) {
|
|
623
|
+
knownCjsModuleTypes[dependentId] = IS_WRAPPED_COMMONJS;
|
|
624
|
+
}
|
|
625
|
+
}
|
|
626
|
+
}
|
|
627
|
+
} else {
|
|
628
|
+
// This makes sure the current transform handler waits for all direct dependencies to be
|
|
629
|
+
// loaded and transformed and therefore for all transitive CommonJS dependencies to be
|
|
630
|
+
// loaded as well so that all cycles have been found and knownCjsModuleTypes is reliable.
|
|
631
|
+
await rollupContext.load(resolved);
|
|
632
|
+
}
|
|
633
|
+
return { id: childId, allowProxy: true };
|
|
634
|
+
})
|
|
635
|
+
);
|
|
636
|
+
return {
|
|
637
|
+
requireTargets: requireTargets.map(({ id: dependencyId, allowProxy }, index) => {
|
|
638
|
+
const isCommonJS = knownCjsModuleTypes[dependencyId];
|
|
639
|
+
return {
|
|
640
|
+
source: sources[index].source,
|
|
641
|
+
id: allowProxy
|
|
642
|
+
? isCommonJS === IS_WRAPPED_COMMONJS
|
|
643
|
+
? wrapId(dependencyId, WRAPPED_SUFFIX)
|
|
644
|
+
: wrapId(dependencyId, PROXY_SUFFIX)
|
|
645
|
+
: dependencyId,
|
|
646
|
+
isCommonJS
|
|
647
|
+
};
|
|
648
|
+
}),
|
|
649
|
+
usesRequireWrapper: knownCjsModuleTypes[parentId] === IS_WRAPPED_COMMONJS
|
|
650
|
+
};
|
|
651
|
+
}
|
|
664
652
|
};
|
|
665
653
|
}
|
|
666
654
|
|
|
@@ -820,8 +808,9 @@ function wrapCode(magicString, uses, moduleName, exportsName) {
|
|
|
820
808
|
}
|
|
821
809
|
magicString
|
|
822
810
|
.trim()
|
|
811
|
+
.indent('\t')
|
|
823
812
|
.prepend(`(function (${args.join(', ')}) {\n`)
|
|
824
|
-
.append(`\n}(${passedArgs.join(', ')}));`);
|
|
813
|
+
.append(`\n} (${passedArgs.join(', ')}));`);
|
|
825
814
|
}
|
|
826
815
|
|
|
827
816
|
function rewriteExportsAndGetExportsBlock(
|
|
@@ -839,12 +828,27 @@ function rewriteExportsAndGetExportsBlock(
|
|
|
839
828
|
HELPERS_NAME,
|
|
840
829
|
exportMode,
|
|
841
830
|
detectWrappedDefault,
|
|
842
|
-
defaultIsModuleExports
|
|
831
|
+
defaultIsModuleExports,
|
|
832
|
+
usesRequireWrapper,
|
|
833
|
+
requireName
|
|
843
834
|
) {
|
|
844
835
|
const exports = [];
|
|
845
836
|
const exportDeclarations = [];
|
|
846
837
|
|
|
847
|
-
if (
|
|
838
|
+
if (usesRequireWrapper) {
|
|
839
|
+
getExportsWhenUsingRequireWrapper(
|
|
840
|
+
magicString,
|
|
841
|
+
wrapped,
|
|
842
|
+
exportMode,
|
|
843
|
+
exports,
|
|
844
|
+
moduleExportsAssignments,
|
|
845
|
+
exportsAssignmentsByName,
|
|
846
|
+
moduleName,
|
|
847
|
+
exportsName,
|
|
848
|
+
requireName,
|
|
849
|
+
defineCompiledEsmExpressions
|
|
850
|
+
);
|
|
851
|
+
} else if (exportMode === 'replace') {
|
|
848
852
|
getExportsForReplacedModuleExports(
|
|
849
853
|
magicString,
|
|
850
854
|
exports,
|
|
@@ -887,6 +891,49 @@ function rewriteExportsAndGetExportsBlock(
|
|
|
887
891
|
return `\n\n${exportDeclarations.join('\n')}`;
|
|
888
892
|
}
|
|
889
893
|
|
|
894
|
+
function getExportsWhenUsingRequireWrapper(
|
|
895
|
+
magicString,
|
|
896
|
+
wrapped,
|
|
897
|
+
exportMode,
|
|
898
|
+
exports,
|
|
899
|
+
moduleExportsAssignments,
|
|
900
|
+
exportsAssignmentsByName,
|
|
901
|
+
moduleName,
|
|
902
|
+
exportsName,
|
|
903
|
+
requireName,
|
|
904
|
+
defineCompiledEsmExpressions
|
|
905
|
+
) {
|
|
906
|
+
if (!wrapped) {
|
|
907
|
+
if (exportMode === 'replace') {
|
|
908
|
+
for (const { left } of moduleExportsAssignments) {
|
|
909
|
+
magicString.overwrite(left.start, left.end, exportsName);
|
|
910
|
+
}
|
|
911
|
+
} else {
|
|
912
|
+
// Collect and rewrite module.exports assignments
|
|
913
|
+
for (const { left } of moduleExportsAssignments) {
|
|
914
|
+
magicString.overwrite(left.start, left.end, `${moduleName}.exports`);
|
|
915
|
+
}
|
|
916
|
+
// Collect and rewrite named exports
|
|
917
|
+
for (const [exportName, { nodes }] of exportsAssignmentsByName) {
|
|
918
|
+
for (const node of nodes) {
|
|
919
|
+
magicString.overwrite(node.start, node.left.end, `${exportsName}.${exportName}`);
|
|
920
|
+
}
|
|
921
|
+
}
|
|
922
|
+
// Collect and rewrite exports.__esModule assignments
|
|
923
|
+
for (const expression of defineCompiledEsmExpressions) {
|
|
924
|
+
const moduleExportsExpression =
|
|
925
|
+
expression.type === 'CallExpression' ? expression.arguments[0] : expression.left.object;
|
|
926
|
+
magicString.overwrite(
|
|
927
|
+
moduleExportsExpression.start,
|
|
928
|
+
moduleExportsExpression.end,
|
|
929
|
+
exportsName
|
|
930
|
+
);
|
|
931
|
+
}
|
|
932
|
+
}
|
|
933
|
+
}
|
|
934
|
+
exports.push(`${requireName} as __require`);
|
|
935
|
+
}
|
|
936
|
+
|
|
890
937
|
function getExportsForReplacedModuleExports(
|
|
891
938
|
magicString,
|
|
892
939
|
exports,
|
|
@@ -974,7 +1021,7 @@ function getExports(
|
|
|
974
1021
|
}
|
|
975
1022
|
|
|
976
1023
|
if (!isRestorableCompiledEsm || defaultIsModuleExports === true) {
|
|
977
|
-
|
|
1024
|
+
exports.push(`${exportsName} as default`);
|
|
978
1025
|
} else if (moduleExportsAssignments.length === 0 || defaultIsModuleExports === false) {
|
|
979
1026
|
exports.push(`${deconflictedDefaultExportName || exportsName} as default`);
|
|
980
1027
|
} else {
|
|
@@ -984,7 +1031,7 @@ function getExports(
|
|
|
984
1031
|
}
|
|
985
1032
|
}
|
|
986
1033
|
|
|
987
|
-
function
|
|
1034
|
+
function isRequireExpression(node, scope) {
|
|
988
1035
|
if (!node) return false;
|
|
989
1036
|
if (node.type !== 'CallExpression') return false;
|
|
990
1037
|
|
|
@@ -1011,11 +1058,6 @@ function isModuleRequire({ object, property }, scope) {
|
|
|
1011
1058
|
);
|
|
1012
1059
|
}
|
|
1013
1060
|
|
|
1014
|
-
function isStaticRequireStatement(node, scope) {
|
|
1015
|
-
if (!isRequireStatement(node, scope)) return false;
|
|
1016
|
-
return !hasDynamicArguments(node);
|
|
1017
|
-
}
|
|
1018
|
-
|
|
1019
1061
|
function hasDynamicArguments(node) {
|
|
1020
1062
|
return (
|
|
1021
1063
|
node.arguments.length > 1 ||
|
|
@@ -1030,89 +1072,58 @@ function isNodeRequirePropertyAccess(parent) {
|
|
|
1030
1072
|
return parent && parent.property && reservedMethod[parent.property.name];
|
|
1031
1073
|
}
|
|
1032
1074
|
|
|
1033
|
-
function isIgnoredRequireStatement(requiredNode, ignoreRequire) {
|
|
1034
|
-
return ignoreRequire(requiredNode.arguments[0].value);
|
|
1035
|
-
}
|
|
1036
|
-
|
|
1037
1075
|
function getRequireStringArg(node) {
|
|
1038
1076
|
return node.arguments[0].type === 'Literal'
|
|
1039
1077
|
? node.arguments[0].value
|
|
1040
1078
|
: node.arguments[0].quasis[0].value.cooked;
|
|
1041
1079
|
}
|
|
1042
1080
|
|
|
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
1081
|
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];
|
|
1082
|
+
const requireExpressions = [];
|
|
1083
|
+
|
|
1084
|
+
function addRequireStatement(
|
|
1085
|
+
sourceId,
|
|
1086
|
+
node,
|
|
1087
|
+
scope,
|
|
1088
|
+
usesReturnValue,
|
|
1089
|
+
isInsideTryBlock,
|
|
1090
|
+
isInsideConditional,
|
|
1091
|
+
toBeRemoved
|
|
1092
|
+
) {
|
|
1093
|
+
requireExpressions.push({
|
|
1094
|
+
sourceId,
|
|
1095
|
+
node,
|
|
1096
|
+
scope,
|
|
1097
|
+
usesReturnValue,
|
|
1098
|
+
isInsideTryBlock,
|
|
1099
|
+
isInsideConditional,
|
|
1100
|
+
toBeRemoved
|
|
1101
|
+
});
|
|
1095
1102
|
}
|
|
1096
1103
|
|
|
1097
|
-
function rewriteRequireExpressionsAndGetImportBlock(
|
|
1104
|
+
async function rewriteRequireExpressionsAndGetImportBlock(
|
|
1098
1105
|
magicString,
|
|
1099
1106
|
topLevelDeclarations,
|
|
1100
|
-
topLevelRequireDeclarators,
|
|
1101
1107
|
reassignedNames,
|
|
1102
1108
|
helpersName,
|
|
1103
|
-
|
|
1109
|
+
dynamicRequireName,
|
|
1104
1110
|
moduleName,
|
|
1105
1111
|
exportsName,
|
|
1106
1112
|
id,
|
|
1107
|
-
exportMode
|
|
1113
|
+
exportMode,
|
|
1114
|
+
resolveRequireSourcesAndGetMeta,
|
|
1115
|
+
needsRequireWrapper,
|
|
1116
|
+
isEsModule,
|
|
1117
|
+
usesRequire,
|
|
1118
|
+
getIgnoreTryCatchRequireStatementMode
|
|
1108
1119
|
) {
|
|
1109
|
-
setRemainingImportNamesAndRewriteRequires(
|
|
1110
|
-
requireExpressionsWithUsedReturnValue,
|
|
1111
|
-
requiredByNode,
|
|
1112
|
-
magicString
|
|
1113
|
-
);
|
|
1114
1120
|
const imports = [];
|
|
1115
1121
|
imports.push(`import * as ${helpersName} from "${HELPERS_ID}";`);
|
|
1122
|
+
if (usesRequire) {
|
|
1123
|
+
imports.push(
|
|
1124
|
+
`import { commonjsRequire as ${dynamicRequireName} } from "${DYNAMIC_MODULES_ID}";`
|
|
1125
|
+
);
|
|
1126
|
+
}
|
|
1116
1127
|
if (exportMode === 'module') {
|
|
1117
1128
|
imports.push(
|
|
1118
1129
|
`import { __module as ${moduleName}, exports as ${exportsName} } from ${JSON.stringify(
|
|
@@ -1124,58 +1135,115 @@ function getRequireHandlers() {
|
|
|
1124
1135
|
`import { __exports as ${exportsName} } from ${JSON.stringify(wrapId(id, EXPORTS_SUFFIX))}`
|
|
1125
1136
|
);
|
|
1126
1137
|
}
|
|
1127
|
-
|
|
1128
|
-
|
|
1129
|
-
|
|
1130
|
-
|
|
1131
|
-
|
|
1132
|
-
|
|
1133
|
-
|
|
1134
|
-
|
|
1135
|
-
|
|
1136
|
-
|
|
1137
|
-
|
|
1138
|
-
|
|
1139
|
-
|
|
1140
|
-
|
|
1141
|
-
|
|
1138
|
+
const requiresBySource = collectSources(requireExpressions);
|
|
1139
|
+
const { requireTargets, usesRequireWrapper } = await resolveRequireSourcesAndGetMeta(
|
|
1140
|
+
id,
|
|
1141
|
+
needsRequireWrapper ? IS_WRAPPED_COMMONJS : !isEsModule,
|
|
1142
|
+
Object.keys(requiresBySource).map((source) => {
|
|
1143
|
+
return {
|
|
1144
|
+
source,
|
|
1145
|
+
isConditional: requiresBySource[source].every((require) => require.isInsideConditional)
|
|
1146
|
+
};
|
|
1147
|
+
})
|
|
1148
|
+
);
|
|
1149
|
+
processRequireExpressions(
|
|
1150
|
+
imports,
|
|
1151
|
+
requireTargets,
|
|
1152
|
+
requiresBySource,
|
|
1153
|
+
getIgnoreTryCatchRequireStatementMode,
|
|
1154
|
+
magicString
|
|
1155
|
+
);
|
|
1156
|
+
return {
|
|
1157
|
+
importBlock: imports.length ? `${imports.join('\n')}\n\n` : '',
|
|
1158
|
+
usesRequireWrapper
|
|
1159
|
+
};
|
|
1142
1160
|
}
|
|
1143
1161
|
|
|
1144
1162
|
return {
|
|
1145
1163
|
addRequireStatement,
|
|
1146
|
-
requiredSources,
|
|
1147
1164
|
rewriteRequireExpressionsAndGetImportBlock
|
|
1148
1165
|
};
|
|
1149
1166
|
}
|
|
1150
1167
|
|
|
1151
|
-
function
|
|
1152
|
-
|
|
1153
|
-
|
|
1168
|
+
function collectSources(requireExpressions) {
|
|
1169
|
+
const requiresBySource = Object.create(null);
|
|
1170
|
+
for (const requireExpression of requireExpressions) {
|
|
1171
|
+
const { sourceId } = requireExpression;
|
|
1172
|
+
if (!requiresBySource[sourceId]) {
|
|
1173
|
+
requiresBySource[sourceId] = [];
|
|
1174
|
+
}
|
|
1175
|
+
const requires = requiresBySource[sourceId];
|
|
1176
|
+
requires.push(requireExpression);
|
|
1177
|
+
}
|
|
1178
|
+
return requiresBySource;
|
|
1179
|
+
}
|
|
1180
|
+
|
|
1181
|
+
function processRequireExpressions(
|
|
1182
|
+
imports,
|
|
1183
|
+
requireTargets,
|
|
1184
|
+
requiresBySource,
|
|
1185
|
+
getIgnoreTryCatchRequireStatementMode,
|
|
1154
1186
|
magicString
|
|
1155
1187
|
) {
|
|
1156
|
-
|
|
1157
|
-
for (const
|
|
1158
|
-
const
|
|
1159
|
-
|
|
1160
|
-
|
|
1161
|
-
|
|
1162
|
-
|
|
1163
|
-
|
|
1164
|
-
|
|
1165
|
-
|
|
1166
|
-
|
|
1188
|
+
const generateRequireName = getGenerateRequireName();
|
|
1189
|
+
for (const { source, id: resolvedId, isCommonJS } of requireTargets) {
|
|
1190
|
+
const requires = requiresBySource[source];
|
|
1191
|
+
const name = generateRequireName(requires);
|
|
1192
|
+
let usesRequired = false;
|
|
1193
|
+
let needsImport = false;
|
|
1194
|
+
for (const { node, usesReturnValue, toBeRemoved, isInsideTryBlock } of requires) {
|
|
1195
|
+
const { canConvertRequire, shouldRemoveRequire } =
|
|
1196
|
+
isInsideTryBlock && isWrappedId(resolvedId, EXTERNAL_SUFFIX)
|
|
1197
|
+
? getIgnoreTryCatchRequireStatementMode(source)
|
|
1198
|
+
: { canConvertRequire: true, shouldRemoveRequire: false };
|
|
1199
|
+
if (shouldRemoveRequire) {
|
|
1200
|
+
if (usesReturnValue) {
|
|
1201
|
+
magicString.overwrite(node.start, node.end, 'undefined');
|
|
1202
|
+
} else {
|
|
1203
|
+
magicString.remove(toBeRemoved.start, toBeRemoved.end);
|
|
1204
|
+
}
|
|
1205
|
+
} else if (canConvertRequire) {
|
|
1206
|
+
needsImport = true;
|
|
1207
|
+
if (isCommonJS === IS_WRAPPED_COMMONJS) {
|
|
1208
|
+
magicString.overwrite(node.start, node.end, `${name}()`);
|
|
1209
|
+
} else if (usesReturnValue) {
|
|
1210
|
+
usesRequired = true;
|
|
1211
|
+
magicString.overwrite(node.start, node.end, name);
|
|
1212
|
+
} else {
|
|
1213
|
+
magicString.remove(toBeRemoved.start, toBeRemoved.end);
|
|
1214
|
+
}
|
|
1215
|
+
}
|
|
1216
|
+
}
|
|
1217
|
+
if (needsImport) {
|
|
1218
|
+
if (isCommonJS === IS_WRAPPED_COMMONJS) {
|
|
1219
|
+
imports.push(`import { __require as ${name} } from ${JSON.stringify(resolvedId)};`);
|
|
1220
|
+
} else {
|
|
1221
|
+
imports.push(`import ${usesRequired ? `${name} from ` : ''}${JSON.stringify(resolvedId)};`);
|
|
1222
|
+
}
|
|
1167
1223
|
}
|
|
1168
|
-
magicString.overwrite(requireExpression.start, requireExpression.end, required.name);
|
|
1169
1224
|
}
|
|
1170
1225
|
}
|
|
1171
1226
|
|
|
1227
|
+
function getGenerateRequireName() {
|
|
1228
|
+
let uid = 0;
|
|
1229
|
+
return (requires) => {
|
|
1230
|
+
let name;
|
|
1231
|
+
const hasNameConflict = ({ scope }) => scope.contains(name);
|
|
1232
|
+
do {
|
|
1233
|
+
name = `require$$${uid}`;
|
|
1234
|
+
uid += 1;
|
|
1235
|
+
} while (requires.some(hasNameConflict));
|
|
1236
|
+
return name;
|
|
1237
|
+
};
|
|
1238
|
+
}
|
|
1239
|
+
|
|
1172
1240
|
/* eslint-disable no-param-reassign, no-shadow, no-underscore-dangle, no-continue */
|
|
1173
1241
|
|
|
1174
1242
|
const exportsPattern = /^(?:module\.)?exports(?:\.([a-zA-Z_$][a-zA-Z_$0-9]*))?$/;
|
|
1175
1243
|
|
|
1176
1244
|
const functionType = /^(?:FunctionDeclaration|FunctionExpression|ArrowFunctionExpression)$/;
|
|
1177
1245
|
|
|
1178
|
-
function transformCommonjs(
|
|
1246
|
+
async function transformCommonjs(
|
|
1179
1247
|
parse,
|
|
1180
1248
|
code,
|
|
1181
1249
|
id,
|
|
@@ -1186,11 +1254,14 @@ function transformCommonjs(
|
|
|
1186
1254
|
getIgnoreTryCatchRequireStatementMode,
|
|
1187
1255
|
sourceMap,
|
|
1188
1256
|
isDynamicRequireModulesEnabled,
|
|
1189
|
-
|
|
1190
|
-
disableWrap,
|
|
1257
|
+
dynamicRequireModules,
|
|
1191
1258
|
commonDir,
|
|
1192
1259
|
astCache,
|
|
1193
|
-
defaultIsModuleExports
|
|
1260
|
+
defaultIsModuleExports,
|
|
1261
|
+
needsRequireWrapper,
|
|
1262
|
+
resolveRequireSourcesAndGetMeta,
|
|
1263
|
+
isRequired,
|
|
1264
|
+
checkDynamicRequire
|
|
1194
1265
|
) {
|
|
1195
1266
|
const ast = astCache || tryParse(parse, code, id);
|
|
1196
1267
|
const magicString = new MagicString__default["default"](code);
|
|
@@ -1200,7 +1271,6 @@ function transformCommonjs(
|
|
|
1200
1271
|
global: false,
|
|
1201
1272
|
require: false
|
|
1202
1273
|
};
|
|
1203
|
-
let usesDynamicRequire = false;
|
|
1204
1274
|
const virtualDynamicRequirePath =
|
|
1205
1275
|
isDynamicRequireModulesEnabled && getVirtualPathForDynamicRequirePath(path.dirname(id), commonDir);
|
|
1206
1276
|
let scope = pluginutils.attachScopes(ast, 'scope');
|
|
@@ -1210,24 +1280,18 @@ function transformCommonjs(
|
|
|
1210
1280
|
let shouldWrap = false;
|
|
1211
1281
|
|
|
1212
1282
|
const globals = new Set();
|
|
1213
|
-
|
|
1214
|
-
//
|
|
1215
|
-
|
|
1216
|
-
|
|
1217
|
-
|
|
1218
|
-
|
|
1219
|
-
const {
|
|
1220
|
-
addRequireStatement,
|
|
1221
|
-
requiredSources,
|
|
1222
|
-
rewriteRequireExpressionsAndGetImportBlock
|
|
1223
|
-
} = getRequireHandlers();
|
|
1283
|
+
// A conditionalNode is a node for which execution is not guaranteed. If such a node is a require
|
|
1284
|
+
// or contains nested requires, those should be handled as function calls unless there is an
|
|
1285
|
+
// unconditional require elsewhere.
|
|
1286
|
+
let currentConditionalNodeEnd = null;
|
|
1287
|
+
const conditionalNodes = new Set();
|
|
1288
|
+
const { addRequireStatement, rewriteRequireExpressionsAndGetImportBlock } = getRequireHandlers();
|
|
1224
1289
|
|
|
1225
1290
|
// See which names are assigned to. This is necessary to prevent
|
|
1226
1291
|
// illegally replacing `var foo = require('foo')` with `import foo from 'foo'`,
|
|
1227
1292
|
// where `foo` is later reassigned. (This happens in the wild. CommonJS, sigh)
|
|
1228
1293
|
const reassignedNames = new Set();
|
|
1229
1294
|
const topLevelDeclarations = [];
|
|
1230
|
-
const topLevelRequireDeclarators = new Set();
|
|
1231
1295
|
const skippedNodes = new Set();
|
|
1232
1296
|
const moduleAccessScopes = new Set([scope]);
|
|
1233
1297
|
const exportsAccessScopes = new Set([scope]);
|
|
@@ -1236,6 +1300,8 @@ function transformCommonjs(
|
|
|
1236
1300
|
const exportsAssignmentsByName = new Map();
|
|
1237
1301
|
const topLevelAssignments = new Set();
|
|
1238
1302
|
const topLevelDefineCompiledEsmExpressions = [];
|
|
1303
|
+
const replacedGlobal = [];
|
|
1304
|
+
const replacedDynamicRequires = [];
|
|
1239
1305
|
|
|
1240
1306
|
estreeWalker.walk(ast, {
|
|
1241
1307
|
enter(node, parent) {
|
|
@@ -1247,6 +1313,12 @@ function transformCommonjs(
|
|
|
1247
1313
|
if (currentTryBlockEnd !== null && node.start > currentTryBlockEnd) {
|
|
1248
1314
|
currentTryBlockEnd = null;
|
|
1249
1315
|
}
|
|
1316
|
+
if (currentConditionalNodeEnd !== null && node.start > currentConditionalNodeEnd) {
|
|
1317
|
+
currentConditionalNodeEnd = null;
|
|
1318
|
+
}
|
|
1319
|
+
if (currentConditionalNodeEnd === null && conditionalNodes.has(node)) {
|
|
1320
|
+
currentConditionalNodeEnd = node.end;
|
|
1321
|
+
}
|
|
1250
1322
|
|
|
1251
1323
|
programDepth += 1;
|
|
1252
1324
|
if (node.scope) ({ scope } = node);
|
|
@@ -1258,11 +1330,6 @@ function transformCommonjs(
|
|
|
1258
1330
|
|
|
1259
1331
|
// eslint-disable-next-line default-case
|
|
1260
1332
|
switch (node.type) {
|
|
1261
|
-
case 'TryStatement':
|
|
1262
|
-
if (currentTryBlockEnd === null) {
|
|
1263
|
-
currentTryBlockEnd = node.block.end;
|
|
1264
|
-
}
|
|
1265
|
-
return;
|
|
1266
1333
|
case 'AssignmentExpression':
|
|
1267
1334
|
if (node.left.type === 'MemberExpression') {
|
|
1268
1335
|
const flattened = getKeypath(node.left);
|
|
@@ -1333,12 +1400,15 @@ function transformCommonjs(
|
|
|
1333
1400
|
return;
|
|
1334
1401
|
}
|
|
1335
1402
|
|
|
1403
|
+
// Transform require.resolve
|
|
1336
1404
|
if (
|
|
1405
|
+
isDynamicRequireModulesEnabled &&
|
|
1337
1406
|
node.callee.object &&
|
|
1338
|
-
node.callee.object
|
|
1339
|
-
node.callee.property.name === 'resolve'
|
|
1340
|
-
hasDynamicModuleForPath(id, '/', dynamicRequireModuleSet)
|
|
1407
|
+
isRequire(node.callee.object, scope) &&
|
|
1408
|
+
node.callee.property.name === 'resolve'
|
|
1341
1409
|
) {
|
|
1410
|
+
checkDynamicRequire();
|
|
1411
|
+
uses.require = true;
|
|
1342
1412
|
const requireNode = node.callee.object;
|
|
1343
1413
|
magicString.appendLeft(
|
|
1344
1414
|
node.end - 1,
|
|
@@ -1346,98 +1416,45 @@ function transformCommonjs(
|
|
|
1346
1416
|
path.dirname(id) === '.' ? null /* default behavior */ : virtualDynamicRequirePath
|
|
1347
1417
|
)}`
|
|
1348
1418
|
);
|
|
1349
|
-
|
|
1350
|
-
requireNode.start,
|
|
1351
|
-
requireNode.end,
|
|
1352
|
-
`${HELPERS_NAME}.commonjsRequire`,
|
|
1353
|
-
{
|
|
1354
|
-
storeName: true
|
|
1355
|
-
}
|
|
1356
|
-
);
|
|
1419
|
+
replacedDynamicRequires.push(requireNode);
|
|
1357
1420
|
return;
|
|
1358
1421
|
}
|
|
1359
1422
|
|
|
1360
|
-
if (!
|
|
1361
|
-
|
|
1362
|
-
skippedNodes.add(node.callee);
|
|
1423
|
+
if (!isRequireExpression(node, scope)) {
|
|
1424
|
+
return;
|
|
1363
1425
|
}
|
|
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
1426
|
|
|
1371
|
-
|
|
1372
|
-
|
|
1373
|
-
|
|
1374
|
-
|
|
1375
|
-
|
|
1376
|
-
|
|
1377
|
-
|
|
1378
|
-
|
|
1379
|
-
|
|
1427
|
+
skippedNodes.add(node.callee);
|
|
1428
|
+
uses.require = true;
|
|
1429
|
+
|
|
1430
|
+
if (hasDynamicArguments(node)) {
|
|
1431
|
+
if (isDynamicRequireModulesEnabled) {
|
|
1432
|
+
magicString.appendLeft(
|
|
1433
|
+
node.end - 1,
|
|
1434
|
+
`, ${JSON.stringify(
|
|
1435
|
+
path.dirname(id) === '.' ? null /* default behavior */ : virtualDynamicRequirePath
|
|
1436
|
+
)}`
|
|
1437
|
+
);
|
|
1380
1438
|
}
|
|
1381
|
-
|
|
1382
|
-
|
|
1383
|
-
|
|
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
|
-
}
|
|
1439
|
+
if (!ignoreDynamicRequires) {
|
|
1440
|
+
checkDynamicRequire();
|
|
1441
|
+
replacedDynamicRequires.push(node.callee);
|
|
1415
1442
|
}
|
|
1443
|
+
return;
|
|
1444
|
+
}
|
|
1416
1445
|
|
|
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
|
-
}
|
|
1446
|
+
const requireStringArg = getRequireStringArg(node);
|
|
1447
|
+
if (!ignoreRequire(requireStringArg)) {
|
|
1448
|
+
const usesReturnValue = parent.type !== 'ExpressionStatement';
|
|
1449
|
+
addRequireStatement(
|
|
1450
|
+
requireStringArg,
|
|
1451
|
+
node,
|
|
1452
|
+
scope,
|
|
1453
|
+
usesReturnValue,
|
|
1454
|
+
currentTryBlockEnd !== null,
|
|
1455
|
+
currentConditionalNodeEnd !== null,
|
|
1456
|
+
parent.type === 'ExpressionStatement' ? parent : node
|
|
1457
|
+
);
|
|
1441
1458
|
}
|
|
1442
1459
|
return;
|
|
1443
1460
|
}
|
|
@@ -1446,45 +1463,44 @@ function transformCommonjs(
|
|
|
1446
1463
|
// skip dead branches
|
|
1447
1464
|
if (isFalsy(node.test)) {
|
|
1448
1465
|
skippedNodes.add(node.consequent);
|
|
1449
|
-
} else if (
|
|
1450
|
-
|
|
1466
|
+
} else if (isTruthy(node.test)) {
|
|
1467
|
+
if (node.alternate) {
|
|
1468
|
+
skippedNodes.add(node.alternate);
|
|
1469
|
+
}
|
|
1470
|
+
} else {
|
|
1471
|
+
conditionalNodes.add(node.consequent);
|
|
1472
|
+
if (node.alternate) {
|
|
1473
|
+
conditionalNodes.add(node.alternate);
|
|
1474
|
+
}
|
|
1475
|
+
}
|
|
1476
|
+
return;
|
|
1477
|
+
case 'ArrowFunctionExpression':
|
|
1478
|
+
case 'FunctionDeclaration':
|
|
1479
|
+
case 'FunctionExpression':
|
|
1480
|
+
// requires in functions should be conditional unless it is an IIFE
|
|
1481
|
+
if (
|
|
1482
|
+
currentConditionalNodeEnd === null &&
|
|
1483
|
+
!(parent.type === 'CallExpression' && parent.callee === node)
|
|
1484
|
+
) {
|
|
1485
|
+
currentConditionalNodeEnd = node.end;
|
|
1451
1486
|
}
|
|
1452
1487
|
return;
|
|
1453
1488
|
case 'Identifier': {
|
|
1454
1489
|
const { name } = node;
|
|
1455
|
-
if (!
|
|
1490
|
+
if (!isReference__default["default"](node, parent) || scope.contains(name)) return;
|
|
1456
1491
|
switch (name) {
|
|
1457
1492
|
case 'require':
|
|
1493
|
+
uses.require = true;
|
|
1458
1494
|
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
1495
|
return;
|
|
1468
1496
|
}
|
|
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
1497
|
if (!ignoreDynamicRequires) {
|
|
1498
|
+
checkDynamicRequire();
|
|
1479
1499
|
if (isShorthandProperty(parent)) {
|
|
1480
|
-
magicString.
|
|
1481
|
-
} else {
|
|
1482
|
-
magicString.overwrite(node.start, node.end, `${HELPERS_NAME}.commonjsRequire`, {
|
|
1483
|
-
storeName: true
|
|
1484
|
-
});
|
|
1500
|
+
magicString.prependRight(node.start, 'require: ');
|
|
1485
1501
|
}
|
|
1502
|
+
replacedDynamicRequires.push(node);
|
|
1486
1503
|
}
|
|
1487
|
-
usesDynamicRequire = true;
|
|
1488
1504
|
return;
|
|
1489
1505
|
case 'module':
|
|
1490
1506
|
case 'exports':
|
|
@@ -1494,9 +1510,7 @@ function transformCommonjs(
|
|
|
1494
1510
|
case 'global':
|
|
1495
1511
|
uses.global = true;
|
|
1496
1512
|
if (!ignoreGlobal) {
|
|
1497
|
-
|
|
1498
|
-
storeName: true
|
|
1499
|
-
});
|
|
1513
|
+
replacedGlobal.push(node);
|
|
1500
1514
|
}
|
|
1501
1515
|
return;
|
|
1502
1516
|
case 'define':
|
|
@@ -1509,11 +1523,26 @@ function transformCommonjs(
|
|
|
1509
1523
|
return;
|
|
1510
1524
|
}
|
|
1511
1525
|
}
|
|
1526
|
+
case 'LogicalExpression':
|
|
1527
|
+
// skip dead branches
|
|
1528
|
+
if (node.operator === '&&') {
|
|
1529
|
+
if (isFalsy(node.left)) {
|
|
1530
|
+
skippedNodes.add(node.right);
|
|
1531
|
+
} else if (!isTruthy(node.left)) {
|
|
1532
|
+
conditionalNodes.add(node.right);
|
|
1533
|
+
}
|
|
1534
|
+
} else if (node.operator === '||') {
|
|
1535
|
+
if (isTruthy(node.left)) {
|
|
1536
|
+
skippedNodes.add(node.right);
|
|
1537
|
+
} else if (!isFalsy(node.left)) {
|
|
1538
|
+
conditionalNodes.add(node.right);
|
|
1539
|
+
}
|
|
1540
|
+
}
|
|
1541
|
+
return;
|
|
1512
1542
|
case 'MemberExpression':
|
|
1513
1543
|
if (!isDynamicRequireModulesEnabled && isModuleRequire(node, scope)) {
|
|
1514
|
-
|
|
1515
|
-
|
|
1516
|
-
});
|
|
1544
|
+
uses.require = true;
|
|
1545
|
+
replacedDynamicRequires.push(node);
|
|
1517
1546
|
skippedNodes.add(node.object);
|
|
1518
1547
|
skippedNodes.add(node.property);
|
|
1519
1548
|
}
|
|
@@ -1529,12 +1558,18 @@ function transformCommonjs(
|
|
|
1529
1558
|
if (lexicalDepth === 0) {
|
|
1530
1559
|
uses.global = true;
|
|
1531
1560
|
if (!ignoreGlobal) {
|
|
1532
|
-
|
|
1533
|
-
storeName: true
|
|
1534
|
-
});
|
|
1561
|
+
replacedGlobal.push(node);
|
|
1535
1562
|
}
|
|
1536
1563
|
}
|
|
1537
1564
|
return;
|
|
1565
|
+
case 'TryStatement':
|
|
1566
|
+
if (currentTryBlockEnd === null) {
|
|
1567
|
+
currentTryBlockEnd = node.block.end;
|
|
1568
|
+
}
|
|
1569
|
+
if (currentConditionalNodeEnd === null) {
|
|
1570
|
+
currentConditionalNodeEnd = node.end;
|
|
1571
|
+
}
|
|
1572
|
+
return;
|
|
1538
1573
|
case 'UnaryExpression':
|
|
1539
1574
|
// rewrite `typeof module`, `typeof module.exports` and `typeof exports` (https://github.com/rollup/rollup-plugin-commonjs/issues/151)
|
|
1540
1575
|
if (node.operator === 'typeof') {
|
|
@@ -1571,34 +1606,45 @@ function transformCommonjs(
|
|
|
1571
1606
|
const nameBase = getName(id);
|
|
1572
1607
|
const exportsName = deconflict([...exportsAccessScopes], globals, nameBase);
|
|
1573
1608
|
const moduleName = deconflict([...moduleAccessScopes], globals, `${nameBase}Module`);
|
|
1609
|
+
const requireName = deconflict([scope], globals, `require${capitalize(nameBase)}`);
|
|
1610
|
+
const isRequiredName = deconflict([scope], globals, `hasRequired${capitalize(nameBase)}`);
|
|
1611
|
+
const helpersName = deconflict([scope], globals, 'commonjsHelpers');
|
|
1612
|
+
const dynamicRequireName = deconflict([scope], globals, 'commonjsRequire');
|
|
1574
1613
|
const deconflictedExportNames = Object.create(null);
|
|
1575
1614
|
for (const [exportName, { scopes }] of exportsAssignmentsByName) {
|
|
1576
1615
|
deconflictedExportNames[exportName] = deconflict([...scopes], globals, exportName);
|
|
1577
1616
|
}
|
|
1578
1617
|
|
|
1618
|
+
for (const node of replacedGlobal) {
|
|
1619
|
+
magicString.overwrite(node.start, node.end, `${helpersName}.commonjsGlobal`, {
|
|
1620
|
+
storeName: true
|
|
1621
|
+
});
|
|
1622
|
+
}
|
|
1623
|
+
for (const node of replacedDynamicRequires) {
|
|
1624
|
+
magicString.overwrite(node.start, node.end, dynamicRequireName, {
|
|
1625
|
+
contentOnly: true,
|
|
1626
|
+
storeName: true
|
|
1627
|
+
});
|
|
1628
|
+
}
|
|
1629
|
+
|
|
1579
1630
|
// We cannot wrap ES/mixed modules
|
|
1580
|
-
shouldWrap =
|
|
1581
|
-
!isEsModule &&
|
|
1582
|
-
!disableWrap &&
|
|
1583
|
-
(shouldWrap || (uses.exports && moduleExportsAssignments.length > 0));
|
|
1631
|
+
shouldWrap = !isEsModule && (shouldWrap || (uses.exports && moduleExportsAssignments.length > 0));
|
|
1584
1632
|
const detectWrappedDefault =
|
|
1585
1633
|
shouldWrap &&
|
|
1586
1634
|
(topLevelDefineCompiledEsmExpressions.length > 0 || code.indexOf('__esModule') >= 0);
|
|
1587
1635
|
|
|
1588
1636
|
if (
|
|
1589
1637
|
!(
|
|
1590
|
-
|
|
1591
|
-
|
|
1638
|
+
shouldWrap ||
|
|
1639
|
+
isRequired ||
|
|
1592
1640
|
uses.module ||
|
|
1593
1641
|
uses.exports ||
|
|
1594
1642
|
uses.require ||
|
|
1595
|
-
usesDynamicRequire ||
|
|
1596
|
-
hasRemovedRequire ||
|
|
1597
1643
|
topLevelDefineCompiledEsmExpressions.length > 0
|
|
1598
1644
|
) &&
|
|
1599
1645
|
(ignoreGlobal || !uses.global)
|
|
1600
1646
|
) {
|
|
1601
|
-
return { meta: { commonjs: { isCommonJS: false } } };
|
|
1647
|
+
return { meta: { commonjs: { isCommonJS: false, isMixedModule: false } } };
|
|
1602
1648
|
}
|
|
1603
1649
|
|
|
1604
1650
|
let leadingComment = '';
|
|
@@ -1620,19 +1666,22 @@ function transformCommonjs(
|
|
|
1620
1666
|
? 'exports'
|
|
1621
1667
|
: 'module';
|
|
1622
1668
|
|
|
1623
|
-
const importBlock = rewriteRequireExpressionsAndGetImportBlock(
|
|
1669
|
+
const { importBlock, usesRequireWrapper } = await rewriteRequireExpressionsAndGetImportBlock(
|
|
1624
1670
|
magicString,
|
|
1625
1671
|
topLevelDeclarations,
|
|
1626
|
-
topLevelRequireDeclarators,
|
|
1627
1672
|
reassignedNames,
|
|
1628
|
-
|
|
1629
|
-
|
|
1673
|
+
helpersName,
|
|
1674
|
+
dynamicRequireName,
|
|
1630
1675
|
moduleName,
|
|
1631
1676
|
exportsName,
|
|
1632
1677
|
id,
|
|
1633
|
-
exportMode
|
|
1678
|
+
exportMode,
|
|
1679
|
+
resolveRequireSourcesAndGetMeta,
|
|
1680
|
+
needsRequireWrapper,
|
|
1681
|
+
isEsModule,
|
|
1682
|
+
uses.require,
|
|
1683
|
+
getIgnoreTryCatchRequireStatementMode
|
|
1634
1684
|
);
|
|
1635
|
-
|
|
1636
1685
|
const exportBlock = isEsModule
|
|
1637
1686
|
? ''
|
|
1638
1687
|
: rewriteExportsAndGetExportsBlock(
|
|
@@ -1647,16 +1696,35 @@ function transformCommonjs(
|
|
|
1647
1696
|
topLevelDefineCompiledEsmExpressions,
|
|
1648
1697
|
deconflictedExportNames,
|
|
1649
1698
|
code,
|
|
1650
|
-
|
|
1699
|
+
helpersName,
|
|
1651
1700
|
exportMode,
|
|
1652
1701
|
detectWrappedDefault,
|
|
1653
|
-
defaultIsModuleExports
|
|
1702
|
+
defaultIsModuleExports,
|
|
1703
|
+
usesRequireWrapper,
|
|
1704
|
+
requireName
|
|
1654
1705
|
);
|
|
1655
1706
|
|
|
1656
1707
|
if (shouldWrap) {
|
|
1657
1708
|
wrapCode(magicString, uses, moduleName, exportsName);
|
|
1658
1709
|
}
|
|
1659
1710
|
|
|
1711
|
+
if (usesRequireWrapper) {
|
|
1712
|
+
magicString.trim().indent('\t');
|
|
1713
|
+
magicString.prepend(
|
|
1714
|
+
`var ${isRequiredName};
|
|
1715
|
+
|
|
1716
|
+
function ${requireName} () {
|
|
1717
|
+
\tif (${isRequiredName}) return ${exportsName};
|
|
1718
|
+
\t${isRequiredName} = 1;
|
|
1719
|
+
`
|
|
1720
|
+
).append(`
|
|
1721
|
+
\treturn ${exportsName};
|
|
1722
|
+
}`);
|
|
1723
|
+
if (exportMode === 'replace') {
|
|
1724
|
+
magicString.prepend(`var ${exportsName};\n`);
|
|
1725
|
+
}
|
|
1726
|
+
}
|
|
1727
|
+
|
|
1660
1728
|
magicString
|
|
1661
1729
|
.trim()
|
|
1662
1730
|
.prepend(leadingComment + importBlock)
|
|
@@ -1665,24 +1733,32 @@ function transformCommonjs(
|
|
|
1665
1733
|
return {
|
|
1666
1734
|
code: magicString.toString(),
|
|
1667
1735
|
map: sourceMap ? magicString.generateMap() : null,
|
|
1668
|
-
syntheticNamedExports: isEsModule ? false : '__moduleExports',
|
|
1669
|
-
meta: {
|
|
1736
|
+
syntheticNamedExports: isEsModule || usesRequireWrapper ? false : '__moduleExports',
|
|
1737
|
+
meta: {
|
|
1738
|
+
commonjs: {
|
|
1739
|
+
isCommonJS: !isEsModule && (usesRequireWrapper ? IS_WRAPPED_COMMONJS : true),
|
|
1740
|
+
isMixedModule: isEsModule
|
|
1741
|
+
}
|
|
1742
|
+
}
|
|
1670
1743
|
};
|
|
1671
1744
|
}
|
|
1672
1745
|
|
|
1673
1746
|
function commonjs(options = {}) {
|
|
1674
|
-
const extensions = options.extensions || ['.js'];
|
|
1675
|
-
const filter = pluginutils.createFilter(options.include, options.exclude);
|
|
1676
1747
|
const {
|
|
1677
1748
|
ignoreGlobal,
|
|
1678
1749
|
ignoreDynamicRequires,
|
|
1679
1750
|
requireReturnsDefault: requireReturnsDefaultOption,
|
|
1680
1751
|
esmExternals
|
|
1681
1752
|
} = options;
|
|
1753
|
+
const extensions = options.extensions || ['.js'];
|
|
1754
|
+
const filter = pluginutils.createFilter(options.include, options.exclude);
|
|
1755
|
+
const { strictRequiresFilter, detectCyclesAndConditional } = getStrictRequiresFilter(options);
|
|
1756
|
+
|
|
1682
1757
|
const getRequireReturnsDefault =
|
|
1683
1758
|
typeof requireReturnsDefaultOption === 'function'
|
|
1684
1759
|
? requireReturnsDefaultOption
|
|
1685
1760
|
: () => requireReturnsDefaultOption;
|
|
1761
|
+
|
|
1686
1762
|
let esmExternalIds;
|
|
1687
1763
|
const isEsmExternal =
|
|
1688
1764
|
typeof esmExternals === 'function'
|
|
@@ -1690,20 +1766,27 @@ function commonjs(options = {}) {
|
|
|
1690
1766
|
: Array.isArray(esmExternals)
|
|
1691
1767
|
? ((esmExternalIds = new Set(esmExternals)), (id) => esmExternalIds.has(id))
|
|
1692
1768
|
: () => esmExternals;
|
|
1769
|
+
|
|
1693
1770
|
const defaultIsModuleExports =
|
|
1694
1771
|
typeof options.defaultIsModuleExports === 'boolean' ? options.defaultIsModuleExports : 'auto';
|
|
1695
1772
|
|
|
1696
|
-
const {
|
|
1697
|
-
|
|
1773
|
+
const {
|
|
1774
|
+
resolveRequireSourcesAndGetMeta,
|
|
1775
|
+
getWrappedIds,
|
|
1776
|
+
isRequiredId
|
|
1777
|
+
} = getResolveRequireSourcesAndGetMeta(extensions, detectCyclesAndConditional);
|
|
1778
|
+
const dynamicRequireRoot =
|
|
1779
|
+
typeof options.dynamicRequireRoot === 'string'
|
|
1780
|
+
? path.resolve(options.dynamicRequireRoot)
|
|
1781
|
+
: process.cwd();
|
|
1782
|
+
const { commonDir, dynamicRequireModules } = getDynamicRequireModules(
|
|
1783
|
+
options.dynamicRequireTargets,
|
|
1784
|
+
dynamicRequireRoot
|
|
1698
1785
|
);
|
|
1699
|
-
const isDynamicRequireModulesEnabled =
|
|
1700
|
-
const commonDir = isDynamicRequireModulesEnabled
|
|
1701
|
-
? getCommonDir__default["default"](null, Array.from(dynamicRequireModuleSet).concat(process.cwd()))
|
|
1702
|
-
: null;
|
|
1786
|
+
const isDynamicRequireModulesEnabled = dynamicRequireModules.size > 0;
|
|
1703
1787
|
|
|
1704
1788
|
const esModulesWithDefaultExport = new Set();
|
|
1705
1789
|
const esModulesWithNamedExports = new Set();
|
|
1706
|
-
const commonJsMetaPromises = new Map();
|
|
1707
1790
|
|
|
1708
1791
|
const ignoreRequire =
|
|
1709
1792
|
typeof options.ignore === 'function'
|
|
@@ -1724,7 +1807,7 @@ function commonjs(options = {}) {
|
|
|
1724
1807
|
|
|
1725
1808
|
return {
|
|
1726
1809
|
canConvertRequire: mode !== 'remove' && mode !== true,
|
|
1727
|
-
|
|
1810
|
+
shouldRemoveRequire: mode === 'remove'
|
|
1728
1811
|
};
|
|
1729
1812
|
};
|
|
1730
1813
|
|
|
@@ -1733,12 +1816,6 @@ function commonjs(options = {}) {
|
|
|
1733
1816
|
const sourceMap = options.sourceMap !== false;
|
|
1734
1817
|
|
|
1735
1818
|
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
1819
|
const { isEsModule, hasDefaultExport, hasNamedExports, ast } = analyzeTopLevelStatements(
|
|
1743
1820
|
this.parse,
|
|
1744
1821
|
code,
|
|
@@ -1752,18 +1829,29 @@ function commonjs(options = {}) {
|
|
|
1752
1829
|
}
|
|
1753
1830
|
|
|
1754
1831
|
if (
|
|
1755
|
-
!
|
|
1756
|
-
(!hasCjsKeywords(code, ignoreGlobal) || (
|
|
1832
|
+
!dynamicRequireModules.has(normalizePathSlashes(id)) &&
|
|
1833
|
+
(!(hasCjsKeywords(code, ignoreGlobal) || isRequiredId(id)) ||
|
|
1834
|
+
(isEsModule && !options.transformMixedEsModules))
|
|
1757
1835
|
) {
|
|
1758
1836
|
return { meta: { commonjs: { isCommonJS: false } } };
|
|
1759
1837
|
}
|
|
1760
1838
|
|
|
1761
|
-
|
|
1762
|
-
|
|
1763
|
-
|
|
1764
|
-
|
|
1765
|
-
|
|
1766
|
-
|
|
1839
|
+
const needsRequireWrapper =
|
|
1840
|
+
!isEsModule &&
|
|
1841
|
+
(dynamicRequireModules.has(normalizePathSlashes(id)) || strictRequiresFilter(id));
|
|
1842
|
+
|
|
1843
|
+
const checkDynamicRequire = () => {
|
|
1844
|
+
if (id.indexOf(dynamicRequireRoot) !== 0) {
|
|
1845
|
+
this.error({
|
|
1846
|
+
code: 'DYNAMIC_REQUIRE_OUTSIDE_ROOT',
|
|
1847
|
+
id,
|
|
1848
|
+
dynamicRequireRoot,
|
|
1849
|
+
message: `"${id}" contains dynamic require statements but it is not within the current dynamicRequireRoot "${dynamicRequireRoot}". You should set dynamicRequireRoot to "${path.dirname(
|
|
1850
|
+
id
|
|
1851
|
+
)}" or one of its parent directories.`
|
|
1852
|
+
});
|
|
1853
|
+
}
|
|
1854
|
+
};
|
|
1767
1855
|
|
|
1768
1856
|
return transformCommonjs(
|
|
1769
1857
|
this.parse,
|
|
@@ -1776,17 +1864,37 @@ function commonjs(options = {}) {
|
|
|
1776
1864
|
getIgnoreTryCatchRequireStatementMode,
|
|
1777
1865
|
sourceMap,
|
|
1778
1866
|
isDynamicRequireModulesEnabled,
|
|
1779
|
-
|
|
1780
|
-
disableWrap,
|
|
1867
|
+
dynamicRequireModules,
|
|
1781
1868
|
commonDir,
|
|
1782
1869
|
ast,
|
|
1783
|
-
defaultIsModuleExports
|
|
1870
|
+
defaultIsModuleExports,
|
|
1871
|
+
needsRequireWrapper,
|
|
1872
|
+
resolveRequireSourcesAndGetMeta(this),
|
|
1873
|
+
isRequiredId(id),
|
|
1874
|
+
checkDynamicRequire
|
|
1784
1875
|
);
|
|
1785
1876
|
}
|
|
1786
1877
|
|
|
1787
1878
|
return {
|
|
1788
1879
|
name: 'commonjs',
|
|
1789
1880
|
|
|
1881
|
+
version,
|
|
1882
|
+
|
|
1883
|
+
options(rawOptions) {
|
|
1884
|
+
// We inject the resolver in the beginning so that "catch-all-resolver" like node-resolver
|
|
1885
|
+
// do not prevent our plugin from resolving entry points ot proxies.
|
|
1886
|
+
const plugins = Array.isArray(rawOptions.plugins)
|
|
1887
|
+
? rawOptions.plugins
|
|
1888
|
+
: rawOptions.plugins
|
|
1889
|
+
? [rawOptions.plugins]
|
|
1890
|
+
: [];
|
|
1891
|
+
plugins.unshift({
|
|
1892
|
+
name: 'commonjs--resolver',
|
|
1893
|
+
resolveId
|
|
1894
|
+
});
|
|
1895
|
+
return { ...rawOptions, plugins };
|
|
1896
|
+
},
|
|
1897
|
+
|
|
1790
1898
|
buildStart() {
|
|
1791
1899
|
validateRollupVersion(this.meta.rollupVersion, peerDependencies.rollup);
|
|
1792
1900
|
if (options.namedExports != null) {
|
|
@@ -1796,44 +1904,43 @@ function commonjs(options = {}) {
|
|
|
1796
1904
|
}
|
|
1797
1905
|
},
|
|
1798
1906
|
|
|
1799
|
-
|
|
1907
|
+
buildEnd() {
|
|
1908
|
+
if (options.strictRequires === 'debug') {
|
|
1909
|
+
const wrappedIds = getWrappedIds();
|
|
1910
|
+
if (wrappedIds.length) {
|
|
1911
|
+
this.warn({
|
|
1912
|
+
code: 'WRAPPED_IDS',
|
|
1913
|
+
ids: wrappedIds,
|
|
1914
|
+
message: `The commonjs plugin automatically wrapped the following files:\n[\n${wrappedIds
|
|
1915
|
+
.map((id) => `\t${JSON.stringify(path.relative(process.cwd(), id))}`)
|
|
1916
|
+
.join(',\n')}\n]`
|
|
1917
|
+
});
|
|
1918
|
+
} else {
|
|
1919
|
+
this.warn({
|
|
1920
|
+
code: 'WRAPPED_IDS',
|
|
1921
|
+
ids: wrappedIds,
|
|
1922
|
+
message: 'The commonjs plugin did not wrap any files.'
|
|
1923
|
+
});
|
|
1924
|
+
}
|
|
1925
|
+
}
|
|
1926
|
+
},
|
|
1800
1927
|
|
|
1801
1928
|
load(id) {
|
|
1802
1929
|
if (id === HELPERS_ID) {
|
|
1803
|
-
return getHelpersModule(
|
|
1804
|
-
}
|
|
1805
|
-
|
|
1806
|
-
if (id.startsWith(HELPERS_ID)) {
|
|
1807
|
-
return getSpecificHelperProxy(id);
|
|
1930
|
+
return getHelpersModule();
|
|
1808
1931
|
}
|
|
1809
1932
|
|
|
1810
1933
|
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
|
-
}
|
|
1934
|
+
const name = getName(unwrapId(id, MODULE_SUFFIX));
|
|
1827
1935
|
return {
|
|
1828
|
-
code
|
|
1936
|
+
code: `var ${name} = {exports: {}}; export {${name} as __module}`,
|
|
1829
1937
|
syntheticNamedExports: '__module',
|
|
1830
1938
|
meta: { commonjs: { isCommonJS: false } }
|
|
1831
1939
|
};
|
|
1832
1940
|
}
|
|
1833
1941
|
|
|
1834
1942
|
if (isWrappedId(id, EXPORTS_SUFFIX)) {
|
|
1835
|
-
const
|
|
1836
|
-
const name = getName(actualId);
|
|
1943
|
+
const name = getName(unwrapId(id, EXPORTS_SUFFIX));
|
|
1837
1944
|
return {
|
|
1838
1945
|
code: `var ${name} = {}; export {${name} as __exports}`,
|
|
1839
1946
|
meta: { commonjs: { isCommonJS: false } }
|
|
@@ -1848,22 +1955,16 @@ function commonjs(options = {}) {
|
|
|
1848
1955
|
);
|
|
1849
1956
|
}
|
|
1850
1957
|
|
|
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))});`;
|
|
1958
|
+
if (isWrappedId(id, ES_IMPORT_SUFFIX)) {
|
|
1959
|
+
return getEsImportProxy(unwrapId(id, ES_IMPORT_SUFFIX), defaultIsModuleExports);
|
|
1861
1960
|
}
|
|
1862
1961
|
|
|
1863
|
-
if (
|
|
1864
|
-
return
|
|
1865
|
-
|
|
1866
|
-
|
|
1962
|
+
if (id === DYNAMIC_MODULES_ID) {
|
|
1963
|
+
return getDynamicModuleRegistry(
|
|
1964
|
+
isDynamicRequireModulesEnabled,
|
|
1965
|
+
dynamicRequireModules,
|
|
1966
|
+
commonDir,
|
|
1967
|
+
ignoreDynamicRequires
|
|
1867
1968
|
);
|
|
1868
1969
|
}
|
|
1869
1970
|
|
|
@@ -1874,43 +1975,24 @@ function commonjs(options = {}) {
|
|
|
1874
1975
|
getRequireReturnsDefault(actualId),
|
|
1875
1976
|
esModulesWithDefaultExport,
|
|
1876
1977
|
esModulesWithNamedExports,
|
|
1877
|
-
|
|
1978
|
+
this.load
|
|
1878
1979
|
);
|
|
1879
1980
|
}
|
|
1880
1981
|
|
|
1881
1982
|
return null;
|
|
1882
1983
|
},
|
|
1883
1984
|
|
|
1884
|
-
transform(code,
|
|
1885
|
-
let id = rawId;
|
|
1886
|
-
|
|
1887
|
-
if (isWrappedId(id, DYNAMIC_REGISTER_SUFFIX)) {
|
|
1888
|
-
id = unwrapId(id, DYNAMIC_REGISTER_SUFFIX);
|
|
1889
|
-
}
|
|
1890
|
-
|
|
1985
|
+
transform(code, id) {
|
|
1891
1986
|
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
|
-
) {
|
|
1987
|
+
if (extName !== '.cjs' && (!filter(id) || !extensions.includes(extName))) {
|
|
1898
1988
|
return null;
|
|
1899
1989
|
}
|
|
1900
1990
|
|
|
1901
1991
|
try {
|
|
1902
|
-
return transformAndCheckExports.call(this, code,
|
|
1992
|
+
return transformAndCheckExports.call(this, code, id);
|
|
1903
1993
|
} catch (err) {
|
|
1904
1994
|
return this.error(err, err.loc);
|
|
1905
1995
|
}
|
|
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
1996
|
}
|
|
1915
1997
|
};
|
|
1916
1998
|
}
|