forked from TypeFox/monaco-languageclient
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vite.config.ts
66 lines (63 loc) · 3.03 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
/* eslint-disable header/header */
import { defineConfig } from 'vite';
import * as path from 'path';
import * as fs from 'fs';
import url from 'url';
export default defineConfig(() => {
const config = {
build: {
target: 'esnext',
rollupOptions: {
input: {
client: path.resolve(__dirname, 'packages/examples/client.html'),
statemachineClient: path.resolve(__dirname, 'packages/examples/statemachine_client.html'),
browser: path.resolve(__dirname, 'packages/examples/browser.html'),
react: path.resolve(__dirname, 'packages/examples/react.html'),
python: path.resolve(__dirname, 'packages/examples/python.html')
}
}
},
resolve: {
// not needed here, see https://github.com/TypeFox/monaco-languageclient#vite-dev-server-troubleshooting
// dedupe: ['monaco-editor', 'vscode']
},
server: {
origin: 'http://localhost:8080',
port: 8080
},
optimizeDeps: {
esbuildOptions: {
plugins: [
// copied from "https://github.com/CodinGame/monaco-vscode-api/blob/main/demo/vite.config.ts"
{
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 => {
const code = fs.readFileSync(args.path, 'utf8');
if (!args.path.includes('@codingame')) {
return { contents: code };
}
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);
const resolved = await 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 };
});
}
}]
}
},
define: {
rootDirectory: JSON.stringify(__dirname)
}
};
return config;
});