@moluoxixi/vite-config 0.0.26 → 0.0.29
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 +1 -1
- package/es/index.mjs +227 -73
- package/es/src/_types/index.d.ts +99 -0
- package/es/src/_utils/getEnv.d.ts +15 -0
- package/lib/index.cjs +1171 -0
- package/lib/index.d.ts +4 -0
- package/lib/src/_types/index.d.ts +188 -0
- package/lib/src/_utils/getEnv.d.ts +22 -0
- package/lib/src/_utils/index.d.ts +1 -0
- package/lib/src/constants/index.d.ts +1 -0
- package/lib/src/index.d.ts +6 -0
- package/lib/src/plugins/addScopedAndReplacePrefix.d.ts +10 -0
- package/lib/src/plugins/vitePluginQiankunStyle.d.ts +10 -0
- package/package.json +18 -6
package/README.md
CHANGED
|
@@ -16,7 +16,7 @@ export default viteConfig(({ mode }) => {
|
|
|
16
16
|
return {
|
|
17
17
|
rootPath: path.resolve(),
|
|
18
18
|
mode: {
|
|
19
|
-
base: {
|
|
19
|
+
base: { VITE_APP_TITLE: viteEnv.VITE_APP_TITLE, VITE_APP_CODE: viteEnv.VITE_APP_CODE, VITE_AUTO_ROUTES: true },
|
|
20
20
|
development: { VITE_DEVTOOLS: true },
|
|
21
21
|
production: {},
|
|
22
22
|
},
|
package/es/index.mjs
CHANGED
|
@@ -46,6 +46,38 @@ function deepMerge(target, source) {
|
|
|
46
46
|
return acc;
|
|
47
47
|
}, { ...target });
|
|
48
48
|
}
|
|
49
|
+
function validateMutuallyExclusive(values, defaultKey) {
|
|
50
|
+
const keys = Object.keys(values);
|
|
51
|
+
const enabledKeys = [];
|
|
52
|
+
for (const key of keys) {
|
|
53
|
+
if (values[key] === true) {
|
|
54
|
+
enabledKeys.push(key);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
if (enabledKeys.length > 1) {
|
|
58
|
+
const keysStr = enabledKeys.join("、");
|
|
59
|
+
const allKeysStr = keys.join("、");
|
|
60
|
+
const selectedKey = enabledKeys.includes(defaultKey) ? defaultKey : enabledKeys[0];
|
|
61
|
+
console.warn(
|
|
62
|
+
`[validateMutuallyExclusive] ${allKeysStr} 只能启用一个,但当前启用了:${keysStr}。已自动选择:${String(selectedKey)}`
|
|
63
|
+
);
|
|
64
|
+
const result2 = {};
|
|
65
|
+
for (const key of keys) {
|
|
66
|
+
result2[key] = key === selectedKey;
|
|
67
|
+
}
|
|
68
|
+
return result2;
|
|
69
|
+
}
|
|
70
|
+
const hasEnabledKey = enabledKeys.length > 0;
|
|
71
|
+
const result = {};
|
|
72
|
+
for (const key of keys) {
|
|
73
|
+
if (!hasEnabledKey && defaultKey && key === defaultKey) {
|
|
74
|
+
result[key] = true;
|
|
75
|
+
} else {
|
|
76
|
+
result[key] = values[key] ?? false;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
return result;
|
|
80
|
+
}
|
|
49
81
|
function changeHtmlClassPrefix(htmlString = "", oldPrefix = "", newPrefix = "") {
|
|
50
82
|
const regex = new RegExp(
|
|
51
83
|
`(class|style)\\s*:\\s*((["']((${oldPrefix}\\b)-).*["'])|((_normalizeClass|_normalizeStyle)\\(.*(${oldPrefix}\\b)-.*\\)))`,
|
|
@@ -128,13 +160,16 @@ async function getViteConfig(Config, params) {
|
|
|
128
160
|
qiankun = config.qiankun,
|
|
129
161
|
namespace = config.namespace,
|
|
130
162
|
dropConsole = config.dropConsole,
|
|
131
|
-
vue = config.vue
|
|
132
|
-
react = config.react,
|
|
133
|
-
vitepress = config.vitepress
|
|
163
|
+
vue: vueRaw = config.vue,
|
|
164
|
+
react: reactRaw = config.react,
|
|
165
|
+
vitepress: vitepressRaw = config.vitepress
|
|
134
166
|
} = viteEnv;
|
|
167
|
+
const { vue, react, vitepress } = validateMutuallyExclusive(
|
|
168
|
+
{ vue: vueRaw, react: reactRaw, vitepress: vitepressRaw },
|
|
169
|
+
"vue"
|
|
170
|
+
);
|
|
135
171
|
const appTitle = config.appTitle;
|
|
136
172
|
const appCode = config.appCode;
|
|
137
|
-
const isOnlyVue = !vitepress && !react && vue;
|
|
138
173
|
const isVueOrVitepress = vue || vitepress;
|
|
139
174
|
const envSystemCode = isDev && !qiankunDevMode ? "el" : namespace ?? appCode;
|
|
140
175
|
const plugins = [
|
|
@@ -146,21 +181,32 @@ async function getViteConfig(Config, params) {
|
|
|
146
181
|
}
|
|
147
182
|
})
|
|
148
183
|
];
|
|
149
|
-
if (
|
|
184
|
+
if (vue) {
|
|
150
185
|
const pluginVue = await dynamicImport(import("@vitejs/plugin-vue"));
|
|
151
186
|
plugins.push(pluginVue());
|
|
152
187
|
}
|
|
188
|
+
if (react) {
|
|
189
|
+
const pluginReact = await dynamicImport(import("@vitejs/plugin-react"));
|
|
190
|
+
plugins.push(pluginReact());
|
|
191
|
+
}
|
|
153
192
|
if (isVueOrVitepress) {
|
|
154
193
|
const vueJsx = await dynamicImport(import("@vitejs/plugin-vue-jsx"));
|
|
155
194
|
plugins.push(vueJsx());
|
|
156
195
|
}
|
|
157
196
|
if (pageRoutes) {
|
|
158
197
|
const Pages = await dynamicImport(import("vite-plugin-pages"));
|
|
198
|
+
const extensions = [];
|
|
199
|
+
if (vue) {
|
|
200
|
+
extensions.push("vue");
|
|
201
|
+
}
|
|
202
|
+
if (react) {
|
|
203
|
+
extensions.push("tsx", "jsx");
|
|
204
|
+
}
|
|
159
205
|
plugins.push(Pages(
|
|
160
206
|
deepMerge(
|
|
161
207
|
{
|
|
162
208
|
dirs: "src/pages",
|
|
163
|
-
extensions
|
|
209
|
+
extensions,
|
|
164
210
|
exclude: [
|
|
165
211
|
"**/components/**",
|
|
166
212
|
"**/__tests__/**"
|
|
@@ -170,11 +216,11 @@ async function getViteConfig(Config, params) {
|
|
|
170
216
|
)
|
|
171
217
|
));
|
|
172
218
|
}
|
|
173
|
-
if (isDev && devtools) {
|
|
219
|
+
if (isDev && devtools && isVueOrVitepress) {
|
|
174
220
|
const vueDevTools = await dynamicImport(import("vite-plugin-vue-devtools"));
|
|
175
221
|
plugins.push(vueDevTools());
|
|
176
222
|
}
|
|
177
|
-
if (autoImport) {
|
|
223
|
+
if (autoImport && isVueOrVitepress) {
|
|
178
224
|
const AutoImport = await dynamicImport(import("unplugin-auto-import/vite"));
|
|
179
225
|
const ElementPlusResolverModule = await dynamicImport(import("unplugin-vue-components/resolvers"));
|
|
180
226
|
const { ElementPlusResolver } = ElementPlusResolverModule;
|
|
@@ -182,21 +228,21 @@ async function getViteConfig(Config, params) {
|
|
|
182
228
|
deepMerge(
|
|
183
229
|
{
|
|
184
230
|
imports: ["vue"],
|
|
185
|
-
resolvers:
|
|
231
|
+
resolvers: [ElementPlusResolver()],
|
|
186
232
|
dts: path.resolve(rootPath, "./typings/auto-imports.d.ts")
|
|
187
233
|
},
|
|
188
234
|
autoImport
|
|
189
235
|
)
|
|
190
236
|
));
|
|
191
237
|
}
|
|
192
|
-
if (autoComponent) {
|
|
238
|
+
if (autoComponent && isVueOrVitepress) {
|
|
193
239
|
const Components = await dynamicImport(import("unplugin-vue-components/vite"));
|
|
194
240
|
const ElementPlusResolverModule = await dynamicImport(import("unplugin-vue-components/resolvers"));
|
|
195
241
|
const { ElementPlusResolver } = ElementPlusResolverModule;
|
|
196
242
|
plugins.push(Components(
|
|
197
243
|
deepMerge(
|
|
198
244
|
{
|
|
199
|
-
resolvers:
|
|
245
|
+
resolvers: [ElementPlusResolver()],
|
|
200
246
|
globs: [],
|
|
201
247
|
dts: path.resolve(rootPath, "./typings/components.d.ts")
|
|
202
248
|
},
|
|
@@ -295,6 +341,7 @@ async function getViteConfig(Config, params) {
|
|
|
295
341
|
{
|
|
296
342
|
strategies: "generateSW",
|
|
297
343
|
registerType: "autoUpdate",
|
|
344
|
+
// 开发模式下也启用
|
|
298
345
|
devOptions: {
|
|
299
346
|
enabled: true
|
|
300
347
|
},
|
|
@@ -302,7 +349,7 @@ async function getViteConfig(Config, params) {
|
|
|
302
349
|
manifest: {
|
|
303
350
|
id: appCode ? `/${appCode}/` : "/",
|
|
304
351
|
start_url: appCode ? `/${appCode}/` : "/",
|
|
305
|
-
name: appTitle || "
|
|
352
|
+
name: appTitle || "应用",
|
|
306
353
|
short_name: appTitle || "应用",
|
|
307
354
|
description: "渐进式 Web 应用",
|
|
308
355
|
display: "standalone",
|
|
@@ -340,7 +387,7 @@ async function getViteConfig(Config, params) {
|
|
|
340
387
|
}));
|
|
341
388
|
}
|
|
342
389
|
}
|
|
343
|
-
if (autoRoutes) {
|
|
390
|
+
if (autoRoutes && isVueOrVitepress) {
|
|
344
391
|
const AutoRoutesPlugin = await dynamicImport(Promise.resolve().then(() => index));
|
|
345
392
|
plugins.push(AutoRoutesPlugin(
|
|
346
393
|
deepMerge(
|
|
@@ -389,6 +436,15 @@ async function getViteConfig(Config, params) {
|
|
|
389
436
|
if (id.includes("@vue") || id.includes("vue")) {
|
|
390
437
|
return "vue-vendor";
|
|
391
438
|
}
|
|
439
|
+
if (id.includes("antd") || id.includes("@ant-design")) {
|
|
440
|
+
return "antd-vendor";
|
|
441
|
+
}
|
|
442
|
+
if (id.includes("react-dom")) {
|
|
443
|
+
return "react-dom-vendor";
|
|
444
|
+
}
|
|
445
|
+
if (id.includes("react")) {
|
|
446
|
+
return "react-vendor";
|
|
447
|
+
}
|
|
392
448
|
return "vendor";
|
|
393
449
|
}
|
|
394
450
|
}
|
|
@@ -520,13 +576,23 @@ function createMatcher(prefixes) {
|
|
|
520
576
|
}
|
|
521
577
|
class VirtualModuleState {
|
|
522
578
|
constructor() {
|
|
579
|
+
/** 标记服务器是否正在关闭,避免关闭阶段再触发无效操作 */
|
|
523
580
|
__publicField(this, "isServerClosing", false);
|
|
581
|
+
/** 标记初始化是否完成,初始化期间的变化会被延迟处理 */
|
|
524
582
|
__publicField(this, "isInitialized", false);
|
|
583
|
+
/** 标记初始化期间是否有文件变化,初始化完成后会处理这些变化 */
|
|
525
584
|
__publicField(this, "hasPendingChange", false);
|
|
585
|
+
/** HMR 热更新的防抖函数,lodash-es debounce 返回的函数有 cancel 方法,可以手动取消 */
|
|
526
586
|
__publicField(this, "hmrDebouncedInvalidate");
|
|
587
|
+
/** watchChange 钩子的防抖函数,用于清理模块缓存 */
|
|
527
588
|
__publicField(this, "watchChangeDebouncedClear");
|
|
589
|
+
/** 文件监听器的防抖函数 */
|
|
528
590
|
__publicField(this, "watcherDebouncedInvalidate");
|
|
529
591
|
}
|
|
592
|
+
/**
|
|
593
|
+
* 清理所有防抖定时器
|
|
594
|
+
* 在服务器关闭时调用,确保不会有待执行的防抖任务
|
|
595
|
+
*/
|
|
530
596
|
clearAll() {
|
|
531
597
|
var _a, _b, _c;
|
|
532
598
|
(_a = this.hmrDebouncedInvalidate) == null ? void 0 : _a.cancel();
|
|
@@ -702,12 +768,23 @@ function createVirtualPlugin(userConfig) {
|
|
|
702
768
|
performInvalidate(server);
|
|
703
769
|
};
|
|
704
770
|
return {
|
|
771
|
+
/**
|
|
772
|
+
* 解析虚拟模块 ID
|
|
773
|
+
* 当 import 语句引用虚拟模块时,Vite 会调用此方法
|
|
774
|
+
* @param args - resolveId 的所有参数
|
|
775
|
+
* @returns 如果匹配虚拟模块 ID,返回该 ID;否则返回 undefined
|
|
776
|
+
*/
|
|
705
777
|
resolveId(...args) {
|
|
706
778
|
const [id] = args;
|
|
707
779
|
if (id === VIRTUAL_MODULE_ID || id.startsWith(`${VIRTUAL_MODULE_ID}/`))
|
|
708
780
|
return id;
|
|
709
781
|
return callUserHook(resolveId, this, ...args);
|
|
710
782
|
},
|
|
783
|
+
/**
|
|
784
|
+
* 配置解析完成钩子
|
|
785
|
+
* 在 Vite 配置解析完成后调用,用于初始化监听路径和生成类型声明文件
|
|
786
|
+
* @param args - configResolved 的所有参数
|
|
787
|
+
*/
|
|
711
788
|
configResolved(...args) {
|
|
712
789
|
const [config] = args;
|
|
713
790
|
resolvedViteConfig = config;
|
|
@@ -719,6 +796,11 @@ function createVirtualPlugin(userConfig) {
|
|
|
719
796
|
writeDtsFile(config, rootDir, dts, generateDts);
|
|
720
797
|
callUserHook(configResolved, this, ...args);
|
|
721
798
|
},
|
|
799
|
+
/**
|
|
800
|
+
* 配置开发服务器钩子
|
|
801
|
+
* 在开发服务器启动时调用,用于设置文件监听、信号处理和清理逻辑
|
|
802
|
+
* @param args - configureServer 的所有参数
|
|
803
|
+
*/
|
|
722
804
|
configureServer(...args) {
|
|
723
805
|
var _a;
|
|
724
806
|
const [server] = args;
|
|
@@ -759,6 +841,16 @@ function createVirtualPlugin(userConfig) {
|
|
|
759
841
|
});
|
|
760
842
|
callUserHook(configureServer, this, ...args);
|
|
761
843
|
},
|
|
844
|
+
/**
|
|
845
|
+
* 处理热更新钩子
|
|
846
|
+
* 当 Vite 检测到文件变化时调用,用于触发虚拟模块的失效和更新
|
|
847
|
+
* @param args - handleHotUpdate 的所有参数
|
|
848
|
+
* @returns 空数组,表示不阻止其他插件的处理
|
|
849
|
+
* @remarks
|
|
850
|
+
* - 使用防抖机制避免频繁触发
|
|
851
|
+
* - 初始化期间的变化会被延迟处理
|
|
852
|
+
* - 只处理匹配监听路径的文件变化
|
|
853
|
+
*/
|
|
762
854
|
handleHotUpdate(...args) {
|
|
763
855
|
const [ctx] = args;
|
|
764
856
|
const { server } = ctx;
|
|
@@ -786,6 +878,16 @@ function createVirtualPlugin(userConfig) {
|
|
|
786
878
|
state.hmrDebouncedInvalidate();
|
|
787
879
|
return callUserHook(handleHotUpdate, this, ...args) || [];
|
|
788
880
|
},
|
|
881
|
+
/**
|
|
882
|
+
* 监听文件变化钩子
|
|
883
|
+
* 当 Vite 的依赖预构建或文件系统检测到变化时调用
|
|
884
|
+
* 主要用于清理模块缓存,让下次加载时重新生成
|
|
885
|
+
* @param args - watchChange 的所有参数
|
|
886
|
+
* @remarks
|
|
887
|
+
* - 与 handleHotUpdate 不同,此钩子主要用于清理缓存,不触发 HMR
|
|
888
|
+
* - 使用防抖机制避免频繁清理
|
|
889
|
+
* - 初始化期间的变化会被延迟处理
|
|
890
|
+
*/
|
|
789
891
|
watchChange(...args) {
|
|
790
892
|
try {
|
|
791
893
|
const [id] = args;
|
|
@@ -817,6 +919,16 @@ function createVirtualPlugin(userConfig) {
|
|
|
817
919
|
} catch {
|
|
818
920
|
}
|
|
819
921
|
},
|
|
922
|
+
/**
|
|
923
|
+
* 加载虚拟模块内容
|
|
924
|
+
* 当 import 语句引用虚拟模块时,Vite 会调用此方法获取模块代码
|
|
925
|
+
* @param args - load 的所有参数
|
|
926
|
+
* @returns 模块代码字符串,如果 ID 不匹配则返回 undefined
|
|
927
|
+
* @remarks
|
|
928
|
+
* - 支持同步和异步的 generateModule 函数
|
|
929
|
+
* - 生成的代码会被缓存,直到模块被失效
|
|
930
|
+
* - 子路径导入(如 'virtual:routes/sub')会传入完整 ID 给 generateModule
|
|
931
|
+
*/
|
|
820
932
|
async load(...args) {
|
|
821
933
|
const [id] = args;
|
|
822
934
|
if (id === VIRTUAL_MODULE_ID || id.startsWith(`${VIRTUAL_MODULE_ID}/`)) {
|
|
@@ -826,9 +938,21 @@ function createVirtualPlugin(userConfig) {
|
|
|
826
938
|
}
|
|
827
939
|
return await callUserHook(load, this, ...args);
|
|
828
940
|
},
|
|
941
|
+
// 使用 rest 参数包含所有其他未使用的钩子(包括 name, transform, enforce 等)
|
|
829
942
|
...restHooks
|
|
830
943
|
};
|
|
831
944
|
}
|
|
945
|
+
function cleanRoute(route) {
|
|
946
|
+
const cleaned = { ...route };
|
|
947
|
+
if (cleaned.children) {
|
|
948
|
+
if (Array.isArray(cleaned.children) && cleaned.children.length > 0) {
|
|
949
|
+
cleaned.children = cleaned.children.map(cleanRoute);
|
|
950
|
+
} else {
|
|
951
|
+
delete cleaned.children;
|
|
952
|
+
}
|
|
953
|
+
}
|
|
954
|
+
return cleaned;
|
|
955
|
+
}
|
|
832
956
|
function findParentRouteHandle(modules2, parentPath) {
|
|
833
957
|
for (const route of modules2) {
|
|
834
958
|
if (route.path === parentPath) {
|
|
@@ -842,24 +966,48 @@ function findParentRouteHandle(modules2, parentPath) {
|
|
|
842
966
|
}
|
|
843
967
|
return void 0;
|
|
844
968
|
}
|
|
969
|
+
function parseModulePath(modulePath) {
|
|
970
|
+
const pathArr = modulePath.split("/").filter((item) => item && !item.includes("."));
|
|
971
|
+
if (pathArr.at(-1) === "src") {
|
|
972
|
+
pathArr.pop();
|
|
973
|
+
}
|
|
974
|
+
const componentName = pathArr.at(-1);
|
|
975
|
+
const path2 = `/${pathArr.join("/")}`;
|
|
976
|
+
const parentPath = `/${pathArr.slice(0, -1).join("/")}`;
|
|
977
|
+
return { pathArr, componentName, path: path2, parentPath };
|
|
978
|
+
}
|
|
979
|
+
function processComponent(componentLoader, componentName, eager) {
|
|
980
|
+
if (eager) {
|
|
981
|
+
return {
|
|
982
|
+
component: componentLoader,
|
|
983
|
+
metaTitle: (componentLoader == null ? void 0 : componentLoader.name) || componentName
|
|
984
|
+
};
|
|
985
|
+
}
|
|
986
|
+
return {
|
|
987
|
+
component: typeof componentLoader === "function" ? componentLoader : componentLoader,
|
|
988
|
+
metaTitle: componentName
|
|
989
|
+
};
|
|
990
|
+
}
|
|
845
991
|
function generateRoutes(files, prefix = "", baseRoute, eager = false) {
|
|
846
992
|
const newBaseRoute = typeof baseRoute === "string" ? { name: baseRoute } : baseRoute;
|
|
847
993
|
const modules2 = newBaseRoute ? [newBaseRoute] : [];
|
|
848
|
-
|
|
994
|
+
const fileKeys = Object.keys(files);
|
|
995
|
+
if (fileKeys.length === 0) {
|
|
996
|
+
return [];
|
|
997
|
+
}
|
|
998
|
+
return fileKeys.sort((a, b) => {
|
|
849
999
|
const aLength = a.split("/").length;
|
|
850
1000
|
const bLength = b.split("/").length;
|
|
851
1001
|
return bLength > aLength ? -1 : 1;
|
|
852
1002
|
}).reduce((modules22 = [], modulePath) => {
|
|
853
1003
|
const componentLoader = files[modulePath];
|
|
854
|
-
if (!componentLoader || modulePath === "install")
|
|
1004
|
+
if (!componentLoader || modulePath === "install") {
|
|
1005
|
+
return modules22;
|
|
1006
|
+
}
|
|
1007
|
+
const { path: path2, parentPath, componentName } = parseModulePath(modulePath);
|
|
1008
|
+
if (!componentName) {
|
|
855
1009
|
return modules22;
|
|
856
|
-
const pathArr = modulePath.split("/").filter((item) => item && !item.includes("."));
|
|
857
|
-
if (pathArr.at(-1) === "src") {
|
|
858
|
-
pathArr.pop();
|
|
859
1010
|
}
|
|
860
|
-
const componentName = pathArr.at(-1);
|
|
861
|
-
const path2 = `/${pathArr.join("/")}`;
|
|
862
|
-
const parentPath = `/${pathArr.slice(0, -1).join("/")}`;
|
|
863
1011
|
let parentRoute;
|
|
864
1012
|
if (newBaseRoute) {
|
|
865
1013
|
newBaseRoute.children = newBaseRoute.children || [];
|
|
@@ -869,56 +1017,64 @@ function generateRoutes(files, prefix = "", baseRoute, eager = false) {
|
|
|
869
1017
|
} else {
|
|
870
1018
|
parentRoute = findParentRouteHandle(modules22, parentPath);
|
|
871
1019
|
}
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
}
|
|
1020
|
+
const { component, metaTitle } = processComponent(componentLoader, componentName, eager);
|
|
1021
|
+
const routeItem = {
|
|
1022
|
+
path: path2,
|
|
1023
|
+
name: componentName,
|
|
1024
|
+
meta: {
|
|
1025
|
+
title: metaTitle
|
|
1026
|
+
},
|
|
1027
|
+
component
|
|
1028
|
+
};
|
|
881
1029
|
if (parentRoute) {
|
|
882
|
-
if (!parentRoute.children)
|
|
1030
|
+
if (!parentRoute.children) {
|
|
883
1031
|
parentRoute.children = [];
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
name: componentName,
|
|
887
|
-
meta: {
|
|
888
|
-
title: metaTitle
|
|
889
|
-
},
|
|
890
|
-
component
|
|
891
|
-
});
|
|
1032
|
+
}
|
|
1033
|
+
parentRoute.children.push(routeItem);
|
|
892
1034
|
} else {
|
|
893
|
-
modules22.push(
|
|
894
|
-
path: path2,
|
|
895
|
-
name: componentName,
|
|
896
|
-
meta: {
|
|
897
|
-
title: metaTitle
|
|
898
|
-
},
|
|
899
|
-
component
|
|
900
|
-
});
|
|
1035
|
+
modules22.push(routeItem);
|
|
901
1036
|
}
|
|
902
1037
|
return modules22;
|
|
903
|
-
}, modules2)
|
|
1038
|
+
}, modules2).map(cleanRoute).filter((route) => {
|
|
1039
|
+
return !(route.children && Array.isArray(route.children) && route.children.length === 0);
|
|
1040
|
+
});
|
|
904
1041
|
}
|
|
905
1042
|
function findDefaultRouteHandle(routes) {
|
|
906
1043
|
var _a, _b, _c;
|
|
1044
|
+
if (!routes || routes.length === 0) {
|
|
1045
|
+
return "";
|
|
1046
|
+
}
|
|
907
1047
|
for (const route of routes) {
|
|
908
1048
|
if ((_a = route.meta) == null ? void 0 : _a.default) {
|
|
909
|
-
return route.path;
|
|
910
|
-
}
|
|
911
|
-
|
|
912
|
-
|
|
1049
|
+
return route.path || "";
|
|
1050
|
+
}
|
|
1051
|
+
if ((_b = route.children) == null ? void 0 : _b.length) {
|
|
1052
|
+
const childPath = findDefaultRouteHandle(route.children);
|
|
1053
|
+
if (childPath) {
|
|
1054
|
+
return childPath;
|
|
913
1055
|
}
|
|
914
1056
|
}
|
|
915
1057
|
}
|
|
916
|
-
return (_c = routes
|
|
1058
|
+
return ((_c = routes[0]) == null ? void 0 : _c.path) || "";
|
|
1059
|
+
}
|
|
1060
|
+
function extractGlob(globVal) {
|
|
1061
|
+
return globVal.glob || globVal;
|
|
1062
|
+
}
|
|
1063
|
+
function getEagerOption(globVal, globalEager) {
|
|
1064
|
+
return globVal.eager ?? globalEager ?? false;
|
|
1065
|
+
}
|
|
1066
|
+
function generateImportCode(varName, glob, eager) {
|
|
1067
|
+
if (eager) {
|
|
1068
|
+
return `const ${varName} = import.meta.glob(${JSON.stringify(glob)}, { eager: true, import: 'default' });
|
|
1069
|
+
`;
|
|
1070
|
+
}
|
|
1071
|
+
return `const ${varName} = import.meta.glob(${JSON.stringify(glob)});
|
|
1072
|
+
`;
|
|
917
1073
|
}
|
|
918
1074
|
function createAutoRoutesPlugin({ routeConfig, virtualModuleId, dts, root, eager: globalEager }) {
|
|
919
1075
|
const VIRTUAL_MODULE_ID = virtualModuleId || "virtual:auto-routes";
|
|
920
1076
|
const watchGlobs = Object.values(routeConfig).flatMap((globVal) => {
|
|
921
|
-
const g = globVal
|
|
1077
|
+
const g = extractGlob(globVal);
|
|
922
1078
|
return Array.isArray(g) ? g : [g];
|
|
923
1079
|
});
|
|
924
1080
|
return createVirtualPlugin(
|
|
@@ -929,43 +1085,41 @@ function createAutoRoutesPlugin({ routeConfig, virtualModuleId, dts, root, eager
|
|
|
929
1085
|
root,
|
|
930
1086
|
watch: watchGlobs,
|
|
931
1087
|
enforce: "pre",
|
|
1088
|
+
// 生成虚拟模块代码:仅负责产出字符串,监听/HMR/缓存由工厂统一处理
|
|
932
1089
|
generateModule: () => {
|
|
933
1090
|
const imports = [];
|
|
934
1091
|
const routes = [];
|
|
935
1092
|
Object.entries(routeConfig).forEach(([prefix, globVal], index2) => {
|
|
936
1093
|
const varName = `files${index2}`;
|
|
937
|
-
const glob = globVal
|
|
938
|
-
const eager = globVal
|
|
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
|
-
}
|
|
1094
|
+
const glob = extractGlob(globVal);
|
|
1095
|
+
const eager = getEagerOption(globVal, globalEager);
|
|
950
1096
|
const baseRoute = globVal.baseRoute;
|
|
951
|
-
|
|
1097
|
+
const baseRouteParam = baseRoute !== void 0 ? JSON.stringify(baseRoute) : "undefined";
|
|
1098
|
+
imports.push(generateImportCode(varName, glob, eager));
|
|
1099
|
+
routes.push(`...generateRoutes(${varName}, '${prefix}',${baseRouteParam}, ${eager})`);
|
|
952
1100
|
});
|
|
1101
|
+
const routesCode = routes.length > 0 ? `[${routes.join(",\n")}]` : "[]";
|
|
953
1102
|
return `
|
|
954
1103
|
${imports.join("\n")}
|
|
955
|
-
|
|
1104
|
+
${findParentRouteHandle}
|
|
1105
|
+
${parseModulePath}
|
|
1106
|
+
${processComponent}
|
|
1107
|
+
${cleanRoute}
|
|
1108
|
+
${findDefaultRouteHandle}
|
|
956
1109
|
|
|
1110
|
+
const findParentRoute = ${findParentRouteHandle}
|
|
1111
|
+
// 用于routes
|
|
957
1112
|
const generateRoutes = ${generateRoutes};
|
|
958
|
-
|
|
1113
|
+
// 用于导出
|
|
959
1114
|
const findDefaultRoute = ${findDefaultRouteHandle};
|
|
960
1115
|
|
|
961
|
-
${
|
|
962
|
-
${findDefaultRouteHandle}
|
|
963
|
-
const routes = [${routes.join(",\n")}];
|
|
1116
|
+
const routes = ${routesCode}.flat().filter(Boolean);
|
|
964
1117
|
|
|
965
1118
|
export { routes, findDefaultRoute };
|
|
966
1119
|
export default routes;
|
|
967
1120
|
`;
|
|
968
1121
|
},
|
|
1122
|
+
// 生成类型声明文件
|
|
969
1123
|
generateDts: () => `// 此文件由ViteConfig自动生成,请勿手动修改
|
|
970
1124
|
declare module 'virtual:auto-routes' {
|
|
971
1125
|
interface RouteModule {
|
package/es/src/_types/index.d.ts
CHANGED
|
@@ -19,35 +19,116 @@ export type QiankunPlugin = (name: string, options?: {
|
|
|
19
19
|
useDevMode?: boolean;
|
|
20
20
|
}) => PluginOption;
|
|
21
21
|
export type CDNOptions = Parameters<typeof importToCDN>[0] & {
|
|
22
|
+
/**
|
|
23
|
+
* CDN的基本url
|
|
24
|
+
*/
|
|
22
25
|
baseUrl?: string;
|
|
26
|
+
/**
|
|
27
|
+
* dev环境是否启用CDN
|
|
28
|
+
*/
|
|
23
29
|
enableInDevMode?: boolean;
|
|
24
30
|
};
|
|
25
31
|
export type VisualizerOptions = Parameters<typeof visualizer>[0];
|
|
32
|
+
/**
|
|
33
|
+
* 插件配置类型,仅在 ModeConfig 中使用
|
|
34
|
+
*/
|
|
26
35
|
export interface PluginConfig {
|
|
36
|
+
/**
|
|
37
|
+
* AutoImport配置,true表示使用默认配置,对象表示覆盖默认配置
|
|
38
|
+
*/
|
|
27
39
|
autoImport?: boolean | unpluginAutoImportOptions;
|
|
40
|
+
/**
|
|
41
|
+
* Components配置,true表示使用默认配置,对象表示覆盖默认配置
|
|
42
|
+
*/
|
|
28
43
|
autoComponent?: boolean | (unpluginVueComponentsOptions & {
|
|
44
|
+
/**
|
|
45
|
+
* 需要排除的element-plus组件
|
|
46
|
+
*/
|
|
29
47
|
elementExcludes?: string[];
|
|
48
|
+
/**
|
|
49
|
+
* 除resolve规则外,额外需要引入的组件所需匹配规则
|
|
50
|
+
*/
|
|
30
51
|
globs?: string[];
|
|
31
52
|
});
|
|
53
|
+
/**
|
|
54
|
+
* 压缩配置,true表示使用默认配置,对象表示覆盖默认配置
|
|
55
|
+
*/
|
|
32
56
|
compression?: boolean | CompressionOptions;
|
|
57
|
+
/**
|
|
58
|
+
* 图片压缩配置,true表示使用默认配置,对象表示覆盖默认配置
|
|
59
|
+
*/
|
|
33
60
|
imagemin?: boolean | ImageminOptions;
|
|
61
|
+
/**
|
|
62
|
+
* CDN配置,true表示使用默认配置,对象表示覆盖默认配置
|
|
63
|
+
*/
|
|
34
64
|
cdn?: boolean | CDNOptions;
|
|
65
|
+
/**
|
|
66
|
+
* 包预览配置,true表示使用默认配置,对象表示覆盖默认配置
|
|
67
|
+
*/
|
|
35
68
|
visualizer?: boolean | VisualizerOptions;
|
|
69
|
+
/**
|
|
70
|
+
* 自动路由配置,true表示使用默认配置,对象表示覆盖默认配置
|
|
71
|
+
*/
|
|
36
72
|
autoRoutes?: boolean | AutoRoutesConfig;
|
|
73
|
+
/**
|
|
74
|
+
* 页面路由配置(vite-plugin-pages),true表示使用默认配置,对象表示覆盖默认配置
|
|
75
|
+
*/
|
|
37
76
|
pageRoutes?: boolean | PagesOptions;
|
|
77
|
+
/**
|
|
78
|
+
* 是否启用vue-devtools
|
|
79
|
+
*/
|
|
38
80
|
devtools?: boolean;
|
|
81
|
+
/**
|
|
82
|
+
* 项目端口
|
|
83
|
+
*/
|
|
39
84
|
port?: number;
|
|
85
|
+
/**
|
|
86
|
+
* 是否在npm run dev时,自动打开浏览器
|
|
87
|
+
*/
|
|
40
88
|
open?: boolean;
|
|
89
|
+
/**
|
|
90
|
+
* dev环境是否启用qiankun
|
|
91
|
+
*/
|
|
41
92
|
qiankunDevMode?: boolean;
|
|
93
|
+
/**
|
|
94
|
+
* 是否启用qiankun
|
|
95
|
+
*/
|
|
42
96
|
qiankun?: boolean;
|
|
97
|
+
/**
|
|
98
|
+
* 命名空间,启用后在非dev环境下的envSystemCode将等于此值
|
|
99
|
+
*/
|
|
43
100
|
namespace?: string;
|
|
101
|
+
/**
|
|
102
|
+
* 是否在打包时,删除console和debugger
|
|
103
|
+
*/
|
|
44
104
|
dropConsole?: boolean;
|
|
105
|
+
/**
|
|
106
|
+
* PWA配置,true表示使用默认配置,对象表示覆盖默认配置
|
|
107
|
+
*/
|
|
45
108
|
pwa?: boolean | VitePWAOptions;
|
|
109
|
+
/**
|
|
110
|
+
* Code Inspector配置,true表示使用默认配置,对象表示覆盖默认配置
|
|
111
|
+
*/
|
|
46
112
|
codeInspector?: boolean | Parameters<typeof import('code-inspector-plugin').codeInspectorPlugin>[0];
|
|
113
|
+
/**
|
|
114
|
+
* 是否是vitepress
|
|
115
|
+
*/
|
|
47
116
|
vitepress?: boolean;
|
|
117
|
+
/**
|
|
118
|
+
* 是否是vue
|
|
119
|
+
*/
|
|
48
120
|
vue?: boolean;
|
|
121
|
+
/**
|
|
122
|
+
* 是否是react
|
|
123
|
+
*/
|
|
49
124
|
react?: boolean;
|
|
125
|
+
/**
|
|
126
|
+
* 项目标题
|
|
127
|
+
*/
|
|
50
128
|
appTitle?: string;
|
|
129
|
+
/**
|
|
130
|
+
* 项目code
|
|
131
|
+
*/
|
|
51
132
|
appCode?: string;
|
|
52
133
|
}
|
|
53
134
|
export interface ModeConfig extends PluginConfig {
|
|
@@ -65,13 +146,31 @@ export interface RouteConfig {
|
|
|
65
146
|
[prefix: string]: string | string[] | objRouteConfig;
|
|
66
147
|
}
|
|
67
148
|
export interface AutoRoutesConfig {
|
|
149
|
+
/**
|
|
150
|
+
* 路由配置
|
|
151
|
+
*/
|
|
68
152
|
routeConfig: RouteConfig;
|
|
153
|
+
/**
|
|
154
|
+
* 虚拟模块ID
|
|
155
|
+
*/
|
|
69
156
|
virtualModuleId?: string;
|
|
157
|
+
/**
|
|
158
|
+
* 声明文件路径,true表示使用默认路径,false表示不生成声明文件
|
|
159
|
+
*/
|
|
70
160
|
dts?: string | boolean;
|
|
161
|
+
/**
|
|
162
|
+
* 项目根目录路径
|
|
163
|
+
*/
|
|
71
164
|
root?: string;
|
|
72
165
|
}
|
|
73
166
|
export interface Config extends PluginConfig {
|
|
167
|
+
/**
|
|
168
|
+
* 根目录
|
|
169
|
+
*/
|
|
74
170
|
rootPath: string;
|
|
171
|
+
/**
|
|
172
|
+
* 环境配置
|
|
173
|
+
*/
|
|
75
174
|
mode?: {
|
|
76
175
|
base?: ModeConfig;
|
|
77
176
|
development?: ModeConfig;
|
|
@@ -1,7 +1,22 @@
|
|
|
1
1
|
import { objType } from '../../../_types/index.ts';
|
|
2
2
|
export declare function isDevFn(mode: string): boolean;
|
|
3
3
|
export declare function isProdFn(mode: string): boolean;
|
|
4
|
+
/**
|
|
5
|
+
* Whether to generate package preview
|
|
6
|
+
*/
|
|
4
7
|
export declare function isReportMode(): boolean;
|
|
8
|
+
/**
|
|
9
|
+
* 将环境变量中的字符串值转换为对应的 JavaScript 数据类型
|
|
10
|
+
*/
|
|
5
11
|
export declare function wrapperEnv(env: Record<string, string>): objType;
|
|
12
|
+
/**
|
|
13
|
+
* Get the environment variables starting with the specified prefix
|
|
14
|
+
* @param match prefix
|
|
15
|
+
* @param confFiles ext
|
|
16
|
+
*/
|
|
6
17
|
export declare function getEnvConfig(match?: string, confFiles?: string[]): {};
|
|
18
|
+
/**
|
|
19
|
+
* Get user root directory
|
|
20
|
+
* @param dir file path
|
|
21
|
+
*/
|
|
7
22
|
export declare function getRootPath(...dir: string[]): string;
|