@moluoxixi/vite-config 0.0.32 → 0.0.35
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/AutoRoutesPlugin/index.mjs +86 -0
- package/es/AutoRoutesPlugin/routeGenerator.mjs +123 -0
- package/es/_utils/base.mjs +8 -0
- package/es/_utils/dynamicImport.mjs +13 -0
- package/es/_utils/object.mjs +56 -0
- package/es/_utils/virtual.mjs +389 -0
- package/es/index.d.ts +2 -0
- package/es/index.mjs +3 -1190
- package/es/src/_utils/detectFramework.mjs +45 -0
- package/es/src/_utils/getEnv.mjs +21 -0
- package/es/src/constants/index.mjs +32 -0
- package/es/src/index.mjs +395 -0
- package/es/src/plugins/addScopedAndReplacePrefix.mjs +54 -0
- package/lib/AutoRoutesPlugin/index.cjs +86 -0
- package/lib/AutoRoutesPlugin/routeGenerator.cjs +123 -0
- package/lib/_utils/base.cjs +9 -0
- package/lib/_utils/dynamicImport.cjs +13 -0
- package/lib/_utils/object.cjs +56 -0
- package/lib/_utils/virtual.cjs +394 -0
- package/lib/index.cjs +9 -1215
- package/lib/index.d.ts +2 -0
- package/lib/src/_utils/detectFramework.cjs +48 -0
- package/lib/src/_utils/getEnv.cjs +25 -0
- package/lib/src/constants/index.cjs +32 -0
- package/lib/src/index.cjs +425 -0
- package/lib/src/plugins/addScopedAndReplacePrefix.cjs +54 -0
- package/package.json +24 -24
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
function detectFramework(config, rootDir) {
|
|
2
|
+
const root = rootDir || Process.cwd();
|
|
3
|
+
const packageJsonPath = Path.resolve(root, "package.json");
|
|
4
|
+
let vue = config.vue;
|
|
5
|
+
let react = config.react;
|
|
6
|
+
let vitepress = config.vitepress;
|
|
7
|
+
const hasExplicitFramework = vue === true || react === true || vitepress === true;
|
|
8
|
+
if (!hasExplicitFramework) {
|
|
9
|
+
let dependencies = {};
|
|
10
|
+
let devDependencies = {};
|
|
11
|
+
if (Fs.existsSync(packageJsonPath)) {
|
|
12
|
+
try {
|
|
13
|
+
const packageJsonContent = Fs.readFileSync(packageJsonPath, "utf-8");
|
|
14
|
+
const packageJson = JSON.parse(packageJsonContent);
|
|
15
|
+
dependencies = packageJson.dependencies || {};
|
|
16
|
+
devDependencies = packageJson.devDependencies || {};
|
|
17
|
+
} catch (error) {
|
|
18
|
+
console.warn(`无法读取 package.json: ${packageJsonPath}`, error);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
if (vue === void 0) {
|
|
22
|
+
if (dependencies.vue || devDependencies.vue) {
|
|
23
|
+
vue = true;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
if (react === void 0 && vue !== true) {
|
|
27
|
+
if (dependencies.react || devDependencies.react) {
|
|
28
|
+
react = true;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
if (vitepress === void 0 && vue !== true && react !== true) {
|
|
32
|
+
if (dependencies.vitepress || devDependencies.vitepress) {
|
|
33
|
+
vitepress = true;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
return {
|
|
38
|
+
vue: vue ?? false,
|
|
39
|
+
react: react ?? false,
|
|
40
|
+
vitepress: vitepress ?? false
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
export {
|
|
44
|
+
detectFramework
|
|
45
|
+
};
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
function wrapperEnv(env) {
|
|
2
|
+
const result = {};
|
|
3
|
+
for (const key in env) {
|
|
4
|
+
if (Object.prototype.hasOwnProperty.call(env, key)) {
|
|
5
|
+
const value = env[key].trim();
|
|
6
|
+
if (value === "true" || value === "false") {
|
|
7
|
+
result[key] = value === "true";
|
|
8
|
+
} else if (!Number.isNaN(Number(value))) {
|
|
9
|
+
result[key] = Number(value);
|
|
10
|
+
} else if (value === "") {
|
|
11
|
+
result[key] = null;
|
|
12
|
+
} else {
|
|
13
|
+
result[key] = value;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
return result;
|
|
18
|
+
}
|
|
19
|
+
export {
|
|
20
|
+
wrapperEnv
|
|
21
|
+
};
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
function getCamelCase(str) {
|
|
2
|
+
return str.replace(/[-_]+/g, " ").replace(/(?:^|\s)\w/g, (match) => match.toUpperCase()).replace(/\s+/g, "");
|
|
3
|
+
}
|
|
4
|
+
function getCdnModules(modules2) {
|
|
5
|
+
function getPath(str) {
|
|
6
|
+
if (!str)
|
|
7
|
+
return "";
|
|
8
|
+
return str.startsWith("/") ? str : `/${str}`;
|
|
9
|
+
}
|
|
10
|
+
return modules2.map((item) => {
|
|
11
|
+
if (typeof item === "string") {
|
|
12
|
+
return {
|
|
13
|
+
name: item,
|
|
14
|
+
var: getCamelCase(item),
|
|
15
|
+
path: ""
|
|
16
|
+
};
|
|
17
|
+
} else {
|
|
18
|
+
return item;
|
|
19
|
+
}
|
|
20
|
+
}).map((item) => {
|
|
21
|
+
return {
|
|
22
|
+
name: item.name,
|
|
23
|
+
var: item.var || getCamelCase(item.name),
|
|
24
|
+
path: getPath(item.path),
|
|
25
|
+
css: getPath(item.css)
|
|
26
|
+
};
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
const modules = getCdnModules([]);
|
|
30
|
+
export {
|
|
31
|
+
modules
|
|
32
|
+
};
|
package/es/src/index.mjs
ADDED
|
@@ -0,0 +1,395 @@
|
|
|
1
|
+
import { dynamicImport } from "../_utils/dynamicImport.mjs";
|
|
2
|
+
import { validateMutuallyExclusive, deepMerge } from "../_utils/object.mjs";
|
|
3
|
+
import { detectFramework } from "./_utils/detectFramework.mjs";
|
|
4
|
+
import addScopedAndReplacePrefixPlugin from "./plugins/addScopedAndReplacePrefix.mjs";
|
|
5
|
+
async function getViteConfig(Config = {}, params) {
|
|
6
|
+
const config = typeof Config === "function" ? Config(params) : Config;
|
|
7
|
+
const { mode = "base" } = params || {};
|
|
8
|
+
const rootPath = config.rootPath || process.cwd();
|
|
9
|
+
const modeConfig = config.mode || {};
|
|
10
|
+
const baseConfig = modeConfig.base || {};
|
|
11
|
+
const currentModeConfig = modeConfig[mode] || {};
|
|
12
|
+
const viteEnv = { ...baseConfig, ...currentModeConfig };
|
|
13
|
+
const isDev = mode === "development";
|
|
14
|
+
const {
|
|
15
|
+
autoImport = config.autoImport ?? true,
|
|
16
|
+
autoComponent = config.autoComponent ?? true,
|
|
17
|
+
compression = config.compression ?? true,
|
|
18
|
+
imagemin = config.imagemin ?? true,
|
|
19
|
+
codeInspector = config.codeInspector ?? true,
|
|
20
|
+
port = config.port,
|
|
21
|
+
visualizer = config.visualizer ?? false,
|
|
22
|
+
autoRoutes = config.autoRoutes ?? false,
|
|
23
|
+
cdn = config.cdn ?? false,
|
|
24
|
+
pageRoutes = config.pageRoutes ?? false,
|
|
25
|
+
pwa = config.pwa ?? false,
|
|
26
|
+
devtools = config.devtools,
|
|
27
|
+
open = config.open,
|
|
28
|
+
qiankunDevMode = config.qiankunDevMode,
|
|
29
|
+
qiankun = config.qiankun,
|
|
30
|
+
namespace = config.namespace,
|
|
31
|
+
dropConsole = config.dropConsole,
|
|
32
|
+
vue: vueRaw = config.vue,
|
|
33
|
+
react: reactRaw = config.react,
|
|
34
|
+
vitepress: vitepressRaw = config.vitepress
|
|
35
|
+
} = viteEnv;
|
|
36
|
+
const frameworkResult = detectFramework(
|
|
37
|
+
{
|
|
38
|
+
vue: vueRaw,
|
|
39
|
+
react: reactRaw,
|
|
40
|
+
vitepress: vitepressRaw
|
|
41
|
+
},
|
|
42
|
+
rootPath
|
|
43
|
+
);
|
|
44
|
+
const { vue, react, vitepress } = validateMutuallyExclusive(
|
|
45
|
+
frameworkResult,
|
|
46
|
+
"vue"
|
|
47
|
+
);
|
|
48
|
+
const appTitle = config.appTitle;
|
|
49
|
+
const appCode = config.appCode;
|
|
50
|
+
const isVueOrVitepress = vue || vitepress;
|
|
51
|
+
const envSystemCode = isDev && !qiankunDevMode ? "el" : namespace ?? appCode;
|
|
52
|
+
const plugins = [
|
|
53
|
+
VitePluginHtml.createHtmlPlugin({
|
|
54
|
+
inject: {
|
|
55
|
+
data: {
|
|
56
|
+
title: appTitle
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
})
|
|
60
|
+
];
|
|
61
|
+
if (vue) {
|
|
62
|
+
const pluginVue = await dynamicImport(Promise.resolve(VitejsPluginVue));
|
|
63
|
+
plugins.push(pluginVue());
|
|
64
|
+
}
|
|
65
|
+
if (react) {
|
|
66
|
+
const pluginReact = await dynamicImport(Promise.resolve(VitejsPluginReact));
|
|
67
|
+
plugins.push(pluginReact());
|
|
68
|
+
}
|
|
69
|
+
if (isVueOrVitepress) {
|
|
70
|
+
const vueJsx = await dynamicImport(Promise.resolve(VitejsPluginVueJsx));
|
|
71
|
+
plugins.push(vueJsx());
|
|
72
|
+
}
|
|
73
|
+
if (pageRoutes) {
|
|
74
|
+
const Pages = await dynamicImport(Promise.resolve(VitePluginPages));
|
|
75
|
+
const extensions = [];
|
|
76
|
+
if (vue) {
|
|
77
|
+
extensions.push("vue");
|
|
78
|
+
}
|
|
79
|
+
if (react) {
|
|
80
|
+
extensions.push("tsx", "jsx");
|
|
81
|
+
}
|
|
82
|
+
plugins.push(Pages(
|
|
83
|
+
deepMerge(
|
|
84
|
+
{
|
|
85
|
+
dirs: "src/pages",
|
|
86
|
+
extensions,
|
|
87
|
+
exclude: [
|
|
88
|
+
"**/components/**",
|
|
89
|
+
"**/__tests__/**"
|
|
90
|
+
]
|
|
91
|
+
},
|
|
92
|
+
pageRoutes
|
|
93
|
+
)
|
|
94
|
+
));
|
|
95
|
+
}
|
|
96
|
+
if (isDev && devtools && isVueOrVitepress) {
|
|
97
|
+
const vueDevTools = await dynamicImport(Promise.resolve(VitePluginVueDevtools));
|
|
98
|
+
plugins.push(vueDevTools());
|
|
99
|
+
}
|
|
100
|
+
if (autoImport && isVueOrVitepress) {
|
|
101
|
+
const AutoImport = await dynamicImport(import("unplugin-auto-import/vite"));
|
|
102
|
+
const ElementPlusResolverModule = await dynamicImport(import("unplugin-vue-components/resolvers"));
|
|
103
|
+
const { ElementPlusResolver } = ElementPlusResolverModule;
|
|
104
|
+
plugins.push(AutoImport(
|
|
105
|
+
deepMerge(
|
|
106
|
+
{
|
|
107
|
+
imports: ["vue"],
|
|
108
|
+
resolvers: [ElementPlusResolver()],
|
|
109
|
+
dts: Path.resolve(rootPath, "./typings/auto-imports.d.ts")
|
|
110
|
+
},
|
|
111
|
+
autoImport
|
|
112
|
+
)
|
|
113
|
+
));
|
|
114
|
+
}
|
|
115
|
+
if (autoComponent && isVueOrVitepress) {
|
|
116
|
+
const Components = await dynamicImport(import("unplugin-vue-components/vite"));
|
|
117
|
+
const ElementPlusResolverModule = await dynamicImport(import("unplugin-vue-components/resolvers"));
|
|
118
|
+
const { ElementPlusResolver } = ElementPlusResolverModule;
|
|
119
|
+
plugins.push(Components(
|
|
120
|
+
deepMerge(
|
|
121
|
+
{
|
|
122
|
+
resolvers: [ElementPlusResolver()],
|
|
123
|
+
globs: [],
|
|
124
|
+
dts: Path.resolve(rootPath, "./typings/components.d.ts")
|
|
125
|
+
},
|
|
126
|
+
autoComponent
|
|
127
|
+
)
|
|
128
|
+
));
|
|
129
|
+
}
|
|
130
|
+
if (compression) {
|
|
131
|
+
const viteCompression = await dynamicImport(Promise.resolve(VitePluginCompression));
|
|
132
|
+
const compressionPlugin = viteCompression;
|
|
133
|
+
plugins.push(compressionPlugin(
|
|
134
|
+
deepMerge(
|
|
135
|
+
{
|
|
136
|
+
algorithm: "brotliCompress",
|
|
137
|
+
verbose: true,
|
|
138
|
+
disable: false,
|
|
139
|
+
ext: ".gz",
|
|
140
|
+
threshold: 10240,
|
|
141
|
+
deleteOriginFile: false
|
|
142
|
+
},
|
|
143
|
+
compression
|
|
144
|
+
)
|
|
145
|
+
));
|
|
146
|
+
}
|
|
147
|
+
if (imagemin) {
|
|
148
|
+
const viteImagemin = await dynamicImport(Promise.resolve(VitePluginImagemin));
|
|
149
|
+
const imageminPlugin = viteImagemin;
|
|
150
|
+
plugins.push(imageminPlugin(
|
|
151
|
+
deepMerge(
|
|
152
|
+
{
|
|
153
|
+
gifsicle: { optimizationLevel: 7, interlaced: false },
|
|
154
|
+
optipng: { optimizationLevel: 7 },
|
|
155
|
+
mozjpeg: { quality: 20 },
|
|
156
|
+
pngquant: { quality: [0.8, 0.9], speed: 4 },
|
|
157
|
+
svgo: {
|
|
158
|
+
plugins: [{ name: "removeViewBox" }, { name: "removeEmptyAttrs", active: false }]
|
|
159
|
+
}
|
|
160
|
+
},
|
|
161
|
+
imagemin
|
|
162
|
+
)
|
|
163
|
+
));
|
|
164
|
+
}
|
|
165
|
+
if (cdn) {
|
|
166
|
+
const importToCDN = await dynamicImport(Promise.resolve(VitePluginCdnImport));
|
|
167
|
+
const { modules } = await dynamicImport(import("./constants/index.mjs"));
|
|
168
|
+
plugins.push(importToCDN(
|
|
169
|
+
deepMerge(
|
|
170
|
+
{
|
|
171
|
+
enableInDevMode: false,
|
|
172
|
+
prodUrl: "/{name}@{version}{path}",
|
|
173
|
+
modules,
|
|
174
|
+
generateScriptTag: (_name, scriptUrl) => {
|
|
175
|
+
const esmArr = ["esm", ".mjs"];
|
|
176
|
+
const isESM = esmArr.some((item) => scriptUrl.includes(item));
|
|
177
|
+
if (isESM) {
|
|
178
|
+
return {
|
|
179
|
+
attrs: {
|
|
180
|
+
src: scriptUrl,
|
|
181
|
+
type: "module",
|
|
182
|
+
crossorigin: "anonymous"
|
|
183
|
+
},
|
|
184
|
+
injectTo: "head"
|
|
185
|
+
};
|
|
186
|
+
} else {
|
|
187
|
+
return {
|
|
188
|
+
attrs: {
|
|
189
|
+
src: scriptUrl,
|
|
190
|
+
crossorigin: "anonymous"
|
|
191
|
+
},
|
|
192
|
+
injectTo: "head"
|
|
193
|
+
};
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
},
|
|
197
|
+
cdn
|
|
198
|
+
)
|
|
199
|
+
));
|
|
200
|
+
}
|
|
201
|
+
if (visualizer) {
|
|
202
|
+
const visualizerPlugin = await dynamicImport(Promise.resolve(RollupPluginVisualizer));
|
|
203
|
+
plugins.push(visualizerPlugin(
|
|
204
|
+
deepMerge(
|
|
205
|
+
{
|
|
206
|
+
open: true
|
|
207
|
+
},
|
|
208
|
+
visualizer
|
|
209
|
+
)
|
|
210
|
+
));
|
|
211
|
+
}
|
|
212
|
+
if (pwa) {
|
|
213
|
+
const pwaModule = await dynamicImport(Promise.resolve(VitePluginPwa));
|
|
214
|
+
const { VitePWA } = pwaModule;
|
|
215
|
+
plugins.push(VitePWA(
|
|
216
|
+
deepMerge(
|
|
217
|
+
{
|
|
218
|
+
strategies: "generateSW",
|
|
219
|
+
registerType: "autoUpdate",
|
|
220
|
+
// 开发模式下也启用
|
|
221
|
+
devOptions: {
|
|
222
|
+
enabled: true
|
|
223
|
+
},
|
|
224
|
+
includeAssets: ["favicon.ico", "apple-touch-icon.png", "mask-icon.svg"],
|
|
225
|
+
manifest: {
|
|
226
|
+
id: appCode ? `/${appCode}/` : "/",
|
|
227
|
+
start_url: appCode ? `/${appCode}/` : "/",
|
|
228
|
+
name: appTitle || "应用",
|
|
229
|
+
short_name: appTitle || "应用",
|
|
230
|
+
description: "渐进式 Web 应用",
|
|
231
|
+
display: "standalone",
|
|
232
|
+
background_color: "#ffffff",
|
|
233
|
+
theme_color: "#BA42BF"
|
|
234
|
+
}
|
|
235
|
+
},
|
|
236
|
+
pwa
|
|
237
|
+
)
|
|
238
|
+
));
|
|
239
|
+
}
|
|
240
|
+
if (isDev && codeInspector) {
|
|
241
|
+
const codeInspectorModule = await dynamicImport(Promise.resolve(CodeInspectorPlugin));
|
|
242
|
+
const { codeInspectorPlugin } = codeInspectorModule;
|
|
243
|
+
plugins.push(codeInspectorPlugin(
|
|
244
|
+
deepMerge(
|
|
245
|
+
{
|
|
246
|
+
bundler: "vite",
|
|
247
|
+
showSwitch: true
|
|
248
|
+
},
|
|
249
|
+
codeInspector
|
|
250
|
+
)
|
|
251
|
+
));
|
|
252
|
+
}
|
|
253
|
+
if (qiankun) {
|
|
254
|
+
const qiankunPlugin = await dynamicImport(Promise.resolve(VitePluginQiankun));
|
|
255
|
+
const qiankunPluginFn = qiankunPlugin;
|
|
256
|
+
plugins.push(qiankunPluginFn(envSystemCode || "el", { useDevMode: qiankunDevMode }));
|
|
257
|
+
if (appCode) {
|
|
258
|
+
plugins.push(addScopedAndReplacePrefixPlugin({
|
|
259
|
+
prefixScoped: `div[data-qiankun='${envSystemCode}']`,
|
|
260
|
+
oldPrefix: "el",
|
|
261
|
+
newPrefix: appCode,
|
|
262
|
+
useDevMode: qiankunDevMode
|
|
263
|
+
}));
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
if (autoRoutes && isVueOrVitepress) {
|
|
267
|
+
const AutoRoutesPlugin = await dynamicImport(import("../AutoRoutesPlugin/index.mjs"));
|
|
268
|
+
plugins.push(AutoRoutesPlugin(
|
|
269
|
+
deepMerge(
|
|
270
|
+
{
|
|
271
|
+
root: rootPath,
|
|
272
|
+
routeConfig: {
|
|
273
|
+
views: ["/src/views/**/index.vue", "!/src/views/**/components/*"],
|
|
274
|
+
examples: "/src/examples/**/index.vue",
|
|
275
|
+
componentExamples: {
|
|
276
|
+
glob: ["/src/components/**/Example.vue", "!/src/components/**/components/*"],
|
|
277
|
+
baseRoute: "组件示例"
|
|
278
|
+
}
|
|
279
|
+
},
|
|
280
|
+
dts: Path.resolve(rootPath, "./typings/auto-routes.d.ts")
|
|
281
|
+
},
|
|
282
|
+
autoRoutes
|
|
283
|
+
)
|
|
284
|
+
));
|
|
285
|
+
}
|
|
286
|
+
const defaultConfig = {
|
|
287
|
+
plugins,
|
|
288
|
+
esbuild: {
|
|
289
|
+
pure: !isDev && dropConsole ? ["console.log", "console.info", "console.debug"] : []
|
|
290
|
+
},
|
|
291
|
+
build: {
|
|
292
|
+
sourcemap: isDev,
|
|
293
|
+
outDir: appCode || "dist",
|
|
294
|
+
cssCodeSplit: true,
|
|
295
|
+
chunkSizeWarningLimit: 1500,
|
|
296
|
+
minify: "esbuild",
|
|
297
|
+
rollupOptions: {
|
|
298
|
+
external: [],
|
|
299
|
+
output: {
|
|
300
|
+
globals: {},
|
|
301
|
+
chunkFileNames: "static/js/[name]-[hash].js",
|
|
302
|
+
entryFileNames: "static/js/[name]-[hash].js",
|
|
303
|
+
assetFileNames: "static/[ext]/[name]-[hash].[ext]",
|
|
304
|
+
manualChunks: (id) => {
|
|
305
|
+
if (id.includes("node_modules")) {
|
|
306
|
+
if (id.includes("lodash-es")) {
|
|
307
|
+
return "lodash-vendor";
|
|
308
|
+
}
|
|
309
|
+
if (id.includes("element-plus")) {
|
|
310
|
+
return "el-vendor";
|
|
311
|
+
}
|
|
312
|
+
if (id.includes("@vue") || id.includes("vue")) {
|
|
313
|
+
return "vue-vendor";
|
|
314
|
+
}
|
|
315
|
+
if (id.includes("antd") || id.includes("@ant-design")) {
|
|
316
|
+
return "antd-vendor";
|
|
317
|
+
}
|
|
318
|
+
if (id.includes("react-dom")) {
|
|
319
|
+
return "react-dom-vendor";
|
|
320
|
+
}
|
|
321
|
+
if (id.includes("react")) {
|
|
322
|
+
return "react-vendor";
|
|
323
|
+
}
|
|
324
|
+
return "vendor";
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
},
|
|
330
|
+
define: {
|
|
331
|
+
__SYSTEM_CODE__: JSON.stringify(envSystemCode),
|
|
332
|
+
process: deepMerge({
|
|
333
|
+
env: {
|
|
334
|
+
VUE_APP_VXE_ENV: "production"
|
|
335
|
+
}
|
|
336
|
+
}, process)
|
|
337
|
+
},
|
|
338
|
+
css: {
|
|
339
|
+
preprocessorOptions: {
|
|
340
|
+
scss: {
|
|
341
|
+
api: "modern-compiler"
|
|
342
|
+
}
|
|
343
|
+
},
|
|
344
|
+
postcss: {
|
|
345
|
+
plugins: [TailwindcssPostcss(), Autoprefixer()]
|
|
346
|
+
},
|
|
347
|
+
devSourcemap: isDev
|
|
348
|
+
},
|
|
349
|
+
resolve: {
|
|
350
|
+
extensions: [".js", ".jsx", ".ts", ".tsx", ".vue"],
|
|
351
|
+
alias: {
|
|
352
|
+
"@": Path.resolve(rootPath, "./src")
|
|
353
|
+
}
|
|
354
|
+
},
|
|
355
|
+
server: {
|
|
356
|
+
host: "0.0.0.0",
|
|
357
|
+
port,
|
|
358
|
+
open,
|
|
359
|
+
cors: true,
|
|
360
|
+
proxy: {}
|
|
361
|
+
}
|
|
362
|
+
};
|
|
363
|
+
if (!vitepress && appCode) {
|
|
364
|
+
defaultConfig.base = `/${appCode}`;
|
|
365
|
+
}
|
|
366
|
+
const viteConfig = typeof config.viteConfig === "function" ? config.viteConfig(params) : config.viteConfig;
|
|
367
|
+
const viteConfigPluginNames = ((viteConfig == null ? void 0 : viteConfig.plugins) || []).map((i) => {
|
|
368
|
+
const plugin = Array.isArray(i) ? i[0] : i;
|
|
369
|
+
return plugin == null ? void 0 : plugin.name;
|
|
370
|
+
}).filter((name) => Boolean(name));
|
|
371
|
+
const defaultPluginNamesMap = (defaultConfig.plugins || []).reduce((nameMap, i) => {
|
|
372
|
+
const plugin = Array.isArray(i) ? i[0] : i;
|
|
373
|
+
const name = plugin == null ? void 0 : plugin.name;
|
|
374
|
+
if (name) {
|
|
375
|
+
nameMap[name] = i;
|
|
376
|
+
}
|
|
377
|
+
return nameMap;
|
|
378
|
+
}, {});
|
|
379
|
+
const uniquePlugin = [];
|
|
380
|
+
Object.keys(defaultPluginNamesMap).forEach((name) => {
|
|
381
|
+
if (!viteConfigPluginNames.includes(name)) {
|
|
382
|
+
uniquePlugin.push(defaultPluginNamesMap[name]);
|
|
383
|
+
}
|
|
384
|
+
});
|
|
385
|
+
defaultConfig.plugins = uniquePlugin;
|
|
386
|
+
return Vite.mergeConfig(defaultConfig, viteConfig || {});
|
|
387
|
+
}
|
|
388
|
+
function createViteConfig(Config) {
|
|
389
|
+
return Vite.defineConfig(async (params) => await getViteConfig(Config, params));
|
|
390
|
+
}
|
|
391
|
+
export {
|
|
392
|
+
createViteConfig,
|
|
393
|
+
createViteConfig as default,
|
|
394
|
+
getViteConfig
|
|
395
|
+
};
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
function changeHtmlClassPrefix(htmlString = "", oldPrefix = "", newPrefix = "") {
|
|
2
|
+
const regex = new RegExp(
|
|
3
|
+
`(class|style)\\s*:\\s*((["']((${oldPrefix}\\b)-).*["'])|((_normalizeClass|_normalizeStyle)\\(.*(${oldPrefix}\\b)-.*\\)))`,
|
|
4
|
+
"g"
|
|
5
|
+
);
|
|
6
|
+
return htmlString.replace(regex, (match = "") => {
|
|
7
|
+
return match.replace(oldPrefix, newPrefix);
|
|
8
|
+
});
|
|
9
|
+
}
|
|
10
|
+
function changeSelectorPrefix(cssString = "", oldPrefix = "", newPrefix = "") {
|
|
11
|
+
const regex = new RegExp(`(\\.${oldPrefix}\\b|#${oldPrefix}\\b|--${oldPrefix}\\b)`, "g");
|
|
12
|
+
return cssString.replace(regex, (match = "") => {
|
|
13
|
+
return match.replace(oldPrefix, newPrefix);
|
|
14
|
+
});
|
|
15
|
+
}
|
|
16
|
+
function addScopedAndReplacePrefixPlugin({
|
|
17
|
+
prefixScoped = "",
|
|
18
|
+
oldPrefix = "",
|
|
19
|
+
newPrefix = "",
|
|
20
|
+
useDevMode = false
|
|
21
|
+
}) {
|
|
22
|
+
let isProduction;
|
|
23
|
+
return {
|
|
24
|
+
name: "addScopedAndReplacePrefix",
|
|
25
|
+
configResolved(config) {
|
|
26
|
+
isProduction = config.command === "build" || config.isProduction;
|
|
27
|
+
},
|
|
28
|
+
transform(code = "", id = "") {
|
|
29
|
+
if (!isProduction && !useDevMode)
|
|
30
|
+
return code;
|
|
31
|
+
if (!oldPrefix || !newPrefix)
|
|
32
|
+
return code;
|
|
33
|
+
if (id.includes("node_modules"))
|
|
34
|
+
return code;
|
|
35
|
+
const cssLangs = ["css", "scss", "less", "stylus", "styl"];
|
|
36
|
+
let newCode = code;
|
|
37
|
+
if (id.endsWith(".vue")) {
|
|
38
|
+
newCode = changeHtmlClassPrefix(newCode, oldPrefix, newPrefix);
|
|
39
|
+
} else if (cssLangs.some((lang) => id.endsWith(`.${lang}`))) {
|
|
40
|
+
if (oldPrefix && newPrefix) {
|
|
41
|
+
newCode = changeSelectorPrefix(newCode, oldPrefix, newPrefix);
|
|
42
|
+
}
|
|
43
|
+
if (prefixScoped) {
|
|
44
|
+
newCode = `${newCode}${prefixScoped}{${newCode}}`;
|
|
45
|
+
}
|
|
46
|
+
return newCode;
|
|
47
|
+
}
|
|
48
|
+
return newCode;
|
|
49
|
+
}
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
export {
|
|
53
|
+
addScopedAndReplacePrefixPlugin as default
|
|
54
|
+
};
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
|
|
3
|
+
const virtual = require("../_utils/virtual.cjs");
|
|
4
|
+
const routeGenerator = require("./routeGenerator.cjs");
|
|
5
|
+
function extractGlob(globVal) {
|
|
6
|
+
return globVal.glob || globVal;
|
|
7
|
+
}
|
|
8
|
+
function getEagerOption(globVal, globalEager) {
|
|
9
|
+
return globVal.eager ?? globalEager ?? false;
|
|
10
|
+
}
|
|
11
|
+
function generateImportCode(varName, glob, eager) {
|
|
12
|
+
if (eager) {
|
|
13
|
+
return `const ${varName} = import.meta.glob(${JSON.stringify(glob)}, { eager: true, import: 'default' });
|
|
14
|
+
`;
|
|
15
|
+
}
|
|
16
|
+
return `const ${varName} = import.meta.glob(${JSON.stringify(glob)});
|
|
17
|
+
`;
|
|
18
|
+
}
|
|
19
|
+
function createAutoRoutesPlugin({ routeConfig, virtualModuleId, dts, root, eager: globalEager }) {
|
|
20
|
+
const VIRTUAL_MODULE_ID = virtualModuleId || "virtual:auto-routes";
|
|
21
|
+
const watchGlobs = Object.values(routeConfig).flatMap((globVal) => {
|
|
22
|
+
const g = extractGlob(globVal);
|
|
23
|
+
return Array.isArray(g) ? g : [g];
|
|
24
|
+
});
|
|
25
|
+
return virtual.createVirtualPlugin(
|
|
26
|
+
{
|
|
27
|
+
name: "vite-plugin-auto-routes",
|
|
28
|
+
virtualModuleId: VIRTUAL_MODULE_ID,
|
|
29
|
+
dts,
|
|
30
|
+
root,
|
|
31
|
+
watch: watchGlobs,
|
|
32
|
+
enforce: "pre",
|
|
33
|
+
// 生成虚拟模块代码:仅负责产出字符串,监听/HMR/缓存由工厂统一处理
|
|
34
|
+
generateModule: () => {
|
|
35
|
+
const imports = [];
|
|
36
|
+
const routes = [];
|
|
37
|
+
Object.entries(routeConfig).forEach(([prefix, globVal], index) => {
|
|
38
|
+
const varName = `files${index}`;
|
|
39
|
+
const glob = extractGlob(globVal);
|
|
40
|
+
const eager = getEagerOption(globVal, globalEager);
|
|
41
|
+
const baseRoute = globVal.baseRoute;
|
|
42
|
+
const baseRouteParam = baseRoute !== void 0 ? JSON.stringify(baseRoute) : "undefined";
|
|
43
|
+
imports.push(generateImportCode(varName, glob, eager));
|
|
44
|
+
routes.push(`...generateRoutes(${varName}, '${prefix}',${baseRouteParam}, ${eager})`);
|
|
45
|
+
});
|
|
46
|
+
const routesCode = routes.length > 0 ? `[${routes.join(",\n")}]` : "[]";
|
|
47
|
+
return `
|
|
48
|
+
${imports.join("\n")}
|
|
49
|
+
${routeGenerator.findParentRouteHandle}
|
|
50
|
+
${routeGenerator.parseModulePath}
|
|
51
|
+
${routeGenerator.processComponent}
|
|
52
|
+
${routeGenerator.cleanRoute}
|
|
53
|
+
${routeGenerator.findDefaultRouteHandle}
|
|
54
|
+
|
|
55
|
+
const findParentRoute = ${routeGenerator.findParentRouteHandle}
|
|
56
|
+
// 用于routes
|
|
57
|
+
const generateRoutes = ${routeGenerator.generateRoutes};
|
|
58
|
+
// 用于导出
|
|
59
|
+
const findDefaultRoute = ${routeGenerator.findDefaultRouteHandle};
|
|
60
|
+
|
|
61
|
+
const routes = ${routesCode}.flat().filter(Boolean);
|
|
62
|
+
|
|
63
|
+
export { routes, findDefaultRoute };
|
|
64
|
+
export default routes;
|
|
65
|
+
`;
|
|
66
|
+
},
|
|
67
|
+
// 生成类型声明文件
|
|
68
|
+
generateDts: () => `// 此文件由ViteConfig自动生成,请勿手动修改
|
|
69
|
+
declare module 'virtual:auto-routes' {
|
|
70
|
+
interface RouteModule {
|
|
71
|
+
path: string
|
|
72
|
+
name: string
|
|
73
|
+
meta?: any
|
|
74
|
+
component: () => Promise<any>
|
|
75
|
+
children?: RouteModule[]
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
const routes: RouteModule[]
|
|
79
|
+
const findDefaultRoute: (routes: any[]) => string
|
|
80
|
+
export { findDefaultRoute, routes }
|
|
81
|
+
export default routes
|
|
82
|
+
}`
|
|
83
|
+
}
|
|
84
|
+
);
|
|
85
|
+
}
|
|
86
|
+
exports.default = createAutoRoutesPlugin;
|