@rollup/plugin-commonjs 21.0.1 → 22.0.0-11
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.js → cjs/index.js} +1028 -823
- package/dist/cjs/index.js.map +1 -0
- package/dist/es/index.js +2115 -0
- package/dist/es/index.js.map +1 -0
- package/dist/es/package.json +1 -0
- package/package.json +12 -7
- package/types/index.d.ts +52 -10
- package/dist/index.es.js +0 -1910
- package/dist/index.es.js.map +0 -1
- package/dist/index.js.map +0 -1
|
@@ -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-11";
|
|
20
20
|
var peerDependencies = {
|
|
21
|
-
rollup: "^2.
|
|
21
|
+
rollup: "^2.67.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,103 +172,249 @@ 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
|
-
resolve,
|
|
478
|
-
promise: null
|
|
479
|
-
};
|
|
480
|
-
commonJSMetaPromises.set(id, commonJSMetaPromise);
|
|
481
|
-
});
|
|
482
|
-
commonJSMetaPromise.promise = promise;
|
|
206
|
+
const COMMONJS_REQUIRE_EXPORT = 'commonjsRequire';
|
|
207
|
+
const CREATE_COMMONJS_REQUIRE_EXPORT = 'createCommonjsRequire';
|
|
483
208
|
|
|
484
|
-
|
|
209
|
+
function getDynamicModuleRegistry(
|
|
210
|
+
isDynamicRequireModulesEnabled,
|
|
211
|
+
dynamicRequireModules,
|
|
212
|
+
commonDir,
|
|
213
|
+
ignoreDynamicRequires
|
|
214
|
+
) {
|
|
215
|
+
if (!isDynamicRequireModulesEnabled) {
|
|
216
|
+
return `export function ${COMMONJS_REQUIRE_EXPORT}(path) {
|
|
217
|
+
${FAILED_REQUIRE_ERROR}
|
|
218
|
+
}`;
|
|
219
|
+
}
|
|
220
|
+
const dynamicModuleImports = [...dynamicRequireModules.values()]
|
|
221
|
+
.map(
|
|
222
|
+
(id, index) =>
|
|
223
|
+
`import ${
|
|
224
|
+
id.endsWith('.json') ? `json${index}` : `{ __require as require${index} }`
|
|
225
|
+
} from ${JSON.stringify(id)};`
|
|
226
|
+
)
|
|
227
|
+
.join('\n');
|
|
228
|
+
const dynamicModuleProps = [...dynamicRequireModules.keys()]
|
|
229
|
+
.map(
|
|
230
|
+
(id, index) =>
|
|
231
|
+
`\t\t${JSON.stringify(getVirtualPathForDynamicRequirePath(id, commonDir))}: ${
|
|
232
|
+
id.endsWith('.json') ? `function () { return json${index}; }` : `require${index}`
|
|
233
|
+
}`
|
|
234
|
+
)
|
|
235
|
+
.join(',\n');
|
|
236
|
+
return `${dynamicModuleImports}
|
|
237
|
+
|
|
238
|
+
var dynamicModules;
|
|
239
|
+
|
|
240
|
+
function getDynamicModules() {
|
|
241
|
+
return dynamicModules || (dynamicModules = {
|
|
242
|
+
${dynamicModuleProps}
|
|
243
|
+
});
|
|
485
244
|
}
|
|
486
245
|
|
|
487
|
-
function
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
246
|
+
export function ${CREATE_COMMONJS_REQUIRE_EXPORT}(originalModuleDir) {
|
|
247
|
+
function handleRequire(path) {
|
|
248
|
+
var resolvedPath = commonjsResolve(path, originalModuleDir);
|
|
249
|
+
if (resolvedPath !== null) {
|
|
250
|
+
return getDynamicModules()[resolvedPath]();
|
|
251
|
+
}
|
|
252
|
+
${ignoreDynamicRequires ? 'return require(path);' : FAILED_REQUIRE_ERROR}
|
|
253
|
+
}
|
|
254
|
+
handleRequire.resolve = function (path) {
|
|
255
|
+
var resolvedPath = commonjsResolve(path, originalModuleDir);
|
|
256
|
+
if (resolvedPath !== null) {
|
|
257
|
+
return resolvedPath;
|
|
258
|
+
}
|
|
259
|
+
return require.resolve(path);
|
|
260
|
+
}
|
|
261
|
+
return handleRequire;
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
function commonjsResolve (path, originalModuleDir) {
|
|
265
|
+
var shouldTryNodeModules = isPossibleNodeModulesPath(path);
|
|
266
|
+
path = normalize(path);
|
|
267
|
+
var relPath;
|
|
268
|
+
if (path[0] === '/') {
|
|
269
|
+
originalModuleDir = '';
|
|
270
|
+
}
|
|
271
|
+
var modules = getDynamicModules();
|
|
272
|
+
var checkedExtensions = ['', '.js', '.json'];
|
|
273
|
+
while (true) {
|
|
274
|
+
if (!shouldTryNodeModules) {
|
|
275
|
+
relPath = normalize(originalModuleDir + '/' + path);
|
|
276
|
+
} else {
|
|
277
|
+
relPath = normalize(originalModuleDir + '/node_modules/' + path);
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
if (relPath.endsWith('/..')) {
|
|
281
|
+
break; // Travelled too far up, avoid infinite loop
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
for (var extensionIndex = 0; extensionIndex < checkedExtensions.length; extensionIndex++) {
|
|
285
|
+
var resolvedPath = relPath + checkedExtensions[extensionIndex];
|
|
286
|
+
if (modules[resolvedPath]) {
|
|
287
|
+
return resolvedPath;
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
if (!shouldTryNodeModules) break;
|
|
291
|
+
var nextDir = normalize(originalModuleDir + '/..');
|
|
292
|
+
if (nextDir === originalModuleDir) break;
|
|
293
|
+
originalModuleDir = nextDir;
|
|
294
|
+
}
|
|
295
|
+
return null;
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
function isPossibleNodeModulesPath (modulePath) {
|
|
299
|
+
var c0 = modulePath[0];
|
|
300
|
+
if (c0 === '/' || c0 === '\\\\') return false;
|
|
301
|
+
var c1 = modulePath[1], c2 = modulePath[2];
|
|
302
|
+
if ((c0 === '.' && (!c1 || c1 === '/' || c1 === '\\\\')) ||
|
|
303
|
+
(c0 === '.' && c1 === '.' && (!c2 || c2 === '/' || c2 === '\\\\'))) return false;
|
|
304
|
+
if (c1 === ':' && (c2 === '/' || c2 === '\\\\')) return false;
|
|
305
|
+
return true;
|
|
497
306
|
}
|
|
498
307
|
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
308
|
+
function normalize (path) {
|
|
309
|
+
path = path.replace(/\\\\/g, '/');
|
|
310
|
+
var parts = path.split('/');
|
|
311
|
+
var slashed = parts[0] === '';
|
|
312
|
+
for (var i = 1; i < parts.length; i++) {
|
|
313
|
+
if (parts[i] === '.' || parts[i] === '') {
|
|
314
|
+
parts.splice(i--, 1);
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
for (var i = 1; i < parts.length; i++) {
|
|
318
|
+
if (parts[i] !== '..') continue;
|
|
319
|
+
if (i > 0 && parts[i - 1] !== '..' && parts[i - 1] !== '.') {
|
|
320
|
+
parts.splice(--i, 2);
|
|
321
|
+
i--;
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
path = parts.join('/');
|
|
325
|
+
if (slashed && path[0] !== '/') path = '/' + path;
|
|
326
|
+
else if (path.length === 0) path = '.';
|
|
327
|
+
return path;
|
|
328
|
+
}`;
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
const isWrappedId = (id, suffix) => id.endsWith(suffix);
|
|
332
|
+
const wrapId = (id, suffix) => `\0${id}${suffix}`;
|
|
333
|
+
const unwrapId = (wrappedId, suffix) => wrappedId.slice(1, -suffix.length);
|
|
334
|
+
|
|
335
|
+
const PROXY_SUFFIX = '?commonjs-proxy';
|
|
336
|
+
const WRAPPED_SUFFIX = '?commonjs-wrapped';
|
|
337
|
+
const EXTERNAL_SUFFIX = '?commonjs-external';
|
|
338
|
+
const EXPORTS_SUFFIX = '?commonjs-exports';
|
|
339
|
+
const MODULE_SUFFIX = '?commonjs-module';
|
|
340
|
+
const ENTRY_SUFFIX = '?commonjs-entry';
|
|
341
|
+
const ES_IMPORT_SUFFIX = '?commonjs-es-import';
|
|
342
|
+
|
|
343
|
+
const DYNAMIC_MODULES_ID = '\0commonjs-dynamic-modules';
|
|
344
|
+
const HELPERS_ID = '\0commonjsHelpers.js';
|
|
345
|
+
|
|
346
|
+
const IS_WRAPPED_COMMONJS = 'withRequireFunction';
|
|
347
|
+
|
|
348
|
+
// `x['default']` is used instead of `x.default` for backward compatibility with ES3 browsers.
|
|
349
|
+
// Minifiers like uglify will usually transpile it back if compatibility with ES3 is not enabled.
|
|
350
|
+
// This could be improved by inspecting Rollup's "generatedCode" option
|
|
351
|
+
|
|
352
|
+
const HELPERS = `
|
|
353
|
+
export var commonjsGlobal = typeof globalThis !== 'undefined' ? globalThis : typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {};
|
|
354
|
+
|
|
355
|
+
export function getDefaultExportFromCjs (x) {
|
|
356
|
+
return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
export function getDefaultExportFromNamespaceIfPresent (n) {
|
|
360
|
+
return n && Object.prototype.hasOwnProperty.call(n, 'default') ? n['default'] : n;
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
export function getDefaultExportFromNamespaceIfNotNamed (n) {
|
|
364
|
+
return n && Object.prototype.hasOwnProperty.call(n, 'default') && Object.keys(n).length === 1 ? n['default'] : n;
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
export function getAugmentedNamespace(n) {
|
|
368
|
+
var f = n.default;
|
|
369
|
+
if (typeof f == "function") {
|
|
370
|
+
var a = function () {
|
|
371
|
+
return f.apply(this, arguments);
|
|
372
|
+
};
|
|
373
|
+
a.prototype = f.prototype;
|
|
374
|
+
} else a = {};
|
|
375
|
+
Object.defineProperty(a, '__esModule', {value: true});
|
|
376
|
+
Object.keys(n).forEach(function (k) {
|
|
377
|
+
var d = Object.getOwnPropertyDescriptor(n, k);
|
|
378
|
+
Object.defineProperty(a, k, d.get ? d : {
|
|
379
|
+
enumerable: true,
|
|
380
|
+
get: function () {
|
|
381
|
+
return n[k];
|
|
382
|
+
}
|
|
383
|
+
});
|
|
384
|
+
});
|
|
385
|
+
return a;
|
|
386
|
+
}
|
|
387
|
+
`;
|
|
388
|
+
|
|
389
|
+
function getHelpersModule() {
|
|
390
|
+
return HELPERS;
|
|
502
391
|
}
|
|
503
392
|
|
|
504
393
|
function getUnknownRequireProxy(id, requireReturnsDefault) {
|
|
505
394
|
if (requireReturnsDefault === true || id.endsWith('.json')) {
|
|
506
|
-
return `export {default} from ${JSON.stringify(id)};`;
|
|
395
|
+
return `export { default } from ${JSON.stringify(id)};`;
|
|
507
396
|
}
|
|
508
397
|
const name = getName(id);
|
|
509
398
|
const exported =
|
|
510
399
|
requireReturnsDefault === 'auto'
|
|
511
|
-
? `import {getDefaultExportFromNamespaceIfNotNamed} from "${HELPERS_ID}"; export default /*@__PURE__*/getDefaultExportFromNamespaceIfNotNamed(${name});`
|
|
400
|
+
? `import { getDefaultExportFromNamespaceIfNotNamed } from "${HELPERS_ID}"; export default /*@__PURE__*/getDefaultExportFromNamespaceIfNotNamed(${name});`
|
|
512
401
|
: requireReturnsDefault === 'preferred'
|
|
513
|
-
? `import {getDefaultExportFromNamespaceIfPresent} from "${HELPERS_ID}"; export default /*@__PURE__*/getDefaultExportFromNamespaceIfPresent(${name});`
|
|
402
|
+
? `import { getDefaultExportFromNamespaceIfPresent } from "${HELPERS_ID}"; export default /*@__PURE__*/getDefaultExportFromNamespaceIfPresent(${name});`
|
|
514
403
|
: !requireReturnsDefault
|
|
515
|
-
? `import {getAugmentedNamespace} from "${HELPERS_ID}"; export default /*@__PURE__*/getAugmentedNamespace(${name});`
|
|
404
|
+
? `import { getAugmentedNamespace } from "${HELPERS_ID}"; export default /*@__PURE__*/getAugmentedNamespace(${name});`
|
|
516
405
|
: `export default ${name};`;
|
|
517
406
|
return `import * as ${name} from ${JSON.stringify(id)}; ${exported}`;
|
|
518
407
|
}
|
|
519
408
|
|
|
520
|
-
function
|
|
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
|
-
async function getStaticRequireProxy(
|
|
538
|
-
id,
|
|
539
|
-
requireReturnsDefault,
|
|
540
|
-
esModulesWithDefaultExport,
|
|
541
|
-
esModulesWithNamedExports,
|
|
542
|
-
commonJsMetaPromises
|
|
543
|
-
) {
|
|
409
|
+
async function getStaticRequireProxy(id, requireReturnsDefault, loadModule) {
|
|
544
410
|
const name = getName(id);
|
|
545
|
-
const
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
411
|
+
const {
|
|
412
|
+
meta: { commonjs: commonjsMeta }
|
|
413
|
+
} = await loadModule({ id });
|
|
414
|
+
if (!commonjsMeta) {
|
|
549
415
|
return getUnknownRequireProxy(id, requireReturnsDefault);
|
|
416
|
+
} else if (commonjsMeta.isCommonJS) {
|
|
417
|
+
return `export { __moduleExports as default } from ${JSON.stringify(id)};`;
|
|
550
418
|
} else if (!requireReturnsDefault) {
|
|
551
419
|
return `import { getAugmentedNamespace } from "${HELPERS_ID}"; import * as ${name} from ${JSON.stringify(
|
|
552
420
|
id
|
|
@@ -554,14 +422,51 @@ async function getStaticRequireProxy(
|
|
|
554
422
|
} else if (
|
|
555
423
|
requireReturnsDefault !== true &&
|
|
556
424
|
(requireReturnsDefault === 'namespace' ||
|
|
557
|
-
!
|
|
558
|
-
(requireReturnsDefault === 'auto' &&
|
|
425
|
+
!commonjsMeta.hasDefaultExport ||
|
|
426
|
+
(requireReturnsDefault === 'auto' && commonjsMeta.hasNamedExports))
|
|
559
427
|
) {
|
|
560
428
|
return `import * as ${name} from ${JSON.stringify(id)}; export default ${name};`;
|
|
561
429
|
}
|
|
562
430
|
return `export { default } from ${JSON.stringify(id)};`;
|
|
563
431
|
}
|
|
564
432
|
|
|
433
|
+
function getEntryProxy(id, defaultIsModuleExports, getModuleInfo) {
|
|
434
|
+
const {
|
|
435
|
+
meta: { commonjs: commonjsMeta },
|
|
436
|
+
hasDefaultExport
|
|
437
|
+
} = getModuleInfo(id);
|
|
438
|
+
if (!commonjsMeta || commonjsMeta.isCommonJS !== IS_WRAPPED_COMMONJS) {
|
|
439
|
+
const stringifiedId = JSON.stringify(id);
|
|
440
|
+
let code = `export * from ${stringifiedId};`;
|
|
441
|
+
if (hasDefaultExport) {
|
|
442
|
+
code += `export { default } from ${stringifiedId};`;
|
|
443
|
+
}
|
|
444
|
+
return code;
|
|
445
|
+
}
|
|
446
|
+
return getEsImportProxy(id, defaultIsModuleExports);
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
function getEsImportProxy(id, defaultIsModuleExports) {
|
|
450
|
+
const name = getName(id);
|
|
451
|
+
const exportsName = `${name}Exports`;
|
|
452
|
+
const requireModule = `require${capitalize(name)}`;
|
|
453
|
+
let code =
|
|
454
|
+
`import { getDefaultExportFromCjs } from "${HELPERS_ID}";\n` +
|
|
455
|
+
`import { __require as ${requireModule} } from ${JSON.stringify(id)};\n` +
|
|
456
|
+
`var ${exportsName} = ${requireModule}();\n` +
|
|
457
|
+
`export { ${exportsName} as __moduleExports };`;
|
|
458
|
+
if (defaultIsModuleExports) {
|
|
459
|
+
code += `\nexport { ${exportsName} as default };`;
|
|
460
|
+
} else {
|
|
461
|
+
code += `export default /*@__PURE__*/getDefaultExportFromCjs(${exportsName});`;
|
|
462
|
+
}
|
|
463
|
+
return {
|
|
464
|
+
code,
|
|
465
|
+
syntheticNamedExports: '__moduleExports',
|
|
466
|
+
meta: { commonjs: { isCommonJS: false } }
|
|
467
|
+
};
|
|
468
|
+
}
|
|
469
|
+
|
|
565
470
|
/* eslint-disable no-param-reassign, no-undefined */
|
|
566
471
|
|
|
567
472
|
function getCandidatesForExtension(resolved, extension) {
|
|
@@ -575,112 +480,291 @@ function getCandidates(resolved, extensions) {
|
|
|
575
480
|
);
|
|
576
481
|
}
|
|
577
482
|
|
|
483
|
+
function resolveExtensions(importee, importer, extensions) {
|
|
484
|
+
// not our problem
|
|
485
|
+
if (importee[0] !== '.' || !importer) return undefined;
|
|
486
|
+
|
|
487
|
+
const resolved = path.resolve(path.dirname(importer), importee);
|
|
488
|
+
const candidates = getCandidates(resolved, extensions);
|
|
489
|
+
|
|
490
|
+
for (let i = 0; i < candidates.length; i += 1) {
|
|
491
|
+
try {
|
|
492
|
+
const stats = fs.statSync(candidates[i]);
|
|
493
|
+
if (stats.isFile()) return { id: candidates[i] };
|
|
494
|
+
} catch (err) {
|
|
495
|
+
/* noop */
|
|
496
|
+
}
|
|
497
|
+
}
|
|
498
|
+
|
|
499
|
+
return undefined;
|
|
500
|
+
}
|
|
501
|
+
|
|
578
502
|
function getResolveId(extensions) {
|
|
579
|
-
function
|
|
580
|
-
//
|
|
581
|
-
|
|
503
|
+
return async function resolveId(importee, importer, resolveOptions) {
|
|
504
|
+
// We assume that all requires are pre-resolved
|
|
505
|
+
const customOptions = resolveOptions.custom;
|
|
506
|
+
if (customOptions && customOptions['node-resolve'] && customOptions['node-resolve'].isRequire) {
|
|
507
|
+
return null;
|
|
508
|
+
}
|
|
509
|
+
if (isWrappedId(importee, WRAPPED_SUFFIX)) {
|
|
510
|
+
return unwrapId(importee, WRAPPED_SUFFIX);
|
|
511
|
+
}
|
|
582
512
|
|
|
583
|
-
|
|
584
|
-
|
|
513
|
+
if (
|
|
514
|
+
importee.endsWith(ENTRY_SUFFIX) ||
|
|
515
|
+
isWrappedId(importee, MODULE_SUFFIX) ||
|
|
516
|
+
isWrappedId(importee, EXPORTS_SUFFIX) ||
|
|
517
|
+
isWrappedId(importee, PROXY_SUFFIX) ||
|
|
518
|
+
isWrappedId(importee, ES_IMPORT_SUFFIX) ||
|
|
519
|
+
isWrappedId(importee, EXTERNAL_SUFFIX) ||
|
|
520
|
+
importee.startsWith(HELPERS_ID) ||
|
|
521
|
+
importee === DYNAMIC_MODULES_ID
|
|
522
|
+
) {
|
|
523
|
+
return importee;
|
|
524
|
+
}
|
|
585
525
|
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
526
|
+
if (importer) {
|
|
527
|
+
if (
|
|
528
|
+
importer === DYNAMIC_MODULES_ID ||
|
|
529
|
+
// Proxies are only importing resolved ids, no need to resolve again
|
|
530
|
+
isWrappedId(importer, PROXY_SUFFIX) ||
|
|
531
|
+
isWrappedId(importer, ES_IMPORT_SUFFIX) ||
|
|
532
|
+
importer.endsWith(ENTRY_SUFFIX)
|
|
533
|
+
) {
|
|
534
|
+
return importee;
|
|
535
|
+
}
|
|
536
|
+
if (isWrappedId(importer, EXTERNAL_SUFFIX)) {
|
|
537
|
+
// We need to return null for unresolved imports so that the proper warning is shown
|
|
538
|
+
if (!(await this.resolve(importee, importer, { skipSelf: true }))) {
|
|
539
|
+
return null;
|
|
540
|
+
}
|
|
541
|
+
// For other external imports, we need to make sure they are handled as external
|
|
542
|
+
return { id: importee, external: true };
|
|
592
543
|
}
|
|
593
544
|
}
|
|
594
545
|
|
|
595
|
-
|
|
596
|
-
|
|
546
|
+
if (importee.startsWith('\0')) {
|
|
547
|
+
return null;
|
|
548
|
+
}
|
|
597
549
|
|
|
598
|
-
|
|
599
|
-
if
|
|
600
|
-
|
|
550
|
+
// If this is an entry point or ESM import, we need to figure out if the importee is wrapped and
|
|
551
|
+
// if that is the case, we need to add a proxy.
|
|
552
|
+
const resolved =
|
|
553
|
+
(await this.resolve(importee, importer, Object.assign({ skipSelf: true }, resolveOptions))) ||
|
|
554
|
+
resolveExtensions(importee, importer, extensions);
|
|
555
|
+
// Make sure that even if other plugins resolve again, we ignore our own proxies
|
|
556
|
+
if (
|
|
557
|
+
!resolved ||
|
|
558
|
+
resolved.external ||
|
|
559
|
+
resolved.id.endsWith(ENTRY_SUFFIX) ||
|
|
560
|
+
isWrappedId(resolved.id, ES_IMPORT_SUFFIX)
|
|
561
|
+
) {
|
|
562
|
+
return resolved;
|
|
563
|
+
}
|
|
564
|
+
const moduleInfo = await this.load(resolved);
|
|
565
|
+
if (resolveOptions.isEntry) {
|
|
566
|
+
moduleInfo.moduleSideEffects = true;
|
|
567
|
+
// We must not precede entry proxies with a `\0` as that will mess up relative external resolution
|
|
568
|
+
return resolved.id + ENTRY_SUFFIX;
|
|
569
|
+
}
|
|
570
|
+
const {
|
|
571
|
+
meta: { commonjs: commonjsMeta }
|
|
572
|
+
} = moduleInfo;
|
|
573
|
+
if (commonjsMeta && commonjsMeta.isCommonJS === IS_WRAPPED_COMMONJS) {
|
|
574
|
+
return wrapId(resolved.id, ES_IMPORT_SUFFIX);
|
|
575
|
+
}
|
|
576
|
+
return resolved;
|
|
577
|
+
};
|
|
578
|
+
}
|
|
579
|
+
|
|
580
|
+
function getRequireResolver(extensions, detectCyclesAndConditional) {
|
|
581
|
+
const knownCjsModuleTypes = Object.create(null);
|
|
582
|
+
const requiredIds = Object.create(null);
|
|
583
|
+
const unconditionallyRequiredIds = Object.create(null);
|
|
584
|
+
const dependencies = Object.create(null);
|
|
585
|
+
const getDependencies = (id) => dependencies[id] || (dependencies[id] = new Set());
|
|
586
|
+
|
|
587
|
+
const isCyclic = (id) => {
|
|
588
|
+
const dependenciesToCheck = new Set(getDependencies(id));
|
|
589
|
+
for (const dependency of dependenciesToCheck) {
|
|
590
|
+
if (dependency === id) {
|
|
591
|
+
return true;
|
|
592
|
+
}
|
|
593
|
+
for (const childDependency of getDependencies(dependency)) {
|
|
594
|
+
dependenciesToCheck.add(childDependency);
|
|
595
|
+
}
|
|
601
596
|
}
|
|
597
|
+
return false;
|
|
598
|
+
};
|
|
602
599
|
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
600
|
+
// Once a module is listed here, its type (wrapped or not) is fixed any may
|
|
601
|
+
// not change for the rest of the current build, to not break already
|
|
602
|
+
// transformed modules.
|
|
603
|
+
const fullyAnalyzedModules = Object.create(null);
|
|
607
604
|
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
if (
|
|
611
|
-
return
|
|
605
|
+
const getTypeForFullyAnalyzedModule = (id) => {
|
|
606
|
+
const knownType = knownCjsModuleTypes[id];
|
|
607
|
+
if (knownType !== true || !detectCyclesAndConditional || fullyAnalyzedModules[id]) {
|
|
608
|
+
return knownType;
|
|
612
609
|
}
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
const isRequiredModule = isWrappedId(importee, REQUIRE_SUFFIX);
|
|
616
|
-
let isModuleRegistration = false;
|
|
617
|
-
|
|
618
|
-
if (isProxyModule) {
|
|
619
|
-
importee = unwrapId(importee, PROXY_SUFFIX);
|
|
620
|
-
} else if (isRequiredModule) {
|
|
621
|
-
importee = unwrapId(importee, REQUIRE_SUFFIX);
|
|
622
|
-
|
|
623
|
-
isModuleRegistration = isWrappedId(importee, DYNAMIC_REGISTER_SUFFIX);
|
|
624
|
-
if (isModuleRegistration) {
|
|
625
|
-
importee = unwrapId(importee, DYNAMIC_REGISTER_SUFFIX);
|
|
626
|
-
}
|
|
610
|
+
if (isCyclic(id)) {
|
|
611
|
+
return (knownCjsModuleTypes[id] = IS_WRAPPED_COMMONJS);
|
|
627
612
|
}
|
|
613
|
+
return knownType;
|
|
614
|
+
};
|
|
628
615
|
|
|
616
|
+
const setInitialParentType = (id, initialCommonJSType) => {
|
|
617
|
+
// Fully analyzed modules may never change type
|
|
618
|
+
if (fullyAnalyzedModules[id]) {
|
|
619
|
+
return;
|
|
620
|
+
}
|
|
621
|
+
knownCjsModuleTypes[id] = initialCommonJSType;
|
|
629
622
|
if (
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
623
|
+
detectCyclesAndConditional &&
|
|
624
|
+
knownCjsModuleTypes[id] === true &&
|
|
625
|
+
requiredIds[id] &&
|
|
626
|
+
!unconditionallyRequiredIds[id]
|
|
633
627
|
) {
|
|
634
|
-
|
|
628
|
+
knownCjsModuleTypes[id] = IS_WRAPPED_COMMONJS;
|
|
635
629
|
}
|
|
630
|
+
};
|
|
636
631
|
|
|
637
|
-
|
|
638
|
-
|
|
632
|
+
const analyzeRequiredModule = async (parentId, resolved, isConditional, loadModule) => {
|
|
633
|
+
const childId = resolved.id;
|
|
634
|
+
requiredIds[childId] = true;
|
|
635
|
+
if (!(isConditional || knownCjsModuleTypes[parentId] === IS_WRAPPED_COMMONJS)) {
|
|
636
|
+
unconditionallyRequiredIds[childId] = true;
|
|
639
637
|
}
|
|
640
638
|
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
639
|
+
getDependencies(parentId).add(childId);
|
|
640
|
+
if (!isCyclic(childId)) {
|
|
641
|
+
// This makes sure the current transform handler waits for all direct
|
|
642
|
+
// dependencies to be loaded and transformed and therefore for all
|
|
643
|
+
// transitive CommonJS dependencies to be loaded as well so that all
|
|
644
|
+
// cycles have been found and knownCjsModuleTypes is reliable.
|
|
645
|
+
await loadModule(resolved);
|
|
646
|
+
}
|
|
647
|
+
};
|
|
648
|
+
|
|
649
|
+
return {
|
|
650
|
+
getWrappedIds: () =>
|
|
651
|
+
Object.keys(knownCjsModuleTypes).filter(
|
|
652
|
+
(id) => knownCjsModuleTypes[id] === IS_WRAPPED_COMMONJS
|
|
653
|
+
),
|
|
654
|
+
isRequiredId: (id) => requiredIds[id],
|
|
655
|
+
async shouldTransformCachedModule({ id: parentId, meta: { commonjs: parentMeta } }) {
|
|
656
|
+
// Ignore modules that did not pass through the original transformer in a previous build
|
|
657
|
+
if (!(parentMeta && parentMeta.requires)) {
|
|
658
|
+
return false;
|
|
659
|
+
}
|
|
660
|
+
setInitialParentType(parentId, parentMeta.initialCommonJSType);
|
|
661
|
+
await Promise.all(
|
|
662
|
+
parentMeta.requires.map(({ resolved, isConditional }) =>
|
|
663
|
+
analyzeRequiredModule(parentId, resolved, isConditional, this.load)
|
|
664
|
+
)
|
|
665
|
+
);
|
|
666
|
+
if (getTypeForFullyAnalyzedModule(parentId) !== parentMeta.isCommonJS) {
|
|
667
|
+
return true;
|
|
653
668
|
}
|
|
654
|
-
|
|
655
|
-
resolved
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
return { id: wrapId(importee, EXTERNAL_SUFFIX), external: false };
|
|
669
|
+
for (const {
|
|
670
|
+
resolved: { id }
|
|
671
|
+
} of parentMeta.requires) {
|
|
672
|
+
if (getTypeForFullyAnalyzedModule(id) !== parentMeta.isRequiredCommonJS[id]) {
|
|
673
|
+
return true;
|
|
674
|
+
}
|
|
661
675
|
}
|
|
662
|
-
|
|
663
|
-
|
|
676
|
+
// Now that we decided to go with the cached copy, neither the parent
|
|
677
|
+
// module nor any of its children may change types anymore
|
|
678
|
+
fullyAnalyzedModules[parentId] = true;
|
|
679
|
+
for (const {
|
|
680
|
+
resolved: { id }
|
|
681
|
+
} of parentMeta.requires) {
|
|
682
|
+
fullyAnalyzedModules[id] = true;
|
|
683
|
+
}
|
|
684
|
+
return false;
|
|
685
|
+
},
|
|
686
|
+
/* eslint-disable no-param-reassign */
|
|
687
|
+
resolveRequireSourcesAndUpdateMeta: (rollupContext) => async (
|
|
688
|
+
parentId,
|
|
689
|
+
isParentCommonJS,
|
|
690
|
+
parentMeta,
|
|
691
|
+
sources
|
|
692
|
+
) => {
|
|
693
|
+
parentMeta.initialCommonJSType = isParentCommonJS;
|
|
694
|
+
parentMeta.requires = [];
|
|
695
|
+
parentMeta.isRequiredCommonJS = Object.create(null);
|
|
696
|
+
setInitialParentType(parentId, isParentCommonJS);
|
|
697
|
+
const requireTargets = await Promise.all(
|
|
698
|
+
sources.map(async ({ source, isConditional }) => {
|
|
699
|
+
// Never analyze or proxy internal modules
|
|
700
|
+
if (source.startsWith('\0')) {
|
|
701
|
+
return { id: source, allowProxy: false };
|
|
702
|
+
}
|
|
703
|
+
const resolved =
|
|
704
|
+
(await rollupContext.resolve(source, parentId, {
|
|
705
|
+
custom: { 'node-resolve': { isRequire: true } }
|
|
706
|
+
})) || resolveExtensions(source, parentId, extensions);
|
|
707
|
+
if (!resolved) {
|
|
708
|
+
return { id: wrapId(source, EXTERNAL_SUFFIX), allowProxy: false };
|
|
709
|
+
}
|
|
710
|
+
const childId = resolved.id;
|
|
711
|
+
if (resolved.external) {
|
|
712
|
+
return { id: wrapId(childId, EXTERNAL_SUFFIX), allowProxy: false };
|
|
713
|
+
}
|
|
714
|
+
parentMeta.requires.push({ resolved, isConditional });
|
|
715
|
+
await analyzeRequiredModule(parentId, resolved, isConditional, rollupContext.load);
|
|
716
|
+
return { id: childId, allowProxy: true };
|
|
717
|
+
})
|
|
718
|
+
);
|
|
719
|
+
parentMeta.isCommonJS = getTypeForFullyAnalyzedModule(parentId);
|
|
720
|
+
fullyAnalyzedModules[parentId] = true;
|
|
721
|
+
return requireTargets.map(({ id: dependencyId, allowProxy }, index) => {
|
|
722
|
+
// eslint-disable-next-line no-multi-assign
|
|
723
|
+
const isCommonJS = (parentMeta.isRequiredCommonJS[
|
|
724
|
+
dependencyId
|
|
725
|
+
] = getTypeForFullyAnalyzedModule(dependencyId));
|
|
726
|
+
fullyAnalyzedModules[dependencyId] = true;
|
|
727
|
+
return {
|
|
728
|
+
source: sources[index].source,
|
|
729
|
+
id: allowProxy
|
|
730
|
+
? isCommonJS === IS_WRAPPED_COMMONJS
|
|
731
|
+
? wrapId(dependencyId, WRAPPED_SUFFIX)
|
|
732
|
+
: wrapId(dependencyId, PROXY_SUFFIX)
|
|
733
|
+
: dependencyId,
|
|
734
|
+
isCommonJS
|
|
735
|
+
};
|
|
736
|
+
});
|
|
737
|
+
}
|
|
664
738
|
};
|
|
665
739
|
}
|
|
666
740
|
|
|
667
|
-
function
|
|
668
|
-
const
|
|
669
|
-
const versionRegexp = /\^(\d+\.\d+)\.\d+/g;
|
|
741
|
+
function validateVersion(actualVersion, peerDependencyVersion, name) {
|
|
742
|
+
const versionRegexp = /\^(\d+\.\d+\.\d+)/g;
|
|
670
743
|
let minMajor = Infinity;
|
|
671
744
|
let minMinor = Infinity;
|
|
745
|
+
let minPatch = Infinity;
|
|
672
746
|
let foundVersion;
|
|
673
747
|
// eslint-disable-next-line no-cond-assign
|
|
674
748
|
while ((foundVersion = versionRegexp.exec(peerDependencyVersion))) {
|
|
675
|
-
const [foundMajor, foundMinor] = foundVersion[1].split('.').map(Number);
|
|
749
|
+
const [foundMajor, foundMinor, foundPatch] = foundVersion[1].split('.').map(Number);
|
|
676
750
|
if (foundMajor < minMajor) {
|
|
677
751
|
minMajor = foundMajor;
|
|
678
752
|
minMinor = foundMinor;
|
|
753
|
+
minPatch = foundPatch;
|
|
679
754
|
}
|
|
680
755
|
}
|
|
681
|
-
if (
|
|
756
|
+
if (!actualVersion) {
|
|
757
|
+
throw new Error(
|
|
758
|
+
`Insufficient ${name} version: "@rollup/plugin-commonjs" requires at least ${name}@${minMajor}.${minMinor}.${minPatch}.`
|
|
759
|
+
);
|
|
760
|
+
}
|
|
761
|
+
const [major, minor, patch] = actualVersion.split('.').map(Number);
|
|
762
|
+
if (
|
|
763
|
+
major < minMajor ||
|
|
764
|
+
(major === minMajor && (minor < minMinor || (minor === minMinor && patch < minPatch)))
|
|
765
|
+
) {
|
|
682
766
|
throw new Error(
|
|
683
|
-
`Insufficient
|
|
767
|
+
`Insufficient ${name} version: "@rollup/plugin-commonjs" requires at least ${name}@${minMajor}.${minMinor}.${minPatch} but found ${name}@${actualVersion}.`
|
|
684
768
|
);
|
|
685
769
|
}
|
|
686
770
|
}
|
|
@@ -820,8 +904,9 @@ function wrapCode(magicString, uses, moduleName, exportsName) {
|
|
|
820
904
|
}
|
|
821
905
|
magicString
|
|
822
906
|
.trim()
|
|
907
|
+
.indent('\t')
|
|
823
908
|
.prepend(`(function (${args.join(', ')}) {\n`)
|
|
824
|
-
.append(`\n}(${passedArgs.join(', ')}));`);
|
|
909
|
+
.append(`\n} (${passedArgs.join(', ')}));`);
|
|
825
910
|
}
|
|
826
911
|
|
|
827
912
|
function rewriteExportsAndGetExportsBlock(
|
|
@@ -839,12 +924,27 @@ function rewriteExportsAndGetExportsBlock(
|
|
|
839
924
|
HELPERS_NAME,
|
|
840
925
|
exportMode,
|
|
841
926
|
detectWrappedDefault,
|
|
842
|
-
defaultIsModuleExports
|
|
927
|
+
defaultIsModuleExports,
|
|
928
|
+
usesRequireWrapper,
|
|
929
|
+
requireName
|
|
843
930
|
) {
|
|
844
931
|
const exports = [];
|
|
845
932
|
const exportDeclarations = [];
|
|
846
933
|
|
|
847
|
-
if (
|
|
934
|
+
if (usesRequireWrapper) {
|
|
935
|
+
getExportsWhenUsingRequireWrapper(
|
|
936
|
+
magicString,
|
|
937
|
+
wrapped,
|
|
938
|
+
exportMode,
|
|
939
|
+
exports,
|
|
940
|
+
moduleExportsAssignments,
|
|
941
|
+
exportsAssignmentsByName,
|
|
942
|
+
moduleName,
|
|
943
|
+
exportsName,
|
|
944
|
+
requireName,
|
|
945
|
+
defineCompiledEsmExpressions
|
|
946
|
+
);
|
|
947
|
+
} else if (exportMode === 'replace') {
|
|
848
948
|
getExportsForReplacedModuleExports(
|
|
849
949
|
magicString,
|
|
850
950
|
exports,
|
|
@@ -887,6 +987,49 @@ function rewriteExportsAndGetExportsBlock(
|
|
|
887
987
|
return `\n\n${exportDeclarations.join('\n')}`;
|
|
888
988
|
}
|
|
889
989
|
|
|
990
|
+
function getExportsWhenUsingRequireWrapper(
|
|
991
|
+
magicString,
|
|
992
|
+
wrapped,
|
|
993
|
+
exportMode,
|
|
994
|
+
exports,
|
|
995
|
+
moduleExportsAssignments,
|
|
996
|
+
exportsAssignmentsByName,
|
|
997
|
+
moduleName,
|
|
998
|
+
exportsName,
|
|
999
|
+
requireName,
|
|
1000
|
+
defineCompiledEsmExpressions
|
|
1001
|
+
) {
|
|
1002
|
+
if (!wrapped) {
|
|
1003
|
+
if (exportMode === 'replace') {
|
|
1004
|
+
for (const { left } of moduleExportsAssignments) {
|
|
1005
|
+
magicString.overwrite(left.start, left.end, exportsName);
|
|
1006
|
+
}
|
|
1007
|
+
} else {
|
|
1008
|
+
// Collect and rewrite module.exports assignments
|
|
1009
|
+
for (const { left } of moduleExportsAssignments) {
|
|
1010
|
+
magicString.overwrite(left.start, left.end, `${moduleName}.exports`);
|
|
1011
|
+
}
|
|
1012
|
+
// Collect and rewrite named exports
|
|
1013
|
+
for (const [exportName, { nodes }] of exportsAssignmentsByName) {
|
|
1014
|
+
for (const node of nodes) {
|
|
1015
|
+
magicString.overwrite(node.start, node.left.end, `${exportsName}.${exportName}`);
|
|
1016
|
+
}
|
|
1017
|
+
}
|
|
1018
|
+
// Collect and rewrite exports.__esModule assignments
|
|
1019
|
+
for (const expression of defineCompiledEsmExpressions) {
|
|
1020
|
+
const moduleExportsExpression =
|
|
1021
|
+
expression.type === 'CallExpression' ? expression.arguments[0] : expression.left.object;
|
|
1022
|
+
magicString.overwrite(
|
|
1023
|
+
moduleExportsExpression.start,
|
|
1024
|
+
moduleExportsExpression.end,
|
|
1025
|
+
exportsName
|
|
1026
|
+
);
|
|
1027
|
+
}
|
|
1028
|
+
}
|
|
1029
|
+
}
|
|
1030
|
+
exports.push(`${requireName} as __require`);
|
|
1031
|
+
}
|
|
1032
|
+
|
|
890
1033
|
function getExportsForReplacedModuleExports(
|
|
891
1034
|
magicString,
|
|
892
1035
|
exports,
|
|
@@ -974,7 +1117,7 @@ function getExports(
|
|
|
974
1117
|
}
|
|
975
1118
|
|
|
976
1119
|
if (!isRestorableCompiledEsm || defaultIsModuleExports === true) {
|
|
977
|
-
|
|
1120
|
+
exports.push(`${exportsName} as default`);
|
|
978
1121
|
} else if (moduleExportsAssignments.length === 0 || defaultIsModuleExports === false) {
|
|
979
1122
|
exports.push(`${deconflictedDefaultExportName || exportsName} as default`);
|
|
980
1123
|
} else {
|
|
@@ -984,7 +1127,7 @@ function getExports(
|
|
|
984
1127
|
}
|
|
985
1128
|
}
|
|
986
1129
|
|
|
987
|
-
function
|
|
1130
|
+
function isRequireExpression(node, scope) {
|
|
988
1131
|
if (!node) return false;
|
|
989
1132
|
if (node.type !== 'CallExpression') return false;
|
|
990
1133
|
|
|
@@ -1011,11 +1154,6 @@ function isModuleRequire({ object, property }, scope) {
|
|
|
1011
1154
|
);
|
|
1012
1155
|
}
|
|
1013
1156
|
|
|
1014
|
-
function isStaticRequireStatement(node, scope) {
|
|
1015
|
-
if (!isRequireStatement(node, scope)) return false;
|
|
1016
|
-
return !hasDynamicArguments(node);
|
|
1017
|
-
}
|
|
1018
|
-
|
|
1019
1157
|
function hasDynamicArguments(node) {
|
|
1020
1158
|
return (
|
|
1021
1159
|
node.arguments.length > 1 ||
|
|
@@ -1030,89 +1168,61 @@ function isNodeRequirePropertyAccess(parent) {
|
|
|
1030
1168
|
return parent && parent.property && reservedMethod[parent.property.name];
|
|
1031
1169
|
}
|
|
1032
1170
|
|
|
1033
|
-
function isIgnoredRequireStatement(requiredNode, ignoreRequire) {
|
|
1034
|
-
return ignoreRequire(requiredNode.arguments[0].value);
|
|
1035
|
-
}
|
|
1036
|
-
|
|
1037
1171
|
function getRequireStringArg(node) {
|
|
1038
1172
|
return node.arguments[0].type === 'Literal'
|
|
1039
1173
|
? node.arguments[0].value
|
|
1040
1174
|
: node.arguments[0].quasis[0].value.cooked;
|
|
1041
1175
|
}
|
|
1042
1176
|
|
|
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
1177
|
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];
|
|
1178
|
+
const requireExpressions = [];
|
|
1179
|
+
|
|
1180
|
+
function addRequireStatement(
|
|
1181
|
+
sourceId,
|
|
1182
|
+
node,
|
|
1183
|
+
scope,
|
|
1184
|
+
usesReturnValue,
|
|
1185
|
+
isInsideTryBlock,
|
|
1186
|
+
isInsideConditional,
|
|
1187
|
+
toBeRemoved
|
|
1188
|
+
) {
|
|
1189
|
+
requireExpressions.push({
|
|
1190
|
+
sourceId,
|
|
1191
|
+
node,
|
|
1192
|
+
scope,
|
|
1193
|
+
usesReturnValue,
|
|
1194
|
+
isInsideTryBlock,
|
|
1195
|
+
isInsideConditional,
|
|
1196
|
+
toBeRemoved
|
|
1197
|
+
});
|
|
1095
1198
|
}
|
|
1096
1199
|
|
|
1097
|
-
function rewriteRequireExpressionsAndGetImportBlock(
|
|
1200
|
+
async function rewriteRequireExpressionsAndGetImportBlock(
|
|
1098
1201
|
magicString,
|
|
1099
1202
|
topLevelDeclarations,
|
|
1100
|
-
topLevelRequireDeclarators,
|
|
1101
1203
|
reassignedNames,
|
|
1102
1204
|
helpersName,
|
|
1103
|
-
|
|
1205
|
+
dynamicRequireName,
|
|
1104
1206
|
moduleName,
|
|
1105
1207
|
exportsName,
|
|
1106
1208
|
id,
|
|
1107
|
-
exportMode
|
|
1209
|
+
exportMode,
|
|
1210
|
+
resolveRequireSourcesAndUpdateMeta,
|
|
1211
|
+
needsRequireWrapper,
|
|
1212
|
+
isEsModule,
|
|
1213
|
+
isDynamicRequireModulesEnabled,
|
|
1214
|
+
getIgnoreTryCatchRequireStatementMode,
|
|
1215
|
+
commonjsMeta
|
|
1108
1216
|
) {
|
|
1109
|
-
setRemainingImportNamesAndRewriteRequires(
|
|
1110
|
-
requireExpressionsWithUsedReturnValue,
|
|
1111
|
-
requiredByNode,
|
|
1112
|
-
magicString
|
|
1113
|
-
);
|
|
1114
1217
|
const imports = [];
|
|
1115
1218
|
imports.push(`import * as ${helpersName} from "${HELPERS_ID}";`);
|
|
1219
|
+
if (dynamicRequireName) {
|
|
1220
|
+
imports.push(
|
|
1221
|
+
`import { ${
|
|
1222
|
+
isDynamicRequireModulesEnabled ? CREATE_COMMONJS_REQUIRE_EXPORT : COMMONJS_REQUIRE_EXPORT
|
|
1223
|
+
} as ${dynamicRequireName} } from "${DYNAMIC_MODULES_ID}";`
|
|
1224
|
+
);
|
|
1225
|
+
}
|
|
1116
1226
|
if (exportMode === 'module') {
|
|
1117
1227
|
imports.push(
|
|
1118
1228
|
`import { __module as ${moduleName}, exports as ${exportsName} } from ${JSON.stringify(
|
|
@@ -1124,58 +1234,113 @@ function getRequireHandlers() {
|
|
|
1124
1234
|
`import { __exports as ${exportsName} } from ${JSON.stringify(wrapId(id, EXPORTS_SUFFIX))}`
|
|
1125
1235
|
);
|
|
1126
1236
|
}
|
|
1127
|
-
|
|
1128
|
-
|
|
1129
|
-
|
|
1130
|
-
|
|
1131
|
-
|
|
1132
|
-
|
|
1133
|
-
|
|
1134
|
-
|
|
1135
|
-
|
|
1136
|
-
|
|
1137
|
-
|
|
1138
|
-
|
|
1139
|
-
|
|
1140
|
-
|
|
1237
|
+
const requiresBySource = collectSources(requireExpressions);
|
|
1238
|
+
const requireTargets = await resolveRequireSourcesAndUpdateMeta(
|
|
1239
|
+
id,
|
|
1240
|
+
needsRequireWrapper ? IS_WRAPPED_COMMONJS : !isEsModule,
|
|
1241
|
+
commonjsMeta,
|
|
1242
|
+
Object.keys(requiresBySource).map((source) => {
|
|
1243
|
+
return {
|
|
1244
|
+
source,
|
|
1245
|
+
isConditional: requiresBySource[source].every((require) => require.isInsideConditional)
|
|
1246
|
+
};
|
|
1247
|
+
})
|
|
1248
|
+
);
|
|
1249
|
+
processRequireExpressions(
|
|
1250
|
+
imports,
|
|
1251
|
+
requireTargets,
|
|
1252
|
+
requiresBySource,
|
|
1253
|
+
getIgnoreTryCatchRequireStatementMode,
|
|
1254
|
+
magicString
|
|
1255
|
+
);
|
|
1141
1256
|
return imports.length ? `${imports.join('\n')}\n\n` : '';
|
|
1142
1257
|
}
|
|
1143
1258
|
|
|
1144
1259
|
return {
|
|
1145
1260
|
addRequireStatement,
|
|
1146
|
-
requiredSources,
|
|
1147
1261
|
rewriteRequireExpressionsAndGetImportBlock
|
|
1148
1262
|
};
|
|
1149
1263
|
}
|
|
1150
1264
|
|
|
1151
|
-
function
|
|
1152
|
-
|
|
1153
|
-
|
|
1265
|
+
function collectSources(requireExpressions) {
|
|
1266
|
+
const requiresBySource = Object.create(null);
|
|
1267
|
+
for (const requireExpression of requireExpressions) {
|
|
1268
|
+
const { sourceId } = requireExpression;
|
|
1269
|
+
if (!requiresBySource[sourceId]) {
|
|
1270
|
+
requiresBySource[sourceId] = [];
|
|
1271
|
+
}
|
|
1272
|
+
const requires = requiresBySource[sourceId];
|
|
1273
|
+
requires.push(requireExpression);
|
|
1274
|
+
}
|
|
1275
|
+
return requiresBySource;
|
|
1276
|
+
}
|
|
1277
|
+
|
|
1278
|
+
function processRequireExpressions(
|
|
1279
|
+
imports,
|
|
1280
|
+
requireTargets,
|
|
1281
|
+
requiresBySource,
|
|
1282
|
+
getIgnoreTryCatchRequireStatementMode,
|
|
1154
1283
|
magicString
|
|
1155
1284
|
) {
|
|
1156
|
-
|
|
1157
|
-
for (const
|
|
1158
|
-
const
|
|
1159
|
-
|
|
1160
|
-
|
|
1161
|
-
|
|
1162
|
-
|
|
1163
|
-
|
|
1164
|
-
|
|
1165
|
-
|
|
1166
|
-
|
|
1285
|
+
const generateRequireName = getGenerateRequireName();
|
|
1286
|
+
for (const { source, id: resolvedId, isCommonJS } of requireTargets) {
|
|
1287
|
+
const requires = requiresBySource[source];
|
|
1288
|
+
const name = generateRequireName(requires);
|
|
1289
|
+
let usesRequired = false;
|
|
1290
|
+
let needsImport = false;
|
|
1291
|
+
for (const { node, usesReturnValue, toBeRemoved, isInsideTryBlock } of requires) {
|
|
1292
|
+
const { canConvertRequire, shouldRemoveRequire } =
|
|
1293
|
+
isInsideTryBlock && isWrappedId(resolvedId, EXTERNAL_SUFFIX)
|
|
1294
|
+
? getIgnoreTryCatchRequireStatementMode(source)
|
|
1295
|
+
: { canConvertRequire: true, shouldRemoveRequire: false };
|
|
1296
|
+
if (shouldRemoveRequire) {
|
|
1297
|
+
if (usesReturnValue) {
|
|
1298
|
+
magicString.overwrite(node.start, node.end, 'undefined');
|
|
1299
|
+
} else {
|
|
1300
|
+
magicString.remove(toBeRemoved.start, toBeRemoved.end);
|
|
1301
|
+
}
|
|
1302
|
+
} else if (canConvertRequire) {
|
|
1303
|
+
needsImport = true;
|
|
1304
|
+
if (isCommonJS === IS_WRAPPED_COMMONJS) {
|
|
1305
|
+
magicString.overwrite(node.start, node.end, `${name}()`);
|
|
1306
|
+
} else if (usesReturnValue) {
|
|
1307
|
+
usesRequired = true;
|
|
1308
|
+
magicString.overwrite(node.start, node.end, name);
|
|
1309
|
+
} else {
|
|
1310
|
+
magicString.remove(toBeRemoved.start, toBeRemoved.end);
|
|
1311
|
+
}
|
|
1312
|
+
}
|
|
1313
|
+
}
|
|
1314
|
+
if (needsImport) {
|
|
1315
|
+
if (isCommonJS === IS_WRAPPED_COMMONJS) {
|
|
1316
|
+
imports.push(`import { __require as ${name} } from ${JSON.stringify(resolvedId)};`);
|
|
1317
|
+
} else {
|
|
1318
|
+
imports.push(`import ${usesRequired ? `${name} from ` : ''}${JSON.stringify(resolvedId)};`);
|
|
1319
|
+
}
|
|
1167
1320
|
}
|
|
1168
|
-
magicString.overwrite(requireExpression.start, requireExpression.end, required.name);
|
|
1169
1321
|
}
|
|
1170
1322
|
}
|
|
1171
1323
|
|
|
1324
|
+
function getGenerateRequireName() {
|
|
1325
|
+
let uid = 0;
|
|
1326
|
+
return (requires) => {
|
|
1327
|
+
let name;
|
|
1328
|
+
const hasNameConflict = ({ scope }) => scope.contains(name);
|
|
1329
|
+
do {
|
|
1330
|
+
name = `require$$${uid}`;
|
|
1331
|
+
uid += 1;
|
|
1332
|
+
} while (requires.some(hasNameConflict));
|
|
1333
|
+
return name;
|
|
1334
|
+
};
|
|
1335
|
+
}
|
|
1336
|
+
|
|
1172
1337
|
/* eslint-disable no-param-reassign, no-shadow, no-underscore-dangle, no-continue */
|
|
1173
1338
|
|
|
1174
1339
|
const exportsPattern = /^(?:module\.)?exports(?:\.([a-zA-Z_$][a-zA-Z_$0-9]*))?$/;
|
|
1175
1340
|
|
|
1176
1341
|
const functionType = /^(?:FunctionDeclaration|FunctionExpression|ArrowFunctionExpression)$/;
|
|
1177
1342
|
|
|
1178
|
-
function transformCommonjs(
|
|
1343
|
+
async function transformCommonjs(
|
|
1179
1344
|
parse,
|
|
1180
1345
|
code,
|
|
1181
1346
|
id,
|
|
@@ -1186,11 +1351,15 @@ function transformCommonjs(
|
|
|
1186
1351
|
getIgnoreTryCatchRequireStatementMode,
|
|
1187
1352
|
sourceMap,
|
|
1188
1353
|
isDynamicRequireModulesEnabled,
|
|
1189
|
-
|
|
1190
|
-
disableWrap,
|
|
1354
|
+
dynamicRequireModules,
|
|
1191
1355
|
commonDir,
|
|
1192
1356
|
astCache,
|
|
1193
|
-
defaultIsModuleExports
|
|
1357
|
+
defaultIsModuleExports,
|
|
1358
|
+
needsRequireWrapper,
|
|
1359
|
+
resolveRequireSourcesAndUpdateMeta,
|
|
1360
|
+
isRequired,
|
|
1361
|
+
checkDynamicRequire,
|
|
1362
|
+
commonjsMeta
|
|
1194
1363
|
) {
|
|
1195
1364
|
const ast = astCache || tryParse(parse, code, id);
|
|
1196
1365
|
const magicString = new MagicString__default["default"](code);
|
|
@@ -1200,7 +1369,6 @@ function transformCommonjs(
|
|
|
1200
1369
|
global: false,
|
|
1201
1370
|
require: false
|
|
1202
1371
|
};
|
|
1203
|
-
let usesDynamicRequire = false;
|
|
1204
1372
|
const virtualDynamicRequirePath =
|
|
1205
1373
|
isDynamicRequireModulesEnabled && getVirtualPathForDynamicRequirePath(path.dirname(id), commonDir);
|
|
1206
1374
|
let scope = pluginutils.attachScopes(ast, 'scope');
|
|
@@ -1210,24 +1378,18 @@ function transformCommonjs(
|
|
|
1210
1378
|
let shouldWrap = false;
|
|
1211
1379
|
|
|
1212
1380
|
const globals = new Set();
|
|
1213
|
-
|
|
1214
|
-
//
|
|
1215
|
-
|
|
1216
|
-
|
|
1217
|
-
|
|
1218
|
-
|
|
1219
|
-
const {
|
|
1220
|
-
addRequireStatement,
|
|
1221
|
-
requiredSources,
|
|
1222
|
-
rewriteRequireExpressionsAndGetImportBlock
|
|
1223
|
-
} = getRequireHandlers();
|
|
1381
|
+
// A conditionalNode is a node for which execution is not guaranteed. If such a node is a require
|
|
1382
|
+
// or contains nested requires, those should be handled as function calls unless there is an
|
|
1383
|
+
// unconditional require elsewhere.
|
|
1384
|
+
let currentConditionalNodeEnd = null;
|
|
1385
|
+
const conditionalNodes = new Set();
|
|
1386
|
+
const { addRequireStatement, rewriteRequireExpressionsAndGetImportBlock } = getRequireHandlers();
|
|
1224
1387
|
|
|
1225
1388
|
// See which names are assigned to. This is necessary to prevent
|
|
1226
1389
|
// illegally replacing `var foo = require('foo')` with `import foo from 'foo'`,
|
|
1227
1390
|
// where `foo` is later reassigned. (This happens in the wild. CommonJS, sigh)
|
|
1228
1391
|
const reassignedNames = new Set();
|
|
1229
1392
|
const topLevelDeclarations = [];
|
|
1230
|
-
const topLevelRequireDeclarators = new Set();
|
|
1231
1393
|
const skippedNodes = new Set();
|
|
1232
1394
|
const moduleAccessScopes = new Set([scope]);
|
|
1233
1395
|
const exportsAccessScopes = new Set([scope]);
|
|
@@ -1236,6 +1398,9 @@ function transformCommonjs(
|
|
|
1236
1398
|
const exportsAssignmentsByName = new Map();
|
|
1237
1399
|
const topLevelAssignments = new Set();
|
|
1238
1400
|
const topLevelDefineCompiledEsmExpressions = [];
|
|
1401
|
+
const replacedGlobal = [];
|
|
1402
|
+
const replacedDynamicRequires = [];
|
|
1403
|
+
const importedVariables = new Set();
|
|
1239
1404
|
|
|
1240
1405
|
estreeWalker.walk(ast, {
|
|
1241
1406
|
enter(node, parent) {
|
|
@@ -1247,6 +1412,12 @@ function transformCommonjs(
|
|
|
1247
1412
|
if (currentTryBlockEnd !== null && node.start > currentTryBlockEnd) {
|
|
1248
1413
|
currentTryBlockEnd = null;
|
|
1249
1414
|
}
|
|
1415
|
+
if (currentConditionalNodeEnd !== null && node.start > currentConditionalNodeEnd) {
|
|
1416
|
+
currentConditionalNodeEnd = null;
|
|
1417
|
+
}
|
|
1418
|
+
if (currentConditionalNodeEnd === null && conditionalNodes.has(node)) {
|
|
1419
|
+
currentConditionalNodeEnd = node.end;
|
|
1420
|
+
}
|
|
1250
1421
|
|
|
1251
1422
|
programDepth += 1;
|
|
1252
1423
|
if (node.scope) ({ scope } = node);
|
|
@@ -1258,11 +1429,6 @@ function transformCommonjs(
|
|
|
1258
1429
|
|
|
1259
1430
|
// eslint-disable-next-line default-case
|
|
1260
1431
|
switch (node.type) {
|
|
1261
|
-
case 'TryStatement':
|
|
1262
|
-
if (currentTryBlockEnd === null) {
|
|
1263
|
-
currentTryBlockEnd = node.block.end;
|
|
1264
|
-
}
|
|
1265
|
-
return;
|
|
1266
1432
|
case 'AssignmentExpression':
|
|
1267
1433
|
if (node.left.type === 'MemberExpression') {
|
|
1268
1434
|
const flattened = getKeypath(node.left);
|
|
@@ -1333,110 +1499,58 @@ function transformCommonjs(
|
|
|
1333
1499
|
return;
|
|
1334
1500
|
}
|
|
1335
1501
|
|
|
1502
|
+
// Transform require.resolve
|
|
1336
1503
|
if (
|
|
1504
|
+
isDynamicRequireModulesEnabled &&
|
|
1337
1505
|
node.callee.object &&
|
|
1338
|
-
node.callee.object
|
|
1339
|
-
node.callee.property.name === 'resolve'
|
|
1340
|
-
hasDynamicModuleForPath(id, '/', dynamicRequireModuleSet)
|
|
1506
|
+
isRequire(node.callee.object, scope) &&
|
|
1507
|
+
node.callee.property.name === 'resolve'
|
|
1341
1508
|
) {
|
|
1509
|
+
checkDynamicRequire(node.start);
|
|
1510
|
+
uses.require = true;
|
|
1342
1511
|
const requireNode = node.callee.object;
|
|
1343
|
-
|
|
1344
|
-
node.end - 1,
|
|
1345
|
-
`,${JSON.stringify(
|
|
1346
|
-
path.dirname(id) === '.' ? null /* default behavior */ : virtualDynamicRequirePath
|
|
1347
|
-
)}`
|
|
1348
|
-
);
|
|
1349
|
-
magicString.overwrite(
|
|
1350
|
-
requireNode.start,
|
|
1351
|
-
requireNode.end,
|
|
1352
|
-
`${HELPERS_NAME}.commonjsRequire`,
|
|
1353
|
-
{
|
|
1354
|
-
storeName: true
|
|
1355
|
-
}
|
|
1356
|
-
);
|
|
1512
|
+
replacedDynamicRequires.push(requireNode);
|
|
1357
1513
|
return;
|
|
1358
1514
|
}
|
|
1359
1515
|
|
|
1360
|
-
if (!
|
|
1361
|
-
|
|
1362
|
-
|
|
1516
|
+
if (!isRequireExpression(node, scope)) {
|
|
1517
|
+
const keypath = getKeypath(node.callee);
|
|
1518
|
+
if (keypath && importedVariables.has(keypath.name)) {
|
|
1519
|
+
// Heuristic to deoptimize requires after a required function has been called
|
|
1520
|
+
currentConditionalNodeEnd = Infinity;
|
|
1521
|
+
}
|
|
1522
|
+
return;
|
|
1363
1523
|
}
|
|
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
1524
|
|
|
1371
|
-
|
|
1372
|
-
|
|
1373
|
-
canConvertRequire,
|
|
1374
|
-
shouldRemoveRequireStatement
|
|
1375
|
-
} = getIgnoreTryCatchRequireStatementMode(node.arguments[0].value));
|
|
1525
|
+
skippedNodes.add(node.callee);
|
|
1526
|
+
uses.require = true;
|
|
1376
1527
|
|
|
1377
|
-
|
|
1378
|
-
|
|
1379
|
-
|
|
1528
|
+
if (hasDynamicArguments(node)) {
|
|
1529
|
+
if (isDynamicRequireModulesEnabled) {
|
|
1530
|
+
checkDynamicRequire(node.start);
|
|
1380
1531
|
}
|
|
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
|
-
}
|
|
1532
|
+
if (!ignoreDynamicRequires) {
|
|
1533
|
+
replacedDynamicRequires.push(node.callee);
|
|
1415
1534
|
}
|
|
1535
|
+
return;
|
|
1536
|
+
}
|
|
1416
1537
|
|
|
1417
|
-
|
|
1418
|
-
|
|
1419
|
-
|
|
1420
|
-
|
|
1421
|
-
|
|
1422
|
-
|
|
1423
|
-
|
|
1424
|
-
|
|
1425
|
-
|
|
1426
|
-
|
|
1427
|
-
|
|
1428
|
-
|
|
1429
|
-
|
|
1430
|
-
|
|
1431
|
-
|
|
1432
|
-
} else {
|
|
1433
|
-
// This is a bare import, e.g. `require('foo');`
|
|
1434
|
-
|
|
1435
|
-
if (!canConvertRequire && !shouldRemoveRequireStatement) {
|
|
1436
|
-
return;
|
|
1538
|
+
const requireStringArg = getRequireStringArg(node);
|
|
1539
|
+
if (!ignoreRequire(requireStringArg)) {
|
|
1540
|
+
const usesReturnValue = parent.type !== 'ExpressionStatement';
|
|
1541
|
+
addRequireStatement(
|
|
1542
|
+
requireStringArg,
|
|
1543
|
+
node,
|
|
1544
|
+
scope,
|
|
1545
|
+
usesReturnValue,
|
|
1546
|
+
currentTryBlockEnd !== null,
|
|
1547
|
+
currentConditionalNodeEnd !== null,
|
|
1548
|
+
parent.type === 'ExpressionStatement' ? parent : node
|
|
1549
|
+
);
|
|
1550
|
+
if (parent.type === 'VariableDeclarator' && parent.id.type === 'Identifier') {
|
|
1551
|
+
for (const name of pluginutils.extractAssignedNames(parent.id)) {
|
|
1552
|
+
importedVariables.add(name);
|
|
1437
1553
|
}
|
|
1438
|
-
|
|
1439
|
-
magicString.remove(parent.start, parent.end);
|
|
1440
1554
|
}
|
|
1441
1555
|
}
|
|
1442
1556
|
return;
|
|
@@ -1446,45 +1560,43 @@ function transformCommonjs(
|
|
|
1446
1560
|
// skip dead branches
|
|
1447
1561
|
if (isFalsy(node.test)) {
|
|
1448
1562
|
skippedNodes.add(node.consequent);
|
|
1449
|
-
} else if (
|
|
1450
|
-
|
|
1563
|
+
} else if (isTruthy(node.test)) {
|
|
1564
|
+
if (node.alternate) {
|
|
1565
|
+
skippedNodes.add(node.alternate);
|
|
1566
|
+
}
|
|
1567
|
+
} else {
|
|
1568
|
+
conditionalNodes.add(node.consequent);
|
|
1569
|
+
if (node.alternate) {
|
|
1570
|
+
conditionalNodes.add(node.alternate);
|
|
1571
|
+
}
|
|
1572
|
+
}
|
|
1573
|
+
return;
|
|
1574
|
+
case 'ArrowFunctionExpression':
|
|
1575
|
+
case 'FunctionDeclaration':
|
|
1576
|
+
case 'FunctionExpression':
|
|
1577
|
+
// requires in functions should be conditional unless it is an IIFE
|
|
1578
|
+
if (
|
|
1579
|
+
currentConditionalNodeEnd === null &&
|
|
1580
|
+
!(parent.type === 'CallExpression' && parent.callee === node)
|
|
1581
|
+
) {
|
|
1582
|
+
currentConditionalNodeEnd = node.end;
|
|
1451
1583
|
}
|
|
1452
1584
|
return;
|
|
1453
1585
|
case 'Identifier': {
|
|
1454
1586
|
const { name } = node;
|
|
1455
|
-
if (!
|
|
1587
|
+
if (!isReference__default["default"](node, parent) || scope.contains(name)) return;
|
|
1456
1588
|
switch (name) {
|
|
1457
1589
|
case 'require':
|
|
1590
|
+
uses.require = true;
|
|
1458
1591
|
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
1592
|
return;
|
|
1468
1593
|
}
|
|
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
1594
|
if (!ignoreDynamicRequires) {
|
|
1479
1595
|
if (isShorthandProperty(parent)) {
|
|
1480
|
-
magicString.
|
|
1481
|
-
} else {
|
|
1482
|
-
magicString.overwrite(node.start, node.end, `${HELPERS_NAME}.commonjsRequire`, {
|
|
1483
|
-
storeName: true
|
|
1484
|
-
});
|
|
1596
|
+
magicString.prependRight(node.start, 'require: ');
|
|
1485
1597
|
}
|
|
1598
|
+
replacedDynamicRequires.push(node);
|
|
1486
1599
|
}
|
|
1487
|
-
usesDynamicRequire = true;
|
|
1488
1600
|
return;
|
|
1489
1601
|
case 'module':
|
|
1490
1602
|
case 'exports':
|
|
@@ -1494,9 +1606,7 @@ function transformCommonjs(
|
|
|
1494
1606
|
case 'global':
|
|
1495
1607
|
uses.global = true;
|
|
1496
1608
|
if (!ignoreGlobal) {
|
|
1497
|
-
|
|
1498
|
-
storeName: true
|
|
1499
|
-
});
|
|
1609
|
+
replacedGlobal.push(node);
|
|
1500
1610
|
}
|
|
1501
1611
|
return;
|
|
1502
1612
|
case 'define':
|
|
@@ -1509,11 +1619,26 @@ function transformCommonjs(
|
|
|
1509
1619
|
return;
|
|
1510
1620
|
}
|
|
1511
1621
|
}
|
|
1622
|
+
case 'LogicalExpression':
|
|
1623
|
+
// skip dead branches
|
|
1624
|
+
if (node.operator === '&&') {
|
|
1625
|
+
if (isFalsy(node.left)) {
|
|
1626
|
+
skippedNodes.add(node.right);
|
|
1627
|
+
} else if (!isTruthy(node.left)) {
|
|
1628
|
+
conditionalNodes.add(node.right);
|
|
1629
|
+
}
|
|
1630
|
+
} else if (node.operator === '||') {
|
|
1631
|
+
if (isTruthy(node.left)) {
|
|
1632
|
+
skippedNodes.add(node.right);
|
|
1633
|
+
} else if (!isFalsy(node.left)) {
|
|
1634
|
+
conditionalNodes.add(node.right);
|
|
1635
|
+
}
|
|
1636
|
+
}
|
|
1637
|
+
return;
|
|
1512
1638
|
case 'MemberExpression':
|
|
1513
1639
|
if (!isDynamicRequireModulesEnabled && isModuleRequire(node, scope)) {
|
|
1514
|
-
|
|
1515
|
-
|
|
1516
|
-
});
|
|
1640
|
+
uses.require = true;
|
|
1641
|
+
replacedDynamicRequires.push(node);
|
|
1517
1642
|
skippedNodes.add(node.object);
|
|
1518
1643
|
skippedNodes.add(node.property);
|
|
1519
1644
|
}
|
|
@@ -1529,12 +1654,18 @@ function transformCommonjs(
|
|
|
1529
1654
|
if (lexicalDepth === 0) {
|
|
1530
1655
|
uses.global = true;
|
|
1531
1656
|
if (!ignoreGlobal) {
|
|
1532
|
-
|
|
1533
|
-
storeName: true
|
|
1534
|
-
});
|
|
1657
|
+
replacedGlobal.push(node);
|
|
1535
1658
|
}
|
|
1536
1659
|
}
|
|
1537
1660
|
return;
|
|
1661
|
+
case 'TryStatement':
|
|
1662
|
+
if (currentTryBlockEnd === null) {
|
|
1663
|
+
currentTryBlockEnd = node.block.end;
|
|
1664
|
+
}
|
|
1665
|
+
if (currentConditionalNodeEnd === null) {
|
|
1666
|
+
currentConditionalNodeEnd = node.end;
|
|
1667
|
+
}
|
|
1668
|
+
return;
|
|
1538
1669
|
case 'UnaryExpression':
|
|
1539
1670
|
// rewrite `typeof module`, `typeof module.exports` and `typeof exports` (https://github.com/rollup/rollup-plugin-commonjs/issues/151)
|
|
1540
1671
|
if (node.operator === 'typeof') {
|
|
@@ -1544,9 +1675,10 @@ function transformCommonjs(
|
|
|
1544
1675
|
if (scope.contains(flattened.name)) return;
|
|
1545
1676
|
|
|
1546
1677
|
if (
|
|
1547
|
-
|
|
1548
|
-
flattened.keypath === 'module' ||
|
|
1549
|
-
|
|
1678
|
+
!isEsModule &&
|
|
1679
|
+
(flattened.keypath === 'module.exports' ||
|
|
1680
|
+
flattened.keypath === 'module' ||
|
|
1681
|
+
flattened.keypath === 'exports')
|
|
1550
1682
|
) {
|
|
1551
1683
|
magicString.overwrite(node.start, node.end, `'object'`, {
|
|
1552
1684
|
storeName: false
|
|
@@ -1571,29 +1703,53 @@ function transformCommonjs(
|
|
|
1571
1703
|
const nameBase = getName(id);
|
|
1572
1704
|
const exportsName = deconflict([...exportsAccessScopes], globals, nameBase);
|
|
1573
1705
|
const moduleName = deconflict([...moduleAccessScopes], globals, `${nameBase}Module`);
|
|
1706
|
+
const requireName = deconflict([scope], globals, `require${capitalize(nameBase)}`);
|
|
1707
|
+
const isRequiredName = deconflict([scope], globals, `hasRequired${capitalize(nameBase)}`);
|
|
1708
|
+
const helpersName = deconflict([scope], globals, 'commonjsHelpers');
|
|
1709
|
+
const dynamicRequireName =
|
|
1710
|
+
replacedDynamicRequires.length > 0 &&
|
|
1711
|
+
deconflict(
|
|
1712
|
+
[scope],
|
|
1713
|
+
globals,
|
|
1714
|
+
isDynamicRequireModulesEnabled ? CREATE_COMMONJS_REQUIRE_EXPORT : COMMONJS_REQUIRE_EXPORT
|
|
1715
|
+
);
|
|
1574
1716
|
const deconflictedExportNames = Object.create(null);
|
|
1575
1717
|
for (const [exportName, { scopes }] of exportsAssignmentsByName) {
|
|
1576
1718
|
deconflictedExportNames[exportName] = deconflict([...scopes], globals, exportName);
|
|
1577
1719
|
}
|
|
1578
1720
|
|
|
1721
|
+
for (const node of replacedGlobal) {
|
|
1722
|
+
magicString.overwrite(node.start, node.end, `${helpersName}.commonjsGlobal`, {
|
|
1723
|
+
storeName: true
|
|
1724
|
+
});
|
|
1725
|
+
}
|
|
1726
|
+
for (const node of replacedDynamicRequires) {
|
|
1727
|
+
magicString.overwrite(
|
|
1728
|
+
node.start,
|
|
1729
|
+
node.end,
|
|
1730
|
+
isDynamicRequireModulesEnabled
|
|
1731
|
+
? `${dynamicRequireName}(${JSON.stringify(virtualDynamicRequirePath)})`
|
|
1732
|
+
: dynamicRequireName,
|
|
1733
|
+
{
|
|
1734
|
+
contentOnly: true,
|
|
1735
|
+
storeName: true
|
|
1736
|
+
}
|
|
1737
|
+
);
|
|
1738
|
+
}
|
|
1739
|
+
|
|
1579
1740
|
// We cannot wrap ES/mixed modules
|
|
1580
|
-
shouldWrap =
|
|
1581
|
-
!isEsModule &&
|
|
1582
|
-
!disableWrap &&
|
|
1583
|
-
(shouldWrap || (uses.exports && moduleExportsAssignments.length > 0));
|
|
1741
|
+
shouldWrap = !isEsModule && (shouldWrap || (uses.exports && moduleExportsAssignments.length > 0));
|
|
1584
1742
|
const detectWrappedDefault =
|
|
1585
1743
|
shouldWrap &&
|
|
1586
1744
|
(topLevelDefineCompiledEsmExpressions.length > 0 || code.indexOf('__esModule') >= 0);
|
|
1587
1745
|
|
|
1588
1746
|
if (
|
|
1589
1747
|
!(
|
|
1590
|
-
|
|
1591
|
-
|
|
1748
|
+
shouldWrap ||
|
|
1749
|
+
isRequired ||
|
|
1592
1750
|
uses.module ||
|
|
1593
1751
|
uses.exports ||
|
|
1594
1752
|
uses.require ||
|
|
1595
|
-
usesDynamicRequire ||
|
|
1596
|
-
hasRemovedRequire ||
|
|
1597
1753
|
topLevelDefineCompiledEsmExpressions.length > 0
|
|
1598
1754
|
) &&
|
|
1599
1755
|
(ignoreGlobal || !uses.global)
|
|
@@ -1620,19 +1776,24 @@ function transformCommonjs(
|
|
|
1620
1776
|
? 'exports'
|
|
1621
1777
|
: 'module';
|
|
1622
1778
|
|
|
1623
|
-
const importBlock = rewriteRequireExpressionsAndGetImportBlock(
|
|
1779
|
+
const importBlock = await rewriteRequireExpressionsAndGetImportBlock(
|
|
1624
1780
|
magicString,
|
|
1625
1781
|
topLevelDeclarations,
|
|
1626
|
-
topLevelRequireDeclarators,
|
|
1627
1782
|
reassignedNames,
|
|
1628
|
-
|
|
1629
|
-
|
|
1783
|
+
helpersName,
|
|
1784
|
+
dynamicRequireName,
|
|
1630
1785
|
moduleName,
|
|
1631
1786
|
exportsName,
|
|
1632
1787
|
id,
|
|
1633
|
-
exportMode
|
|
1788
|
+
exportMode,
|
|
1789
|
+
resolveRequireSourcesAndUpdateMeta,
|
|
1790
|
+
needsRequireWrapper,
|
|
1791
|
+
isEsModule,
|
|
1792
|
+
isDynamicRequireModulesEnabled,
|
|
1793
|
+
getIgnoreTryCatchRequireStatementMode,
|
|
1794
|
+
commonjsMeta
|
|
1634
1795
|
);
|
|
1635
|
-
|
|
1796
|
+
const usesRequireWrapper = commonjsMeta.isCommonJS === IS_WRAPPED_COMMONJS;
|
|
1636
1797
|
const exportBlock = isEsModule
|
|
1637
1798
|
? ''
|
|
1638
1799
|
: rewriteExportsAndGetExportsBlock(
|
|
@@ -1647,16 +1808,35 @@ function transformCommonjs(
|
|
|
1647
1808
|
topLevelDefineCompiledEsmExpressions,
|
|
1648
1809
|
deconflictedExportNames,
|
|
1649
1810
|
code,
|
|
1650
|
-
|
|
1811
|
+
helpersName,
|
|
1651
1812
|
exportMode,
|
|
1652
1813
|
detectWrappedDefault,
|
|
1653
|
-
defaultIsModuleExports
|
|
1814
|
+
defaultIsModuleExports,
|
|
1815
|
+
usesRequireWrapper,
|
|
1816
|
+
requireName
|
|
1654
1817
|
);
|
|
1655
1818
|
|
|
1656
1819
|
if (shouldWrap) {
|
|
1657
1820
|
wrapCode(magicString, uses, moduleName, exportsName);
|
|
1658
1821
|
}
|
|
1659
1822
|
|
|
1823
|
+
if (usesRequireWrapper) {
|
|
1824
|
+
magicString.trim().indent('\t');
|
|
1825
|
+
magicString.prepend(
|
|
1826
|
+
`var ${isRequiredName};
|
|
1827
|
+
|
|
1828
|
+
function ${requireName} () {
|
|
1829
|
+
\tif (${isRequiredName}) return ${exportsName};
|
|
1830
|
+
\t${isRequiredName} = 1;
|
|
1831
|
+
`
|
|
1832
|
+
).append(`
|
|
1833
|
+
\treturn ${exportsName};
|
|
1834
|
+
}`);
|
|
1835
|
+
if (exportMode === 'replace') {
|
|
1836
|
+
magicString.prepend(`var ${exportsName};\n`);
|
|
1837
|
+
}
|
|
1838
|
+
}
|
|
1839
|
+
|
|
1660
1840
|
magicString
|
|
1661
1841
|
.trim()
|
|
1662
1842
|
.prepend(leadingComment + importBlock)
|
|
@@ -1665,24 +1845,29 @@ function transformCommonjs(
|
|
|
1665
1845
|
return {
|
|
1666
1846
|
code: magicString.toString(),
|
|
1667
1847
|
map: sourceMap ? magicString.generateMap() : null,
|
|
1668
|
-
syntheticNamedExports: isEsModule ? false : '__moduleExports',
|
|
1669
|
-
meta: { commonjs:
|
|
1848
|
+
syntheticNamedExports: isEsModule || usesRequireWrapper ? false : '__moduleExports',
|
|
1849
|
+
meta: { commonjs: commonjsMeta }
|
|
1670
1850
|
};
|
|
1671
1851
|
}
|
|
1672
1852
|
|
|
1853
|
+
const PLUGIN_NAME = 'commonjs';
|
|
1854
|
+
|
|
1673
1855
|
function commonjs(options = {}) {
|
|
1674
|
-
const extensions = options.extensions || ['.js'];
|
|
1675
|
-
const filter = pluginutils.createFilter(options.include, options.exclude);
|
|
1676
1856
|
const {
|
|
1677
1857
|
ignoreGlobal,
|
|
1678
1858
|
ignoreDynamicRequires,
|
|
1679
1859
|
requireReturnsDefault: requireReturnsDefaultOption,
|
|
1680
1860
|
esmExternals
|
|
1681
1861
|
} = options;
|
|
1862
|
+
const extensions = options.extensions || ['.js'];
|
|
1863
|
+
const filter = pluginutils.createFilter(options.include, options.exclude);
|
|
1864
|
+
const { strictRequiresFilter, detectCyclesAndConditional } = getStrictRequiresFilter(options);
|
|
1865
|
+
|
|
1682
1866
|
const getRequireReturnsDefault =
|
|
1683
1867
|
typeof requireReturnsDefaultOption === 'function'
|
|
1684
1868
|
? requireReturnsDefaultOption
|
|
1685
1869
|
: () => requireReturnsDefaultOption;
|
|
1870
|
+
|
|
1686
1871
|
let esmExternalIds;
|
|
1687
1872
|
const isEsmExternal =
|
|
1688
1873
|
typeof esmExternals === 'function'
|
|
@@ -1690,20 +1875,19 @@ function commonjs(options = {}) {
|
|
|
1690
1875
|
: Array.isArray(esmExternals)
|
|
1691
1876
|
? ((esmExternalIds = new Set(esmExternals)), (id) => esmExternalIds.has(id))
|
|
1692
1877
|
: () => esmExternals;
|
|
1878
|
+
|
|
1693
1879
|
const defaultIsModuleExports =
|
|
1694
1880
|
typeof options.defaultIsModuleExports === 'boolean' ? options.defaultIsModuleExports : 'auto';
|
|
1695
1881
|
|
|
1696
|
-
const
|
|
1697
|
-
options.
|
|
1882
|
+
const dynamicRequireRoot =
|
|
1883
|
+
typeof options.dynamicRequireRoot === 'string'
|
|
1884
|
+
? path.resolve(options.dynamicRequireRoot)
|
|
1885
|
+
: process.cwd();
|
|
1886
|
+
const { commonDir, dynamicRequireModules } = getDynamicRequireModules(
|
|
1887
|
+
options.dynamicRequireTargets,
|
|
1888
|
+
dynamicRequireRoot
|
|
1698
1889
|
);
|
|
1699
|
-
const isDynamicRequireModulesEnabled =
|
|
1700
|
-
const commonDir = isDynamicRequireModulesEnabled
|
|
1701
|
-
? getCommonDir__default["default"](null, Array.from(dynamicRequireModuleSet).concat(process.cwd()))
|
|
1702
|
-
: null;
|
|
1703
|
-
|
|
1704
|
-
const esModulesWithDefaultExport = new Set();
|
|
1705
|
-
const esModulesWithNamedExports = new Set();
|
|
1706
|
-
const commonJsMetaPromises = new Map();
|
|
1890
|
+
const isDynamicRequireModulesEnabled = dynamicRequireModules.size > 0;
|
|
1707
1891
|
|
|
1708
1892
|
const ignoreRequire =
|
|
1709
1893
|
typeof options.ignore === 'function'
|
|
@@ -1724,7 +1908,7 @@ function commonjs(options = {}) {
|
|
|
1724
1908
|
|
|
1725
1909
|
return {
|
|
1726
1910
|
canConvertRequire: mode !== 'remove' && mode !== true,
|
|
1727
|
-
|
|
1911
|
+
shouldRemoveRequire: mode === 'remove'
|
|
1728
1912
|
};
|
|
1729
1913
|
};
|
|
1730
1914
|
|
|
@@ -1732,38 +1916,52 @@ function commonjs(options = {}) {
|
|
|
1732
1916
|
|
|
1733
1917
|
const sourceMap = options.sourceMap !== false;
|
|
1734
1918
|
|
|
1735
|
-
|
|
1736
|
-
|
|
1737
|
-
// eslint-disable-next-line no-param-reassign
|
|
1738
|
-
code =
|
|
1739
|
-
getDynamicPackagesEntryIntro(dynamicRequireModuleDirPaths, dynamicRequireModuleSet) + code;
|
|
1740
|
-
}
|
|
1919
|
+
// Initialized in buildStart
|
|
1920
|
+
let requireResolver;
|
|
1741
1921
|
|
|
1922
|
+
function transformAndCheckExports(code, id) {
|
|
1742
1923
|
const { isEsModule, hasDefaultExport, hasNamedExports, ast } = analyzeTopLevelStatements(
|
|
1743
1924
|
this.parse,
|
|
1744
1925
|
code,
|
|
1745
1926
|
id
|
|
1746
1927
|
);
|
|
1928
|
+
|
|
1929
|
+
const commonjsMeta = this.getModuleInfo(id).meta.commonjs || {};
|
|
1747
1930
|
if (hasDefaultExport) {
|
|
1748
|
-
|
|
1931
|
+
commonjsMeta.hasDefaultExport = true;
|
|
1749
1932
|
}
|
|
1750
1933
|
if (hasNamedExports) {
|
|
1751
|
-
|
|
1934
|
+
commonjsMeta.hasNamedExports = true;
|
|
1752
1935
|
}
|
|
1753
1936
|
|
|
1754
1937
|
if (
|
|
1755
|
-
!
|
|
1756
|
-
(!hasCjsKeywords(code, ignoreGlobal) || (
|
|
1938
|
+
!dynamicRequireModules.has(normalizePathSlashes(id)) &&
|
|
1939
|
+
(!(hasCjsKeywords(code, ignoreGlobal) || requireResolver.isRequiredId(id)) ||
|
|
1940
|
+
(isEsModule && !options.transformMixedEsModules))
|
|
1757
1941
|
) {
|
|
1758
|
-
|
|
1942
|
+
commonjsMeta.isCommonJS = false;
|
|
1943
|
+
return { meta: { commonjs: commonjsMeta } };
|
|
1759
1944
|
}
|
|
1760
1945
|
|
|
1761
|
-
|
|
1762
|
-
|
|
1763
|
-
|
|
1764
|
-
|
|
1765
|
-
|
|
1766
|
-
|
|
1946
|
+
const needsRequireWrapper =
|
|
1947
|
+
!isEsModule &&
|
|
1948
|
+
(dynamicRequireModules.has(normalizePathSlashes(id)) || strictRequiresFilter(id));
|
|
1949
|
+
|
|
1950
|
+
const checkDynamicRequire = (position) => {
|
|
1951
|
+
if (id.indexOf(dynamicRequireRoot) !== 0) {
|
|
1952
|
+
this.error(
|
|
1953
|
+
{
|
|
1954
|
+
code: 'DYNAMIC_REQUIRE_OUTSIDE_ROOT',
|
|
1955
|
+
id,
|
|
1956
|
+
dynamicRequireRoot,
|
|
1957
|
+
message: `"${id}" contains dynamic require statements but it is not within the current dynamicRequireRoot "${dynamicRequireRoot}". You should set dynamicRequireRoot to "${path.dirname(
|
|
1958
|
+
id
|
|
1959
|
+
)}" or one of its parent directories.`
|
|
1960
|
+
},
|
|
1961
|
+
position
|
|
1962
|
+
);
|
|
1963
|
+
}
|
|
1964
|
+
};
|
|
1767
1965
|
|
|
1768
1966
|
return transformCommonjs(
|
|
1769
1967
|
this.parse,
|
|
@@ -1776,64 +1974,89 @@ function commonjs(options = {}) {
|
|
|
1776
1974
|
getIgnoreTryCatchRequireStatementMode,
|
|
1777
1975
|
sourceMap,
|
|
1778
1976
|
isDynamicRequireModulesEnabled,
|
|
1779
|
-
|
|
1780
|
-
disableWrap,
|
|
1977
|
+
dynamicRequireModules,
|
|
1781
1978
|
commonDir,
|
|
1782
1979
|
ast,
|
|
1783
|
-
defaultIsModuleExports
|
|
1980
|
+
defaultIsModuleExports,
|
|
1981
|
+
needsRequireWrapper,
|
|
1982
|
+
requireResolver.resolveRequireSourcesAndUpdateMeta(this),
|
|
1983
|
+
requireResolver.isRequiredId(id),
|
|
1984
|
+
checkDynamicRequire,
|
|
1985
|
+
commonjsMeta
|
|
1784
1986
|
);
|
|
1785
1987
|
}
|
|
1786
1988
|
|
|
1787
1989
|
return {
|
|
1788
|
-
name:
|
|
1990
|
+
name: PLUGIN_NAME,
|
|
1991
|
+
|
|
1992
|
+
version,
|
|
1993
|
+
|
|
1994
|
+
options(rawOptions) {
|
|
1995
|
+
// We inject the resolver in the beginning so that "catch-all-resolver" like node-resolver
|
|
1996
|
+
// do not prevent our plugin from resolving entry points ot proxies.
|
|
1997
|
+
const plugins = Array.isArray(rawOptions.plugins)
|
|
1998
|
+
? [...rawOptions.plugins]
|
|
1999
|
+
: rawOptions.plugins
|
|
2000
|
+
? [rawOptions.plugins]
|
|
2001
|
+
: [];
|
|
2002
|
+
plugins.unshift({
|
|
2003
|
+
name: 'commonjs--resolver',
|
|
2004
|
+
resolveId
|
|
2005
|
+
});
|
|
2006
|
+
return { ...rawOptions, plugins };
|
|
2007
|
+
},
|
|
1789
2008
|
|
|
1790
|
-
buildStart() {
|
|
1791
|
-
|
|
2009
|
+
buildStart({ plugins }) {
|
|
2010
|
+
validateVersion(this.meta.rollupVersion, peerDependencies.rollup, 'rollup');
|
|
2011
|
+
const nodeResolve = plugins.find(({ name }) => name === 'node-resolve');
|
|
2012
|
+
if (nodeResolve) {
|
|
2013
|
+
validateVersion(nodeResolve.version, '^13.0.6', '@rollup/plugin-node-resolve');
|
|
2014
|
+
}
|
|
1792
2015
|
if (options.namedExports != null) {
|
|
1793
2016
|
this.warn(
|
|
1794
2017
|
'The namedExports option from "@rollup/plugin-commonjs" is deprecated. Named exports are now handled automatically.'
|
|
1795
2018
|
);
|
|
1796
2019
|
}
|
|
2020
|
+
requireResolver = getRequireResolver(extensions, detectCyclesAndConditional);
|
|
1797
2021
|
},
|
|
1798
2022
|
|
|
1799
|
-
|
|
2023
|
+
buildEnd() {
|
|
2024
|
+
if (options.strictRequires === 'debug') {
|
|
2025
|
+
const wrappedIds = requireResolver.getWrappedIds();
|
|
2026
|
+
if (wrappedIds.length) {
|
|
2027
|
+
this.warn({
|
|
2028
|
+
code: 'WRAPPED_IDS',
|
|
2029
|
+
ids: wrappedIds,
|
|
2030
|
+
message: `The commonjs plugin automatically wrapped the following files:\n[\n${wrappedIds
|
|
2031
|
+
.map((id) => `\t${JSON.stringify(path.relative(process.cwd(), id))}`)
|
|
2032
|
+
.join(',\n')}\n]`
|
|
2033
|
+
});
|
|
2034
|
+
} else {
|
|
2035
|
+
this.warn({
|
|
2036
|
+
code: 'WRAPPED_IDS',
|
|
2037
|
+
ids: wrappedIds,
|
|
2038
|
+
message: 'The commonjs plugin did not wrap any files.'
|
|
2039
|
+
});
|
|
2040
|
+
}
|
|
2041
|
+
}
|
|
2042
|
+
},
|
|
1800
2043
|
|
|
1801
2044
|
load(id) {
|
|
1802
2045
|
if (id === HELPERS_ID) {
|
|
1803
|
-
return getHelpersModule(
|
|
1804
|
-
}
|
|
1805
|
-
|
|
1806
|
-
if (id.startsWith(HELPERS_ID)) {
|
|
1807
|
-
return getSpecificHelperProxy(id);
|
|
2046
|
+
return getHelpersModule();
|
|
1808
2047
|
}
|
|
1809
2048
|
|
|
1810
2049
|
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
|
-
}
|
|
2050
|
+
const name = getName(unwrapId(id, MODULE_SUFFIX));
|
|
1827
2051
|
return {
|
|
1828
|
-
code
|
|
2052
|
+
code: `var ${name} = {exports: {}}; export {${name} as __module}`,
|
|
1829
2053
|
syntheticNamedExports: '__module',
|
|
1830
2054
|
meta: { commonjs: { isCommonJS: false } }
|
|
1831
2055
|
};
|
|
1832
2056
|
}
|
|
1833
2057
|
|
|
1834
2058
|
if (isWrappedId(id, EXPORTS_SUFFIX)) {
|
|
1835
|
-
const
|
|
1836
|
-
const name = getName(actualId);
|
|
2059
|
+
const name = getName(unwrapId(id, EXPORTS_SUFFIX));
|
|
1837
2060
|
return {
|
|
1838
2061
|
code: `var ${name} = {}; export {${name} as __exports}`,
|
|
1839
2062
|
meta: { commonjs: { isCommonJS: false } }
|
|
@@ -1848,69 +2071,51 @@ function commonjs(options = {}) {
|
|
|
1848
2071
|
);
|
|
1849
2072
|
}
|
|
1850
2073
|
|
|
1851
|
-
|
|
1852
|
-
|
|
1853
|
-
|
|
1854
|
-
|
|
1855
|
-
|
|
1856
|
-
|
|
2074
|
+
// entry suffix is just appended to not mess up relative external resolution
|
|
2075
|
+
if (id.endsWith(ENTRY_SUFFIX)) {
|
|
2076
|
+
return getEntryProxy(
|
|
2077
|
+
id.slice(0, -ENTRY_SUFFIX.length),
|
|
2078
|
+
defaultIsModuleExports,
|
|
2079
|
+
this.getModuleInfo
|
|
2080
|
+
);
|
|
1857
2081
|
}
|
|
1858
2082
|
|
|
1859
|
-
if (
|
|
1860
|
-
return
|
|
2083
|
+
if (isWrappedId(id, ES_IMPORT_SUFFIX)) {
|
|
2084
|
+
return getEsImportProxy(unwrapId(id, ES_IMPORT_SUFFIX), defaultIsModuleExports);
|
|
1861
2085
|
}
|
|
1862
2086
|
|
|
1863
|
-
if (
|
|
1864
|
-
return
|
|
1865
|
-
|
|
1866
|
-
|
|
2087
|
+
if (id === DYNAMIC_MODULES_ID) {
|
|
2088
|
+
return getDynamicModuleRegistry(
|
|
2089
|
+
isDynamicRequireModulesEnabled,
|
|
2090
|
+
dynamicRequireModules,
|
|
2091
|
+
commonDir,
|
|
2092
|
+
ignoreDynamicRequires
|
|
1867
2093
|
);
|
|
1868
2094
|
}
|
|
1869
2095
|
|
|
1870
2096
|
if (isWrappedId(id, PROXY_SUFFIX)) {
|
|
1871
2097
|
const actualId = unwrapId(id, PROXY_SUFFIX);
|
|
1872
|
-
return getStaticRequireProxy(
|
|
1873
|
-
actualId,
|
|
1874
|
-
getRequireReturnsDefault(actualId),
|
|
1875
|
-
esModulesWithDefaultExport,
|
|
1876
|
-
esModulesWithNamedExports,
|
|
1877
|
-
commonJsMetaPromises
|
|
1878
|
-
);
|
|
2098
|
+
return getStaticRequireProxy(actualId, getRequireReturnsDefault(actualId), this.load);
|
|
1879
2099
|
}
|
|
1880
2100
|
|
|
1881
2101
|
return null;
|
|
1882
2102
|
},
|
|
1883
2103
|
|
|
1884
|
-
|
|
1885
|
-
|
|
1886
|
-
|
|
1887
|
-
if (isWrappedId(id, DYNAMIC_REGISTER_SUFFIX)) {
|
|
1888
|
-
id = unwrapId(id, DYNAMIC_REGISTER_SUFFIX);
|
|
1889
|
-
}
|
|
2104
|
+
shouldTransformCachedModule(...args) {
|
|
2105
|
+
return requireResolver.shouldTransformCachedModule.call(this, ...args);
|
|
2106
|
+
},
|
|
1890
2107
|
|
|
2108
|
+
transform(code, id) {
|
|
1891
2109
|
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
|
-
) {
|
|
2110
|
+
if (extName !== '.cjs' && (!filter(id) || !extensions.includes(extName))) {
|
|
1898
2111
|
return null;
|
|
1899
2112
|
}
|
|
1900
2113
|
|
|
1901
2114
|
try {
|
|
1902
|
-
return transformAndCheckExports.call(this, code,
|
|
2115
|
+
return transformAndCheckExports.call(this, code, id);
|
|
1903
2116
|
} catch (err) {
|
|
1904
2117
|
return this.error(err, err.loc);
|
|
1905
2118
|
}
|
|
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
2119
|
}
|
|
1915
2120
|
};
|
|
1916
2121
|
}
|