forked from xiaoluoboding/vue-sonner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vite.config.ts
106 lines (98 loc) · 2.61 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
import { defineConfig, UserConfig } from 'vite'
import { resolve } from 'path'
import vue from '@vitejs/plugin-vue'
import UnoCSS from 'unocss/vite'
import Icons from 'unplugin-icons/vite'
import IconsResolver from 'unplugin-icons/resolver'
import Components from 'unplugin-vue-components/vite'
import CleanCSS from 'clean-css'
const cleanCssInstance = new CleanCSS({})
function minify(code: string) {
return cleanCssInstance.minify(code).styles
}
let cssCodeStr = ''
export default defineConfig(({ command, mode }) => {
let userConfig: UserConfig = {}
const commonPlugins = [
vue(),
UnoCSS(),
Components({
resolvers: [
IconsResolver({
prefix: ''
})
]
}),
Icons()
]
if (mode === 'lib') {
userConfig.build = {
lib: {
entry: resolve(__dirname, 'packages/index.ts'),
name: 'VueSonner',
fileName: 'vue-sonner'
},
outDir: 'lib',
emptyOutDir: true,
cssCodeSplit: false,
sourcemap: true,
rollupOptions: {
external: ['vue'],
output: [
{
format: 'cjs',
entryFileNames: `vue-sonner.cjs`,
},
{
format: 'es',
entryFileNames: `vue-sonner.js`,
preserveModules: false
},
],
}
}
userConfig.plugins = [
...commonPlugins,
{
name: 'inline-css',
transform(code, id) {
const isCSS = (path: string) => /\.css$/.test(path)
if(!isCSS(id)) return
const cssCode = minify(code)
cssCodeStr = cssCode
return {
code: '',
map: { mappings: '' },
}
},
renderChunk(code, { isEntry }) {
if(!isEntry) return
return {
code: `\
function __insertCSSVueSonner(code) {
if (!code || typeof document == 'undefined') return
let head = document.head || document.getElementsByTagName('head')[0]
let style = document.createElement('style')
style.type = 'text/css'
head.appendChild(style)
;style.styleSheet ? (style.styleSheet.cssText = code) : style.appendChild(document.createTextNode(code))
}\n
__insertCSSVueSonner(${JSON.stringify(cssCodeStr)})
\n ${code}`,
map: { mappings: '' },
}
},
},
]
}
return {
resolve: {
alias: {
'@': resolve(__dirname, '/packages'),
'~': resolve(__dirname, '/src')
}
},
plugins: [...commonPlugins],
...userConfig
}
})