-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathvite.config.ts
188 lines (183 loc) · 5.49 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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
import { defineConfig, type UserConfig, type ConfigEnv } from 'vite';
import react from '@vitejs/plugin-react-swc';
// import viteImagemin from 'vite-plugin-imagemin';
import compression from 'vite-plugin-compression';
import { VitePWA } from 'vite-plugin-pwa';
import legacy from '@vitejs/plugin-legacy';
import { visualizer } from 'rollup-plugin-visualizer';
import eslint from 'vite-plugin-eslint';
import postcssPreset from 'postcss-preset-env';
import path from 'path';
import sassVar2JSON from './scripts/sass-to-json';
/** 更改 antd 主题样式更新需要重启 */
const theme = sassVar2JSON();
console.log('theme', theme);
/** open sentry */
const OPEN_SENTRY: boolean = false; // process.env.NODE_ENV === 'production'
// https://vitejs.dev/config/
export default defineConfig(((env: ConfigEnv) => {
const isDev = env.mode === 'development';
return {
server: {
// port: 3000, // 设置端口号
proxy: {
'/api': {
target: 'http://localhost:8080',
changeOrigin: true,
// rewrite: path => path.replace(/^\/api/, '')
},
// 图片上传
'/images': {
target: 'http://localhost:8080',
changeOrigin: true,
// rewrite: path => path.replace(/^\/api/, '')
},
},
},
resolve: {
alias: {
'@': path.resolve(__dirname, 'src'),
},
},
css: {
postcss: {
plugins: isDev ? [] : [postcssPreset()],
},
preprocessorOptions: {
scss: {
// More info: https://sass-lang.com/d/legacy-js-api
// Deprecation Warning: The legacy JS API is deprecated and will be removed in Dart Sass 2.0.0.
api: 'modern-compiler',
},
},
},
build: {
minify: 'terser',
rollupOptions: {
output: {
/**
* 以对象的方式使用
* 例如 lodash 模块打包成一个 chunk,名称是 lodash
*/
manualChunks: {
dll: [
'react',
'react-dom',
'react-router-dom',
'axios',
'redux',
'react-redux',
'@reduxjs/toolkit',
'js-cookie',
'classnames',
],
sentry: OPEN_SENTRY ? ['@sentry/browser'] : [],
},
},
},
chunkSizeWarningLimit: 1000,
terserOptions: {
// 清除console和debugger
compress: {
drop_console: true,
drop_debugger: true,
},
},
},
define: {
$__THEME__$: theme, // antd theme
$__SENTRY__$: OPEN_SENTRY, // process.env.NODE_ENV === 'production' // open sentry
$__SENTRY__DSN__$: JSON.stringify('https://examplePublicKey@o0.ingest.sentry.io/0'),
$__IS_PRODUCTION__$: env.mode === 'production',
},
plugins: [
react(),
// https://www.npmjs.com/package/@vitejs/plugin-legacy
// Vite's default browser support baseline is Native ESM, native ESM dynamic import, and import.meta. This plugin provides support for legacy browsers that do not support those features when building for production.
// 低版本浏览器兼容
legacy({
targets: ['chrome 52', 'Android >= 39', 'iOS >= 10.3', 'IE >= 11'], // 需要兼容的目标列表,可以设置多个
modernPolyfills: true,
// additionalLegacyPolyfills: ['regenerator-runtime/runtime'], // 面向IE11时需要此插件
// polyfills: ['es.promise', 'es.array.iterator']
}),
// viteImagemin({
// gifsicle: {
// optimizationLevel: 7,
// interlaced: false,
// },
// optipng: {
// optimizationLevel: 7,
// },
// mozjpeg: {
// quality: 20,
// },
// pngquant: {
// quality: [0.8, 0.9],
// speed: 4,
// },
// svgo: {
// plugins: [
// {
// name: 'removeViewBox',
// },
// {
// name: 'removeEmptyAttrs',
// active: false,
// },
// ],
// },
// }),
compression(),
VitePWA({
registerType: 'autoUpdate',
workbox: {
clientsClaim: true,
skipWaiting: true,
globPatterns: ['**/*.{js,css,html,ico,jpg,png,svg,gif,webmanifest}'],
},
includeAssets: ['**/*.{png}'],
manifest: {
name: 'React App',
start_url: '/',
short_name: 'React App',
description: 'React Antd Typescript Starter',
background_color: '#242424',
theme_color: '#00b96b',
icons: [
{
src: 'icons/192x192.png',
sizes: '192x192',
type: 'image/png',
},
{
src: 'icons/512x512.png',
sizes: '512x512',
type: 'image/png',
},
],
screenshots: [
{
src: 'icons/screenshot_320x320.png',
sizes: '320x320',
type: 'image/png',
form_factor: 'narrow',
},
{
src: 'icons/screenshot_1024x593.png',
sizes: '1024x593',
type: 'image/png',
form_factor: 'wide',
},
],
},
}),
process.env.ANALYZER
? visualizer({
gzipSize: true,
})
: null,
isDev ? eslint() : undefined,
].filter(Boolean),
};
}) as UserConfig);