-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvite.config.ts
76 lines (63 loc) · 2.02 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
import { sync as glob } from 'fast-glob';
import { copyFileSync, existsSync, mkdirSync } from 'fs';
import { join as pathJoin, resolve as pathResolve } from 'path';
import { defineConfig, loadEnv, PluginOption } from 'vite';
import vue from '@vitejs/plugin-vue';
import vueJSX from '@vitejs/plugin-vue-jsx';
let BASE_URL = '/';
if (process.env.GH) {
BASE_URL = '/jptools/';
}
function cloneIndexHtmlPlugin(routes: string[] = []): PluginOption {
const name = 'CloneIndexHtmlPlugin';
const outDir = 'dist'; // config's `build.outDir`
const src = pathJoin(outDir, 'index.html');
return {
name,
closeBundle: () => {
const PAGE_EXT = '.vue';
const PAGE_INDEX = 'index';
routes.push(
...glob(`**/*${PAGE_EXT}`, {
cwd: 'src/pages',
})
.map((p) => {
p = p.substring(0, p.length - PAGE_EXT.length);
if (p.endsWith('/' + PAGE_INDEX)) {
p = p.substring(0, p.length - PAGE_INDEX.length - 1);
}
return p;
})
.filter((p) => p !== PAGE_INDEX),
);
routes.map((p) => {
const dir = pathResolve(outDir, p);
if (!existsSync(dir)) {
mkdirSync(dir, { recursive: true });
}
const dst = pathJoin(outDir, p, 'index.html');
copyFileSync(src, dst);
console.log(`${name}: Copied ${src} to ${dst}`);
});
},
};
}
// https://vitejs.dev/config/
export default defineConfig(({ command, mode }) => {
const env = loadEnv(mode, process.cwd());
const defaultEnv: Record<string, string> = {
NODE_API: process.env['NODE_API'] || 'http://localhost:7000/api',
};
defaultEnv['KUROMOJI_API'] = defaultEnv['NODE_API'] + '/tokenizer/kuromoji';
Object.entries(defaultEnv).map(([k, v]) => {
const k1 = `VITE_${k}`;
process.env[k1] = env[k1] || v;
});
return {
base: BASE_URL,
plugins: [vue(), vueJSX(), cloneIndexHtmlPlugin()],
resolve: {
alias: [{ find: /^@\/(.+)$/, replacement: pathResolve('src', '$1') }],
},
};
});