-
Notifications
You must be signed in to change notification settings - Fork 0
/
vite.config.ts
76 lines (71 loc) · 2 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
import type { ConfigEnv } from 'vite'
import react from '@vitejs/plugin-react'
import { defineConfig, splitVendorChunkPlugin } from 'vite'
import jotaiDebugLabel from 'jotai/babel/plugin-debug-label'
import jotaiReactRefresh from 'jotai/babel/plugin-react-refresh'
import tsConfigPath from 'vite-tsconfig-paths'
import { readFileSync } from 'fs'
function transCssUrl(env: ConfigEnv) {
const fileRegex = /.*src.*\.scss(^|\?used)/g
const urlRegex = /background.*url\(.+\)/g
return {
name: 'transform-css-url',
load(id: string) {
if (env.mode === 'production') {
fileRegex.lastIndex = 0
urlRegex.lastIndex = 0
if (fileRegex.test(id)) {
let src = readFileSync(id.replace(/\?used$/g, ''), 'utf8')
if (urlRegex.test(src)) {
const reg = /(?<=(url\(('|")?))\/assets\//g
src = src.replace(reg, '../')
}
return {
code: src,
map: null,
}
}
}
},
}
}
export default defineConfig((env) => ({
plugins: [
env.mode === 'development' &&
react({
babel: { plugins: [jotaiDebugLabel, jotaiReactRefresh] },
}),
tsConfigPath(),
splitVendorChunkPlugin(),
transCssUrl(env),
],
base: './',
build: {
reportCompressedSize: false,
outDir: 'build',
rollupOptions: {
output: {
chunkFileNames: 'assets/js/[name]-[hash].js',
entryFileNames: 'assets/js/[name]-[hash].js',
assetFileNames: ({ name }) => {
if (/\.(gif|jpe?g|png|svg)$/.test(name ?? '')) {
return 'assets/imgs/[name]-[hash][extname]'
}
if (/\.css$/.test(name ?? '')) {
return 'assets/css/[name]-[hash][extname]'
}
return 'assets/[name].[ext]'
},
},
},
},
esbuild: { jsxInject: "import React from 'react'" },
// css: {
// preprocessorOptions: {
// scss: {
// additionalData:
// '@use "sass:math"; @import "src/styles/variables.scss";',
// },
// },
// },
}))