-
Notifications
You must be signed in to change notification settings - Fork 51
/
vite.config.ts
52 lines (46 loc) · 1.45 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
import { readFile } from 'node:fs/promises';
import MagicString from 'magic-string';
import type { PackageJson } from 'type-fest';
import { defineConfig, type Plugin } from 'vite';
const packageJson: PackageJson = await readFile(new URL('./package.json', import.meta.url), 'utf8').then(JSON.parse);
// This plugin adds "__REGISTER__()" function definition everywhere where it finds
// the call for that function. It is necessary for a correct code for tests.
function loadRegisterJs(): Plugin {
return {
enforce: 'pre',
name: 'vite-hilla-register',
async transform(code) {
if (code.includes('__REGISTER__()') && !code.includes('function __REGISTER__')) {
const registerCode = await readFile(new URL('scripts/register.js', import.meta.url), 'utf8').then((c) =>
c.replace('export', ''),
);
const _code: MagicString = new MagicString(code);
_code.prepend(registerCode);
return {
code: _code.toString(),
map: _code.generateMap(),
};
}
return null;
},
};
}
// https://vitejs.dev/config/
export default defineConfig({
resolve: {
alias: {
'/base': '', // support '/base' prefix for karma
},
},
build: {
target: 'esnext',
},
esbuild: {
define: {
__NAME__: `'${packageJson.name ?? '@hilla/unknown'}'`,
__VERSION__: `'${packageJson.version ?? '0.0.0'}'`,
},
},
plugins: [loadRegisterJs()],
root: process.cwd(),
});