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