@moluoxixi/vite-config 0.0.25-beta.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +32 -0
- package/es/index.d.ts +4 -0
- package/es/index.mjs +996 -0
- package/es/src/_types/index.d.ts +89 -0
- package/es/src/_utils/getEnv.d.ts +7 -0
- package/es/src/_utils/index.d.ts +1 -0
- package/es/src/constants/index.d.ts +1 -0
- package/es/src/index.d.ts +6 -0
- package/es/src/plugins/addScopedAndReplacePrefix.d.ts +10 -0
- package/es/src/plugins/vitePluginQiankunStyle.d.ts +10 -0
- package/package.json +55 -0
package/README.md
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
# ViteConfig
|
|
2
|
+
|
|
3
|
+
一行集成稳定的 Vite 配置:内置 Vue 生态、自动引入、自动路由、微前端、构建优化与 CDN 等能力,并提供清晰开关。
|
|
4
|
+
|
|
5
|
+
## 快速开始
|
|
6
|
+
|
|
7
|
+
```ts
|
|
8
|
+
// vite.config.ts
|
|
9
|
+
import viteConfig, { wrapperEnv } from '@moluoxixi/viteconfig'
|
|
10
|
+
import path from 'node:path'
|
|
11
|
+
import { loadEnv } from 'vite'
|
|
12
|
+
|
|
13
|
+
export default viteConfig(({ mode }) => {
|
|
14
|
+
const env = loadEnv(mode!, process.cwd())
|
|
15
|
+
const viteEnv = wrapperEnv(env)
|
|
16
|
+
return {
|
|
17
|
+
rootPath: path.resolve(),
|
|
18
|
+
mode: {
|
|
19
|
+
base: { VITE_GLOB_APP_TITLE: viteEnv.VITE_GLOB_APP_TITLE, VITE_GLOB_APP_CODE: viteEnv.VITE_GLOB_APP_CODE, VITE_AUTO_ROUTES: true },
|
|
20
|
+
development: { VITE_DEVTOOLS: true },
|
|
21
|
+
production: {},
|
|
22
|
+
},
|
|
23
|
+
viteConfig: {},
|
|
24
|
+
autoRoutes: { routeConfig: {} },
|
|
25
|
+
}
|
|
26
|
+
})
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
## API 概览
|
|
30
|
+
|
|
31
|
+
- `createViteConfig(Config: ViteConfigType): UserConfigExport`
|
|
32
|
+
- 配置项主要包含:`rootPath`、`mode`、`viteConfig`、`autoRoutes`、`unpluginAutoImportOptions`、`unpluginVueComponentsOptions`、`CDNImportOptions`
|
package/es/index.d.ts
ADDED
package/es/index.mjs
ADDED
|
@@ -0,0 +1,996 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
3
|
+
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
4
|
+
import "vue";
|
|
5
|
+
import fs from "node:fs";
|
|
6
|
+
import path from "node:path";
|
|
7
|
+
import process$1 from "node:process";
|
|
8
|
+
import "dotenv";
|
|
9
|
+
import tailwindcss from "@tailwindcss/postcss";
|
|
10
|
+
import autoprefixer from "autoprefixer";
|
|
11
|
+
import { defineConfig, mergeConfig, normalizePath } from "vite";
|
|
12
|
+
import { createHtmlPlugin } from "vite-plugin-html";
|
|
13
|
+
import { debounce } from "lodash-es";
|
|
14
|
+
function getType(obj, type) {
|
|
15
|
+
{
|
|
16
|
+
return Object.prototype.toString.call(obj).slice(8, -1).toLowerCase() === type.toLowerCase();
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
async function dynamicImport(modulePromise, exportName = "default") {
|
|
20
|
+
const module = await modulePromise;
|
|
21
|
+
if (exportName === "default") {
|
|
22
|
+
return module.default ?? module;
|
|
23
|
+
}
|
|
24
|
+
if (!(exportName in module)) {
|
|
25
|
+
throw new Error(`模块中不存在导出 "${exportName}"`);
|
|
26
|
+
}
|
|
27
|
+
return module[exportName];
|
|
28
|
+
}
|
|
29
|
+
function isObject(value) {
|
|
30
|
+
return Object.prototype.toString.call(value) === "[object Object]";
|
|
31
|
+
}
|
|
32
|
+
function deepMerge(target, source) {
|
|
33
|
+
if (!isObject(source)) {
|
|
34
|
+
return target;
|
|
35
|
+
}
|
|
36
|
+
return Object.keys(source).reduce((acc, key) => {
|
|
37
|
+
const sourceValue = source[key];
|
|
38
|
+
const targetValue = acc[key];
|
|
39
|
+
if (isObject(sourceValue) && isObject(targetValue)) {
|
|
40
|
+
acc[key] = deepMerge(targetValue, sourceValue);
|
|
41
|
+
} else if (isObject(sourceValue)) {
|
|
42
|
+
acc[key] = deepMerge({}, sourceValue);
|
|
43
|
+
} else {
|
|
44
|
+
acc[key] = sourceValue;
|
|
45
|
+
}
|
|
46
|
+
return acc;
|
|
47
|
+
}, { ...target });
|
|
48
|
+
}
|
|
49
|
+
function changeHtmlClassPrefix(htmlString = "", oldPrefix = "", newPrefix = "") {
|
|
50
|
+
const regex = new RegExp(
|
|
51
|
+
`(class|style)\\s*:\\s*((["']((${oldPrefix}\\b)-).*["'])|((_normalizeClass|_normalizeStyle)\\(.*(${oldPrefix}\\b)-.*\\)))`,
|
|
52
|
+
"g"
|
|
53
|
+
);
|
|
54
|
+
return htmlString.replace(regex, (match = "") => {
|
|
55
|
+
return match.replace(oldPrefix, newPrefix);
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
function changeSelectorPrefix(cssString = "", oldPrefix = "", newPrefix = "") {
|
|
59
|
+
const regex = new RegExp(`(\\.${oldPrefix}\\b|#${oldPrefix}\\b|--${oldPrefix}\\b)`, "g");
|
|
60
|
+
return cssString.replace(regex, (match = "") => {
|
|
61
|
+
return match.replace(oldPrefix, newPrefix);
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
function addScopedAndReplacePrefixPlugin({
|
|
65
|
+
prefixScoped = "",
|
|
66
|
+
oldPrefix = "",
|
|
67
|
+
newPrefix = "",
|
|
68
|
+
useDevMode = false
|
|
69
|
+
}) {
|
|
70
|
+
let isProduction;
|
|
71
|
+
return {
|
|
72
|
+
name: "addScopedAndReplacePrefix",
|
|
73
|
+
configResolved(config) {
|
|
74
|
+
isProduction = config.command === "build" || config.isProduction;
|
|
75
|
+
},
|
|
76
|
+
transform(code = "", id = "") {
|
|
77
|
+
if (!isProduction && !useDevMode)
|
|
78
|
+
return code;
|
|
79
|
+
if (!oldPrefix || !newPrefix)
|
|
80
|
+
return code;
|
|
81
|
+
if (id.includes("node_modules"))
|
|
82
|
+
return code;
|
|
83
|
+
const cssLangs = ["css", "scss", "less", "stylus", "styl"];
|
|
84
|
+
let newCode = code;
|
|
85
|
+
if (id.endsWith(".vue")) {
|
|
86
|
+
newCode = changeHtmlClassPrefix(newCode, oldPrefix, newPrefix);
|
|
87
|
+
} else if (cssLangs.some((lang) => id.endsWith(`.${lang}`))) {
|
|
88
|
+
if (oldPrefix && newPrefix) {
|
|
89
|
+
newCode = changeSelectorPrefix(newCode, oldPrefix, newPrefix);
|
|
90
|
+
}
|
|
91
|
+
if (prefixScoped) {
|
|
92
|
+
newCode = `${newCode}${prefixScoped}{${newCode}}`;
|
|
93
|
+
}
|
|
94
|
+
return newCode;
|
|
95
|
+
}
|
|
96
|
+
return newCode;
|
|
97
|
+
}
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
async function getViteConfig(Config, params) {
|
|
101
|
+
const configResult = typeof Config === "function" ? Config(params) : Config;
|
|
102
|
+
if (!configResult || !configResult.rootPath) {
|
|
103
|
+
throw new Error("rootPath is required in ViteConfig");
|
|
104
|
+
}
|
|
105
|
+
const config = configResult;
|
|
106
|
+
const { mode = "base" } = params || {};
|
|
107
|
+
const rootPath = config.rootPath;
|
|
108
|
+
const modeConfig = config.mode || {};
|
|
109
|
+
const baseConfig = modeConfig.base || {};
|
|
110
|
+
const currentModeConfig = modeConfig[mode] || {};
|
|
111
|
+
const viteEnv = { ...baseConfig, ...currentModeConfig };
|
|
112
|
+
const isDev = mode === "development";
|
|
113
|
+
const {
|
|
114
|
+
autoImport = config.autoImport ?? true,
|
|
115
|
+
autoComponent = config.autoComponent ?? true,
|
|
116
|
+
compression = config.compression ?? true,
|
|
117
|
+
imagemin = config.imagemin ?? true,
|
|
118
|
+
codeInspector = config.codeInspector ?? true,
|
|
119
|
+
port = config.port,
|
|
120
|
+
visualizer = config.visualizer ?? false,
|
|
121
|
+
autoRoutes = config.autoRoutes ?? false,
|
|
122
|
+
cdn = config.cdn ?? false,
|
|
123
|
+
pageRoutes = config.pageRoutes ?? false,
|
|
124
|
+
pwa = config.pwa ?? false,
|
|
125
|
+
devtools = config.devtools,
|
|
126
|
+
open = config.open,
|
|
127
|
+
qiankunDevMode = config.qiankunDevMode,
|
|
128
|
+
qiankun = config.qiankun,
|
|
129
|
+
namespace = config.namespace,
|
|
130
|
+
dropConsole = config.dropConsole,
|
|
131
|
+
vue = config.vue ?? true,
|
|
132
|
+
react = config.react,
|
|
133
|
+
vitepress = config.vitepress
|
|
134
|
+
} = viteEnv;
|
|
135
|
+
const appTitle = config.appTitle;
|
|
136
|
+
const appCode = config.appCode;
|
|
137
|
+
const isOnlyVue = !vitepress && !react && vue;
|
|
138
|
+
const isVueOrVitepress = vue || vitepress;
|
|
139
|
+
const envSystemCode = isDev && !qiankunDevMode ? "el" : namespace ?? appCode;
|
|
140
|
+
const plugins = [
|
|
141
|
+
createHtmlPlugin({
|
|
142
|
+
inject: {
|
|
143
|
+
data: {
|
|
144
|
+
title: appTitle
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
})
|
|
148
|
+
];
|
|
149
|
+
if (isOnlyVue) {
|
|
150
|
+
const pluginVue = await dynamicImport(import("@vitejs/plugin-vue"));
|
|
151
|
+
plugins.push(pluginVue());
|
|
152
|
+
}
|
|
153
|
+
if (isVueOrVitepress) {
|
|
154
|
+
const vueJsx = await dynamicImport(import("@vitejs/plugin-vue-jsx"));
|
|
155
|
+
plugins.push(vueJsx());
|
|
156
|
+
}
|
|
157
|
+
if (pageRoutes) {
|
|
158
|
+
const Pages = await dynamicImport(import("vite-plugin-pages"));
|
|
159
|
+
plugins.push(Pages(
|
|
160
|
+
deepMerge(
|
|
161
|
+
{
|
|
162
|
+
dirs: "src/pages",
|
|
163
|
+
extensions: [isOnlyVue && "vue"].filter(Boolean),
|
|
164
|
+
exclude: [
|
|
165
|
+
"**/components/**",
|
|
166
|
+
"**/__tests__/**"
|
|
167
|
+
]
|
|
168
|
+
},
|
|
169
|
+
pageRoutes
|
|
170
|
+
)
|
|
171
|
+
));
|
|
172
|
+
}
|
|
173
|
+
if (isDev && devtools) {
|
|
174
|
+
const vueDevTools = await dynamicImport(import("vite-plugin-vue-devtools"));
|
|
175
|
+
plugins.push(vueDevTools());
|
|
176
|
+
}
|
|
177
|
+
if (autoImport) {
|
|
178
|
+
const AutoImport = await dynamicImport(import("unplugin-auto-import/vite"));
|
|
179
|
+
const ElementPlusResolverModule = await dynamicImport(import("unplugin-vue-components/resolvers"));
|
|
180
|
+
const { ElementPlusResolver } = ElementPlusResolverModule;
|
|
181
|
+
plugins.push(AutoImport(
|
|
182
|
+
deepMerge(
|
|
183
|
+
{
|
|
184
|
+
imports: ["vue"],
|
|
185
|
+
resolvers: isVueOrVitepress ? [ElementPlusResolver()] : [],
|
|
186
|
+
dts: path.resolve(rootPath, "./typings/auto-imports.d.ts")
|
|
187
|
+
},
|
|
188
|
+
autoImport
|
|
189
|
+
)
|
|
190
|
+
));
|
|
191
|
+
}
|
|
192
|
+
if (autoComponent) {
|
|
193
|
+
const Components = await dynamicImport(import("unplugin-vue-components/vite"));
|
|
194
|
+
const ElementPlusResolverModule = await dynamicImport(import("unplugin-vue-components/resolvers"));
|
|
195
|
+
const { ElementPlusResolver } = ElementPlusResolverModule;
|
|
196
|
+
plugins.push(Components(
|
|
197
|
+
deepMerge(
|
|
198
|
+
{
|
|
199
|
+
resolvers: isVueOrVitepress ? [ElementPlusResolver()] : [],
|
|
200
|
+
globs: [],
|
|
201
|
+
dts: path.resolve(rootPath, "./typings/components.d.ts")
|
|
202
|
+
},
|
|
203
|
+
autoComponent
|
|
204
|
+
)
|
|
205
|
+
));
|
|
206
|
+
}
|
|
207
|
+
if (compression) {
|
|
208
|
+
const viteCompression = await dynamicImport(import("vite-plugin-compression"));
|
|
209
|
+
const compressionPlugin = viteCompression;
|
|
210
|
+
plugins.push(compressionPlugin(
|
|
211
|
+
deepMerge(
|
|
212
|
+
{
|
|
213
|
+
algorithm: "brotliCompress",
|
|
214
|
+
verbose: true,
|
|
215
|
+
disable: false,
|
|
216
|
+
ext: ".gz",
|
|
217
|
+
threshold: 10240,
|
|
218
|
+
deleteOriginFile: false
|
|
219
|
+
},
|
|
220
|
+
compression
|
|
221
|
+
)
|
|
222
|
+
));
|
|
223
|
+
}
|
|
224
|
+
if (imagemin) {
|
|
225
|
+
const viteImagemin = await dynamicImport(import("vite-plugin-imagemin"));
|
|
226
|
+
const imageminPlugin = viteImagemin;
|
|
227
|
+
plugins.push(imageminPlugin(
|
|
228
|
+
deepMerge(
|
|
229
|
+
{
|
|
230
|
+
gifsicle: { optimizationLevel: 7, interlaced: false },
|
|
231
|
+
optipng: { optimizationLevel: 7 },
|
|
232
|
+
mozjpeg: { quality: 20 },
|
|
233
|
+
pngquant: { quality: [0.8, 0.9], speed: 4 },
|
|
234
|
+
svgo: {
|
|
235
|
+
plugins: [{ name: "removeViewBox" }, { name: "removeEmptyAttrs", active: false }]
|
|
236
|
+
}
|
|
237
|
+
},
|
|
238
|
+
imagemin
|
|
239
|
+
)
|
|
240
|
+
));
|
|
241
|
+
}
|
|
242
|
+
if (cdn) {
|
|
243
|
+
const importToCDN = await dynamicImport(import("vite-plugin-cdn-import"));
|
|
244
|
+
const { modules: modules2 } = await dynamicImport(Promise.resolve().then(() => index$1));
|
|
245
|
+
plugins.push(importToCDN(
|
|
246
|
+
deepMerge(
|
|
247
|
+
{
|
|
248
|
+
enableInDevMode: false,
|
|
249
|
+
prodUrl: "/{name}@{version}{path}",
|
|
250
|
+
modules: modules2,
|
|
251
|
+
generateScriptTag: (_name, scriptUrl) => {
|
|
252
|
+
const esmArr = ["esm", ".mjs"];
|
|
253
|
+
const isESM = esmArr.some((item) => scriptUrl.includes(item));
|
|
254
|
+
if (isESM) {
|
|
255
|
+
return {
|
|
256
|
+
attrs: {
|
|
257
|
+
src: scriptUrl,
|
|
258
|
+
type: "module",
|
|
259
|
+
crossorigin: "anonymous"
|
|
260
|
+
},
|
|
261
|
+
injectTo: "head"
|
|
262
|
+
};
|
|
263
|
+
} else {
|
|
264
|
+
return {
|
|
265
|
+
attrs: {
|
|
266
|
+
src: scriptUrl,
|
|
267
|
+
crossorigin: "anonymous"
|
|
268
|
+
},
|
|
269
|
+
injectTo: "head"
|
|
270
|
+
};
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
},
|
|
274
|
+
cdn
|
|
275
|
+
)
|
|
276
|
+
));
|
|
277
|
+
}
|
|
278
|
+
if (visualizer) {
|
|
279
|
+
const visualizerModule = await dynamicImport(import("rollup-plugin-visualizer"));
|
|
280
|
+
const { visualizer: visualizerPlugin } = visualizerModule;
|
|
281
|
+
plugins.push(visualizerPlugin(
|
|
282
|
+
deepMerge(
|
|
283
|
+
{
|
|
284
|
+
open: true
|
|
285
|
+
},
|
|
286
|
+
visualizer
|
|
287
|
+
)
|
|
288
|
+
));
|
|
289
|
+
}
|
|
290
|
+
if (pwa) {
|
|
291
|
+
const pwaModule = await dynamicImport(import("vite-plugin-pwa"));
|
|
292
|
+
const { VitePWA } = pwaModule;
|
|
293
|
+
plugins.push(VitePWA(
|
|
294
|
+
deepMerge(
|
|
295
|
+
{
|
|
296
|
+
strategies: "generateSW",
|
|
297
|
+
registerType: "autoUpdate",
|
|
298
|
+
devOptions: {
|
|
299
|
+
enabled: true
|
|
300
|
+
},
|
|
301
|
+
includeAssets: ["favicon.ico", "apple-touch-icon.png", "mask-icon.svg"],
|
|
302
|
+
manifest: {
|
|
303
|
+
id: appCode ? `/${appCode}/` : "/",
|
|
304
|
+
start_url: appCode ? `/${appCode}/` : "/",
|
|
305
|
+
name: appTitle || "Vue 应用",
|
|
306
|
+
short_name: appTitle || "应用",
|
|
307
|
+
description: "渐进式 Web 应用",
|
|
308
|
+
display: "standalone",
|
|
309
|
+
background_color: "#ffffff",
|
|
310
|
+
theme_color: "#BA42BF"
|
|
311
|
+
}
|
|
312
|
+
},
|
|
313
|
+
pwa
|
|
314
|
+
)
|
|
315
|
+
));
|
|
316
|
+
}
|
|
317
|
+
if (isDev && codeInspector) {
|
|
318
|
+
const codeInspectorModule = await dynamicImport(import("code-inspector-plugin"));
|
|
319
|
+
const { codeInspectorPlugin } = codeInspectorModule;
|
|
320
|
+
plugins.push(codeInspectorPlugin(
|
|
321
|
+
deepMerge(
|
|
322
|
+
{
|
|
323
|
+
bundler: "vite",
|
|
324
|
+
showSwitch: true
|
|
325
|
+
},
|
|
326
|
+
codeInspector
|
|
327
|
+
)
|
|
328
|
+
));
|
|
329
|
+
}
|
|
330
|
+
if (qiankun) {
|
|
331
|
+
const qiankunPlugin = await dynamicImport(import("vite-plugin-qiankun"));
|
|
332
|
+
const qiankunPluginFn = qiankunPlugin;
|
|
333
|
+
plugins.push(qiankunPluginFn(envSystemCode || "el", { useDevMode: qiankunDevMode }));
|
|
334
|
+
if (appCode) {
|
|
335
|
+
plugins.push(addScopedAndReplacePrefixPlugin({
|
|
336
|
+
prefixScoped: `div[data-qiankun='${envSystemCode}']`,
|
|
337
|
+
oldPrefix: "el",
|
|
338
|
+
newPrefix: appCode,
|
|
339
|
+
useDevMode: qiankunDevMode
|
|
340
|
+
}));
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
if (autoRoutes) {
|
|
344
|
+
const AutoRoutesPlugin = await dynamicImport(Promise.resolve().then(() => index));
|
|
345
|
+
plugins.push(AutoRoutesPlugin(
|
|
346
|
+
deepMerge(
|
|
347
|
+
{
|
|
348
|
+
root: rootPath,
|
|
349
|
+
routeConfig: {
|
|
350
|
+
views: ["/src/views/**/index.vue", "!/src/views/**/components/*"],
|
|
351
|
+
examples: "/src/examples/**/index.vue",
|
|
352
|
+
componentExamples: {
|
|
353
|
+
glob: ["/src/components/**/Example.vue", "!/src/components/**/components/*"],
|
|
354
|
+
baseRoute: "组件示例"
|
|
355
|
+
}
|
|
356
|
+
},
|
|
357
|
+
dts: path.resolve(rootPath, "./typings/auto-routes.d.ts")
|
|
358
|
+
},
|
|
359
|
+
autoRoutes
|
|
360
|
+
)
|
|
361
|
+
));
|
|
362
|
+
}
|
|
363
|
+
const defaultConfig = {
|
|
364
|
+
plugins,
|
|
365
|
+
esbuild: {
|
|
366
|
+
pure: !isDev && dropConsole ? ["console.log", "console.info", "console.debug"] : []
|
|
367
|
+
},
|
|
368
|
+
build: {
|
|
369
|
+
sourcemap: isDev,
|
|
370
|
+
outDir: appCode || "dist",
|
|
371
|
+
cssCodeSplit: true,
|
|
372
|
+
chunkSizeWarningLimit: 1500,
|
|
373
|
+
minify: "esbuild",
|
|
374
|
+
rollupOptions: {
|
|
375
|
+
external: [],
|
|
376
|
+
output: {
|
|
377
|
+
globals: {},
|
|
378
|
+
chunkFileNames: "static/js/[name]-[hash].js",
|
|
379
|
+
entryFileNames: "static/js/[name]-[hash].js",
|
|
380
|
+
assetFileNames: "static/[ext]/[name]-[hash].[ext]",
|
|
381
|
+
manualChunks: (id) => {
|
|
382
|
+
if (id.includes("node_modules")) {
|
|
383
|
+
if (id.includes("lodash-es")) {
|
|
384
|
+
return "lodash-vendor";
|
|
385
|
+
}
|
|
386
|
+
if (id.includes("element-plus")) {
|
|
387
|
+
return "el-vendor";
|
|
388
|
+
}
|
|
389
|
+
if (id.includes("@vue") || id.includes("vue")) {
|
|
390
|
+
return "vue-vendor";
|
|
391
|
+
}
|
|
392
|
+
return "vendor";
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
}
|
|
397
|
+
},
|
|
398
|
+
define: {
|
|
399
|
+
__SYSTEM_CODE__: JSON.stringify(envSystemCode),
|
|
400
|
+
process: deepMerge({
|
|
401
|
+
env: {
|
|
402
|
+
VUE_APP_VXE_ENV: "production"
|
|
403
|
+
}
|
|
404
|
+
}, process)
|
|
405
|
+
},
|
|
406
|
+
css: {
|
|
407
|
+
preprocessorOptions: {
|
|
408
|
+
scss: {
|
|
409
|
+
api: "modern-compiler"
|
|
410
|
+
}
|
|
411
|
+
},
|
|
412
|
+
postcss: {
|
|
413
|
+
plugins: [tailwindcss(), autoprefixer()]
|
|
414
|
+
},
|
|
415
|
+
devSourcemap: isDev
|
|
416
|
+
},
|
|
417
|
+
resolve: {
|
|
418
|
+
extensions: [".js", ".jsx", ".ts", ".tsx", ".vue"],
|
|
419
|
+
alias: {
|
|
420
|
+
"@": path.resolve(rootPath, "./src")
|
|
421
|
+
}
|
|
422
|
+
},
|
|
423
|
+
server: {
|
|
424
|
+
host: "0.0.0.0",
|
|
425
|
+
port,
|
|
426
|
+
open,
|
|
427
|
+
cors: true,
|
|
428
|
+
proxy: {}
|
|
429
|
+
}
|
|
430
|
+
};
|
|
431
|
+
if (!vitepress && appCode) {
|
|
432
|
+
defaultConfig.base = `/${appCode}`;
|
|
433
|
+
}
|
|
434
|
+
const viteConfig = typeof config.viteConfig === "function" ? config.viteConfig(params) : config.viteConfig;
|
|
435
|
+
const viteConfigPluginNames = ((viteConfig == null ? void 0 : viteConfig.plugins) || []).map((i) => {
|
|
436
|
+
const plugin = Array.isArray(i) ? i[0] : i;
|
|
437
|
+
return plugin == null ? void 0 : plugin.name;
|
|
438
|
+
}).filter((name) => Boolean(name));
|
|
439
|
+
const defaultPluginNamesMap = (defaultConfig.plugins || []).reduce((nameMap, i) => {
|
|
440
|
+
const plugin = Array.isArray(i) ? i[0] : i;
|
|
441
|
+
const name = plugin == null ? void 0 : plugin.name;
|
|
442
|
+
if (name) {
|
|
443
|
+
nameMap[name] = i;
|
|
444
|
+
}
|
|
445
|
+
return nameMap;
|
|
446
|
+
}, {});
|
|
447
|
+
const uniquePlugin = [];
|
|
448
|
+
Object.keys(defaultPluginNamesMap).forEach((name) => {
|
|
449
|
+
if (!viteConfigPluginNames.includes(name)) {
|
|
450
|
+
uniquePlugin.push(defaultPluginNamesMap[name]);
|
|
451
|
+
}
|
|
452
|
+
});
|
|
453
|
+
defaultConfig.plugins = uniquePlugin;
|
|
454
|
+
return mergeConfig(defaultConfig, viteConfig || {});
|
|
455
|
+
}
|
|
456
|
+
function createViteConfig(Config) {
|
|
457
|
+
return defineConfig(async (params) => await getViteConfig(Config, params));
|
|
458
|
+
}
|
|
459
|
+
function wrapperEnv(env) {
|
|
460
|
+
const result = {};
|
|
461
|
+
for (const key in env) {
|
|
462
|
+
if (Object.prototype.hasOwnProperty.call(env, key)) {
|
|
463
|
+
const value = env[key].trim();
|
|
464
|
+
if (value === "true" || value === "false") {
|
|
465
|
+
result[key] = value === "true";
|
|
466
|
+
} else if (!Number.isNaN(Number(value))) {
|
|
467
|
+
result[key] = Number(value);
|
|
468
|
+
} else if (value === "") {
|
|
469
|
+
result[key] = null;
|
|
470
|
+
} else {
|
|
471
|
+
result[key] = value;
|
|
472
|
+
}
|
|
473
|
+
}
|
|
474
|
+
}
|
|
475
|
+
return result;
|
|
476
|
+
}
|
|
477
|
+
function getCamelCase(str) {
|
|
478
|
+
return str.replace(/[-_]+/g, " ").replace(/(?:^|\s)\w/g, (match) => match.toUpperCase()).replace(/\s+/g, "");
|
|
479
|
+
}
|
|
480
|
+
function getCdnModules(modules2) {
|
|
481
|
+
function getPath(str) {
|
|
482
|
+
if (!str)
|
|
483
|
+
return "";
|
|
484
|
+
return str.startsWith("/") ? str : `/${str}`;
|
|
485
|
+
}
|
|
486
|
+
return modules2.map((item) => {
|
|
487
|
+
if (typeof item === "string") {
|
|
488
|
+
return {
|
|
489
|
+
name: item,
|
|
490
|
+
var: getCamelCase(item),
|
|
491
|
+
path: ""
|
|
492
|
+
};
|
|
493
|
+
} else {
|
|
494
|
+
return item;
|
|
495
|
+
}
|
|
496
|
+
}).map((item) => {
|
|
497
|
+
return {
|
|
498
|
+
name: item.name,
|
|
499
|
+
var: item.var || getCamelCase(item.name),
|
|
500
|
+
path: getPath(item.path),
|
|
501
|
+
css: getPath(item.css)
|
|
502
|
+
};
|
|
503
|
+
});
|
|
504
|
+
}
|
|
505
|
+
const modules = getCdnModules([]);
|
|
506
|
+
const index$1 = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
|
|
507
|
+
__proto__: null,
|
|
508
|
+
modules
|
|
509
|
+
}, Symbol.toStringTag, { value: "Module" }));
|
|
510
|
+
function resolvePatternsToAbsolute(patterns, rootDir) {
|
|
511
|
+
return patterns.map((p) => normalizePath(path.isAbsolute(p) ? p : path.resolve(rootDir, p)));
|
|
512
|
+
}
|
|
513
|
+
function extractStaticPrefixFromGlob(absPattern) {
|
|
514
|
+
const special = ["*", "?", "{", "}", "!", "(", ")", "[", "]"];
|
|
515
|
+
const idx = absPattern.split("").findIndex((ch) => special.includes(ch));
|
|
516
|
+
return idx === -1 ? absPattern : absPattern.slice(0, idx);
|
|
517
|
+
}
|
|
518
|
+
function createMatcher(prefixes) {
|
|
519
|
+
return (absFile) => prefixes.some((prefix) => absFile.startsWith(prefix));
|
|
520
|
+
}
|
|
521
|
+
class VirtualModuleState {
|
|
522
|
+
constructor() {
|
|
523
|
+
__publicField(this, "isServerClosing", false);
|
|
524
|
+
__publicField(this, "isInitialized", false);
|
|
525
|
+
__publicField(this, "hasPendingChange", false);
|
|
526
|
+
__publicField(this, "hmrDebouncedInvalidate");
|
|
527
|
+
__publicField(this, "watchChangeDebouncedClear");
|
|
528
|
+
__publicField(this, "watcherDebouncedInvalidate");
|
|
529
|
+
}
|
|
530
|
+
clearAll() {
|
|
531
|
+
var _a, _b, _c;
|
|
532
|
+
(_a = this.hmrDebouncedInvalidate) == null ? void 0 : _a.cancel();
|
|
533
|
+
(_b = this.watchChangeDebouncedClear) == null ? void 0 : _b.cancel();
|
|
534
|
+
(_c = this.watcherDebouncedInvalidate) == null ? void 0 : _c.cancel();
|
|
535
|
+
}
|
|
536
|
+
}
|
|
537
|
+
function invalidateModules(server, virtualModuleId, moduleCache) {
|
|
538
|
+
const ids = Array.from(moduleCache.keys()).filter(
|
|
539
|
+
(k) => k === virtualModuleId || k.startsWith(`${virtualModuleId}/`)
|
|
540
|
+
);
|
|
541
|
+
const mods = [];
|
|
542
|
+
for (const vid of ids) {
|
|
543
|
+
moduleCache.delete(vid);
|
|
544
|
+
const mod = server.moduleGraph.getModuleById(vid);
|
|
545
|
+
if (mod) {
|
|
546
|
+
server.moduleGraph.invalidateModule(mod);
|
|
547
|
+
mods.push(mod);
|
|
548
|
+
}
|
|
549
|
+
}
|
|
550
|
+
return mods;
|
|
551
|
+
}
|
|
552
|
+
function sendHmrUpdate(server, mods) {
|
|
553
|
+
var _a;
|
|
554
|
+
if (mods.length === 0) {
|
|
555
|
+
server == null ? void 0 : server.ws.send({ type: "full-reload" });
|
|
556
|
+
return;
|
|
557
|
+
}
|
|
558
|
+
try {
|
|
559
|
+
for (const mod of mods) {
|
|
560
|
+
try {
|
|
561
|
+
(_a = server.reloadModule) == null ? void 0 : _a.call(server, mod);
|
|
562
|
+
} catch {
|
|
563
|
+
}
|
|
564
|
+
}
|
|
565
|
+
server == null ? void 0 : server.ws.send({
|
|
566
|
+
type: "update",
|
|
567
|
+
updates: mods.map((mod) => ({
|
|
568
|
+
type: "js-update",
|
|
569
|
+
path: mod.url,
|
|
570
|
+
acceptedPath: mod.url,
|
|
571
|
+
timestamp: Date.now()
|
|
572
|
+
}))
|
|
573
|
+
});
|
|
574
|
+
} catch {
|
|
575
|
+
server == null ? void 0 : server.ws.send({ type: "full-reload" });
|
|
576
|
+
}
|
|
577
|
+
}
|
|
578
|
+
function setupFileWatcher(options) {
|
|
579
|
+
var _a;
|
|
580
|
+
const { server, watchPatterns, isWatchedPath, onInvalidate, debounceMs, onInitialized } = options;
|
|
581
|
+
let watcherReady = false;
|
|
582
|
+
let netReady = false;
|
|
583
|
+
let enabled = false;
|
|
584
|
+
const debouncedInvalidate = debounce(onInvalidate, debounceMs, {
|
|
585
|
+
trailing: true,
|
|
586
|
+
leading: false
|
|
587
|
+
});
|
|
588
|
+
const maybeEnable = () => {
|
|
589
|
+
var _a2;
|
|
590
|
+
if (enabled || !watcherReady || !netReady)
|
|
591
|
+
return;
|
|
592
|
+
enabled = true;
|
|
593
|
+
try {
|
|
594
|
+
if (watchPatterns.length > 0)
|
|
595
|
+
server.watcher.add(watchPatterns);
|
|
596
|
+
} catch {
|
|
597
|
+
}
|
|
598
|
+
const handleFileChange = (eventName, file) => {
|
|
599
|
+
if (eventName === "change" || eventName === "unlink" || eventName === "unlinkDir" || watcherReady && (eventName === "add" || eventName === "addDir")) {
|
|
600
|
+
const abs = normalizePath(
|
|
601
|
+
path.isAbsolute(file) ? file : path.resolve(server.config.root, file)
|
|
602
|
+
);
|
|
603
|
+
if (isWatchedPath(abs))
|
|
604
|
+
debouncedInvalidate();
|
|
605
|
+
}
|
|
606
|
+
};
|
|
607
|
+
server.watcher.on("all", handleFileChange);
|
|
608
|
+
const cleanup = () => {
|
|
609
|
+
debouncedInvalidate.cancel();
|
|
610
|
+
server.watcher.off("all", handleFileChange);
|
|
611
|
+
};
|
|
612
|
+
server.watcher.once("close", cleanup);
|
|
613
|
+
(_a2 = server.httpServer) == null ? void 0 : _a2.once("close", cleanup);
|
|
614
|
+
onInitialized == null ? void 0 : onInitialized();
|
|
615
|
+
};
|
|
616
|
+
server.watcher.once("ready", () => {
|
|
617
|
+
watcherReady = true;
|
|
618
|
+
maybeEnable();
|
|
619
|
+
});
|
|
620
|
+
(_a = server.httpServer) == null ? void 0 : _a.once("listening", () => {
|
|
621
|
+
netReady = true;
|
|
622
|
+
maybeEnable();
|
|
623
|
+
});
|
|
624
|
+
const wsAny = server.ws;
|
|
625
|
+
if (typeof (wsAny == null ? void 0 : wsAny.on) === "function") {
|
|
626
|
+
const connectionHandler = () => {
|
|
627
|
+
var _a2, _b;
|
|
628
|
+
netReady = true;
|
|
629
|
+
maybeEnable();
|
|
630
|
+
try {
|
|
631
|
+
(_a2 = wsAny.off) == null ? void 0 : _a2.call(wsAny, "connection", connectionHandler);
|
|
632
|
+
} catch {
|
|
633
|
+
}
|
|
634
|
+
try {
|
|
635
|
+
(_b = wsAny.removeListener) == null ? void 0 : _b.call(wsAny, "connection", connectionHandler);
|
|
636
|
+
} catch {
|
|
637
|
+
}
|
|
638
|
+
};
|
|
639
|
+
wsAny.on("connection", connectionHandler);
|
|
640
|
+
}
|
|
641
|
+
}
|
|
642
|
+
function writeDtsFile(config, rootDir, dts, generateDts) {
|
|
643
|
+
if (dts === false || !generateDts)
|
|
644
|
+
return;
|
|
645
|
+
try {
|
|
646
|
+
const dtsPath = typeof dts === "string" ? path.isAbsolute(dts) ? dts : path.resolve(rootDir, dts) : path.resolve(rootDir, "./src/typings/virtual-module.d.ts");
|
|
647
|
+
const content = generateDts({ config });
|
|
648
|
+
const normalized = normalizePath(dtsPath);
|
|
649
|
+
const dir = path.dirname(normalized);
|
|
650
|
+
fs.mkdirSync(dir, { recursive: true });
|
|
651
|
+
fs.writeFileSync(normalized, content, "utf-8");
|
|
652
|
+
} catch {
|
|
653
|
+
}
|
|
654
|
+
}
|
|
655
|
+
function callUserHook(hook, context, ...args) {
|
|
656
|
+
if (!hook)
|
|
657
|
+
return void 0;
|
|
658
|
+
if (typeof hook === "function") {
|
|
659
|
+
return hook.call(context, ...args);
|
|
660
|
+
} else if (hook.handler) {
|
|
661
|
+
return hook.handler.call(context, ...args);
|
|
662
|
+
}
|
|
663
|
+
return void 0;
|
|
664
|
+
}
|
|
665
|
+
function createVirtualPlugin(userConfig) {
|
|
666
|
+
const {
|
|
667
|
+
virtualModuleId,
|
|
668
|
+
dts,
|
|
669
|
+
root,
|
|
670
|
+
watch,
|
|
671
|
+
debounceMs = 2e3,
|
|
672
|
+
...restConfig
|
|
673
|
+
} = userConfig;
|
|
674
|
+
const VIRTUAL_MODULE_ID = virtualModuleId;
|
|
675
|
+
const {
|
|
676
|
+
resolveId,
|
|
677
|
+
load,
|
|
678
|
+
configResolved,
|
|
679
|
+
configureServer,
|
|
680
|
+
handleHotUpdate,
|
|
681
|
+
watchChange,
|
|
682
|
+
generateModule,
|
|
683
|
+
generateDts,
|
|
684
|
+
...restHooks
|
|
685
|
+
} = restConfig;
|
|
686
|
+
const moduleCache = /* @__PURE__ */ new Map();
|
|
687
|
+
const state = new VirtualModuleState();
|
|
688
|
+
let resolvedViteConfig;
|
|
689
|
+
let watchPatterns = [];
|
|
690
|
+
let isWatchedPath = () => true;
|
|
691
|
+
const performInvalidate = (server) => {
|
|
692
|
+
if (state.isServerClosing)
|
|
693
|
+
return;
|
|
694
|
+
const mods = invalidateModules(server, VIRTUAL_MODULE_ID, moduleCache);
|
|
695
|
+
sendHmrUpdate(server, mods);
|
|
696
|
+
};
|
|
697
|
+
const handleFileChange = (server) => {
|
|
698
|
+
if (!state.isInitialized) {
|
|
699
|
+
state.hasPendingChange = true;
|
|
700
|
+
return;
|
|
701
|
+
}
|
|
702
|
+
performInvalidate(server);
|
|
703
|
+
};
|
|
704
|
+
return {
|
|
705
|
+
resolveId(...args) {
|
|
706
|
+
const [id] = args;
|
|
707
|
+
if (id === VIRTUAL_MODULE_ID || id.startsWith(`${VIRTUAL_MODULE_ID}/`))
|
|
708
|
+
return id;
|
|
709
|
+
return callUserHook(resolveId, this, ...args);
|
|
710
|
+
},
|
|
711
|
+
configResolved(...args) {
|
|
712
|
+
const [config] = args;
|
|
713
|
+
resolvedViteConfig = config;
|
|
714
|
+
const rootDir = root || config.root;
|
|
715
|
+
const patterns = Array.isArray(watch) ? watch : watch ? [watch] : [];
|
|
716
|
+
watchPatterns = resolvePatternsToAbsolute(patterns, rootDir);
|
|
717
|
+
const watchPrefixes = watchPatterns.map(extractStaticPrefixFromGlob);
|
|
718
|
+
isWatchedPath = watchPrefixes.length === 0 ? () => true : createMatcher(watchPrefixes);
|
|
719
|
+
writeDtsFile(config, rootDir, dts, generateDts);
|
|
720
|
+
callUserHook(configResolved, this, ...args);
|
|
721
|
+
},
|
|
722
|
+
configureServer(...args) {
|
|
723
|
+
var _a;
|
|
724
|
+
const [server] = args;
|
|
725
|
+
const handleSignal = () => {
|
|
726
|
+
var _a2;
|
|
727
|
+
if (state.isServerClosing)
|
|
728
|
+
return;
|
|
729
|
+
state.isServerClosing = true;
|
|
730
|
+
Promise.resolve((_a2 = server == null ? void 0 : server.close) == null ? void 0 : _a2.call(server)).finally(() => {
|
|
731
|
+
try {
|
|
732
|
+
process$1.exit(0);
|
|
733
|
+
} catch {
|
|
734
|
+
}
|
|
735
|
+
});
|
|
736
|
+
};
|
|
737
|
+
process$1.once("SIGINT", handleSignal);
|
|
738
|
+
process$1.once("SIGTERM", handleSignal);
|
|
739
|
+
(_a = server.httpServer) == null ? void 0 : _a.once("close", () => {
|
|
740
|
+
state.isServerClosing = true;
|
|
741
|
+
state.clearAll();
|
|
742
|
+
});
|
|
743
|
+
setupFileWatcher({
|
|
744
|
+
server,
|
|
745
|
+
watchPatterns,
|
|
746
|
+
isWatchedPath,
|
|
747
|
+
onInvalidate: () => handleFileChange(server),
|
|
748
|
+
debounceMs,
|
|
749
|
+
onInitialized: () => {
|
|
750
|
+
state.isInitialized = true;
|
|
751
|
+
if (state.hasPendingChange) {
|
|
752
|
+
state.hasPendingChange = false;
|
|
753
|
+
setTimeout(() => {
|
|
754
|
+
if (!state.isServerClosing)
|
|
755
|
+
performInvalidate(server);
|
|
756
|
+
}, 0);
|
|
757
|
+
}
|
|
758
|
+
}
|
|
759
|
+
});
|
|
760
|
+
callUserHook(configureServer, this, ...args);
|
|
761
|
+
},
|
|
762
|
+
handleHotUpdate(...args) {
|
|
763
|
+
const [ctx] = args;
|
|
764
|
+
const { server } = ctx;
|
|
765
|
+
const rootDir = root || (resolvedViteConfig == null ? void 0 : resolvedViteConfig.root) || server.config.root;
|
|
766
|
+
const abs = normalizePath(
|
|
767
|
+
path.isAbsolute(ctx.file) ? ctx.file : path.resolve(rootDir, ctx.file)
|
|
768
|
+
);
|
|
769
|
+
if (!isWatchedPath(abs) || state.isServerClosing)
|
|
770
|
+
return;
|
|
771
|
+
if (!state.isInitialized) {
|
|
772
|
+
state.hasPendingChange = true;
|
|
773
|
+
return [];
|
|
774
|
+
}
|
|
775
|
+
if (!state.hmrDebouncedInvalidate) {
|
|
776
|
+
state.hmrDebouncedInvalidate = debounce(
|
|
777
|
+
() => {
|
|
778
|
+
if (state.isServerClosing)
|
|
779
|
+
return;
|
|
780
|
+
performInvalidate(server);
|
|
781
|
+
},
|
|
782
|
+
debounceMs,
|
|
783
|
+
{ trailing: true, leading: false }
|
|
784
|
+
);
|
|
785
|
+
}
|
|
786
|
+
state.hmrDebouncedInvalidate();
|
|
787
|
+
return callUserHook(handleHotUpdate, this, ...args) || [];
|
|
788
|
+
},
|
|
789
|
+
watchChange(...args) {
|
|
790
|
+
try {
|
|
791
|
+
const [id] = args;
|
|
792
|
+
const rootDir = root || (resolvedViteConfig == null ? void 0 : resolvedViteConfig.root) || process$1.cwd();
|
|
793
|
+
const absId = normalizePath(path.isAbsolute(id) ? id : path.resolve(rootDir, id));
|
|
794
|
+
if (!isWatchedPath(absId) || state.isServerClosing) {
|
|
795
|
+
return;
|
|
796
|
+
}
|
|
797
|
+
if (!state.isInitialized) {
|
|
798
|
+
state.hasPendingChange = true;
|
|
799
|
+
return;
|
|
800
|
+
}
|
|
801
|
+
if (!state.watchChangeDebouncedClear) {
|
|
802
|
+
state.watchChangeDebouncedClear = debounce(
|
|
803
|
+
() => {
|
|
804
|
+
if (state.isServerClosing)
|
|
805
|
+
return;
|
|
806
|
+
for (const k of Array.from(moduleCache.keys())) {
|
|
807
|
+
if (k === VIRTUAL_MODULE_ID || k.startsWith(`${VIRTUAL_MODULE_ID}/`))
|
|
808
|
+
moduleCache.delete(k);
|
|
809
|
+
}
|
|
810
|
+
},
|
|
811
|
+
debounceMs,
|
|
812
|
+
{ trailing: true, leading: false }
|
|
813
|
+
);
|
|
814
|
+
}
|
|
815
|
+
state.watchChangeDebouncedClear();
|
|
816
|
+
return callUserHook(watchChange, this, ...args);
|
|
817
|
+
} catch {
|
|
818
|
+
}
|
|
819
|
+
},
|
|
820
|
+
async load(...args) {
|
|
821
|
+
const [id] = args;
|
|
822
|
+
if (id === VIRTUAL_MODULE_ID || id.startsWith(`${VIRTUAL_MODULE_ID}/`)) {
|
|
823
|
+
const code = getType(generateModule, "asyncfunction") ? await generateModule({ id, config: resolvedViteConfig }) : generateModule({ id, config: resolvedViteConfig });
|
|
824
|
+
moduleCache.set(id, code);
|
|
825
|
+
return code;
|
|
826
|
+
}
|
|
827
|
+
return await callUserHook(load, this, ...args);
|
|
828
|
+
},
|
|
829
|
+
...restHooks
|
|
830
|
+
};
|
|
831
|
+
}
|
|
832
|
+
function findParentRouteHandle(modules2, parentPath) {
|
|
833
|
+
for (const route of modules2) {
|
|
834
|
+
if (route.path === parentPath) {
|
|
835
|
+
return route;
|
|
836
|
+
}
|
|
837
|
+
if (route.children) {
|
|
838
|
+
const found = findParentRouteHandle(route.children, parentPath);
|
|
839
|
+
if (found)
|
|
840
|
+
return found;
|
|
841
|
+
}
|
|
842
|
+
}
|
|
843
|
+
return void 0;
|
|
844
|
+
}
|
|
845
|
+
function generateRoutes(files, prefix = "", baseRoute, eager = false) {
|
|
846
|
+
const newBaseRoute = typeof baseRoute === "string" ? { name: baseRoute } : baseRoute;
|
|
847
|
+
const modules2 = newBaseRoute ? [newBaseRoute] : [];
|
|
848
|
+
return Object.keys(files).sort((a, b) => {
|
|
849
|
+
const aLength = a.split("/").length;
|
|
850
|
+
const bLength = b.split("/").length;
|
|
851
|
+
return bLength > aLength ? -1 : 1;
|
|
852
|
+
}).reduce((modules22 = [], modulePath) => {
|
|
853
|
+
const componentLoader = files[modulePath];
|
|
854
|
+
if (!componentLoader || modulePath === "install")
|
|
855
|
+
return modules22;
|
|
856
|
+
const pathArr = modulePath.split("/").filter((item) => item && !item.includes("."));
|
|
857
|
+
if (pathArr.at(-1) === "src") {
|
|
858
|
+
pathArr.pop();
|
|
859
|
+
}
|
|
860
|
+
const componentName = pathArr.at(-1);
|
|
861
|
+
const path2 = `/${pathArr.join("/")}`;
|
|
862
|
+
const parentPath = `/${pathArr.slice(0, -1).join("/")}`;
|
|
863
|
+
let parentRoute;
|
|
864
|
+
if (newBaseRoute) {
|
|
865
|
+
newBaseRoute.children = newBaseRoute.children || [];
|
|
866
|
+
newBaseRoute.name = newBaseRoute.name || prefix;
|
|
867
|
+
newBaseRoute.path = `/${(newBaseRoute.path || parentPath).split("/").filter((item) => item && !item.includes(".")).join("/")}`;
|
|
868
|
+
parentRoute = newBaseRoute;
|
|
869
|
+
} else {
|
|
870
|
+
parentRoute = findParentRouteHandle(modules22, parentPath);
|
|
871
|
+
}
|
|
872
|
+
let component;
|
|
873
|
+
let metaTitle = componentName;
|
|
874
|
+
if (eager) {
|
|
875
|
+
component = componentLoader;
|
|
876
|
+
metaTitle = (component == null ? void 0 : component.name) || componentName;
|
|
877
|
+
} else {
|
|
878
|
+
component = typeof componentLoader === "function" ? componentLoader : componentLoader;
|
|
879
|
+
metaTitle = componentName;
|
|
880
|
+
}
|
|
881
|
+
if (parentRoute) {
|
|
882
|
+
if (!parentRoute.children)
|
|
883
|
+
parentRoute.children = [];
|
|
884
|
+
parentRoute.children.push({
|
|
885
|
+
path: path2,
|
|
886
|
+
name: componentName,
|
|
887
|
+
meta: {
|
|
888
|
+
title: metaTitle
|
|
889
|
+
},
|
|
890
|
+
component
|
|
891
|
+
});
|
|
892
|
+
} else {
|
|
893
|
+
modules22.push({
|
|
894
|
+
path: path2,
|
|
895
|
+
name: componentName,
|
|
896
|
+
meta: {
|
|
897
|
+
title: metaTitle
|
|
898
|
+
},
|
|
899
|
+
component
|
|
900
|
+
});
|
|
901
|
+
}
|
|
902
|
+
return modules22;
|
|
903
|
+
}, modules2);
|
|
904
|
+
}
|
|
905
|
+
function findDefaultRouteHandle(routes) {
|
|
906
|
+
var _a, _b, _c;
|
|
907
|
+
for (const route of routes) {
|
|
908
|
+
if ((_a = route.meta) == null ? void 0 : _a.default) {
|
|
909
|
+
return route.path;
|
|
910
|
+
} else {
|
|
911
|
+
if ((_b = route.children) == null ? void 0 : _b.length) {
|
|
912
|
+
return findDefaultRouteHandle(route.children);
|
|
913
|
+
}
|
|
914
|
+
}
|
|
915
|
+
}
|
|
916
|
+
return (_c = routes == null ? void 0 : routes[0]) == null ? void 0 : _c.path;
|
|
917
|
+
}
|
|
918
|
+
function createAutoRoutesPlugin({ routeConfig, virtualModuleId, dts, root, eager: globalEager }) {
|
|
919
|
+
const VIRTUAL_MODULE_ID = virtualModuleId || "virtual:auto-routes";
|
|
920
|
+
const watchGlobs = Object.values(routeConfig).flatMap((globVal) => {
|
|
921
|
+
const g = globVal.glob || globVal;
|
|
922
|
+
return Array.isArray(g) ? g : [g];
|
|
923
|
+
});
|
|
924
|
+
return createVirtualPlugin(
|
|
925
|
+
{
|
|
926
|
+
name: "vite-plugin-auto-routes",
|
|
927
|
+
virtualModuleId: VIRTUAL_MODULE_ID,
|
|
928
|
+
dts,
|
|
929
|
+
root,
|
|
930
|
+
watch: watchGlobs,
|
|
931
|
+
enforce: "pre",
|
|
932
|
+
generateModule: () => {
|
|
933
|
+
const imports = [];
|
|
934
|
+
const routes = [];
|
|
935
|
+
Object.entries(routeConfig).forEach(([prefix, globVal], index2) => {
|
|
936
|
+
const varName = `files${index2}`;
|
|
937
|
+
const glob = globVal.glob || globVal;
|
|
938
|
+
const eager = globVal.eager ?? globalEager ?? false;
|
|
939
|
+
if (eager) {
|
|
940
|
+
imports.push(
|
|
941
|
+
`const ${varName} = import.meta.glob(${JSON.stringify(glob)}, { eager: true, import: 'default' });
|
|
942
|
+
`
|
|
943
|
+
);
|
|
944
|
+
} else {
|
|
945
|
+
imports.push(
|
|
946
|
+
`const ${varName} = import.meta.glob(${JSON.stringify(glob)});
|
|
947
|
+
`
|
|
948
|
+
);
|
|
949
|
+
}
|
|
950
|
+
const baseRoute = globVal.baseRoute;
|
|
951
|
+
routes.push(`...generateRoutes(${varName}, '${prefix}',${JSON.stringify(baseRoute)}, ${eager})`);
|
|
952
|
+
});
|
|
953
|
+
return `
|
|
954
|
+
${imports.join("\n")}
|
|
955
|
+
const findParentRoute = ${findParentRouteHandle}
|
|
956
|
+
|
|
957
|
+
const generateRoutes = ${generateRoutes};
|
|
958
|
+
|
|
959
|
+
const findDefaultRoute = ${findDefaultRouteHandle};
|
|
960
|
+
|
|
961
|
+
${findParentRouteHandle}
|
|
962
|
+
${findDefaultRouteHandle}
|
|
963
|
+
const routes = [${routes.join(",\n")}];
|
|
964
|
+
|
|
965
|
+
export { routes, findDefaultRoute };
|
|
966
|
+
export default routes;
|
|
967
|
+
`;
|
|
968
|
+
},
|
|
969
|
+
generateDts: () => `// 此文件由ViteConfig自动生成,请勿手动修改
|
|
970
|
+
declare module 'virtual:auto-routes' {
|
|
971
|
+
interface RouteModule {
|
|
972
|
+
path: string
|
|
973
|
+
name: string
|
|
974
|
+
meta?: any
|
|
975
|
+
component: () => Promise<any>
|
|
976
|
+
children?: RouteModule[]
|
|
977
|
+
}
|
|
978
|
+
|
|
979
|
+
const routes: RouteModule[]
|
|
980
|
+
const findDefaultRoute: (routes: any[]) => string
|
|
981
|
+
export { findDefaultRoute, routes }
|
|
982
|
+
export default routes
|
|
983
|
+
}`
|
|
984
|
+
}
|
|
985
|
+
);
|
|
986
|
+
}
|
|
987
|
+
const index = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
|
|
988
|
+
__proto__: null,
|
|
989
|
+
default: createAutoRoutesPlugin
|
|
990
|
+
}, Symbol.toStringTag, { value: "Module" }));
|
|
991
|
+
export {
|
|
992
|
+
createViteConfig as ViteConfig,
|
|
993
|
+
createViteConfig as default,
|
|
994
|
+
getViteConfig,
|
|
995
|
+
wrapperEnv
|
|
996
|
+
};
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import { visualizer } from 'rollup-plugin-visualizer';
|
|
2
|
+
import { Options as unpluginAutoImportOptions } from 'unplugin-auto-import/types';
|
|
3
|
+
import { Options as unpluginVueComponentsOptions } from 'unplugin-vue-components/types';
|
|
4
|
+
import { ConfigEnv, PluginOption, UserConfig } from 'vite';
|
|
5
|
+
import { default as importToCDN } from 'vite-plugin-cdn-import';
|
|
6
|
+
import { UserOptions as PagesOptions } from 'vite-plugin-pages';
|
|
7
|
+
import { Options as VitePWAOptions } from 'vite-plugin-pwa';
|
|
8
|
+
type ViteCompressionModule = typeof import('vite-plugin-compression');
|
|
9
|
+
type ViteImageminModule = typeof import('vite-plugin-imagemin');
|
|
10
|
+
export type CompressionOptions = ViteCompressionModule extends {
|
|
11
|
+
default: infer F;
|
|
12
|
+
} ? F extends (...args: any[]) => any ? Parameters<F>[0] : never : never;
|
|
13
|
+
export type ImageminOptions = ViteImageminModule extends {
|
|
14
|
+
default: infer F;
|
|
15
|
+
} ? F extends (...args: any[]) => any ? Parameters<F>[0] : never : never;
|
|
16
|
+
export type CompressionPlugin = (options?: CompressionOptions) => PluginOption;
|
|
17
|
+
export type ImageminPlugin = (options?: ImageminOptions) => PluginOption;
|
|
18
|
+
export type QiankunPlugin = (name: string, options?: {
|
|
19
|
+
useDevMode?: boolean;
|
|
20
|
+
}) => PluginOption;
|
|
21
|
+
export type CDNOptions = Parameters<typeof importToCDN>[0] & {
|
|
22
|
+
baseUrl?: string;
|
|
23
|
+
enableInDevMode?: boolean;
|
|
24
|
+
};
|
|
25
|
+
export type VisualizerOptions = Parameters<typeof visualizer>[0];
|
|
26
|
+
export interface PluginConfig {
|
|
27
|
+
autoImport?: boolean | unpluginAutoImportOptions;
|
|
28
|
+
autoComponent?: boolean | (unpluginVueComponentsOptions & {
|
|
29
|
+
elementExcludes?: string[];
|
|
30
|
+
globs?: string[];
|
|
31
|
+
});
|
|
32
|
+
compression?: boolean | CompressionOptions;
|
|
33
|
+
imagemin?: boolean | ImageminOptions;
|
|
34
|
+
cdn?: boolean | CDNOptions;
|
|
35
|
+
visualizer?: boolean | VisualizerOptions;
|
|
36
|
+
autoRoutes?: boolean | AutoRoutesConfig;
|
|
37
|
+
pageRoutes?: boolean | PagesOptions;
|
|
38
|
+
devtools?: boolean;
|
|
39
|
+
port?: number;
|
|
40
|
+
open?: boolean;
|
|
41
|
+
qiankunDevMode?: boolean;
|
|
42
|
+
qiankun?: boolean;
|
|
43
|
+
namespace?: string;
|
|
44
|
+
dropConsole?: boolean;
|
|
45
|
+
pwa?: boolean | VitePWAOptions;
|
|
46
|
+
codeInspector?: boolean | Parameters<typeof import('code-inspector-plugin').codeInspectorPlugin>[0];
|
|
47
|
+
vitepress?: boolean;
|
|
48
|
+
vue?: boolean;
|
|
49
|
+
react?: boolean;
|
|
50
|
+
appTitle?: string;
|
|
51
|
+
appCode?: string;
|
|
52
|
+
}
|
|
53
|
+
export interface ModeConfig extends PluginConfig {
|
|
54
|
+
}
|
|
55
|
+
export interface objRouteConfig {
|
|
56
|
+
glob: string | string[];
|
|
57
|
+
baseRoute?: {
|
|
58
|
+
path: string;
|
|
59
|
+
name: string;
|
|
60
|
+
meta?: any;
|
|
61
|
+
children?: any[];
|
|
62
|
+
} | string;
|
|
63
|
+
}
|
|
64
|
+
export interface RouteConfig {
|
|
65
|
+
[prefix: string]: string | string[] | objRouteConfig;
|
|
66
|
+
}
|
|
67
|
+
export interface AutoRoutesConfig {
|
|
68
|
+
routeConfig: RouteConfig;
|
|
69
|
+
virtualModuleId?: string;
|
|
70
|
+
dts?: string | boolean;
|
|
71
|
+
root?: string;
|
|
72
|
+
}
|
|
73
|
+
export interface Config extends PluginConfig {
|
|
74
|
+
rootPath: string;
|
|
75
|
+
mode?: {
|
|
76
|
+
base?: ModeConfig;
|
|
77
|
+
development?: ModeConfig;
|
|
78
|
+
production?: ModeConfig;
|
|
79
|
+
};
|
|
80
|
+
viteConfig?: UserConfig | ((mode: ConfigEnv) => UserConfig);
|
|
81
|
+
}
|
|
82
|
+
export type ViteConfigType = Config | ((mode: ConfigEnv) => Config);
|
|
83
|
+
export type PluginType = PluginOption & {
|
|
84
|
+
name: string;
|
|
85
|
+
};
|
|
86
|
+
export interface PluginMap {
|
|
87
|
+
[key: string]: PluginOption;
|
|
88
|
+
}
|
|
89
|
+
export {};
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { objType } from '../../../_types/index.ts';
|
|
2
|
+
export declare function isDevFn(mode: string): boolean;
|
|
3
|
+
export declare function isProdFn(mode: string): boolean;
|
|
4
|
+
export declare function isReportMode(): boolean;
|
|
5
|
+
export declare function wrapperEnv(env: Record<string, string>): objType;
|
|
6
|
+
export declare function getEnvConfig(match?: string, confFiles?: string[]): {};
|
|
7
|
+
export declare function getRootPath(...dir: string[]): string;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './getEnv.ts';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const modules: any;
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { ConfigEnv } from 'vite';
|
|
2
|
+
import { ViteConfigType } from './_types/index.ts';
|
|
3
|
+
declare function getViteConfig(Config: ViteConfigType, params?: ConfigEnv): Promise<Record<string, any>>;
|
|
4
|
+
declare function createViteConfig(Config: ViteConfigType): import('vite').UserConfigFnPromise;
|
|
5
|
+
export { createViteConfig, getViteConfig, };
|
|
6
|
+
export default createViteConfig;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export default function addScopedAndReplacePrefixPlugin({ prefixScoped, oldPrefix, newPrefix, useDevMode, }: {
|
|
2
|
+
prefixScoped?: string | undefined;
|
|
3
|
+
oldPrefix?: string | undefined;
|
|
4
|
+
newPrefix?: string | undefined;
|
|
5
|
+
useDevMode?: boolean | undefined;
|
|
6
|
+
}): {
|
|
7
|
+
name: string;
|
|
8
|
+
configResolved(config: any): void;
|
|
9
|
+
transform(code?: string, id?: string): string;
|
|
10
|
+
};
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { Plugin } from 'vite';
|
|
2
|
+
export interface QiankunCssInjectOptions {
|
|
3
|
+
target?: string;
|
|
4
|
+
include?: string | string[];
|
|
5
|
+
exclude?: string | string[];
|
|
6
|
+
cssInclude?: string | string[];
|
|
7
|
+
cssExclude?: string | string[];
|
|
8
|
+
appId?: string;
|
|
9
|
+
}
|
|
10
|
+
export default function qiankunCssInject(options?: QiankunCssInjectOptions): Plugin | false;
|
package/package.json
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@moluoxixi/vite-config",
|
|
3
|
+
"version": "0.0.25-beta.1",
|
|
4
|
+
"description": "ViteConfig 组件",
|
|
5
|
+
"sideEffects": [
|
|
6
|
+
"*.css",
|
|
7
|
+
"*.scss"
|
|
8
|
+
],
|
|
9
|
+
"peerDependencies": {
|
|
10
|
+
"vite": "6.2.4",
|
|
11
|
+
"vue": "3.5.19"
|
|
12
|
+
},
|
|
13
|
+
"dependencies": {
|
|
14
|
+
"dotenv": "16.6.1",
|
|
15
|
+
"@tailwindcss/postcss": "4.1.5",
|
|
16
|
+
"@vitejs/plugin-vue": "5.2.4",
|
|
17
|
+
"@vitejs/plugin-vue-jsx": "4.2.0",
|
|
18
|
+
"autoprefixer": "10.4.21",
|
|
19
|
+
"code-inspector-plugin": "1.2.10",
|
|
20
|
+
"rollup-plugin-visualizer": "5.14.0",
|
|
21
|
+
"unplugin-auto-import": "19.3.0",
|
|
22
|
+
"unplugin-vue-components": "28.8.0",
|
|
23
|
+
"vite-plugin-cdn-import": "1.0.1",
|
|
24
|
+
"vite-plugin-compression": "0.5.1",
|
|
25
|
+
"vite-plugin-html": "3.2.2",
|
|
26
|
+
"vite-plugin-imagemin": "0.6.1",
|
|
27
|
+
"vite-plugin-pages": "0.33.1",
|
|
28
|
+
"vite-plugin-pwa": "1.1.0",
|
|
29
|
+
"vite-plugin-qiankun": "1.0.15",
|
|
30
|
+
"vite-plugin-vue-devtools": "7.7.7",
|
|
31
|
+
"lodash-es": "4.17.21",
|
|
32
|
+
"postcss": "8.5.6",
|
|
33
|
+
"postcss-selector-parser": "7.1.0"
|
|
34
|
+
},
|
|
35
|
+
"publishConfig": {
|
|
36
|
+
"access": "public"
|
|
37
|
+
},
|
|
38
|
+
"license": "MIT",
|
|
39
|
+
"module": "es/index.mjs",
|
|
40
|
+
"types": "es/index.d.ts",
|
|
41
|
+
"exports": {
|
|
42
|
+
"./es": {
|
|
43
|
+
"import": {
|
|
44
|
+
"types": "./es/index.d.ts",
|
|
45
|
+
"default": "./es/index.mjs"
|
|
46
|
+
}
|
|
47
|
+
},
|
|
48
|
+
".": {
|
|
49
|
+
"import": {
|
|
50
|
+
"types": "./es/index.d.ts",
|
|
51
|
+
"default": "./es/index.mjs"
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
}
|