@rollup/plugin-commonjs 11.0.2 → 11.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +18 -0
- package/README.md +27 -0
- package/dist/index.es.js +635 -209
- package/dist/index.es.js.map +1 -1
- package/dist/index.js +632 -206
- package/dist/index.js.map +1 -1
- package/package.json +8 -6
- package/types/index.d.ts +55 -41
package/dist/index.js
CHANGED
|
@@ -6,9 +6,11 @@ var fs = require('fs');
|
|
|
6
6
|
var path = require('path');
|
|
7
7
|
var resolve = require('resolve');
|
|
8
8
|
var pluginutils = require('@rollup/pluginutils');
|
|
9
|
+
var glob = _interopDefault(require('glob'));
|
|
9
10
|
var estreeWalker = require('estree-walker');
|
|
10
11
|
var MagicString = _interopDefault(require('magic-string'));
|
|
11
12
|
var isReference = _interopDefault(require('is-reference'));
|
|
13
|
+
var getCommonDir = _interopDefault(require('commondir'));
|
|
12
14
|
|
|
13
15
|
function _slicedToArray(arr, i) {
|
|
14
16
|
return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _nonIterableRest();
|
|
@@ -52,144 +54,6 @@ function _nonIterableRest() {
|
|
|
52
54
|
throw new TypeError("Invalid attempt to destructure non-iterable instance");
|
|
53
55
|
}
|
|
54
56
|
|
|
55
|
-
var peerDependencies = {
|
|
56
|
-
rollup: "^1.20.0"
|
|
57
|
-
};
|
|
58
|
-
|
|
59
|
-
const PROXY_SUFFIX = '?commonjs-proxy';
|
|
60
|
-
const getProxyId = id => `\0${id}${PROXY_SUFFIX}`;
|
|
61
|
-
const getIdFromProxyId = proxyId => proxyId.slice(1, -PROXY_SUFFIX.length);
|
|
62
|
-
const EXTERNAL_SUFFIX = '?commonjs-external';
|
|
63
|
-
const getExternalProxyId = id => `\0${id}${EXTERNAL_SUFFIX}`;
|
|
64
|
-
const getIdFromExternalProxyId = proxyId => proxyId.slice(1, -EXTERNAL_SUFFIX.length);
|
|
65
|
-
const HELPERS_ID = '\0commonjsHelpers.js'; // `x['default']` is used instead of `x.default` for backward compatibility with ES3 browsers.
|
|
66
|
-
// Minifiers like uglify will usually transpile it back if compatibility with ES3 is not enabled.
|
|
67
|
-
|
|
68
|
-
const HELPERS = `
|
|
69
|
-
export var commonjsGlobal = typeof globalThis !== 'undefined' ? globalThis : typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {};
|
|
70
|
-
|
|
71
|
-
export function commonjsRequire () {
|
|
72
|
-
throw new Error('Dynamic requires are not currently supported by @rollup/plugin-commonjs');
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
export function unwrapExports (x) {
|
|
76
|
-
return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
export function createCommonjsModule(fn, module) {
|
|
80
|
-
return module = { exports: {} }, fn(module, module.exports), module.exports;
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
export function getCjsExportFromNamespace (n) {
|
|
84
|
-
return n && n['default'] || n;
|
|
85
|
-
}`;
|
|
86
|
-
|
|
87
|
-
/* eslint-disable no-undefined */
|
|
88
|
-
const isCjsPromises = new Map();
|
|
89
|
-
function getIsCjsPromise(id) {
|
|
90
|
-
let isCjsPromise = isCjsPromises.get(id);
|
|
91
|
-
if (isCjsPromise) return isCjsPromise.promise;
|
|
92
|
-
const promise = new Promise(resolve => {
|
|
93
|
-
isCjsPromise = {
|
|
94
|
-
resolve,
|
|
95
|
-
promise: undefined
|
|
96
|
-
};
|
|
97
|
-
isCjsPromises.set(id, isCjsPromise);
|
|
98
|
-
});
|
|
99
|
-
isCjsPromise.promise = promise;
|
|
100
|
-
return promise;
|
|
101
|
-
}
|
|
102
|
-
function setIsCjsPromise(id, resolution) {
|
|
103
|
-
const isCjsPromise = isCjsPromises.get(id);
|
|
104
|
-
|
|
105
|
-
if (isCjsPromise) {
|
|
106
|
-
if (isCjsPromise.resolve) {
|
|
107
|
-
isCjsPromise.resolve(resolution);
|
|
108
|
-
isCjsPromise.resolve = undefined;
|
|
109
|
-
}
|
|
110
|
-
} else {
|
|
111
|
-
isCjsPromises.set(id, {
|
|
112
|
-
promise: Promise.resolve(resolution),
|
|
113
|
-
resolve: undefined
|
|
114
|
-
});
|
|
115
|
-
}
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
/* eslint-disable no-param-reassign, no-undefined */
|
|
119
|
-
|
|
120
|
-
function getCandidatesForExtension(resolved, extension) {
|
|
121
|
-
return [resolved + extension, `${resolved}${path.sep}index${extension}`];
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
function getCandidates(resolved, extensions) {
|
|
125
|
-
return extensions.reduce((paths, extension) => paths.concat(getCandidatesForExtension(resolved, extension)), [resolved]);
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
function getResolveId(extensions) {
|
|
129
|
-
function resolveExtensions(importee, importer) {
|
|
130
|
-
// not our problem
|
|
131
|
-
if (importee[0] !== '.' || !importer) return undefined;
|
|
132
|
-
const resolved = path.resolve(path.dirname(importer), importee);
|
|
133
|
-
const candidates = getCandidates(resolved, extensions);
|
|
134
|
-
|
|
135
|
-
for (let i = 0; i < candidates.length; i += 1) {
|
|
136
|
-
try {
|
|
137
|
-
const stats = fs.statSync(candidates[i]);
|
|
138
|
-
if (stats.isFile()) return {
|
|
139
|
-
id: candidates[i]
|
|
140
|
-
};
|
|
141
|
-
} catch (err) {
|
|
142
|
-
/* noop */
|
|
143
|
-
}
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
return undefined;
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
function resolveId(importee, importer) {
|
|
150
|
-
const isProxyModule = importee.endsWith(PROXY_SUFFIX);
|
|
151
|
-
|
|
152
|
-
if (isProxyModule) {
|
|
153
|
-
importee = getIdFromProxyId(importee);
|
|
154
|
-
} else if (importee.startsWith('\0')) {
|
|
155
|
-
if (importee === HELPERS_ID) {
|
|
156
|
-
return importee;
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
return null;
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
if (importer && importer.endsWith(PROXY_SUFFIX)) {
|
|
163
|
-
importer = getIdFromProxyId(importer);
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
return this.resolve(importee, importer, {
|
|
167
|
-
skipSelf: true
|
|
168
|
-
}).then(resolved => {
|
|
169
|
-
if (!resolved) {
|
|
170
|
-
resolved = resolveExtensions(importee, importer);
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
if (isProxyModule) {
|
|
174
|
-
if (!resolved) {
|
|
175
|
-
return {
|
|
176
|
-
id: getExternalProxyId(importee),
|
|
177
|
-
external: false
|
|
178
|
-
};
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
resolved.id = (resolved.external ? getExternalProxyId : getProxyId)(resolved.id);
|
|
182
|
-
resolved.external = false;
|
|
183
|
-
return resolved;
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
return resolved;
|
|
187
|
-
});
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
return resolveId;
|
|
191
|
-
}
|
|
192
|
-
|
|
193
57
|
/* eslint-disable no-undefined */
|
|
194
58
|
const operators = {
|
|
195
59
|
'==': x => equals(x.left, x.right, false),
|
|
@@ -241,6 +105,157 @@ function equals(a, b, strict) {
|
|
|
241
105
|
return undefined;
|
|
242
106
|
}
|
|
243
107
|
|
|
108
|
+
const PROXY_SUFFIX = '?commonjs-proxy';
|
|
109
|
+
const getProxyId = id => `\0${id}${PROXY_SUFFIX}`;
|
|
110
|
+
const getIdFromProxyId = proxyId => proxyId.slice(1, -PROXY_SUFFIX.length);
|
|
111
|
+
const EXTERNAL_SUFFIX = '?commonjs-external';
|
|
112
|
+
const getExternalProxyId = id => `\0${id}${EXTERNAL_SUFFIX}`;
|
|
113
|
+
const getIdFromExternalProxyId = proxyId => proxyId.slice(1, -EXTERNAL_SUFFIX.length);
|
|
114
|
+
const VIRTUAL_PATH_BASE = '/$$rollup_base$$';
|
|
115
|
+
const getVirtualPathForDynamicRequirePath = (path, commonDir) => {
|
|
116
|
+
if (path.startsWith(commonDir)) return VIRTUAL_PATH_BASE + path.slice(commonDir.length);
|
|
117
|
+
return path;
|
|
118
|
+
};
|
|
119
|
+
const DYNAMIC_REGISTER_PREFIX = '\0commonjs-dynamic-register:';
|
|
120
|
+
const DYNAMIC_JSON_PREFIX = '\0commonjs-dynamic-json:';
|
|
121
|
+
const DYNAMIC_PACKAGES_ID = '\0commonjs-dynamic-packages';
|
|
122
|
+
const HELPERS_ID = '\0commonjsHelpers.js'; // `x['default']` is used instead of `x.default` for backward compatibility with ES3 browsers.
|
|
123
|
+
// Minifiers like uglify will usually transpile it back if compatibility with ES3 is not enabled.
|
|
124
|
+
|
|
125
|
+
const HELPERS = `
|
|
126
|
+
export var commonjsGlobal = typeof globalThis !== 'undefined' ? globalThis : typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {};
|
|
127
|
+
|
|
128
|
+
export function unwrapExports (x) {
|
|
129
|
+
return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
export function createCommonjsModule(fn, module) {
|
|
133
|
+
return module = { exports: {} }, fn(module, module.exports), module.exports;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
export function getCjsExportFromNamespace (n) {
|
|
137
|
+
return n && n['default'] || n;
|
|
138
|
+
}
|
|
139
|
+
`;
|
|
140
|
+
const HELPER_NON_DYNAMIC = `
|
|
141
|
+
export function commonjsRequire () {
|
|
142
|
+
throw new Error('Dynamic requires are not currently supported by @rollup/plugin-commonjs');
|
|
143
|
+
}
|
|
144
|
+
`;
|
|
145
|
+
const HELPERS_DYNAMIC = `
|
|
146
|
+
export function commonjsRegister (path, loader) {
|
|
147
|
+
DYNAMIC_REQUIRE_LOADERS[path] = loader;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
const DYNAMIC_REQUIRE_LOADERS = Object.create(null);
|
|
151
|
+
const DYNAMIC_REQUIRE_CACHE = Object.create(null);
|
|
152
|
+
const DEFAULT_PARENT_MODULE = {
|
|
153
|
+
id: '<' + 'rollup>', exports: {}, parent: undefined, filename: null, loaded: false, children: [], paths: []
|
|
154
|
+
};
|
|
155
|
+
const CHECKED_EXTENSIONS = ['', '.js', '.json'];
|
|
156
|
+
|
|
157
|
+
function normalize (path) {
|
|
158
|
+
path = path.replace(/\\\\/g, '/');
|
|
159
|
+
const parts = path.split('/');
|
|
160
|
+
const slashed = parts[0] === '';
|
|
161
|
+
for (let i = 1; i < parts.length; i++) {
|
|
162
|
+
if (parts[i] === '.' || parts[i] === '') {
|
|
163
|
+
parts.splice(i--, 1);
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
for (let i = 1; i < parts.length; i++) {
|
|
167
|
+
if (parts[i] !== '..') continue;
|
|
168
|
+
if (i > 0 && parts[i - 1] !== '..' && parts[i - 1] !== '.') {
|
|
169
|
+
parts.splice(--i, 2);
|
|
170
|
+
i--;
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
path = parts.join('/');
|
|
174
|
+
if (slashed && path[0] !== '/')
|
|
175
|
+
path = '/' + path;
|
|
176
|
+
else if (path.length === 0)
|
|
177
|
+
path = '.';
|
|
178
|
+
return path;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
function join () {
|
|
182
|
+
if (arguments.length === 0)
|
|
183
|
+
return '.';
|
|
184
|
+
let joined;
|
|
185
|
+
for (let i = 0; i < arguments.length; ++i) {
|
|
186
|
+
let arg = arguments[i];
|
|
187
|
+
if (arg.length > 0) {
|
|
188
|
+
if (joined === undefined)
|
|
189
|
+
joined = arg;
|
|
190
|
+
else
|
|
191
|
+
joined += '/' + arg;
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
if (joined === undefined)
|
|
195
|
+
return '.';
|
|
196
|
+
|
|
197
|
+
return joined;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
function isPossibleNodeModulesPath (modulePath) {
|
|
201
|
+
let c0 = modulePath[0];
|
|
202
|
+
if (c0 === '/' || c0 === '\\\\') return false;
|
|
203
|
+
let c1 = modulePath[1], c2 = modulePath[2];
|
|
204
|
+
if ((c0 === '.' && (!c1 || c1 === '/' || c1 === '\\\\')) ||
|
|
205
|
+
(c0 === '.' && c1 === '.' && (!c2 || c2 === '/' || c2 === '\\\\'))) return false;
|
|
206
|
+
if (c1 === ':' && (c2 === '/' || c2 === '\\\\'))
|
|
207
|
+
return false;
|
|
208
|
+
return true;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
export function commonjsRequire (path, originalModuleDir) {
|
|
212
|
+
const shouldTryNodeModules = isPossibleNodeModulesPath(path);
|
|
213
|
+
path = normalize(path);
|
|
214
|
+
let relPath;
|
|
215
|
+
while (true) {
|
|
216
|
+
if (!shouldTryNodeModules) {
|
|
217
|
+
relPath = originalModuleDir ? normalize(originalModuleDir + '/' + path) : path;
|
|
218
|
+
} else if (originalModuleDir) {
|
|
219
|
+
relPath = normalize(originalModuleDir + '/node_modules/' + path);
|
|
220
|
+
} else {
|
|
221
|
+
relPath = normalize(join('node_modules', path));
|
|
222
|
+
}
|
|
223
|
+
for (let extensionIndex = 0; extensionIndex < CHECKED_EXTENSIONS.length; extensionIndex++) {
|
|
224
|
+
const resolvedPath = relPath + CHECKED_EXTENSIONS[extensionIndex];
|
|
225
|
+
let cachedModule = DYNAMIC_REQUIRE_CACHE[resolvedPath];
|
|
226
|
+
if (cachedModule) return cachedModule.exports;
|
|
227
|
+
const loader = DYNAMIC_REQUIRE_LOADERS[resolvedPath];
|
|
228
|
+
if (loader) {
|
|
229
|
+
DYNAMIC_REQUIRE_CACHE[resolvedPath] = cachedModule = {
|
|
230
|
+
id: resolvedPath,
|
|
231
|
+
filename: resolvedPath,
|
|
232
|
+
exports: {},
|
|
233
|
+
parent: DEFAULT_PARENT_MODULE,
|
|
234
|
+
loaded: false,
|
|
235
|
+
children: [],
|
|
236
|
+
paths: []
|
|
237
|
+
};
|
|
238
|
+
try {
|
|
239
|
+
loader.call(commonjsGlobal, cachedModule, cachedModule.exports);
|
|
240
|
+
} catch (error) {
|
|
241
|
+
delete DYNAMIC_REQUIRE_CACHE[resolvedPath];
|
|
242
|
+
throw error;
|
|
243
|
+
}
|
|
244
|
+
cachedModule.loaded = true;
|
|
245
|
+
return cachedModule.exports;
|
|
246
|
+
};
|
|
247
|
+
}
|
|
248
|
+
if (!shouldTryNodeModules) break;
|
|
249
|
+
const nextDir = normalize(originalModuleDir + '/..');
|
|
250
|
+
if (nextDir === originalModuleDir) break;
|
|
251
|
+
originalModuleDir = nextDir;
|
|
252
|
+
}
|
|
253
|
+
return require(path);
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
commonjsRequire.cache = DYNAMIC_REQUIRE_CACHE;
|
|
257
|
+
`;
|
|
258
|
+
|
|
244
259
|
/* eslint-disable import/prefer-default-export */
|
|
245
260
|
function getName(id) {
|
|
246
261
|
const name = pluginutils.makeLegalIdentifier(path.basename(id, path.extname(id)));
|
|
@@ -266,7 +281,7 @@ const functionType = /^(?:FunctionDeclaration|FunctionExpression|ArrowFunctionEx
|
|
|
266
281
|
|
|
267
282
|
function deconflict(scope, globals, identifier) {
|
|
268
283
|
let i = 1;
|
|
269
|
-
let deconflicted = identifier;
|
|
284
|
+
let deconflicted = pluginutils.makeLegalIdentifier(identifier);
|
|
270
285
|
|
|
271
286
|
while (scope.contains(deconflicted) || globals.has(deconflicted) || deconflicted in blacklist) {
|
|
272
287
|
deconflicted = `${identifier}_${i}`;
|
|
@@ -288,6 +303,9 @@ function tryParse(parse, code, id) {
|
|
|
288
303
|
}
|
|
289
304
|
}
|
|
290
305
|
|
|
306
|
+
function normalizePathSlashes(path) {
|
|
307
|
+
return path.replace(/\\/g, '/');
|
|
308
|
+
}
|
|
291
309
|
function hasCjsKeywords(code, ignoreGlobal) {
|
|
292
310
|
const firstpass = ignoreGlobal ? firstpassNoGlobal : firstpassGlobal;
|
|
293
311
|
return firstpass.test(code);
|
|
@@ -363,7 +381,26 @@ function checkEsModule(parse, code, id) {
|
|
|
363
381
|
ast
|
|
364
382
|
};
|
|
365
383
|
}
|
|
366
|
-
|
|
384
|
+
|
|
385
|
+
function getDefinePropertyCallName(node, targetName) {
|
|
386
|
+
if (node.type !== 'CallExpression') return;
|
|
387
|
+
const _node$callee = node.callee,
|
|
388
|
+
object = _node$callee.object,
|
|
389
|
+
property = _node$callee.property;
|
|
390
|
+
if (!object || object.type !== 'Identifier' || object.name !== 'Object') return;
|
|
391
|
+
if (!property || property.type !== 'Identifier' || property.name !== 'defineProperty') return;
|
|
392
|
+
if (node.arguments.length !== 3) return;
|
|
393
|
+
|
|
394
|
+
const _node$arguments = _slicedToArray(node.arguments, 2),
|
|
395
|
+
target = _node$arguments[0],
|
|
396
|
+
val = _node$arguments[1];
|
|
397
|
+
|
|
398
|
+
if (target.type !== 'Identifier' || target.name !== targetName) return; // eslint-disable-next-line consistent-return
|
|
399
|
+
|
|
400
|
+
return val.value;
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
function transformCommonjs(parse, code, id, isEntry, isEsModule, ignoreGlobal, ignoreRequire, customNamedExports, sourceMap, isDynamicRequireModulesEnabled, dynamicRequireModuleSet, commonDir, astCache) {
|
|
367
404
|
const ast = astCache || tryParse(parse, code, id);
|
|
368
405
|
const magicString = new MagicString(code);
|
|
369
406
|
const required = {}; // Because objects have no guaranteed ordering, yet we need it,
|
|
@@ -386,6 +423,7 @@ function transformCommonjs(parse, code, id, isEntry, ignoreGlobal, ignoreRequire
|
|
|
386
423
|
const namedExports = {}; // TODO handle transpiled modules
|
|
387
424
|
|
|
388
425
|
let shouldWrap = /__esModule/.test(code);
|
|
426
|
+
let usesDynamicHelpers = false;
|
|
389
427
|
|
|
390
428
|
function isRequireStatement(node) {
|
|
391
429
|
if (!node) return false;
|
|
@@ -403,19 +441,30 @@ function transformCommonjs(parse, code, id, isEntry, ignoreGlobal, ignoreRequire
|
|
|
403
441
|
function isStaticRequireStatement(node) {
|
|
404
442
|
if (!isRequireStatement(node)) return false;
|
|
405
443
|
if (hasDynamicArguments(node)) return false;
|
|
406
|
-
if (ignoreRequire(node.arguments[0].value)) return false;
|
|
407
444
|
return true;
|
|
408
445
|
}
|
|
409
446
|
|
|
447
|
+
function isIgnoredRequireStatement(requiredNode) {
|
|
448
|
+
return ignoreRequire(requiredNode.arguments[0].value);
|
|
449
|
+
}
|
|
450
|
+
|
|
410
451
|
function getRequireStringArg(node) {
|
|
411
452
|
return node.arguments[0].type === 'Literal' ? node.arguments[0].value : node.arguments[0].quasis[0].value.cooked;
|
|
412
453
|
}
|
|
413
454
|
|
|
414
455
|
function getRequired(node, name) {
|
|
415
|
-
|
|
456
|
+
let sourceId = getRequireStringArg(node);
|
|
457
|
+
const isDynamicRegister = sourceId.startsWith(DYNAMIC_REGISTER_PREFIX);
|
|
458
|
+
|
|
459
|
+
if (isDynamicRegister) {
|
|
460
|
+
sourceId = sourceId.substr(DYNAMIC_REGISTER_PREFIX.length);
|
|
461
|
+
}
|
|
462
|
+
|
|
416
463
|
const existing = required[sourceId]; // eslint-disable-next-line no-undefined
|
|
417
464
|
|
|
418
465
|
if (existing === undefined) {
|
|
466
|
+
const isDynamic = hasDynamicModuleForPath(sourceId);
|
|
467
|
+
|
|
419
468
|
if (!name) {
|
|
420
469
|
do {
|
|
421
470
|
name = `require$$${uid}`;
|
|
@@ -423,15 +472,58 @@ function transformCommonjs(parse, code, id, isEntry, ignoreGlobal, ignoreRequire
|
|
|
423
472
|
} while (scope.contains(name));
|
|
424
473
|
}
|
|
425
474
|
|
|
426
|
-
|
|
475
|
+
if (isDynamicRegister && sourceId.endsWith('.json')) {
|
|
476
|
+
sourceId = DYNAMIC_JSON_PREFIX + sourceId;
|
|
477
|
+
}
|
|
478
|
+
|
|
479
|
+
if (isDynamicRegister || !isDynamic || sourceId.endsWith('.json')) {
|
|
480
|
+
sources.push([sourceId, !isDynamicRegister]);
|
|
481
|
+
}
|
|
482
|
+
|
|
427
483
|
required[sourceId] = {
|
|
428
484
|
source: sourceId,
|
|
429
485
|
name,
|
|
430
|
-
importsDefault: false
|
|
486
|
+
importsDefault: false,
|
|
487
|
+
isDynamic
|
|
431
488
|
};
|
|
432
489
|
}
|
|
433
490
|
|
|
434
491
|
return required[sourceId];
|
|
492
|
+
}
|
|
493
|
+
|
|
494
|
+
function hasDynamicModuleForPath(source) {
|
|
495
|
+
if (!/[/\\]/.test(source)) {
|
|
496
|
+
try {
|
|
497
|
+
const resolvedPath = normalizePathSlashes(resolve.sync(source, {
|
|
498
|
+
basedir: path.dirname(id)
|
|
499
|
+
}));
|
|
500
|
+
|
|
501
|
+
if (dynamicRequireModuleSet.has(resolvedPath)) {
|
|
502
|
+
return true;
|
|
503
|
+
}
|
|
504
|
+
} catch (ex) {
|
|
505
|
+
// Probably a node.js internal module
|
|
506
|
+
return false;
|
|
507
|
+
}
|
|
508
|
+
|
|
509
|
+
return false;
|
|
510
|
+
}
|
|
511
|
+
|
|
512
|
+
for (var _i = 0, _arr = ['', '.js', '.json']; _i < _arr.length; _i++) {
|
|
513
|
+
const attemptExt = _arr[_i];
|
|
514
|
+
const resolvedPath = normalizePathSlashes(path.resolve(path.dirname(id), source + attemptExt));
|
|
515
|
+
|
|
516
|
+
if (dynamicRequireModuleSet.has(resolvedPath)) {
|
|
517
|
+
return true;
|
|
518
|
+
}
|
|
519
|
+
}
|
|
520
|
+
|
|
521
|
+
return false;
|
|
522
|
+
}
|
|
523
|
+
|
|
524
|
+
function shouldUseSimulatedRequire(required) {
|
|
525
|
+
return hasDynamicModuleForPath(required.source) && ( // We only do `commonjsRequire` for json if it's the `commonjsRegister` call.
|
|
526
|
+
required.source.startsWith(DYNAMIC_REGISTER_PREFIX) || !required.source.endsWith('.json'));
|
|
435
527
|
} // do a first pass, see which names are assigned to. This is necessary to prevent
|
|
436
528
|
// illegally replacing `var foo = require('foo')` with `import foo from 'foo'`,
|
|
437
529
|
// where `foo` is later reassigned. (This happens in the wild. CommonJS, sigh)
|
|
@@ -513,10 +605,20 @@ function transformCommonjs(parse, code, id, isEntry, ignoreGlobal, ignoreRequire
|
|
|
513
605
|
if (isReference(node, parent) && !scope.contains(node.name)) {
|
|
514
606
|
if (node.name in uses) {
|
|
515
607
|
if (node.name === 'require') {
|
|
516
|
-
if (
|
|
608
|
+
if (!isDynamicRequireModulesEnabled && isStaticRequireStatement(parent)) {
|
|
609
|
+
return;
|
|
610
|
+
}
|
|
611
|
+
|
|
612
|
+
if (isDynamicRequireModulesEnabled && isRequireStatement(parent)) {
|
|
613
|
+
magicString.appendLeft(parent.end - 1, ',' + JSON.stringify(path.dirname(id) === '.' ? null
|
|
614
|
+
/* default behavior */
|
|
615
|
+
: getVirtualPathForDynamicRequirePath(normalizePathSlashes(path.dirname(id)), commonDir)));
|
|
616
|
+
}
|
|
617
|
+
|
|
517
618
|
magicString.overwrite(node.start, node.end, `${HELPERS_NAME}.commonjsRequire`, {
|
|
518
619
|
storeName: true
|
|
519
620
|
});
|
|
621
|
+
usesDynamicHelpers = true;
|
|
520
622
|
}
|
|
521
623
|
|
|
522
624
|
uses[node.name] = true;
|
|
@@ -571,10 +673,12 @@ function transformCommonjs(parse, code, id, isEntry, ignoreGlobal, ignoreRequire
|
|
|
571
673
|
|
|
572
674
|
if (match[1]) namedExports[match[1]] = true;
|
|
573
675
|
return;
|
|
574
|
-
}
|
|
676
|
+
}
|
|
575
677
|
|
|
678
|
+
const name = getDefinePropertyCallName(node, 'exports');
|
|
679
|
+
if (name && name === pluginutils.makeLegalIdentifier(name)) namedExports[name] = true; // if this is `var x = require('x')`, we can do `import x from 'x'`
|
|
576
680
|
|
|
577
|
-
if (node.type === 'VariableDeclarator' && node.id.type === 'Identifier' && isStaticRequireStatement(node.init)) {
|
|
681
|
+
if (node.type === 'VariableDeclarator' && node.id.type === 'Identifier' && isStaticRequireStatement(node.init) && !isIgnoredRequireStatement(node.init)) {
|
|
578
682
|
// for now, only do this for top-level requires. maybe fix this in future
|
|
579
683
|
if (scope.parent) return; // edge case — CJS allows you to assign to imports. ES doesn't
|
|
580
684
|
|
|
@@ -582,12 +686,15 @@ function transformCommonjs(parse, code, id, isEntry, ignoreGlobal, ignoreRequire
|
|
|
582
686
|
const required = getRequired(node.init, node.id.name);
|
|
583
687
|
required.importsDefault = true;
|
|
584
688
|
|
|
585
|
-
if (required.name === node.id.name) {
|
|
689
|
+
if (required.name === node.id.name && !required.isDynamic) {
|
|
586
690
|
node._shouldRemove = true;
|
|
587
691
|
}
|
|
588
692
|
}
|
|
589
693
|
|
|
590
|
-
if (!isStaticRequireStatement(node))
|
|
694
|
+
if (!isStaticRequireStatement(node) || isIgnoredRequireStatement(node)) {
|
|
695
|
+
return;
|
|
696
|
+
}
|
|
697
|
+
|
|
591
698
|
const required = getRequired(node);
|
|
592
699
|
|
|
593
700
|
if (parent.type === 'ExpressionStatement') {
|
|
@@ -595,7 +702,15 @@ function transformCommonjs(parse, code, id, isEntry, ignoreGlobal, ignoreRequire
|
|
|
595
702
|
magicString.remove(parent.start, parent.end);
|
|
596
703
|
} else {
|
|
597
704
|
required.importsDefault = true;
|
|
598
|
-
|
|
705
|
+
|
|
706
|
+
if (shouldUseSimulatedRequire(required)) {
|
|
707
|
+
magicString.overwrite(node.start, node.end, `${HELPERS_NAME}.commonjsRequire(${JSON.stringify(getVirtualPathForDynamicRequirePath(normalizePathSlashes(required.source), commonDir))}, ${JSON.stringify(path.dirname(id) === '.' ? null
|
|
708
|
+
/* default behavior */
|
|
709
|
+
: getVirtualPathForDynamicRequirePath(normalizePathSlashes(path.dirname(id)), commonDir))})`);
|
|
710
|
+
usesDynamicHelpers = true;
|
|
711
|
+
} else {
|
|
712
|
+
magicString.overwrite(node.start, node.end, required.name);
|
|
713
|
+
}
|
|
599
714
|
}
|
|
600
715
|
|
|
601
716
|
node.callee._skip = true;
|
|
@@ -642,10 +757,10 @@ function transformCommonjs(parse, code, id, isEntry, ignoreGlobal, ignoreRequire
|
|
|
642
757
|
return null;
|
|
643
758
|
}
|
|
644
759
|
|
|
645
|
-
const includeHelpers = shouldWrap || uses.global || uses.require;
|
|
646
|
-
const importBlock = `${(includeHelpers ? [`import * as ${HELPERS_NAME} from '${HELPERS_ID}';`] : []).concat(sources.map(source => // import the actual module before the proxy, so that we know
|
|
760
|
+
const includeHelpers = usesDynamicHelpers || shouldWrap || uses.global || uses.require;
|
|
761
|
+
const importBlock = `${(includeHelpers ? [`import * as ${HELPERS_NAME} from '${HELPERS_ID}';`] : []).concat(sources.map(([source]) => // import the actual module before the proxy, so that we know
|
|
647
762
|
// what kind of proxy to build
|
|
648
|
-
`import '${source}';`), sources.map(source => {
|
|
763
|
+
`import '${source}';`), sources.filter(([, importProxy]) => importProxy).map(([source]) => {
|
|
649
764
|
const _required$source = required[source],
|
|
650
765
|
name = _required$source.name,
|
|
651
766
|
importsDefault = _required$source.importsDefault;
|
|
@@ -656,7 +771,7 @@ function transformCommonjs(parse, code, id, isEntry, ignoreGlobal, ignoreRequire
|
|
|
656
771
|
let wrapperEnd = '';
|
|
657
772
|
const moduleName = deconflict(scope, globals, getName(id));
|
|
658
773
|
|
|
659
|
-
if (!isEntry) {
|
|
774
|
+
if (!isEntry && !isEsModule) {
|
|
660
775
|
const exportModuleExports = {
|
|
661
776
|
str: `export { ${moduleName} as __moduleExports };`,
|
|
662
777
|
name: '__moduleExports'
|
|
@@ -685,43 +800,71 @@ function transformCommonjs(parse, code, id, isEntry, ignoreGlobal, ignoreRequire
|
|
|
685
800
|
wrapperEnd = `\n});`;
|
|
686
801
|
} else {
|
|
687
802
|
const names = [];
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
const flattened = flatten(left);
|
|
692
|
-
if (!flattened) return;
|
|
693
|
-
const match = exportsPattern.exec(flattened.keypath);
|
|
694
|
-
if (!match) return;
|
|
803
|
+
var _iteratorNormalCompletion3 = true;
|
|
804
|
+
var _didIteratorError3 = false;
|
|
805
|
+
var _iteratorError3 = undefined;
|
|
695
806
|
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
} else {
|
|
700
|
-
const _match = _slicedToArray(match, 2),
|
|
701
|
-
name = _match[1];
|
|
807
|
+
try {
|
|
808
|
+
for (var _iterator3 = ast.body[Symbol.iterator](), _step3; !(_iteratorNormalCompletion3 = (_step3 = _iterator3.next()).done); _iteratorNormalCompletion3 = true) {
|
|
809
|
+
const node = _step3.value;
|
|
702
810
|
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
deconflicted
|
|
707
|
-
});
|
|
708
|
-
magicString.overwrite(node.start, left.end, `var ${deconflicted}`);
|
|
709
|
-
const declaration = name === deconflicted ? `export { ${name} };` : `export { ${deconflicted} as ${name} };`;
|
|
811
|
+
if (node.type === 'ExpressionStatement' && node.expression.type === 'AssignmentExpression') {
|
|
812
|
+
const left = node.expression.left;
|
|
813
|
+
const flattened = flatten(left);
|
|
710
814
|
|
|
711
|
-
if (
|
|
712
|
-
|
|
713
|
-
str: declaration,
|
|
714
|
-
name
|
|
715
|
-
});
|
|
716
|
-
delete namedExports[name];
|
|
815
|
+
if (!flattened) {
|
|
816
|
+
continue;
|
|
717
817
|
}
|
|
718
818
|
|
|
719
|
-
|
|
819
|
+
const match = exportsPattern.exec(flattened.keypath);
|
|
820
|
+
|
|
821
|
+
if (!match) {
|
|
822
|
+
continue;
|
|
823
|
+
}
|
|
824
|
+
|
|
825
|
+
if (flattened.keypath === 'module.exports') {
|
|
826
|
+
hasDefaultExport = true;
|
|
827
|
+
magicString.overwrite(left.start, left.end, `var ${moduleName}`);
|
|
828
|
+
} else {
|
|
829
|
+
const _match = _slicedToArray(match, 2),
|
|
830
|
+
name = _match[1];
|
|
831
|
+
|
|
832
|
+
const deconflicted = deconflict(scope, globals, name);
|
|
833
|
+
names.push({
|
|
834
|
+
name,
|
|
835
|
+
deconflicted
|
|
836
|
+
});
|
|
837
|
+
magicString.overwrite(node.start, left.end, `var ${deconflicted}`);
|
|
838
|
+
const declaration = name === deconflicted ? `export { ${name} };` : `export { ${deconflicted} as ${name} };`;
|
|
839
|
+
|
|
840
|
+
if (name !== 'default') {
|
|
841
|
+
namedExportDeclarations.push({
|
|
842
|
+
str: declaration,
|
|
843
|
+
name
|
|
844
|
+
});
|
|
845
|
+
delete namedExports[name];
|
|
846
|
+
}
|
|
847
|
+
|
|
848
|
+
defaultExportPropertyAssignments.push(`${moduleName}.${name} = ${deconflicted};`);
|
|
849
|
+
}
|
|
720
850
|
}
|
|
721
851
|
}
|
|
722
|
-
})
|
|
852
|
+
} catch (err) {
|
|
853
|
+
_didIteratorError3 = true;
|
|
854
|
+
_iteratorError3 = err;
|
|
855
|
+
} finally {
|
|
856
|
+
try {
|
|
857
|
+
if (!_iteratorNormalCompletion3 && _iterator3.return != null) {
|
|
858
|
+
_iterator3.return();
|
|
859
|
+
}
|
|
860
|
+
} finally {
|
|
861
|
+
if (_didIteratorError3) {
|
|
862
|
+
throw _iteratorError3;
|
|
863
|
+
}
|
|
864
|
+
}
|
|
865
|
+
}
|
|
723
866
|
|
|
724
|
-
if (!hasDefaultExport && (names.length || !isEntry)) {
|
|
867
|
+
if (!hasDefaultExport && (names.length || !isEntry && !isEsModule)) {
|
|
725
868
|
wrapperEnd = `\n\nvar ${moduleName} = {\n${names.map(({
|
|
726
869
|
name,
|
|
727
870
|
deconflicted
|
|
@@ -732,10 +875,10 @@ function transformCommonjs(parse, code, id, isEntry, ignoreGlobal, ignoreRequire
|
|
|
732
875
|
Object.keys(namedExports).filter(key => !blacklist[key]).forEach(addExport);
|
|
733
876
|
const defaultExport = /__esModule/.test(code) ? `export default ${HELPERS_NAME}.unwrapExports(${moduleName});` : `export default ${moduleName};`;
|
|
734
877
|
const named = namedExportDeclarations.filter(x => x.name !== 'default' || !hasDefaultExport).map(x => x.str);
|
|
735
|
-
const exportBlock = `\n\n${[defaultExport].concat(named).concat(hasDefaultExport ? defaultExportPropertyAssignments : []).join('\n')}`;
|
|
878
|
+
const exportBlock = `\n\n${(isEsModule ? [] : [defaultExport]).concat(named).concat(hasDefaultExport ? defaultExportPropertyAssignments : []).join('\n')}`;
|
|
736
879
|
magicString.trim().prepend(importBlock + wrapperStart).trim().append(wrapperEnd);
|
|
737
880
|
|
|
738
|
-
if (hasDefaultExport || named.length > 0 || shouldWrap || !isEntry) {
|
|
881
|
+
if (hasDefaultExport || named.length > 0 || shouldWrap || !isEntry && !isEsModule) {
|
|
739
882
|
magicString.append(exportBlock);
|
|
740
883
|
}
|
|
741
884
|
|
|
@@ -747,10 +890,195 @@ function transformCommonjs(parse, code, id, isEntry, ignoreGlobal, ignoreRequire
|
|
|
747
890
|
};
|
|
748
891
|
}
|
|
749
892
|
|
|
893
|
+
function getDynamicRequirePaths(patterns) {
|
|
894
|
+
const dynamicRequireModuleSet = new Set();
|
|
895
|
+
var _iteratorNormalCompletion = true;
|
|
896
|
+
var _didIteratorError = false;
|
|
897
|
+
var _iteratorError = undefined;
|
|
898
|
+
|
|
899
|
+
try {
|
|
900
|
+
for (var _iterator = (!patterns || Array.isArray(patterns) ? patterns || [] : [patterns])[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
|
|
901
|
+
const pattern = _step.value;
|
|
902
|
+
const isNegated = pattern.startsWith('!');
|
|
903
|
+
const modifySet = Set.prototype[isNegated ? 'delete' : 'add'].bind(dynamicRequireModuleSet);
|
|
904
|
+
var _iteratorNormalCompletion2 = true;
|
|
905
|
+
var _didIteratorError2 = false;
|
|
906
|
+
var _iteratorError2 = undefined;
|
|
907
|
+
|
|
908
|
+
try {
|
|
909
|
+
for (var _iterator2 = glob.sync(isNegated ? pattern.substr(1) : pattern)[Symbol.iterator](), _step2; !(_iteratorNormalCompletion2 = (_step2 = _iterator2.next()).done); _iteratorNormalCompletion2 = true) {
|
|
910
|
+
const path$1 = _step2.value;
|
|
911
|
+
modifySet(normalizePathSlashes(path.resolve(path$1)));
|
|
912
|
+
}
|
|
913
|
+
} catch (err) {
|
|
914
|
+
_didIteratorError2 = true;
|
|
915
|
+
_iteratorError2 = err;
|
|
916
|
+
} finally {
|
|
917
|
+
try {
|
|
918
|
+
if (!_iteratorNormalCompletion2 && _iterator2.return != null) {
|
|
919
|
+
_iterator2.return();
|
|
920
|
+
}
|
|
921
|
+
} finally {
|
|
922
|
+
if (_didIteratorError2) {
|
|
923
|
+
throw _iteratorError2;
|
|
924
|
+
}
|
|
925
|
+
}
|
|
926
|
+
}
|
|
927
|
+
}
|
|
928
|
+
} catch (err) {
|
|
929
|
+
_didIteratorError = true;
|
|
930
|
+
_iteratorError = err;
|
|
931
|
+
} finally {
|
|
932
|
+
try {
|
|
933
|
+
if (!_iteratorNormalCompletion && _iterator.return != null) {
|
|
934
|
+
_iterator.return();
|
|
935
|
+
}
|
|
936
|
+
} finally {
|
|
937
|
+
if (_didIteratorError) {
|
|
938
|
+
throw _iteratorError;
|
|
939
|
+
}
|
|
940
|
+
}
|
|
941
|
+
}
|
|
942
|
+
|
|
943
|
+
const dynamicRequireModuleDirPaths = Array.from(dynamicRequireModuleSet.values()).filter(path => {
|
|
944
|
+
try {
|
|
945
|
+
if (fs.statSync(path).isDirectory()) return true;
|
|
946
|
+
} catch (ignored) {// Nothing to do here
|
|
947
|
+
}
|
|
948
|
+
|
|
949
|
+
return false;
|
|
950
|
+
});
|
|
951
|
+
return {
|
|
952
|
+
dynamicRequireModuleSet,
|
|
953
|
+
dynamicRequireModuleDirPaths
|
|
954
|
+
};
|
|
955
|
+
}
|
|
956
|
+
|
|
957
|
+
var peerDependencies = {
|
|
958
|
+
rollup: "^1.20.0||^2.0.0"
|
|
959
|
+
};
|
|
960
|
+
|
|
961
|
+
/* eslint-disable no-undefined */
|
|
962
|
+
const isCjsPromises = new Map();
|
|
963
|
+
function getIsCjsPromise(id) {
|
|
964
|
+
let isCjsPromise = isCjsPromises.get(id);
|
|
965
|
+
if (isCjsPromise) return isCjsPromise.promise;
|
|
966
|
+
const promise = new Promise(resolve => {
|
|
967
|
+
isCjsPromise = {
|
|
968
|
+
resolve,
|
|
969
|
+
promise: undefined
|
|
970
|
+
};
|
|
971
|
+
isCjsPromises.set(id, isCjsPromise);
|
|
972
|
+
});
|
|
973
|
+
isCjsPromise.promise = promise;
|
|
974
|
+
return promise;
|
|
975
|
+
}
|
|
976
|
+
function setIsCjsPromise(id, resolution) {
|
|
977
|
+
const isCjsPromise = isCjsPromises.get(id);
|
|
978
|
+
|
|
979
|
+
if (isCjsPromise) {
|
|
980
|
+
if (isCjsPromise.resolve) {
|
|
981
|
+
isCjsPromise.resolve(resolution);
|
|
982
|
+
isCjsPromise.resolve = undefined;
|
|
983
|
+
}
|
|
984
|
+
} else {
|
|
985
|
+
isCjsPromises.set(id, {
|
|
986
|
+
promise: Promise.resolve(resolution),
|
|
987
|
+
resolve: undefined
|
|
988
|
+
});
|
|
989
|
+
}
|
|
990
|
+
}
|
|
991
|
+
|
|
992
|
+
/* eslint-disable no-param-reassign, no-undefined */
|
|
993
|
+
|
|
994
|
+
function getCandidatesForExtension(resolved, extension) {
|
|
995
|
+
return [resolved + extension, `${resolved}${path.sep}index${extension}`];
|
|
996
|
+
}
|
|
997
|
+
|
|
998
|
+
function getCandidates(resolved, extensions) {
|
|
999
|
+
return extensions.reduce((paths, extension) => paths.concat(getCandidatesForExtension(resolved, extension)), [resolved]);
|
|
1000
|
+
}
|
|
1001
|
+
|
|
1002
|
+
function getResolveId(extensions) {
|
|
1003
|
+
function resolveExtensions(importee, importer) {
|
|
1004
|
+
// not our problem
|
|
1005
|
+
if (importee[0] !== '.' || !importer) return undefined;
|
|
1006
|
+
const resolved = path.resolve(path.dirname(importer), importee);
|
|
1007
|
+
const candidates = getCandidates(resolved, extensions);
|
|
1008
|
+
|
|
1009
|
+
for (let i = 0; i < candidates.length; i += 1) {
|
|
1010
|
+
try {
|
|
1011
|
+
const stats = fs.statSync(candidates[i]);
|
|
1012
|
+
if (stats.isFile()) return {
|
|
1013
|
+
id: candidates[i]
|
|
1014
|
+
};
|
|
1015
|
+
} catch (err) {
|
|
1016
|
+
/* noop */
|
|
1017
|
+
}
|
|
1018
|
+
}
|
|
1019
|
+
|
|
1020
|
+
return undefined;
|
|
1021
|
+
}
|
|
1022
|
+
|
|
1023
|
+
function resolveId(importee, importer) {
|
|
1024
|
+
const isProxyModule = importee.endsWith(PROXY_SUFFIX);
|
|
1025
|
+
|
|
1026
|
+
if (isProxyModule) {
|
|
1027
|
+
importee = getIdFromProxyId(importee);
|
|
1028
|
+
} else if (importee.startsWith('\0')) {
|
|
1029
|
+
if (importee === HELPERS_ID || importee === DYNAMIC_PACKAGES_ID || importee.startsWith(DYNAMIC_JSON_PREFIX)) {
|
|
1030
|
+
return importee;
|
|
1031
|
+
}
|
|
1032
|
+
|
|
1033
|
+
return null;
|
|
1034
|
+
}
|
|
1035
|
+
|
|
1036
|
+
if (importee.startsWith(DYNAMIC_JSON_PREFIX)) {
|
|
1037
|
+
return importee;
|
|
1038
|
+
}
|
|
1039
|
+
|
|
1040
|
+
if (importer && importer.endsWith(PROXY_SUFFIX)) {
|
|
1041
|
+
importer = getIdFromProxyId(importer);
|
|
1042
|
+
}
|
|
1043
|
+
|
|
1044
|
+
return this.resolve(importee, importer, {
|
|
1045
|
+
skipSelf: true
|
|
1046
|
+
}).then(resolved => {
|
|
1047
|
+
if (!resolved) {
|
|
1048
|
+
resolved = resolveExtensions(importee, importer);
|
|
1049
|
+
}
|
|
1050
|
+
|
|
1051
|
+
if (isProxyModule) {
|
|
1052
|
+
if (!resolved) {
|
|
1053
|
+
return {
|
|
1054
|
+
id: getExternalProxyId(importee),
|
|
1055
|
+
external: false
|
|
1056
|
+
};
|
|
1057
|
+
}
|
|
1058
|
+
|
|
1059
|
+
resolved.id = (resolved.external ? getExternalProxyId : getProxyId)(resolved.id);
|
|
1060
|
+
resolved.external = false;
|
|
1061
|
+
return resolved;
|
|
1062
|
+
}
|
|
1063
|
+
|
|
1064
|
+
return resolved;
|
|
1065
|
+
});
|
|
1066
|
+
}
|
|
1067
|
+
|
|
1068
|
+
return resolveId;
|
|
1069
|
+
}
|
|
1070
|
+
|
|
750
1071
|
function commonjs(options = {}) {
|
|
751
1072
|
const extensions = options.extensions || ['.js'];
|
|
752
1073
|
const filter = pluginutils.createFilter(options.include, options.exclude);
|
|
753
1074
|
const ignoreGlobal = options.ignoreGlobal;
|
|
1075
|
+
|
|
1076
|
+
const _getDynamicRequirePat = getDynamicRequirePaths(options.dynamicRequireTargets),
|
|
1077
|
+
dynamicRequireModuleSet = _getDynamicRequirePat.dynamicRequireModuleSet,
|
|
1078
|
+
dynamicRequireModuleDirPaths = _getDynamicRequirePat.dynamicRequireModuleDirPaths;
|
|
1079
|
+
|
|
1080
|
+
const isDynamicRequireModulesEnabled = dynamicRequireModuleSet.size > 0;
|
|
1081
|
+
const commonDir = isDynamicRequireModulesEnabled ? getCommonDir(null, Array.from(dynamicRequireModuleSet).concat(process.cwd())) : null;
|
|
754
1082
|
const customNamedExports = {};
|
|
755
1083
|
|
|
756
1084
|
if (options.namedExports) {
|
|
@@ -790,9 +1118,7 @@ function commonjs(options = {}) {
|
|
|
790
1118
|
}
|
|
791
1119
|
|
|
792
1120
|
const esModulesWithoutDefaultExport = new Set();
|
|
793
|
-
const esModulesWithDefaultExport = new Set();
|
|
794
|
-
|
|
795
|
-
const allowDynamicRequire = !!options.ignore;
|
|
1121
|
+
const esModulesWithDefaultExport = new Set();
|
|
796
1122
|
const ignoreRequire = typeof options.ignore === 'function' ? options.ignore : Array.isArray(options.ignore) ? id => options.ignore.includes(id) : () => false;
|
|
797
1123
|
const resolveId = getResolveId(extensions);
|
|
798
1124
|
const sourceMap = options.sourceMap !== false;
|
|
@@ -803,22 +1129,23 @@ function commonjs(options = {}) {
|
|
|
803
1129
|
hasDefaultExport = _checkEsModule.hasDefaultExport,
|
|
804
1130
|
ast = _checkEsModule.ast;
|
|
805
1131
|
|
|
806
|
-
|
|
1132
|
+
const isDynamicRequireModule = dynamicRequireModuleSet.has(normalizePathSlashes(id));
|
|
1133
|
+
|
|
1134
|
+
if (isEsModule && !isDynamicRequireModule) {
|
|
807
1135
|
(hasDefaultExport ? esModulesWithDefaultExport : esModulesWithoutDefaultExport).add(id);
|
|
808
|
-
return null;
|
|
809
1136
|
} // it is not an ES module but it does not have CJS-specific elements.
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
}
|
|
1137
|
+
else if (!hasCjsKeywords(code, ignoreGlobal)) {
|
|
1138
|
+
esModulesWithoutDefaultExport.add(id);
|
|
1139
|
+
setIsCjsPromise(id, false);
|
|
1140
|
+
return null;
|
|
1141
|
+
}
|
|
816
1142
|
|
|
817
1143
|
const normalizedId = path.normalize(id);
|
|
818
|
-
const transformed = transformCommonjs(this.parse, code, id, this.getModuleInfo(id).isEntry, ignoreGlobal, ignoreRequire, customNamedExports[normalizedId], sourceMap,
|
|
1144
|
+
const transformed = transformCommonjs(this.parse, code, id, this.getModuleInfo(id).isEntry, isEsModule, ignoreGlobal || isEsModule, ignoreRequire, customNamedExports[normalizedId], sourceMap, isDynamicRequireModulesEnabled, dynamicRequireModuleSet, commonDir, ast);
|
|
1145
|
+
setIsCjsPromise(id, isEsModule ? false : Boolean(transformed));
|
|
819
1146
|
|
|
820
1147
|
if (!transformed) {
|
|
821
|
-
esModulesWithoutDefaultExport.add(id);
|
|
1148
|
+
if (!isEsModule || isDynamicRequireModule) esModulesWithoutDefaultExport.add(id);
|
|
822
1149
|
return null;
|
|
823
1150
|
}
|
|
824
1151
|
|
|
@@ -849,32 +1176,131 @@ function commonjs(options = {}) {
|
|
|
849
1176
|
resolveId,
|
|
850
1177
|
|
|
851
1178
|
load(id) {
|
|
852
|
-
if (id === HELPERS_ID)
|
|
1179
|
+
if (id === HELPERS_ID) {
|
|
1180
|
+
let code = HELPERS; // Do not bloat everyone's code with the module manager code
|
|
1181
|
+
|
|
1182
|
+
if (isDynamicRequireModulesEnabled) code += HELPERS_DYNAMIC;else code += HELPER_NON_DYNAMIC;
|
|
1183
|
+
return code;
|
|
1184
|
+
} // generate proxy modules
|
|
1185
|
+
|
|
853
1186
|
|
|
854
1187
|
if (id.endsWith(EXTERNAL_SUFFIX)) {
|
|
855
1188
|
const actualId = getIdFromExternalProxyId(id);
|
|
856
1189
|
const name = getName(actualId);
|
|
1190
|
+
if (actualId === HELPERS_ID || actualId === DYNAMIC_PACKAGES_ID) // These do not export default
|
|
1191
|
+
return `import * as ${name} from ${JSON.stringify(actualId)}; export default ${name};`;
|
|
857
1192
|
return `import ${name} from ${JSON.stringify(actualId)}; export default ${name};`;
|
|
858
1193
|
}
|
|
859
1194
|
|
|
860
|
-
if (id
|
|
861
|
-
const
|
|
1195
|
+
if (id === DYNAMIC_PACKAGES_ID) {
|
|
1196
|
+
let code = `const { commonjsRegister } = require('${HELPERS_ID}');`;
|
|
1197
|
+
var _iteratorNormalCompletion = true;
|
|
1198
|
+
var _didIteratorError = false;
|
|
1199
|
+
var _iteratorError = undefined;
|
|
1200
|
+
|
|
1201
|
+
try {
|
|
1202
|
+
for (var _iterator = dynamicRequireModuleDirPaths[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
|
|
1203
|
+
const dir = _step.value;
|
|
1204
|
+
let entryPoint = 'index.js';
|
|
1205
|
+
|
|
1206
|
+
try {
|
|
1207
|
+
if (fs.existsSync(path.join(dir, 'package.json'))) {
|
|
1208
|
+
entryPoint = JSON.parse(fs.readFileSync(path.join(dir, 'package.json'), {
|
|
1209
|
+
encoding: 'utf8'
|
|
1210
|
+
})).main || entryPoint;
|
|
1211
|
+
}
|
|
1212
|
+
} catch (ignored) {// ignored
|
|
1213
|
+
}
|
|
1214
|
+
|
|
1215
|
+
code += `\ncommonjsRegister(${JSON.stringify(getVirtualPathForDynamicRequirePath(dir, commonDir))}, function (module, exports) {
|
|
1216
|
+
module.exports = require(${JSON.stringify(normalizePathSlashes(path.join(dir, entryPoint)))});
|
|
1217
|
+
});`;
|
|
1218
|
+
}
|
|
1219
|
+
} catch (err) {
|
|
1220
|
+
_didIteratorError = true;
|
|
1221
|
+
_iteratorError = err;
|
|
1222
|
+
} finally {
|
|
1223
|
+
try {
|
|
1224
|
+
if (!_iteratorNormalCompletion && _iterator.return != null) {
|
|
1225
|
+
_iterator.return();
|
|
1226
|
+
}
|
|
1227
|
+
} finally {
|
|
1228
|
+
if (_didIteratorError) {
|
|
1229
|
+
throw _iteratorError;
|
|
1230
|
+
}
|
|
1231
|
+
}
|
|
1232
|
+
}
|
|
1233
|
+
|
|
1234
|
+
return code;
|
|
1235
|
+
}
|
|
1236
|
+
|
|
1237
|
+
let actualId = id;
|
|
1238
|
+
const isDynamicJson = actualId.startsWith(DYNAMIC_JSON_PREFIX);
|
|
1239
|
+
|
|
1240
|
+
if (isDynamicJson) {
|
|
1241
|
+
actualId = actualId.slice(DYNAMIC_JSON_PREFIX.length);
|
|
1242
|
+
}
|
|
1243
|
+
|
|
1244
|
+
const normalizedPath = normalizePathSlashes(actualId);
|
|
1245
|
+
|
|
1246
|
+
if (isDynamicJson) {
|
|
1247
|
+
return `require('${HELPERS_ID}').commonjsRegister(${JSON.stringify(getVirtualPathForDynamicRequirePath(normalizedPath, commonDir))}, function (module, exports) {
|
|
1248
|
+
module.exports = require(${JSON.stringify(normalizedPath)});
|
|
1249
|
+
});`;
|
|
1250
|
+
}
|
|
1251
|
+
|
|
1252
|
+
if (dynamicRequireModuleSet.has(normalizedPath) && !normalizedPath.endsWith('.json')) {
|
|
1253
|
+
// Try our best to still export the module fully.
|
|
1254
|
+
// The commonjs polyfill should take care of circular references.
|
|
1255
|
+
return `require('${HELPERS_ID}').commonjsRegister(${JSON.stringify(getVirtualPathForDynamicRequirePath(normalizedPath, commonDir))}, function (module, exports) {
|
|
1256
|
+
${fs.readFileSync(normalizedPath, {
|
|
1257
|
+
encoding: 'utf8'
|
|
1258
|
+
})}
|
|
1259
|
+
});`;
|
|
1260
|
+
}
|
|
1261
|
+
|
|
1262
|
+
if (actualId.endsWith(PROXY_SUFFIX)) {
|
|
1263
|
+
actualId = getIdFromProxyId(actualId);
|
|
862
1264
|
const name = getName(actualId);
|
|
863
1265
|
return getIsCjsPromise(actualId).then(isCjs => {
|
|
864
|
-
if (isCjs) return `import { __moduleExports } from ${JSON.stringify(actualId)}; export default __moduleExports;`;else if (esModulesWithoutDefaultExport.has(actualId)) return `import * as ${name} from ${JSON.stringify(actualId)}; export default ${name};`;else if (esModulesWithDefaultExport.has(actualId)) {
|
|
1266
|
+
if (dynamicRequireModuleSet.has(normalizePathSlashes(actualId)) && !actualId.endsWith('.json')) return `import {commonjsRequire} from '${HELPERS_ID}'; const ${name} = commonjsRequire(${JSON.stringify(getVirtualPathForDynamicRequirePath(normalizePathSlashes(actualId), commonDir))}); export default (${name} && ${name}['default']) || ${name}`;else if (isCjs) return `import { __moduleExports } from ${JSON.stringify(actualId)}; export default __moduleExports;`;else if (esModulesWithoutDefaultExport.has(actualId)) return `import * as ${name} from ${JSON.stringify(actualId)}; export default ${name};`;else if (esModulesWithDefaultExport.has(actualId)) {
|
|
865
1267
|
return `export {default} from ${JSON.stringify(actualId)};`;
|
|
866
1268
|
}
|
|
867
1269
|
return `import * as ${name} from ${JSON.stringify(actualId)}; import {getCjsExportFromNamespace} from "${HELPERS_ID}"; export default getCjsExportFromNamespace(${name})`;
|
|
868
1270
|
});
|
|
869
1271
|
}
|
|
870
1272
|
|
|
1273
|
+
if (isDynamicRequireModulesEnabled && this.getModuleInfo(id).isEntry) {
|
|
1274
|
+
let code;
|
|
1275
|
+
|
|
1276
|
+
try {
|
|
1277
|
+
code = fs.readFileSync(actualId, {
|
|
1278
|
+
encoding: 'utf8'
|
|
1279
|
+
});
|
|
1280
|
+
} catch (ex) {
|
|
1281
|
+
this.warn(`Failed to read file ${actualId}, dynamic modules might not work correctly`);
|
|
1282
|
+
return null;
|
|
1283
|
+
}
|
|
1284
|
+
|
|
1285
|
+
let dynamicImports = Array.from(dynamicRequireModuleSet).map(dynamicId => `require(${JSON.stringify(DYNAMIC_REGISTER_PREFIX + dynamicId)});`).join('\n');
|
|
1286
|
+
|
|
1287
|
+
if (dynamicRequireModuleDirPaths.length) {
|
|
1288
|
+
dynamicImports += `require(${JSON.stringify(DYNAMIC_REGISTER_PREFIX + DYNAMIC_PACKAGES_ID)});`;
|
|
1289
|
+
}
|
|
1290
|
+
|
|
1291
|
+
code = dynamicImports + '\n' + code;
|
|
1292
|
+
return code;
|
|
1293
|
+
}
|
|
1294
|
+
|
|
871
1295
|
return null;
|
|
872
1296
|
},
|
|
873
1297
|
|
|
874
1298
|
transform(code, id) {
|
|
875
|
-
if (
|
|
876
|
-
|
|
877
|
-
|
|
1299
|
+
if (id !== DYNAMIC_PACKAGES_ID && !id.startsWith(DYNAMIC_JSON_PREFIX)) {
|
|
1300
|
+
if (!filter(id) || extensions.indexOf(path.extname(id)) === -1) {
|
|
1301
|
+
setIsCjsPromise(id, null);
|
|
1302
|
+
return null;
|
|
1303
|
+
}
|
|
878
1304
|
}
|
|
879
1305
|
|
|
880
1306
|
let transformed;
|
|
@@ -883,10 +1309,10 @@ function commonjs(options = {}) {
|
|
|
883
1309
|
transformed = transformAndCheckExports.call(this, code, id);
|
|
884
1310
|
} catch (err) {
|
|
885
1311
|
transformed = null;
|
|
1312
|
+
setIsCjsPromise(id, false);
|
|
886
1313
|
this.error(err, err.loc);
|
|
887
1314
|
}
|
|
888
1315
|
|
|
889
|
-
setIsCjsPromise(id, Boolean(transformed));
|
|
890
1316
|
return transformed;
|
|
891
1317
|
}
|
|
892
1318
|
|