-
-
Notifications
You must be signed in to change notification settings - Fork 561
/
helpers.js
247 lines (216 loc) · 7.48 KB
/
helpers.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
export const isWrappedId = (id, suffix) => id.endsWith(suffix);
export const wrapId = (id, suffix) => `\0${id}${suffix}`;
export const unwrapId = (wrappedId, suffix) => wrappedId.slice(1, -suffix.length);
export const PROXY_SUFFIX = '?commonjs-proxy';
export const REQUIRE_SUFFIX = '?commonjs-require';
export const EXTERNAL_SUFFIX = '?commonjs-external';
export const DYNAMIC_REGISTER_SUFFIX = '?commonjs-dynamic-register';
export const DYNAMIC_JSON_PREFIX = '\0commonjs-dynamic-json:';
export const DYNAMIC_PACKAGES_ID = '\0commonjs-dynamic-packages';
export const HELPERS_ID = '\0commonjsHelpers.js';
// `x['default']` is used instead of `x.default` for backward compatibility with ES3 browsers.
// Minifiers like uglify will usually transpile it back if compatibility with ES3 is not enabled.
// This will no longer be necessary once Rollup switches to ES6 output, likely
// in Rollup 3
const HELPERS = `
export var commonjsGlobal = typeof globalThis !== 'undefined' ? globalThis : typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {};
export function getDefaultExportFromCjs (x) {
return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;
}
export function getDefaultExportFromNamespaceIfPresent (n) {
return n && Object.prototype.hasOwnProperty.call(n, 'default') ? n['default'] : n;
}
export function getDefaultExportFromNamespaceIfNotNamed (n) {
return n && Object.prototype.hasOwnProperty.call(n, 'default') && Object.keys(n).length === 1 ? n['default'] : n;
}
export function getAugmentedNamespace(n) {
if (n.__esModule) return n;
var a = Object.defineProperty({}, '__esModule', {value: true});
Object.keys(n).forEach(function (k) {
var d = Object.getOwnPropertyDescriptor(n, k);
Object.defineProperty(a, k, d.get ? d : {
enumerable: true,
get: function () {
return n[k];
}
});
});
return a;
}
`;
const FAILED_REQUIRE_ERROR = `throw new Error('Could not dynamically require "' + path + '". Please configure the dynamicRequireTargets or/and ignoreDynamicRequires option of @rollup/plugin-commonjs appropriately for this require call to work.');`;
const HELPER_NON_DYNAMIC = `
export function createCommonjsModule(fn) {
var module = { exports: {} }
return fn(module, module.exports), module.exports;
}
export function commonjsRequire (path) {
${FAILED_REQUIRE_ERROR}
}
`;
const getDynamicHelpers = (ignoreDynamicRequires) => `
export function createCommonjsModule(fn, basedir, module) {
return module = {
path: basedir,
exports: {},
require: function (path, base) {
return commonjsRequire(path, (base === undefined || base === null) ? module.path : base);
}
}, fn(module, module.exports), module.exports;
}
export function commonjsRegister (path, loader) {
DYNAMIC_REQUIRE_LOADERS[path] = loader;
}
const DYNAMIC_REQUIRE_LOADERS = Object.create(null);
const DYNAMIC_REQUIRE_CACHE = Object.create(null);
const DEFAULT_PARENT_MODULE = {
id: '<' + 'rollup>', exports: {}, parent: undefined, filename: null, loaded: false, children: [], paths: []
};
const CHECKED_EXTENSIONS = ['', '.js', '.json'];
function normalize (path) {
path = path.replace(/\\\\/g, '/');
const parts = path.split('/');
const slashed = parts[0] === '';
for (let i = 1; i < parts.length; i++) {
if (parts[i] === '.' || parts[i] === '') {
parts.splice(i--, 1);
}
}
for (let i = 1; i < parts.length; i++) {
if (parts[i] !== '..') continue;
if (i > 0 && parts[i - 1] !== '..' && parts[i - 1] !== '.') {
parts.splice(--i, 2);
i--;
}
}
path = parts.join('/');
if (slashed && path[0] !== '/')
path = '/' + path;
else if (path.length === 0)
path = '.';
return path;
}
function join () {
if (arguments.length === 0)
return '.';
let joined;
for (let i = 0; i < arguments.length; ++i) {
let arg = arguments[i];
if (arg.length > 0) {
if (joined === undefined)
joined = arg;
else
joined += '/' + arg;
}
}
if (joined === undefined)
return '.';
return joined;
}
function isPossibleNodeModulesPath (modulePath) {
let c0 = modulePath[0];
if (c0 === '/' || c0 === '\\\\') return false;
let c1 = modulePath[1], c2 = modulePath[2];
if ((c0 === '.' && (!c1 || c1 === '/' || c1 === '\\\\')) ||
(c0 === '.' && c1 === '.' && (!c2 || c2 === '/' || c2 === '\\\\'))) return false;
if (c1 === ':' && (c2 === '/' || c2 === '\\\\'))
return false;
return true;
}
function dirname (path) {
if (path.length === 0)
return '.';
let i = path.length - 1;
while (i > 0) {
const c = path.charCodeAt(i);
if ((c === 47 || c === 92) && i !== path.length - 1)
break;
i--;
}
if (i > 0)
return path.substr(0, i);
if (path.chartCodeAt(0) === 47 || path.chartCodeAt(0) === 92)
return path.charAt(0);
return '.';
}
export function commonjsResolveImpl (path, originalModuleDir, testCache) {
const shouldTryNodeModules = isPossibleNodeModulesPath(path);
path = normalize(path);
let relPath;
if (path[0] === '/') {
originalModuleDir = '/';
}
while (true) {
if (!shouldTryNodeModules) {
relPath = originalModuleDir ? normalize(originalModuleDir + '/' + path) : path;
} else if (originalModuleDir) {
relPath = normalize(originalModuleDir + '/node_modules/' + path);
} else {
relPath = normalize(join('node_modules', path));
}
if (relPath.endsWith('/..')) {
break; // Travelled too far up, avoid infinite loop
}
for (let extensionIndex = 0; extensionIndex < CHECKED_EXTENSIONS.length; extensionIndex++) {
const resolvedPath = relPath + CHECKED_EXTENSIONS[extensionIndex];
if (DYNAMIC_REQUIRE_CACHE[resolvedPath]) {
return resolvedPath;
};
if (DYNAMIC_REQUIRE_LOADERS[resolvedPath]) {
return resolvedPath;
};
}
if (!shouldTryNodeModules) break;
const nextDir = normalize(originalModuleDir + '/..');
if (nextDir === originalModuleDir) break;
originalModuleDir = nextDir;
}
return null;
}
export function commonjsResolve (path, originalModuleDir) {
const resolvedPath = commonjsResolveImpl(path, originalModuleDir);
if (resolvedPath !== null) {
return resolvedPath;
}
return require.resolve(path);
}
export function commonjsRequire (path, originalModuleDir) {
const resolvedPath = commonjsResolveImpl(path, originalModuleDir, true);
if (resolvedPath !== null) {
let cachedModule = DYNAMIC_REQUIRE_CACHE[resolvedPath];
if (cachedModule) return cachedModule.exports;
const loader = DYNAMIC_REQUIRE_LOADERS[resolvedPath];
if (loader) {
DYNAMIC_REQUIRE_CACHE[resolvedPath] = cachedModule = {
id: resolvedPath,
filename: resolvedPath,
path: dirname(resolvedPath),
exports: {},
parent: DEFAULT_PARENT_MODULE,
loaded: false,
children: [],
paths: [],
require: function (path, base) {
return commonjsRequire(path, (base === undefined || base === null) ? cachedModule.path : base);
}
};
try {
loader.call(commonjsGlobal, cachedModule, cachedModule.exports);
} catch (error) {
delete DYNAMIC_REQUIRE_CACHE[resolvedPath];
throw error;
}
cachedModule.loaded = true;
return cachedModule.exports;
};
}
${ignoreDynamicRequires ? 'return require(path);' : FAILED_REQUIRE_ERROR}
}
commonjsRequire.cache = DYNAMIC_REQUIRE_CACHE;
commonjsRequire.resolve = commonjsResolve;
`;
export function getHelpersModule(isDynamicRequireModulesEnabled, ignoreDynamicRequires) {
return `${HELPERS}${
isDynamicRequireModulesEnabled ? getDynamicHelpers(ignoreDynamicRequires) : HELPER_NON_DYNAMIC
}`;
}