This repository has been archived by the owner on Apr 19, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
198 lines (165 loc) · 4.7 KB
/
index.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
const { resolve, relative } = require("path");
const { writeFileSync, mkdirSync, existsSync } = require("fs");
function resolveImportMap(dir) {
const pckg = getPackageJson(dir);
const map = pckg.importmap;
if (typeof map === "string") {
const target = resolve(dir, map);
if (existsSync(target)) {
return require(target);
} else {
console.warn(
`Could not find the referenced import maps "${map}" from ${dir}. Skipping.`
);
}
} else if (typeof map === "object") {
return map;
}
return undefined;
}
function getPackageJson(dir) {
if (dir) {
const path = resolve(dir, "package.json");
return require(path);
}
return {};
}
function getPackageDir(dir) {
const path = resolve(dir, "package.json");
if (existsSync(path)) {
return dir;
}
const upper = resolve(dir, "..");
if (upper !== dir) {
return getPackageDir(upper);
}
return undefined;
}
function createFile(dir, name, content) {
const path = resolve(dir, `${name}.js`);
writeFileSync(path, content, "utf8");
return path;
}
function getSourcePath(root, file) {
return `/${relative(root, file)}`;
}
function resolvePath(root, dir, file) {
if (file.startsWith('/')) {
return getSourcePath(root, `.${file}`);
} else {
return getSourcePath(root, resolve(dir, file));
}
}
function getHashFor(root, dir, file) {
if (!file.startsWith('http:') && !file.startsWith('https:')) {
const path = resolvePath(root, dir, file);
return `require.resolve(${JSON.stringify(path)})[0][0]`;
} else {
return JSON.stringify(file);
}
}
function getImportFor(root, dir, file) {
if (!file.startsWith('http:') && !file.startsWith('https:')) {
const path = resolvePath(root, dir, file);
return `import(${JSON.stringify(path)})`;
} else {
return `new Promise((resolve, reject) => {
const xhr = new XMLHttpRequest();
xhr.open("GET", ${JSON.stringify(file)});
xhr.onreadystatechange = function() {
if (xhr.readyState === 4) {
if (xhr.status === 200) {
const result = {
exports: {},
};
const f = new Function('module', 'exports', 'require', xhr.responseText);
f(result, result.exports, require);
resolve(result.exports);
} else {
reject(xhr.statusText);
}
}
};
xhr.send();
})`;
}
}
let original;
module.exports = function(bundler) {
const root = bundler.options.rootDir;
const dir = getPackageDir(root);
const map = resolveImportMap(dir);
const keys = Object.keys((map && map.imports) || {});
if (keys.length > 0) {
const modules = {};
const temp = resolve(__dirname, "_temp");
const resolver = bundler.resolver;
const getAlias = original || (original = resolver.__proto__.getAlias);
if (!existsSync(temp)) {
mkdirSync(temp);
}
createFile(
temp,
"index",
`
if (!window.__importMaps) {
const imports = [];
window.__importMaps = true;
window.__resolveImport = function (id) {
for (const item of imports) {
if (item.id === id) {
return item;
}
}
return {};
};
window.__registerImports = function (newImports) {
newImports.forEach(i => {
if (!imports.some(j => j.id === i.id)) {
const item = {
id: i.id,
data: undefined,
};
item.loading = i.load().then(data => (item.data = data), err => console.error(err));
imports.push(item);
}
});
};
}
const localImports = [${keys.map(id => `{
id: ${getHashFor(root, dir, map.imports[id])},
ref: ${JSON.stringify(id)},
load: () => ${getImportFor(root, dir, map.imports[id])},
}`).join(',')}];
window.__registerImports(localImports);
function getId(ref) {
const [id] = localImports.filter(i => i.ref === ref).map(i => i.id);
return id;
}
exports.ready = function (ref) {
const refs = Array.isArray(ref) ? ref : (ref ? [ref] : localImports.map(i => i.ref));
return Promise.all(refs.map(ref => window.__resolveImport(getId(ref)).loading));
};
exports.resolve = function (ref) {
return window.__resolveImport(getId(ref)).data;
}
`
);
resolver.__proto__.getAlias = function(filename, dir, aliases) {
if (filename === 'importmap') {
return resolve(temp, 'index.js');
} else if (keys.includes(filename)) {
const m = modules[filename];
if (!m) {
return (modules[filename] = createFile(
temp,
Object.keys(modules).length.toString(),
`module.exports = require('importmap').resolve(${JSON.stringify(filename)})`,
));
}
return m;
}
return getAlias.call(resolver, filename, dir, aliases);
};
}
};