forked from mue/mue
-
Notifications
You must be signed in to change notification settings - Fork 2
/
vite.config.mjs
159 lines (150 loc) · 4.75 KB
/
vite.config.mjs
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import { defineConfig, loadEnv } from 'vite';
import react from '@vitejs/plugin-react-swc';
import path from 'path';
import fs from 'fs';
import ADMZip from 'adm-zip';
import * as pkg from './package.json';
import progress from 'vite-plugin-progress';
const isProd = process.env.NODE_ENV === 'production';
const prepareBuilds = () => ({
name: 'prepareBuilds',
buildEnd() {
if (isProd) {
// make directories if not exist
fs.mkdirSync(path.resolve(__dirname, './build'), { recursive: true });
fs.mkdirSync(path.resolve(__dirname, './dist'), { recursive: true });
// chrome
fs.mkdirSync(path.resolve(__dirname, './build/chrome'), { recursive: true });
fs.copyFileSync(
path.resolve(__dirname, './manifest/chrome.json'),
path.resolve(__dirname, './build/chrome/manifest.json'),
);
fs.copyFileSync(
path.resolve(__dirname, './manifest/background.js'),
path.resolve(__dirname, './build/chrome/background.js'),
);
fs.cpSync(
path.resolve(__dirname, './manifest/_locales'),
path.resolve(__dirname, './build/chrome/_locales'),
{
recursive: true,
},
);
fs.cpSync(path.resolve(__dirname, './dist'), path.resolve(__dirname, './build/chrome/'), {
recursive: true,
});
fs.cpSync(
path.resolve(__dirname, './src/assets/icons'),
path.resolve(__dirname, './build/chrome/icons'),
{
recursive: true,
},
);
fs.mkdirSync(path.resolve(__dirname, './build/chrome/src/assets'), { recursive: true });
fs.cpSync(
path.resolve(__dirname, './src/assets'),
path.resolve(__dirname, './build/chrome/src/assets'),
{
recursive: true,
},
);
// firefox
fs.mkdirSync(path.resolve(__dirname, './build/firefox'), { recursive: true });
fs.copyFileSync(
path.resolve(__dirname, './manifest/firefox.json'),
path.resolve(__dirname, './build/firefox/manifest.json'),
);
fs.copyFileSync(
path.resolve(__dirname, './manifest/background.js'),
path.resolve(__dirname, './build/firefox/background.js'),
);
fs.cpSync(path.resolve(__dirname, './dist'), path.resolve(__dirname, './build/firefox/'), {
recursive: true,
});
fs.cpSync(
path.resolve(__dirname, './src/assets/icons'),
path.resolve(__dirname, './build/firefox/icons'),
{
recursive: true,
},
);
fs.cpSync(
path.resolve(__dirname, './src/assets'),
path.resolve(__dirname, './build/firefox/src/assets'),
{
recursive: true,
},
);
// create zip
const zip = new ADMZip();
zip.addLocalFolder(path.resolve(__dirname, './build/chrome'));
zip.writeZip(path.resolve(__dirname, `./build/chrome-${pkg.version}.zip`));
const zip2 = new ADMZip();
zip2.addLocalFolder(path.resolve(__dirname, './build/firefox'));
zip2.writeZip(path.resolve(__dirname, `./build/firefox-${pkg.version}.zip`));
//todo: fix this
// temp copy src for /dist too
fs.cpSync(
path.resolve(__dirname, './src/assets'),
path.resolve(__dirname, './dist/src/assets'),
{
recursive: true,
},
);
}
},
});
export default defineConfig(({ command, mode }) => {
const env = loadEnv(mode, process.cwd(), '');
return {
define: {
__APP_ENV__: JSON.stringify(env.APP_ENV),
},
plugins: [react(), prepareBuilds(), progress()],
server: {
open: true,
hmr: {
protocol: 'ws',
host: 'localhost',
},
},
build: {
minify: isProd ? 'esbuild' : false,
sourcemap: !isProd,
rollupOptions: {
output: {
manualChunks(id) {
if (id.includes('node_modules')) {
if (id.includes('@mui')) {
return 'vendor_mui';
}
return 'vendor';
}
},
},
},
},
resolve: {
extensions: ['.js', '.jsx'],
alias: {
'@': path.resolve(__dirname, './src'),
i18n: path.resolve(__dirname, './src/i18n'),
components: path.resolve(__dirname, './src/components'),
assets: path.resolve(__dirname, './src/assets'),
config: path.resolve(__dirname, './src/config'),
features: path.resolve(__dirname, './src/features'),
lib: path.resolve(__dirname, './src/lib'),
scss: path.resolve(__dirname, './src/scss'),
translations: path.resolve(__dirname, './src/i18n/locales'),
utils: path.resolve(__dirname, './src/utils'),
},
},
css: {
preprocessorOptions: {
scss: {
api: 'modern-compiler',
},
},
},
};
});