-
Notifications
You must be signed in to change notification settings - Fork 0
/
dynamicImport.js
144 lines (134 loc) · 4.91 KB
/
dynamicImport.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
{
const bootstrap = ({global, module, require}) => {
const {process, dynamicImport, document, location} = global;
if (!dynamicImport) {
let baseURL = null;
const dynamicImport = async (specifier, referrer) => {
dynamicImport.base ||
(dynamicImport.base = `${(baseURL === null &&
(baseURL =
document && document.baseURI
? `${new URL('./', (dynamicImport['(document.baseURI)'] = document.baseURI))}`
: location && location.href
? `${new URL('./', (dynamicImport['(location.href)'] = location.href))}`
: (module &&
module.filename &&
pathToFileURL(
(dynamicImport['(module.filename)'] = (dynamicImport['(module)'] = module).filename),
)) ||
(process &&
process.cwd &&
pathToFileURL(
(dynamicImport['(process.cwd)'] = process.cwd()).replace(/([/\\]?)$/, (m, i, s) =>
m || /^[a-z]:\\/.test(s) ? '\\' : '/',
),
)) ||
(dynamicImport['(currentFileURL())'] = currentFileURL()))) ||
''}`);
// console.log({...dynamicImport});
referrer = referrer || dynamicImport.base || undefined;
// console.log({specifier, referrer});
const src = `${referrer ? new URL(specifier, referrer) : new URL(specifier)}`;
if (!('import' in dynamicImport)) {
try {
dynamicImport.import = null;
dynamicImport.import = (1, eval)(`async specifier => import(specifier)`);
} catch (exception) {
const promises = new Map();
if (require) {
dynamicImport.require = require;
dynamicImport.import = (specifier, referrer = dynamicImport.base) => {
let path, promise;
(promise = promises.get(src)) ||
promises.set(src, (promise = async () => dynamicImport.require(fileURLToPath(src))));
return promise;
};
} else if (document && document.body && document.createElement) {
const type = 'module';
dynamicImport.import = (specifier, referrer = dynamicImport.base) => {
let script, promise;
(promise = promises.get(src)) ||
promises.set(
src,
(promise = new Promise((onload, onerror) => {
document.body.append(
Object.assign((script = document.createElement('script')), {src, type, onload, onerror}),
);
})),
// .then(() => new Promise(resolve => requestAnimationFrame(resolve)))
);
promise.finally(() => script && script.remove());
return promise;
};
} else {
dynamicImport.import = () => {
throw exception;
};
}
}
}
return dynamicImport.import(src);
};
if (module) {
const prototype = Object.getPrototypeOf(module);
prototype.import = {
import(specifier) {
const {filename, url: referrer = (module.url = pathToFileURL(filename))} = this;
return dynamicImport(specifier, referrer);
},
}.import;
module.exports = dynamicImport;
}
return dynamicImport;
}
};
{
const scope = {
global: (1, eval)('this'),
module: (typeof module === 'object' && module) || undefined,
require: (typeof require === 'function' && require) || undefined,
};
scope.global.dynamicImport = bootstrap(scope);
}
function pathToFileURL(path) {
const WindowsPath = /^[a-z]:\\|^[^/]*\\(?![ \\])/;
const WindowsSeparators = /\\/g;
pathToFileURL = path =>
WindowsPath.test(path) ? new URL(`file://${path.replace(WindowsSeparators, '/')}`) : new URL(path, 'file:///');
return pathToFileURL(...arguments);
// const WindowsAbsolutePath = /^[a-z]:\\/;
// const WindowsRelativePath = /[.]{1,2}\\/;
}
function fileURLToPath(url) {
const WindowsPathname = /^\/[a-z]:\//;
const URLSeparators = /\//g;
fileURLToPath = url => {
const {protocol, pathname} = new URL(url);
const path = decodeURIComponent(pathname);
return WindowsPathname.test(path) ? path.slice(1).replace(URLSeparators, '\\') : path;
};
return fileURLToPath(...arguments);
}
function currentFileURL() {
/* TEST:
(async () => (await (1, eval)('{ (specifier) => import(specifier) }')(`data:text/javascript,export default Error('').stack`)).default)().catch(exception => Error('').stack).then(stack => `${stack}`.replace(/^[^]+?\n\s+(?:@|at .* [(](?=\S[^\n]+:\d+[)])|at (?=[^\n]+[^)](?:\n|$)))(\S[^\n]*?)(?:[:]\d+){2}[^]*$/, '$1')).then(console.log)
*/
let {
filename,
url = (currentFileURL.url =
((filename ||
(filename = currentFileURL.filename =
(currentFileURL.stack = `${Error('').stack}`)
.replace(
/^[^]+?\n\s+(?:@|at .* [(](?=\S[^\n]+:\d+[)])|at (?=[^\n]+[^)](?:\n|$)))(\S[^\n]*?)(?:[:]\d+){2}[^]*$/,
'$1',
)
.trim()
.replace(/^[^]*\n[^]*$/, '') || undefined)) &&
((/^(https?|file|data):/.test(filename) && new URL(filename)) ||
(/[/\\]/.test(filename) && pathToFileURL(filename)))) ||
undefined),
} = currentFileURL;
return url;
}
}