-
-
Notifications
You must be signed in to change notification settings - Fork 521
Expand file tree
/
Copy pathpreload.js
More file actions
48 lines (41 loc) · 1.32 KB
/
preload.js
File metadata and controls
48 lines (41 loc) · 1.32 KB
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
import { promises as fs } from 'node:fs';
import path from 'node:path';
/**
* @returns {import('vite').Plugin}
*/
export function preloadPlugin() {
return {
name: 'preload-plugin',
async closeBundle() {
const [manifestContent, replContent, assetsDir] = await Promise.all([
fs.readFile(path.resolve('build', '.vite/manifest.json'), 'utf-8'),
fs.readFile(path.resolve('build', 'repl/index.html'), 'utf-8'),
fs.readdir(path.resolve('build', 'assets'))
]);
const replWorker = `assets/${assetsDir.find((file) => file.startsWith('repl.worker-'))}`;
const manifest = JSON.parse(manifestContent);
let errorOverlay = '';
for (const file in manifest) {
if (manifest[file].name == 'error-overlay') {
errorOverlay = file;
break;
}
}
const preloadTags = [
'src/components/controllers/repl-page.jsx',
'src/components/controllers/repl/runner.jsx',
'src/components/code-editor/index.jsx',
errorOverlay,
replWorker
].map((file) => {
const filePath = manifest[file] ? manifest[file].file : file;
return `\n\t\t<link rel="modulepreload" href="/${filePath}"></link>`;
}).join('');
const replWithPreload = replContent.replace(
'</head>',
`${preloadTags}</head>`
);
await fs.writeFile(path.resolve('build', 'repl/index.html'), replWithPreload);
}
};
}