-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvite.config.js
84 lines (74 loc) · 1.89 KB
/
vite.config.js
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
import { defineConfig, loadEnv } from 'vite';
import path, { resolve } from 'node:path';
import react from '@vitejs/plugin-react';
import { compileHtml, getHtmlFilesToProcess } from './build/utils.mjs';
import { DEFAULT_LANG, i18n } from './build/i18n-utils.mjs';
import { fileURLToPath } from 'node:url';
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const SRC = resolve(__dirname, 'src');
export default defineConfig(({ mode }) => {
process.env = {
...loadEnv(mode, process.cwd(), ''),
...process.env,
VITE_APP_LANG: process.env.locale || DEFAULT_LANG,
};
const isProduction = mode === 'production';
return {
css: {
devSourcemap: true,
},
publicDir: resolve(__dirname, 'public'),
root: SRC,
envDir: __dirname,
plugins: [
isProduction && parseTranslationTag(),
react(),
{
name: 'custom-hmr',
handleHotUpdate,
},
],
build: {
outDir: getOutDir(),
emptyOutDir: false,
rollupOptions: {
input: getHtmlFilesToProcess(),
},
},
};
});
function parseTranslationTag() {
i18n.setLocale(process.env.locale || 'es');
return {
name: 'transform-html',
transformIndexHtml: {
enforce: 'pre',
transform(html) {
return compileHtml(html, {
...i18n,
});
},
},
};
}
function getOutDir() {
const path = resolve(__dirname, 'dist');
if (process.env.locale === DEFAULT_LANG) {
return path;
}
return resolve(path, process.env.locale || DEFAULT_LANG);
}
function handleHotUpdate(context) {
const filename = path.resolve(context.file);
if (
filename.endsWith('.html') ||
filename.endsWith('.ejs') ||
filename.endsWith('.json')
) {
console.info(
`File ${path.basename(filename)} has been changed. Sending full-reload.`
);
context.server.ws.send({ type: 'full-reload' });
return [];
}
}