-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathvite.config.ts
138 lines (132 loc) · 3.87 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
import react from '@vitejs/plugin-react';
import history from 'connect-history-api-fallback';
import fs from 'fs';
import { cpus } from 'os';
import { extname, join, parse, resolve } from 'path';
import { visualizer } from 'rollup-plugin-visualizer';
import { defineConfig } from 'vite';
import svgr from 'vite-plugin-svgr';
import tsconfigPaths from 'vite-tsconfig-paths';
import { inspectorServer } from '@react-dev-inspector/vite-plugin'
function entryPoints(...paths) {
const entries = paths.map(parse).map(entry => {
const { dir, base, name, ext } = entry;
const key = join(dir, name);
const path = resolve(__dirname, dir, base);
return [key, path];
});
const config = Object.fromEntries(entries);
return config;
}
// https://vitejs.dev/config/
export default defineConfig(({ mode }) => ({
define: {
'process.env': {},
'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV),
'process.env.PUBLIC_URL': JSON.stringify(process.env.PUBLIC_URL),
},
plugins: [
react({
babel: {
plugins: [
'babel-plugin-styled-components',
['@babel/plugin-proposal-decorators', { legacy: true }],
],
},
}),
inspectorServer(),
{
name: 'historyApiFallback',
configureServer(server) {
server.middlewares.use(
history({
htmlAcceptHeaders: ['text/html', 'application/xhtml+xml'],
disableDotRule: false,
rewrites: [
{ from: /^\/$/, to: '/index.html' },
{ from: /^\/shareChart\/\w/, to: '/shareChart.html' },
{ from: /^\/shareDashboard\/\w/, to: '/shareDashboard.html' },
{ from: /^\/shareStoryPlayer\/\w/, to: '/shareStoryPlayer.html' },
],
}),
);
},
},
{
name: 'custom-chart-plugins',
configureServer(server) {
server.middlewares.use((req, res, next) => {
if (req.url === '/api/v1/plugins/custom/charts') {
const pluginPath = 'custom-chart-plugins';
const dir = fs.readdirSync(`./public/${pluginPath}`);
res.setHeader('Content-Type', 'application/json');
res.end(
JSON.stringify({
data: (dir || [])
.filter(file => extname(file) === '.js')
.map(file => `${pluginPath}/${file}`),
errCode: 0,
success: true,
}),
);
return;
}
next();
});
},
},
svgr({
svgrOptions: {},
}),
tsconfigPaths(),
visualizer({ open: process.env.NODE_ENV === 'development' }),
],
build: {
emptyOutDir: true,
sourcemap: false,
outDir: './build',
assetsDir: 'static',
assetsInlineLimit: 4096 * 2,
chunkSizeWarningLimit: 1024,
rollupOptions: {
maxParallelFileOps: Math.max(1, cpus().length - 1),
input: entryPoints(
'index.html',
'shareChart.html',
'shareDashboard.html',
'shareStoryPlayer.html',
),
output: {
chunkFileNames: 'static/js/[name].[hash].js',
assetFileNames: assetInfo => {
// 用后缀名称进行区别处理
// 处理其他资源文件名 e.g. css png 等
let subDir = 'media';
if (extname(assetInfo?.name) === '.css') {
subDir = 'css';
}
return `static/${subDir}/[name].[hash].[ext]`;
},
// 入口文件名
entryFileNames: 'static/js/[name].[hash].js',
},
},
},
server: {
open: true,
port: 8000,
proxy: {
'/api/v1': {
changeOrigin: true,
target: "http://datart-demo.retech.cc/"
},
'/resources': {
changeOrigin: true,
target: "http://datart-demo.retech.cc/"
},
},
},
esbuild: {
drop: mode === 'production' ? ['console', 'debugger'] : [],
},
}));