-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvite.config.js
263 lines (248 loc) · 8.93 KB
/
vite.config.js
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
import path from 'node:path';
import { defineConfig as defineViteConfig } from 'vite';
import viteVue from '@vitejs/plugin-vue';
import AutoImport from 'unplugin-auto-import/vite';
import Components from 'unplugin-vue-components/vite';
import * as ComponentResolvers from 'unplugin-vue-components/resolvers';
import { visualizer } from 'rollup-plugin-visualizer';
import Inspect from 'vite-plugin-inspect';
// import { viteZip as ZipFile } from 'vite-plugin-zip-file';
import { ensureDirSync, readJsonSync, outputJsonSync } from 'fs-extra/esm';
import portfinder from 'portfinder';
import { mergeAndConcat } from 'merge-anything';
import Unocss from 'unocss/vite';
import { defineConfig as defineUnocssConfig } from 'unocss';
import Icons from 'unplugin-icons/vite';
import IconsResolver from 'unplugin-icons/resolver';
import ReactivityTransform from '@vue-macros/reactivity-transform/vite';
import VueDevTools from 'vite-plugin-vue-devtools';
// import Markdown from 'vite-plugin-md';
// 内部文件
import { yiteRouter } from './plugins/router.js';
import { yiteI18n } from './plugins/i18n.js';
import { unocssConfig } from './unocss.js';
import { fnFileProtocolPath, fnOmit, fnImport, log4state } from './utils/index.js';
import { fnAppDir } from './system.js';
const appDir = fnAppDir(process.env.YITE_CLI_WORK_DIR);
export default defineViteConfig(async ({ command, mode }) => {
// 没有则生成目录
ensureDirSync(appDir, '.cache');
const yiteConfigPath = fnFileProtocolPath(path.resolve(appDir, 'yite.config.js'));
const { yiteConfig } = await fnImport(yiteConfigPath, 'yiteConfig', {});
if (!yiteConfig.viteConfig) {
console.log(`${log4state('error')} 请确认是否存在 yite.config.js 文件`);
process.exit();
}
const pkg = readJsonSync(path.resolve(appDir, 'package.json'), { throws: false }) || {};
const findPort = await portfinder.getPortPromise({ port: 8000, stopPort: 9000 });
// 每个项目依赖包进行分割
// let splitDependencies = {};
// let includeDeps = [];
// for (let prop in pkg.dependencies) {
// if (pkg.dependencies.hasOwnProperty(prop)) {
// splitDependencies[prop] = [prop];
// includeDeps.push(prop);
// }
// }
// 自动导入插件
const autoImportConfig = mergeAndConcat(
{
include: [
//
/\.[tj]sx?$/, // .ts, .tsx, .js, .jsx
/\.vue$/,
/\.vue\?vue/, // .vue
/\.md$/ // .md
],
imports: [
'vue',
{
'vue-router': [
//
'RouterLink',
'RouterView',
'START_LOCATION',
'createMemoryHistory',
'createRouter',
'createWebHashHistory',
'createWebHistory',
'isNavigationFailure',
'loadRouteLocation',
'onBeforeRouteLeave',
'onBeforeRouteUpdate',
'useLink',
'useRoute',
'useRouter'
],
pinia: [
//
'PiniaVuePlugin',
'acceptHMRUpdate',
'createPinia',
'defineStore',
'disposePinia',
'getActivePinia',
'mapActions',
'mapGetters',
'mapState',
'mapStores',
'mapWritableState',
'setActivePinia',
'setMapStoreSuffix',
'skipHydrate',
'storeToRefs'
]
}
],
dirs: [
//
path.resolve(appDir, 'src', 'plugins'),
path.resolve(appDir, 'src', 'hooks'),
path.resolve(appDir, 'src', 'utils'),
path.resolve(appDir, 'src', 'config')
],
defaultExportByFilename: true,
vueTemplate: true,
dts: '.cache/auto-imports.d.ts',
resolvers: [],
eslintrc: {
enabled: true,
filepath: './.cache/eslintrc-auto-import.json',
globalsPropValue: 'readonly'
}
},
fnOmit(yiteConfig?.autoImport || {}, ['resolvers']),
{
resolvers: yiteConfig?.autoImport?.resolvers?.map((item) => ComponentResolvers[item.name](item.options)) || []
}
);
// 自动导入组件
const componentsConfig = mergeAndConcat(
{
dirs: [
//
path.resolve(appDir, 'src', 'components')
],
dts: '.cache/components.d.ts',
version: 3,
directoryAsNamespace: true,
resolvers: [IconsResolver()]
},
fnOmit(yiteConfig?.autoComponent || {}, ['resolvers']),
{
resolvers:
yiteConfig?.autoComponent?.resolvers?.map((item) => {
return ComponentResolvers[item.name](item.options);
}) || []
}
);
// 代码分割
// let chunkSplitConfig = {
// strategy: 'default',
// customChunk: (args) => {
// if (args.file.endsWith('.png')) {
// return 'png';
// }
// return null;
// },
// customSplitting: Object.assign(splitDependencies, yiteConfig?.chunkSplit || {})
// };
// 插件列表
let allPlugins = [];
// allPlugins.push(Markdown()) ;
allPlugins.push(yiteRouter({}));
allPlugins.push(yiteI18n({}));
allPlugins.push(ReactivityTransform());
allPlugins.push(
Icons({
compiler: 'vue3'
})
);
allPlugins.push(Components(componentsConfig));
allPlugins.push(AutoImport(autoImportConfig));
allPlugins.push(Unocss(defineUnocssConfig(unocssConfig)));
// allPlugins.push(ChunkSplit(chunkSplitConfig));
// 默认不使用二维码,多个网卡情况下会很乱
// allPlugins.push(YiteQrcode());
// allPlugins.push(ZipFile(zipPlugin));
allPlugins.push(
visualizer({
filename: '.cache/stats.html',
title: pkg?.name || '编译可视化'
})
);
allPlugins.push(
viteVue({
include: [/\.vue$/, /\.md$/],
...(yiteConfig?.pluginsConfig?.vue || {})
})
);
if (yiteConfig?.devtool === true) {
allPlugins.push(VueDevTools({}));
}
if (yiteConfig?.inspect === true) {
allPlugins.push(Inspect({}));
}
const viteConfig = mergeAndConcat(
{
plugins: allPlugins,
css: {
preprocessorOptions: {
scss: {}
}
},
resolve: {
alias: [
{
find: '@',
replacement: path.resolve(appDir, 'src')
},
{
find: 'vue-i18n',
replacement: 'vue-i18n/dist/vue-i18n.esm-bundler.js'
}
]
},
optimizeDeps: {
include: []
},
root: appDir,
base: './',
mode: process.env.YITE_CLI_ENV,
envDir: path.resolve(appDir, 'src', 'env'),
logLevel: 'info',
build: {
minify: process.env.YITE_CLI_MODE === 'development' ? false : true,
reportCompressedSize: false,
chunkSizeWarningLimit: 4096,
target: ['es2022'],
rollupOptions: {
plugins: [],
output: {
// TODO: 进一步研究 22
// assetFileNames: ({ name }) => {
// if (/\.(gif|jpe?g|png|svg)$/.test(name ?? '')) {
// return 'assets/images/[name]-[hash][extname]';
// }
// if (/\.css$/.test(name ?? '')) {
// return 'assets/css/[name]-[hash][extname]';
// }
// }
// return 'assets/[name]-[hash][extname]';
// }
}
}
},
server: {
host: '0.0.0.0',
port: findPort,
watch: {
ignored: ['**/node_modules/**/*', '**/.git/**/*']
}
}
},
yiteConfig?.viteConfig || {}
);
outputJsonSync(path.resolve(appDir, '.cache', 'vite-config.json'), viteConfig);
return viteConfig;
});