-
Notifications
You must be signed in to change notification settings - Fork 2
/
vite.config.ts
312 lines (257 loc) · 8.49 KB
/
vite.config.ts
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
// @ts-expect-error node type
import fs from "node:fs";
// @ts-expect-error node type
import url from "node:url";
// @ts-expect-error node type
import os from "node:os";
// @ts-expect-error node type
import { Worker } from "node:worker_threads";
declare const require: (packageName: string) => unknown;
declare const $require: typeof require;
import { defineConfig } from "vite";
import type { PluginOption } from "vite";
import { build } from "esbuild";
import type { Plugin } from "esbuild";
import fastGlob from "fast-glob";
import pkg from "./package.json" assert { type: "json" };
const mvaDeps = Object.keys(pkg.dependencies).filter((name) => name.startsWith("@codingame"));
// https://vitejs.dev/config/
export default defineConfig({
plugins: [extensionWorkerTranformer(), CompressAssetsSetupPWA()],
define: {
__APP_NAME: "'JSIDE'",
__APP_VERSION: JSON.stringify(pkg.version),
__APP_DATE: JSON.stringify(new Date().toLocaleDateString())
},
build: {
target: "es2020",
reportCompressedSize: false,
cssCodeSplit: false,
assetsInlineLimit: 2048,
modulePreload: {
resolveDependencies: () => []
},
rollupOptions: {
output: {
manualChunks: {
vscode: [...mvaDeps]
}
}
}
},
server: {
headers: {
"Cross-Origin-Embedder-Policy": "credentialless",
"Cross-Origin-Opener-Policy": "same-origin",
"Cross-Origin-Resource-Policy": "cross-origin"
}
},
optimizeDeps: {
include: ["vscode-semver", ...mvaDeps],
esbuildOptions: {
plugins: [importMetaResolver()]
}
},
resolve: {
dedupe: ["vscode", ...mvaDeps]
}
});
function importMetaResolver() {
return {
name: "import.meta.url",
setup({ onLoad }) {
// Help vite that bundles/move files in dev mode without touching `import.meta.url` which breaks asset urls
onLoad({ filter: /.*\.js/, namespace: "file" }, async (args: { path: string }) => {
const code = fs.readFileSync(args.path, "utf8");
const assetImportMetaUrlRE =
/\bnew\s+URL\s*\(\s*('[^']+'|"[^"]+"|`[^`]+`)\s*,\s*import\.meta\.url\s*(?:,\s*)?\)/g;
let i = 0;
let newCode = "";
for (let match = assetImportMetaUrlRE.exec(code); match != null; match = assetImportMetaUrlRE.exec(code)) {
newCode += code.slice(i, match.index);
const path = match[1].slice(1, -1);
// @ts-expect-error ESNext feature
const resolved = import.meta.resolve!(path, url.pathToFileURL(args.path));
newCode += `new URL(${JSON.stringify(url.fileURLToPath(resolved))}, import.meta.url)`;
i = assetImportMetaUrlRE.lastIndex;
}
newCode += code.slice(i);
return { contents: newCode };
});
}
};
}
// VSCode worker extension file must be in CJS format and bundle must be enabled.
// I left it that way because I couldn't find a better way.
function extensionWorkerTranformer(): PluginOption {
let isBuildMode = false;
let wasmAbsolutePath = "";
let wasmOutputPath = "";
let tempWorkerFile = "";
const Provider: Plugin = {
name: "VSCodeExtensionProvier",
setup(build) {
build.onLoad({ filter: /\.wasm$/ }, (args) => {
// @ts-expect-error process is node global namespace
const path = args.path.replace(process.cwd(), "");
wasmAbsolutePath = args.path;
wasmOutputPath = path;
if (isBuildMode) {
const file = path.split("/").pop();
wasmOutputPath = `/assets/${file}`;
}
return {
contents: `export default "${wasmOutputPath}";`
};
});
}
};
return {
name: "ExtensionWorker",
enforce: "pre",
apply(_, env) {
if (env.command === "build") isBuildMode = true;
return true;
},
async transform(code, id) {
const fileRe = isBuildMode ? /extensions\/bundler\/index.ts$/ : /extensions\/bundler\/worker.ts$/;
if (fileRe.test(id)) {
const workerFile = isBuildMode ? id.replace(/index.ts$/, "worker.ts") : id;
const {
outputFiles: [file]
} = await build({
entryPoints: [workerFile],
plugins: [Provider],
platform: "node",
format: "cjs",
external: ["vscode"],
bundle: true,
minify: true,
write: false
});
if (isBuildMode) {
code = code.replace(
'new URL("./worker.ts", import.meta.url).toString()',
'new URL("./worker.js", import.meta.url).toString()'
);
tempWorkerFile = workerFile.replace(".ts", ".js");
fs.writeFileSync(tempWorkerFile, file.text);
}
return { code: isBuildMode ? code : file.text, map: null, moduleSideEffects: true };
}
},
closeBundle() {
if (!isBuildMode || wasmAbsolutePath.length === 0) return;
fs.cpSync(wasmAbsolutePath, `dist${wasmOutputPath}`);
fs.rmSync(tempWorkerFile);
}
};
}
function CompressAssetsSetupPWA(): PluginOption {
return {
name: "CompressAssetsSetupPWA",
enforce: "post",
apply: "build",
async closeBundle() {
await removeSomeFiles();
setupPWA();
await compressAssets();
}
};
}
async function removeSomeFiles() {
const files = fastGlob.sync([
"./dist/assets/diagnosticMessages*",
"./dist/assets/*.md",
"./dist/assets/*.svg",
"./dist/assets/*.png",
"./dist/assets/vs_code_editor_walkthrough*",
"./dist/assets/*.map",
"./dist/assets/notebookProfile*",
"./dist/assets/jquery*"
]);
const promises = files.map((file) => fs.promises.rm(file));
await Promise.all(promises);
}
async function setupPWA() {
let assets: string[] = fastGlob.sync("./dist/**", {
ignore: ["./dist/sw.js", "./dist/*.png", "./dist/**/*.map"]
});
// convert: ./dist/sw.js -> /sw.js
assets = assets.map((path) => path.slice("./dist".length));
const rootFileIndex = assets.findIndex((name) => name === "/index.html");
assets[rootFileIndex] = "/";
const contents = fs.readFileSync("./dist/sw.js", "utf-8").split("\n");
contents[0] = `const VERSION = "${pkg.version}";`;
contents[1] = `const files = ${JSON.stringify(assets)};`;
fs.writeFileSync("./dist/sw.js", contents.join("\n"), "utf-8");
console.log("PWA builded.");
}
async function compressAssets() {
const assets: string[] = fastGlob.sync("./dist/assets/*");
const threads = new Thread((filePath) => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const fs = $require("node:fs") as any;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const zlib = $require("node:zlib") as any;
const contents = fs.readFileSync(filePath);
const compressed = zlib.brotliCompressSync(contents, {
params: {
[zlib.constants.BROTLI_PARAM_QUALITY]: zlib.constants.BROTLI_MAX_QUALITY,
[zlib.constants.BROTLI_PARAM_MODE]: zlib.constants.BROTLI_MODE_TEXT
}
});
fs.writeFileSync(filePath, compressed);
});
await Promise.all(assets.map((filePath) => threads.run(filePath)));
console.log("Assets Compressed.");
}
class Thread<Args extends unknown[]> {
threads = new Set<Worker>();
queue: Array<() => void> = [];
maxThreadCount = (os?.availableParallelism() ?? os.cpus()) - 1;
code: string;
constructor(fun: (...args: Args) => unknown) {
this.code = this.buildWorkerCode(fun.toString());
}
async run(...args: Args): Promise<unknown> {
const worker = await this.getWorker();
return new Promise((resolve, reject) => {
worker.resolveMessage = resolve;
worker.rejectMessage = reject;
worker.postMessage(args);
});
}
async getWorker() {
if (this.threads.size <= this.maxThreadCount) {
const worker = new Worker(this.code, { eval: true });
worker.on("message", (ev) => {
worker.resolveMessage(ev);
worker.unref();
this.threads.delete(worker);
if (this.queue.length > 0) {
const resolve = this.queue.shift();
resolve();
}
});
worker.on("error", (err) => {
worker.rejectMessage();
console.error(err);
});
this.threads.add(worker);
return worker;
}
return new Promise<void>((resolve) => {
this.queue.push(resolve);
}).then(() => this.getWorker());
}
buildWorkerCode(fn: string) {
return `const { parentPort } = require("worker_threads");
const $require = require;
const fun = ${fn};
parentPort.on("message", async args => {
const res = await fun(...args);
parentPort.postMessage(res);
});`;
}
}