-
Notifications
You must be signed in to change notification settings - Fork 7
/
vite.config.ts
164 lines (160 loc) · 5.22 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
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
160
161
162
163
164
import { crx, defineManifest } from '@crxjs/vite-plugin'
import inject from '@rollup/plugin-inject'
import react from '@vitejs/plugin-react'
import path, { resolve } from 'path'
import { visualizer } from 'rollup-plugin-visualizer'
import { FileSystemIconLoader } from 'unplugin-icons/loaders'
import Icons from 'unplugin-icons/vite'
import type { Plugin } from 'vite'
import { defineConfig } from 'vite'
import commonjsExternals from 'vite-plugin-commonjs-externals'
import pkg from './package.json'
import _manifest from './public/manifest.json'
import { ModeEnum } from './src/types'
function renderChunks(deps: Record<string, string>) {
const chunks = {
chia: [
'@rigidity/bls-signatures',
'@rigidity/chia',
'@rigidity/clvm',
'bip39-web',
'buffer',
'decimal.js-light',
],
ui: [
'@emotion/react',
'@emotion/styled',
'@headlessui/react',
'classnames',
],
form: ['@hookform/resolvers', 'joi', 'react-hook-form'],
'state-management': ['mobx', 'mobx-react-lite'],
i18n: [
'i18next',
'i18next-browser-languagedetector',
'i18next-http-backend',
],
storage: ['@extend-chrome/storage', 'dexie', 'dexie-react-hooks'],
}
Object.keys(deps).forEach((key) => {
if (
[
'react',
'react-router-dom',
'react-dom',
...chunks.chia,
...chunks.ui,
...chunks.form,
...chunks['state-management'],
...chunks.i18n,
...chunks.storage,
].includes(key)
) {
return
}
chunks[key] = [key]
})
return chunks
}
const manifest = (mode) =>
defineManifest({
..._manifest,
name: `${_manifest.name}${mode === ModeEnum.production ? '' : '(Dev)'}`,
version: pkg.version.split('-')[0],
content_scripts: [
{
js: ['src/content-scripts/index.ts'],
matches: ['*://*/*'],
run_at: 'document_start',
},
],
background: {
service_worker: 'src/background/index.ts',
type: 'module',
},
action: {
default_popup:
mode === ModeEnum.production ? 'index.html' : 'index-dev.html',
},
})
// https://vitejs.dev/config/
export default defineConfig(({ mode }) => ({
define: {
__APP_VERSION__: JSON.stringify(process.env.npm_package_version),
// ...(mode !== ModeEnum.production &&
// mode !== ModeEnum.report && {
// global: {},
// }),
},
plugins: [
Icons({
compiler: 'jsx',
jsx: 'react',
customCollections: {
hoogii: FileSystemIconLoader('./public/images/icons', (svg) =>
svg
.replace(/width="([^"]*)"/, '')
.replace(/height="([^"]*)"/, '')
.replace(/fill="([^"]*)"/g, '')
.replace(/stroke="([^"]*)"/g, '')
.replace(/^<svg /, '<svg fill="currentColor" ')
),
},
}),
commonjsExternals({
externals: ['path', 'fs'],
}),
react(),
crx({ manifest: manifest(mode) }),
mode === ModeEnum.report &&
(visualizer({
filename: `reports/stats-${new Date().toISOString()}.html`,
}) as Plugin),
mode === ModeEnum.development &&
inject({
Buffer: ['buffer', 'Buffer'],
}),
]
.filter(Boolean)
.map((item) => item as Plugin),
optimizeDeps: {
exclude: ['Buffer'], // <= The libraries that need shimming should be excluded from dependency optimization.
esbuildOptions: { target: 'esnext', supported: { bigint: true } },
},
resolve: {
alias: {
'~': path.resolve(__dirname, './src'),
'~config': path.resolve(__dirname, './config'),
'~tabs': path.resolve(__dirname, './src/tabs'),
},
},
build: {
target: ['esnext'],
sourcemap: false,
rollupOptions: {
// reference: https://vitejs.dev/guide/build.html#multi-page-app
input: {
extension: resolve(__dirname, 'index.html'),
popup: resolve(__dirname, 'popup.html'),
tabs: resolve(__dirname, 'tabs.html'),
...(mode === ModeEnum.production
? {}
: {
dev: resolve(__dirname, 'index-dev.html'),
'dev-tools': resolve(__dirname, 'tools/index.html'),
}),
},
output: {
manualChunks: {
vendor: ['react', 'react-router-dom', 'react-dom'],
...renderChunks(pkg.dependencies),
},
},
plugins: [
inject({
Buffer: ['buffer', 'Buffer'],
}) as Plugin,
],
},
},
}))