-
Notifications
You must be signed in to change notification settings - Fork 0
/
vite.config.ts
172 lines (164 loc) · 5.3 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
import { writeFileSync } from 'node:fs';
import { fileURLToPath, URL } from 'node:url';
import vue from '@vitejs/plugin-vue';
import { defineConfig, type Plugin, type UserConfig } from 'vite';
import { visualizer } from 'rollup-plugin-visualizer';
import UnoCSS from 'unocss/vite';
import ElementPlus from 'unplugin-element-plus/vite';
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers';
import Components from 'unplugin-vue-components/vite';
import { checker } from 'vite-plugin-checker';
import pkg from './package.json';
/**
* Vite Configure
*
* @see {@link https://vitejs.dev/config/}
*/
export default defineConfig(({ command, mode }): UserConfig => {
const config: UserConfig = {
// https://vitejs.dev/config/shared-options.html#base
base: './',
// https://vitejs.dev/config/shared-options.html#define
define: { 'process.env': {} },
plugins: [
// Vue3
vue(),
// vite-plugin-checker
// https://github.com/fi3ework/vite-plugin-checker
checker({
typescript: true,
// vueTsc: true,
// eslint: { lintCommand: 'eslint' },
// stylelint: { lintCommand: 'stylelint' },
}),
Components({
// allow auto load markdown components under `./src/components/`
extensions: ['vue', 'md'],
// allow auto import and register components used in markdown
include: [/\.vue$/, /\.vue\?vue/, /\.md$/],
resolvers: [
ElementPlusResolver({
importStyle: 'sass',
}),
],
}),
// Element Plus
// https://github.com/element-plus/unplugin-element-plus
ElementPlus({
// https://github.com/element-plus/unplugin-element-plus#usesource
useSource: false,
// https://github.com/element-plus/unplugin-element-plus#lib
// lib: 'element-plus',
// https://github.com/element-plus/unplugin-element-plus#format
// format: 'esm',
// https://github.com/element-plus/unplugin-element-plus#prefix
// prefix: 'el'
// https://github.com/element-plus/unplugin-element-plus#ignorecomponents
ignoreComponents: [],
// Replace default locale
// see https://github.com/element-plus/element-plus/tree/dev/packages/locale/lang
// defaultLocale: 'ja',
}),
// UnoCSS
// See uno.config.css
UnoCSS(),
// Fix Invalid event arguments.
patchRawWindow(),
],
// Resolver
resolve: {
// https://vitejs.dev/config/shared-options.html#resolve-alias
alias: {
'@': fileURLToPath(new URL('./src', import.meta.url)),
'~': fileURLToPath(new URL('./node_modules', import.meta.url)),
},
extensions: ['.js', '.json', '.jsx', '.mjs', '.ts', '.tsx', '.vue'],
},
css: {
preprocessorOptions: {
scss: {
additionalData: '@use "@/styles/element/index.scss" as *;',
},
},
},
// Build Options
// https://vitejs.dev/config/build-options.html
build: {
// Build Target
// https://vitejs.dev/config/build-options.html#build-target
target: 'esnext',
// Minify option
// https://vitejs.dev/config/build-options.html#build-minify
minify: 'esbuild',
// Rollup Options
// https://vitejs.dev/config/build-options.html#build-rollupoptions
rollupOptions: {
output: {
manualChunks: {
// Split external library from transpiled code.
vue: ['vue', 'vue-router', 'pinia', 'pinia-plugin-persistedstate'],
tinycolor: ['@ctrl/tinycolor/dist/module'],
'element-plus': ['element-plus'],
},
plugins: [
mode === 'analyze'
? // rollup-plugin-visualizer
// https://github.com/btd/rollup-plugin-visualizer
visualizer({
open: true,
filename: 'dist/stats.html',
})
: undefined,
],
},
},
},
esbuild: {
// Drop console when production build.
drop: command === 'serve' ? [] : ['console'],
},
};
// Write meta data.
writeFileSync(
fileURLToPath(new URL('./src/Meta.ts', import.meta.url)),
`import type MetaInterface from '@/interfaces/MetaInterface';
// This file is auto-generated by the build system.
const meta: MetaInterface = {
version: '${pkg.version}',
date: '${new Date().toISOString()}',
};
export default meta;
`
);
return config;
});
/**
* Fix for Invalid event arguments: event validation failed for event "click".
* @see {@link https://github.com/micro-zoe/micro-app/issues/756}
*/
function patchRawWindow(): Plugin {
return {
name: 'patchRawWindow',
transform(code, id) {
if (id.includes('/.vite/deps/chunk')) {
let isFind = false;
const _code = code.replace(
/instanceof (([A-Z]+[a-zA-Z]+)?Event)/g,
(_, $1: string) => {
isFind = true;
return `instanceof (patchRawWindow('${$1}') || ${$1})`;
}
);
const fn = `function patchRawWindow(key) {
if (window.__MICRO_APP_ENVIRONMENT__ && window[key]) {
return window.rawWindow[key]
} else {
return false
}
}\n`;
return isFind ? fn + _code : _code;
}
return code;
},
};
}