-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathvite.config.js
117 lines (114 loc) · 3.38 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
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
import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'
import { resolve } from 'path'
import fs from 'fs-extra'
import { rollup } from 'rollup'
import commonjs from '@rollup/plugin-commonjs'
import { nodeResolve } from '@rollup/plugin-node-resolve'
import typescript from '@rollup/plugin-typescript'
import replace from '@rollup/plugin-replace'
const root = resolve(__dirname, 'src');
const outDir = resolve(__dirname, 'dist');
const publicDir = resolve(__dirname, 'public');
const getFeatureDirectories = () => {
return fs.readdirSync(root, { withFileTypes: true })
.filter(dirent => dirent.isDirectory() && dirent.name !== 'components')
.map(dirent => dirent.name);
};
export default defineConfig({
root,
publicDir,
plugins: [
react(),
{
name: 'build-content-script',
async writeBundle() {
const contentScriptInput = resolve(__dirname, 'src/content-main.ts');
const bundle = await rollup({
input: contentScriptInput,
plugins: [
nodeResolve(),
commonjs(),
typescript({
tsconfig: './tsconfig.json',
module: 'esnext',
}),
replace({
'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV),
preventAssignment: true
})
]
});
await bundle.write({
file: resolve(outDir, 'content.js'),
format: 'iife',
name: 'content'
});
await bundle.close();
}
},
{
name: 'copy-manifest',
buildEnd() {
const manifestPath = resolve(publicDir, 'manifest.json');
if (fs.existsSync(manifestPath)) {
fs.copySync(manifestPath, resolve(outDir, 'manifest.json'));
}
}
},
{
name: 'copy-icons',
buildEnd() {
fs.copySync(resolve(publicDir, 'icons'), resolve(outDir, 'icons'), { overwrite: true });
}
}
],
build: {
outDir,
emptyOutDir: true,
sourcemap: 'inline',
rollupOptions: {
input: {
...getFeatureDirectories().reduce((acc, feature) => {
if (feature === 'DevTool' || feature === 'PopUp') {
acc[`${feature}/index`] = resolve(__dirname, `src/${feature}/${feature}.html`);
}
if (feature === 'DevTool') {
acc[`${feature}/DevtoolRouter`] = resolve(__dirname, `src/${feature}/DevtoolRouter.tsx`);
}
return acc;
}, {}),
'background': resolve(__dirname, 'src/background-main.ts'),
},
output: {
entryFileNames: '[name].js',
chunkFileNames: 'assets/[name].[hash].js',
assetFileNames: (assetInfo) => {
const info = assetInfo.name.split('/');
const feature = info[0];
if (['DevTool', 'PopUp'].includes(feature)) {
if (assetInfo.name.endsWith('.html')) {
return `${feature}/[name][extname]`;
}
return `${feature}/assets/[name][extname]`;
}
return 'assets/[name][extname]';
},
},
},
target: ['chrome116', 'firefox115'],
minify: true,
},
resolve: {
alias: {
'@': resolve(__dirname, './src'),
},
},
define: {
'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV),
__DEV__: process.env.NODE_ENV !== 'production',
},
optimizeDeps: {
include: ['webextension-polyfill'],
}
})