@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
package/lib/index.d.ts
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
|
+
import { AutoRoutesConfig, CDNOptions, CompressionPlugin, ImageminPlugin, PluginConfig, PluginType, QiankunPlugin, VisualizerOptions, ViteConfigType } from './src/_types';
|
|
1
2
|
import { createViteConfig, getViteConfig } from './src/index.ts';
|
|
2
3
|
export default createViteConfig;
|
|
3
4
|
export { wrapperEnv } from './src/_utils/index.ts';
|
|
4
5
|
export { getViteConfig, createViteConfig as ViteConfig };
|
|
6
|
+
export type { AutoRoutesConfig, CDNOptions, CompressionPlugin, ImageminPlugin, PluginConfig, PluginType, QiankunPlugin, VisualizerOptions, ViteConfigType };
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
+
const fs = require("node:fs");
|
|
4
|
+
const path = require("node:path");
|
|
5
|
+
const process = require("node:process");
|
|
6
|
+
function detectFramework(config, rootDir) {
|
|
7
|
+
const root = rootDir || process.cwd();
|
|
8
|
+
const packageJsonPath = path.resolve(root, "package.json");
|
|
9
|
+
let vue = config.vue;
|
|
10
|
+
let react = config.react;
|
|
11
|
+
let vitepress = config.vitepress;
|
|
12
|
+
const hasExplicitFramework = vue === true || react === true || vitepress === true;
|
|
13
|
+
if (!hasExplicitFramework) {
|
|
14
|
+
let dependencies = {};
|
|
15
|
+
let devDependencies = {};
|
|
16
|
+
if (fs.existsSync(packageJsonPath)) {
|
|
17
|
+
try {
|
|
18
|
+
const packageJsonContent = fs.readFileSync(packageJsonPath, "utf-8");
|
|
19
|
+
const packageJson = JSON.parse(packageJsonContent);
|
|
20
|
+
dependencies = packageJson.dependencies || {};
|
|
21
|
+
devDependencies = packageJson.devDependencies || {};
|
|
22
|
+
} catch (error) {
|
|
23
|
+
console.warn(`无法读取 package.json: ${packageJsonPath}`, error);
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
if (vue === void 0) {
|
|
27
|
+
if (dependencies.vue || devDependencies.vue) {
|
|
28
|
+
vue = true;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
if (react === void 0 && vue !== true) {
|
|
32
|
+
if (dependencies.react || devDependencies.react) {
|
|
33
|
+
react = true;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
if (vitepress === void 0 && vue !== true && react !== true) {
|
|
37
|
+
if (dependencies.vitepress || devDependencies.vitepress) {
|
|
38
|
+
vitepress = true;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
return {
|
|
43
|
+
vue: vue ?? false,
|
|
44
|
+
react: react ?? false,
|
|
45
|
+
vitepress: vitepress ?? false
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
exports.detectFramework = detectFramework;
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
+
require("node:fs");
|
|
4
|
+
require("node:path");
|
|
5
|
+
require("node:process");
|
|
6
|
+
require("dotenv");
|
|
7
|
+
function wrapperEnv(env) {
|
|
8
|
+
const result = {};
|
|
9
|
+
for (const key in env) {
|
|
10
|
+
if (Object.prototype.hasOwnProperty.call(env, key)) {
|
|
11
|
+
const value = env[key].trim();
|
|
12
|
+
if (value === "true" || value === "false") {
|
|
13
|
+
result[key] = value === "true";
|
|
14
|
+
} else if (!Number.isNaN(Number(value))) {
|
|
15
|
+
result[key] = Number(value);
|
|
16
|
+
} else if (value === "") {
|
|
17
|
+
result[key] = null;
|
|
18
|
+
} else {
|
|
19
|
+
result[key] = value;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
return result;
|
|
24
|
+
}
|
|
25
|
+
exports.wrapperEnv = wrapperEnv;
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
+
function getCamelCase(str) {
|
|
4
|
+
return str.replace(/[-_]+/g, " ").replace(/(?:^|\s)\w/g, (match) => match.toUpperCase()).replace(/\s+/g, "");
|
|
5
|
+
}
|
|
6
|
+
function getCdnModules(modules2) {
|
|
7
|
+
function getPath(str) {
|
|
8
|
+
if (!str)
|
|
9
|
+
return "";
|
|
10
|
+
return str.startsWith("/") ? str : `/${str}`;
|
|
11
|
+
}
|
|
12
|
+
return modules2.map((item) => {
|
|
13
|
+
if (typeof item === "string") {
|
|
14
|
+
return {
|
|
15
|
+
name: item,
|
|
16
|
+
var: getCamelCase(item),
|
|
17
|
+
path: ""
|
|
18
|
+
};
|
|
19
|
+
} else {
|
|
20
|
+
return item;
|
|
21
|
+
}
|
|
22
|
+
}).map((item) => {
|
|
23
|
+
return {
|
|
24
|
+
name: item.name,
|
|
25
|
+
var: item.var || getCamelCase(item.name),
|
|
26
|
+
path: getPath(item.path),
|
|
27
|
+
css: getPath(item.css)
|
|
28
|
+
};
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
const modules = getCdnModules([]);
|
|
32
|
+
exports.modules = modules;
|
|
@@ -0,0 +1,425 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __copyProps = (to, from, except, desc) => {
|
|
9
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
10
|
+
for (let key of __getOwnPropNames(from))
|
|
11
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
12
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
13
|
+
}
|
|
14
|
+
return to;
|
|
15
|
+
};
|
|
16
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
17
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
18
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
19
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
20
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
21
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
22
|
+
mod
|
|
23
|
+
));
|
|
24
|
+
Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
|
|
25
|
+
const path = require("node:path");
|
|
26
|
+
const tailwindcss = require("@tailwindcss/postcss");
|
|
27
|
+
const autoprefixer = require("autoprefixer");
|
|
28
|
+
const vite = require("vite");
|
|
29
|
+
const vitePluginHtml = require("vite-plugin-html");
|
|
30
|
+
require("vue");
|
|
31
|
+
const dynamicImport = require("../_utils/dynamicImport.cjs");
|
|
32
|
+
require("node:fs");
|
|
33
|
+
require("node:process");
|
|
34
|
+
const object = require("../_utils/object.cjs");
|
|
35
|
+
const detectFramework = require("./_utils/detectFramework.cjs");
|
|
36
|
+
const addScopedAndReplacePrefix = require("./plugins/addScopedAndReplacePrefix.cjs");
|
|
37
|
+
async function getViteConfig(Config = {}, params) {
|
|
38
|
+
const config = typeof Config === "function" ? Config(params) : Config;
|
|
39
|
+
const { mode = "base" } = params || {};
|
|
40
|
+
const rootPath = config.rootPath || process.cwd();
|
|
41
|
+
const modeConfig = config.mode || {};
|
|
42
|
+
const baseConfig = modeConfig.base || {};
|
|
43
|
+
const currentModeConfig = modeConfig[mode] || {};
|
|
44
|
+
const viteEnv = { ...baseConfig, ...currentModeConfig };
|
|
45
|
+
const isDev = mode === "development";
|
|
46
|
+
const {
|
|
47
|
+
autoImport = config.autoImport ?? true,
|
|
48
|
+
autoComponent = config.autoComponent ?? true,
|
|
49
|
+
compression = config.compression ?? true,
|
|
50
|
+
imagemin = config.imagemin ?? true,
|
|
51
|
+
codeInspector = config.codeInspector ?? true,
|
|
52
|
+
port = config.port,
|
|
53
|
+
visualizer = config.visualizer ?? false,
|
|
54
|
+
autoRoutes = config.autoRoutes ?? false,
|
|
55
|
+
cdn = config.cdn ?? false,
|
|
56
|
+
pageRoutes = config.pageRoutes ?? false,
|
|
57
|
+
pwa = config.pwa ?? false,
|
|
58
|
+
devtools = config.devtools,
|
|
59
|
+
open = config.open,
|
|
60
|
+
qiankunDevMode = config.qiankunDevMode,
|
|
61
|
+
qiankun = config.qiankun,
|
|
62
|
+
namespace = config.namespace,
|
|
63
|
+
dropConsole = config.dropConsole,
|
|
64
|
+
vue: vueRaw = config.vue,
|
|
65
|
+
react: reactRaw = config.react,
|
|
66
|
+
vitepress: vitepressRaw = config.vitepress
|
|
67
|
+
} = viteEnv;
|
|
68
|
+
const frameworkResult = detectFramework.detectFramework(
|
|
69
|
+
{
|
|
70
|
+
vue: vueRaw,
|
|
71
|
+
react: reactRaw,
|
|
72
|
+
vitepress: vitepressRaw
|
|
73
|
+
},
|
|
74
|
+
rootPath
|
|
75
|
+
);
|
|
76
|
+
const { vue, react, vitepress } = object.validateMutuallyExclusive(
|
|
77
|
+
frameworkResult,
|
|
78
|
+
"vue"
|
|
79
|
+
);
|
|
80
|
+
const appTitle = config.appTitle;
|
|
81
|
+
const appCode = config.appCode;
|
|
82
|
+
const isVueOrVitepress = vue || vitepress;
|
|
83
|
+
const envSystemCode = isDev && !qiankunDevMode ? "el" : namespace ?? appCode;
|
|
84
|
+
const plugins = [
|
|
85
|
+
vitePluginHtml.createHtmlPlugin({
|
|
86
|
+
inject: {
|
|
87
|
+
data: {
|
|
88
|
+
title: appTitle
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
})
|
|
92
|
+
];
|
|
93
|
+
if (vue) {
|
|
94
|
+
const pluginVue = await dynamicImport.dynamicImport(import("@vitejs/plugin-vue"));
|
|
95
|
+
plugins.push(pluginVue());
|
|
96
|
+
}
|
|
97
|
+
if (react) {
|
|
98
|
+
const pluginReact = await dynamicImport.dynamicImport(import("@vitejs/plugin-react"));
|
|
99
|
+
plugins.push(pluginReact());
|
|
100
|
+
}
|
|
101
|
+
if (isVueOrVitepress) {
|
|
102
|
+
const vueJsx = await dynamicImport.dynamicImport(import("@vitejs/plugin-vue-jsx"));
|
|
103
|
+
plugins.push(vueJsx());
|
|
104
|
+
}
|
|
105
|
+
if (pageRoutes) {
|
|
106
|
+
const Pages = await dynamicImport.dynamicImport(import("vite-plugin-pages"));
|
|
107
|
+
const extensions = [];
|
|
108
|
+
if (vue) {
|
|
109
|
+
extensions.push("vue");
|
|
110
|
+
}
|
|
111
|
+
if (react) {
|
|
112
|
+
extensions.push("tsx", "jsx");
|
|
113
|
+
}
|
|
114
|
+
plugins.push(Pages(
|
|
115
|
+
object.deepMerge(
|
|
116
|
+
{
|
|
117
|
+
dirs: "src/pages",
|
|
118
|
+
extensions,
|
|
119
|
+
exclude: [
|
|
120
|
+
"**/components/**",
|
|
121
|
+
"**/__tests__/**"
|
|
122
|
+
]
|
|
123
|
+
},
|
|
124
|
+
pageRoutes
|
|
125
|
+
)
|
|
126
|
+
));
|
|
127
|
+
}
|
|
128
|
+
if (isDev && devtools && isVueOrVitepress) {
|
|
129
|
+
const vueDevTools = await dynamicImport.dynamicImport(import("vite-plugin-vue-devtools"));
|
|
130
|
+
plugins.push(vueDevTools());
|
|
131
|
+
}
|
|
132
|
+
if (autoImport && isVueOrVitepress) {
|
|
133
|
+
const AutoImport = await dynamicImport.dynamicImport(import("unplugin-auto-import/vite"));
|
|
134
|
+
const ElementPlusResolverModule = await dynamicImport.dynamicImport(import("unplugin-vue-components/resolvers"));
|
|
135
|
+
const { ElementPlusResolver } = ElementPlusResolverModule;
|
|
136
|
+
plugins.push(AutoImport(
|
|
137
|
+
object.deepMerge(
|
|
138
|
+
{
|
|
139
|
+
imports: ["vue"],
|
|
140
|
+
resolvers: [ElementPlusResolver()],
|
|
141
|
+
dts: path.resolve(rootPath, "./typings/auto-imports.d.ts")
|
|
142
|
+
},
|
|
143
|
+
autoImport
|
|
144
|
+
)
|
|
145
|
+
));
|
|
146
|
+
}
|
|
147
|
+
if (autoComponent && isVueOrVitepress) {
|
|
148
|
+
const Components = await dynamicImport.dynamicImport(import("unplugin-vue-components/vite"));
|
|
149
|
+
const ElementPlusResolverModule = await dynamicImport.dynamicImport(import("unplugin-vue-components/resolvers"));
|
|
150
|
+
const { ElementPlusResolver } = ElementPlusResolverModule;
|
|
151
|
+
plugins.push(Components(
|
|
152
|
+
object.deepMerge(
|
|
153
|
+
{
|
|
154
|
+
resolvers: [ElementPlusResolver()],
|
|
155
|
+
globs: [],
|
|
156
|
+
dts: path.resolve(rootPath, "./typings/components.d.ts")
|
|
157
|
+
},
|
|
158
|
+
autoComponent
|
|
159
|
+
)
|
|
160
|
+
));
|
|
161
|
+
}
|
|
162
|
+
if (compression) {
|
|
163
|
+
const viteCompression = await dynamicImport.dynamicImport(import("vite-plugin-compression"));
|
|
164
|
+
const compressionPlugin = viteCompression;
|
|
165
|
+
plugins.push(compressionPlugin(
|
|
166
|
+
object.deepMerge(
|
|
167
|
+
{
|
|
168
|
+
algorithm: "brotliCompress",
|
|
169
|
+
verbose: true,
|
|
170
|
+
disable: false,
|
|
171
|
+
ext: ".gz",
|
|
172
|
+
threshold: 10240,
|
|
173
|
+
deleteOriginFile: false
|
|
174
|
+
},
|
|
175
|
+
compression
|
|
176
|
+
)
|
|
177
|
+
));
|
|
178
|
+
}
|
|
179
|
+
if (imagemin) {
|
|
180
|
+
const viteImagemin = await dynamicImport.dynamicImport(import("vite-plugin-imagemin"));
|
|
181
|
+
const imageminPlugin = viteImagemin;
|
|
182
|
+
plugins.push(imageminPlugin(
|
|
183
|
+
object.deepMerge(
|
|
184
|
+
{
|
|
185
|
+
gifsicle: { optimizationLevel: 7, interlaced: false },
|
|
186
|
+
optipng: { optimizationLevel: 7 },
|
|
187
|
+
mozjpeg: { quality: 20 },
|
|
188
|
+
pngquant: { quality: [0.8, 0.9], speed: 4 },
|
|
189
|
+
svgo: {
|
|
190
|
+
plugins: [{ name: "removeViewBox" }, { name: "removeEmptyAttrs", active: false }]
|
|
191
|
+
}
|
|
192
|
+
},
|
|
193
|
+
imagemin
|
|
194
|
+
)
|
|
195
|
+
));
|
|
196
|
+
}
|
|
197
|
+
if (cdn) {
|
|
198
|
+
const importToCDN = await dynamicImport.dynamicImport(import("vite-plugin-cdn-import"));
|
|
199
|
+
const { modules } = await dynamicImport.dynamicImport(Promise.resolve().then(() => require("./constants/index.cjs")));
|
|
200
|
+
plugins.push(importToCDN(
|
|
201
|
+
object.deepMerge(
|
|
202
|
+
{
|
|
203
|
+
enableInDevMode: false,
|
|
204
|
+
prodUrl: "/{name}@{version}{path}",
|
|
205
|
+
modules,
|
|
206
|
+
generateScriptTag: (_name, scriptUrl) => {
|
|
207
|
+
const esmArr = ["esm", ".mjs"];
|
|
208
|
+
const isESM = esmArr.some((item) => scriptUrl.includes(item));
|
|
209
|
+
if (isESM) {
|
|
210
|
+
return {
|
|
211
|
+
attrs: {
|
|
212
|
+
src: scriptUrl,
|
|
213
|
+
type: "module",
|
|
214
|
+
crossorigin: "anonymous"
|
|
215
|
+
},
|
|
216
|
+
injectTo: "head"
|
|
217
|
+
};
|
|
218
|
+
} else {
|
|
219
|
+
return {
|
|
220
|
+
attrs: {
|
|
221
|
+
src: scriptUrl,
|
|
222
|
+
crossorigin: "anonymous"
|
|
223
|
+
},
|
|
224
|
+
injectTo: "head"
|
|
225
|
+
};
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
},
|
|
229
|
+
cdn
|
|
230
|
+
)
|
|
231
|
+
));
|
|
232
|
+
}
|
|
233
|
+
if (visualizer) {
|
|
234
|
+
const visualizerPlugin = await dynamicImport.dynamicImport(import("rollup-plugin-visualizer"));
|
|
235
|
+
plugins.push(visualizerPlugin(
|
|
236
|
+
object.deepMerge(
|
|
237
|
+
{
|
|
238
|
+
open: true
|
|
239
|
+
},
|
|
240
|
+
visualizer
|
|
241
|
+
)
|
|
242
|
+
));
|
|
243
|
+
}
|
|
244
|
+
if (pwa) {
|
|
245
|
+
const pwaModule = await dynamicImport.dynamicImport(import("vite-plugin-pwa"));
|
|
246
|
+
const { VitePWA } = pwaModule;
|
|
247
|
+
plugins.push(VitePWA(
|
|
248
|
+
object.deepMerge(
|
|
249
|
+
{
|
|
250
|
+
strategies: "generateSW",
|
|
251
|
+
registerType: "autoUpdate",
|
|
252
|
+
// 开发模式下也启用
|
|
253
|
+
devOptions: {
|
|
254
|
+
enabled: true
|
|
255
|
+
},
|
|
256
|
+
includeAssets: ["favicon.ico", "apple-touch-icon.png", "mask-icon.svg"],
|
|
257
|
+
manifest: {
|
|
258
|
+
id: appCode ? `/${appCode}/` : "/",
|
|
259
|
+
start_url: appCode ? `/${appCode}/` : "/",
|
|
260
|
+
name: appTitle || "应用",
|
|
261
|
+
short_name: appTitle || "应用",
|
|
262
|
+
description: "渐进式 Web 应用",
|
|
263
|
+
display: "standalone",
|
|
264
|
+
background_color: "#ffffff",
|
|
265
|
+
theme_color: "#BA42BF"
|
|
266
|
+
}
|
|
267
|
+
},
|
|
268
|
+
pwa
|
|
269
|
+
)
|
|
270
|
+
));
|
|
271
|
+
}
|
|
272
|
+
if (isDev && codeInspector) {
|
|
273
|
+
const codeInspectorModule = await dynamicImport.dynamicImport(import("code-inspector-plugin"));
|
|
274
|
+
const { codeInspectorPlugin } = codeInspectorModule;
|
|
275
|
+
plugins.push(codeInspectorPlugin(
|
|
276
|
+
object.deepMerge(
|
|
277
|
+
{
|
|
278
|
+
bundler: "vite",
|
|
279
|
+
showSwitch: true
|
|
280
|
+
},
|
|
281
|
+
codeInspector
|
|
282
|
+
)
|
|
283
|
+
));
|
|
284
|
+
}
|
|
285
|
+
if (qiankun) {
|
|
286
|
+
const qiankunPlugin = await dynamicImport.dynamicImport(import("vite-plugin-qiankun"));
|
|
287
|
+
const qiankunPluginFn = qiankunPlugin;
|
|
288
|
+
plugins.push(qiankunPluginFn(envSystemCode || "el", { useDevMode: qiankunDevMode }));
|
|
289
|
+
if (appCode) {
|
|
290
|
+
plugins.push(addScopedAndReplacePrefix.default({
|
|
291
|
+
prefixScoped: `div[data-qiankun='${envSystemCode}']`,
|
|
292
|
+
oldPrefix: "el",
|
|
293
|
+
newPrefix: appCode,
|
|
294
|
+
useDevMode: qiankunDevMode
|
|
295
|
+
}));
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
if (autoRoutes && isVueOrVitepress) {
|
|
299
|
+
const AutoRoutesPlugin = await dynamicImport.dynamicImport(Promise.resolve().then(() => require("../AutoRoutesPlugin/index.cjs")));
|
|
300
|
+
plugins.push(AutoRoutesPlugin(
|
|
301
|
+
object.deepMerge(
|
|
302
|
+
{
|
|
303
|
+
root: rootPath,
|
|
304
|
+
routeConfig: {
|
|
305
|
+
views: ["/src/views/**/index.vue", "!/src/views/**/components/*"],
|
|
306
|
+
examples: "/src/examples/**/index.vue",
|
|
307
|
+
componentExamples: {
|
|
308
|
+
glob: ["/src/components/**/Example.vue", "!/src/components/**/components/*"],
|
|
309
|
+
baseRoute: "组件示例"
|
|
310
|
+
}
|
|
311
|
+
},
|
|
312
|
+
dts: path.resolve(rootPath, "./typings/auto-routes.d.ts")
|
|
313
|
+
},
|
|
314
|
+
autoRoutes
|
|
315
|
+
)
|
|
316
|
+
));
|
|
317
|
+
}
|
|
318
|
+
const defaultConfig = {
|
|
319
|
+
plugins,
|
|
320
|
+
esbuild: {
|
|
321
|
+
pure: !isDev && dropConsole ? ["console.log", "console.info", "console.debug"] : []
|
|
322
|
+
},
|
|
323
|
+
build: {
|
|
324
|
+
sourcemap: isDev,
|
|
325
|
+
outDir: appCode || "dist",
|
|
326
|
+
cssCodeSplit: true,
|
|
327
|
+
chunkSizeWarningLimit: 1500,
|
|
328
|
+
minify: "esbuild",
|
|
329
|
+
rollupOptions: {
|
|
330
|
+
external: [],
|
|
331
|
+
output: {
|
|
332
|
+
globals: {},
|
|
333
|
+
chunkFileNames: "static/js/[name]-[hash].js",
|
|
334
|
+
entryFileNames: "static/js/[name]-[hash].js",
|
|
335
|
+
assetFileNames: "static/[ext]/[name]-[hash].[ext]",
|
|
336
|
+
manualChunks: (id) => {
|
|
337
|
+
if (id.includes("node_modules")) {
|
|
338
|
+
if (id.includes("lodash-es")) {
|
|
339
|
+
return "lodash-vendor";
|
|
340
|
+
}
|
|
341
|
+
if (id.includes("element-plus")) {
|
|
342
|
+
return "el-vendor";
|
|
343
|
+
}
|
|
344
|
+
if (id.includes("@vue") || id.includes("vue")) {
|
|
345
|
+
return "vue-vendor";
|
|
346
|
+
}
|
|
347
|
+
if (id.includes("antd") || id.includes("@ant-design")) {
|
|
348
|
+
return "antd-vendor";
|
|
349
|
+
}
|
|
350
|
+
if (id.includes("react-dom")) {
|
|
351
|
+
return "react-dom-vendor";
|
|
352
|
+
}
|
|
353
|
+
if (id.includes("react")) {
|
|
354
|
+
return "react-vendor";
|
|
355
|
+
}
|
|
356
|
+
return "vendor";
|
|
357
|
+
}
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
},
|
|
362
|
+
define: {
|
|
363
|
+
__SYSTEM_CODE__: JSON.stringify(envSystemCode),
|
|
364
|
+
process: object.deepMerge({
|
|
365
|
+
env: {
|
|
366
|
+
VUE_APP_VXE_ENV: "production"
|
|
367
|
+
}
|
|
368
|
+
}, process)
|
|
369
|
+
},
|
|
370
|
+
css: {
|
|
371
|
+
preprocessorOptions: {
|
|
372
|
+
scss: {
|
|
373
|
+
api: "modern-compiler"
|
|
374
|
+
}
|
|
375
|
+
},
|
|
376
|
+
postcss: {
|
|
377
|
+
plugins: [tailwindcss(), autoprefixer()]
|
|
378
|
+
},
|
|
379
|
+
devSourcemap: isDev
|
|
380
|
+
},
|
|
381
|
+
resolve: {
|
|
382
|
+
extensions: [".js", ".jsx", ".ts", ".tsx", ".vue"],
|
|
383
|
+
alias: {
|
|
384
|
+
"@": path.resolve(rootPath, "./src")
|
|
385
|
+
}
|
|
386
|
+
},
|
|
387
|
+
server: {
|
|
388
|
+
host: "0.0.0.0",
|
|
389
|
+
port,
|
|
390
|
+
open,
|
|
391
|
+
cors: true,
|
|
392
|
+
proxy: {}
|
|
393
|
+
}
|
|
394
|
+
};
|
|
395
|
+
if (!vitepress && appCode) {
|
|
396
|
+
defaultConfig.base = `/${appCode}`;
|
|
397
|
+
}
|
|
398
|
+
const viteConfig = typeof config.viteConfig === "function" ? config.viteConfig(params) : config.viteConfig;
|
|
399
|
+
const viteConfigPluginNames = ((viteConfig == null ? void 0 : viteConfig.plugins) || []).map((i) => {
|
|
400
|
+
const plugin = Array.isArray(i) ? i[0] : i;
|
|
401
|
+
return plugin == null ? void 0 : plugin.name;
|
|
402
|
+
}).filter((name) => Boolean(name));
|
|
403
|
+
const defaultPluginNamesMap = (defaultConfig.plugins || []).reduce((nameMap, i) => {
|
|
404
|
+
const plugin = Array.isArray(i) ? i[0] : i;
|
|
405
|
+
const name = plugin == null ? void 0 : plugin.name;
|
|
406
|
+
if (name) {
|
|
407
|
+
nameMap[name] = i;
|
|
408
|
+
}
|
|
409
|
+
return nameMap;
|
|
410
|
+
}, {});
|
|
411
|
+
const uniquePlugin = [];
|
|
412
|
+
Object.keys(defaultPluginNamesMap).forEach((name) => {
|
|
413
|
+
if (!viteConfigPluginNames.includes(name)) {
|
|
414
|
+
uniquePlugin.push(defaultPluginNamesMap[name]);
|
|
415
|
+
}
|
|
416
|
+
});
|
|
417
|
+
defaultConfig.plugins = uniquePlugin;
|
|
418
|
+
return vite.mergeConfig(defaultConfig, viteConfig || {});
|
|
419
|
+
}
|
|
420
|
+
function createViteConfig(Config) {
|
|
421
|
+
return vite.defineConfig(async (params) => await getViteConfig(Config, params));
|
|
422
|
+
}
|
|
423
|
+
exports.createViteConfig = createViteConfig;
|
|
424
|
+
exports.default = createViteConfig;
|
|
425
|
+
exports.getViteConfig = getViteConfig;
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
|
|
3
|
+
function changeHtmlClassPrefix(htmlString = "", oldPrefix = "", newPrefix = "") {
|
|
4
|
+
const regex = new RegExp(
|
|
5
|
+
`(class|style)\\s*:\\s*((["']((${oldPrefix}\\b)-).*["'])|((_normalizeClass|_normalizeStyle)\\(.*(${oldPrefix}\\b)-.*\\)))`,
|
|
6
|
+
"g"
|
|
7
|
+
);
|
|
8
|
+
return htmlString.replace(regex, (match = "") => {
|
|
9
|
+
return match.replace(oldPrefix, newPrefix);
|
|
10
|
+
});
|
|
11
|
+
}
|
|
12
|
+
function changeSelectorPrefix(cssString = "", oldPrefix = "", newPrefix = "") {
|
|
13
|
+
const regex = new RegExp(`(\\.${oldPrefix}\\b|#${oldPrefix}\\b|--${oldPrefix}\\b)`, "g");
|
|
14
|
+
return cssString.replace(regex, (match = "") => {
|
|
15
|
+
return match.replace(oldPrefix, newPrefix);
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
function addScopedAndReplacePrefixPlugin({
|
|
19
|
+
prefixScoped = "",
|
|
20
|
+
oldPrefix = "",
|
|
21
|
+
newPrefix = "",
|
|
22
|
+
useDevMode = false
|
|
23
|
+
}) {
|
|
24
|
+
let isProduction;
|
|
25
|
+
return {
|
|
26
|
+
name: "addScopedAndReplacePrefix",
|
|
27
|
+
configResolved(config) {
|
|
28
|
+
isProduction = config.command === "build" || config.isProduction;
|
|
29
|
+
},
|
|
30
|
+
transform(code = "", id = "") {
|
|
31
|
+
if (!isProduction && !useDevMode)
|
|
32
|
+
return code;
|
|
33
|
+
if (!oldPrefix || !newPrefix)
|
|
34
|
+
return code;
|
|
35
|
+
if (id.includes("node_modules"))
|
|
36
|
+
return code;
|
|
37
|
+
const cssLangs = ["css", "scss", "less", "stylus", "styl"];
|
|
38
|
+
let newCode = code;
|
|
39
|
+
if (id.endsWith(".vue")) {
|
|
40
|
+
newCode = changeHtmlClassPrefix(newCode, oldPrefix, newPrefix);
|
|
41
|
+
} else if (cssLangs.some((lang) => id.endsWith(`.${lang}`))) {
|
|
42
|
+
if (oldPrefix && newPrefix) {
|
|
43
|
+
newCode = changeSelectorPrefix(newCode, oldPrefix, newPrefix);
|
|
44
|
+
}
|
|
45
|
+
if (prefixScoped) {
|
|
46
|
+
newCode = `${newCode}${prefixScoped}{${newCode}}`;
|
|
47
|
+
}
|
|
48
|
+
return newCode;
|
|
49
|
+
}
|
|
50
|
+
return newCode;
|
|
51
|
+
}
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
exports.default = addScopedAndReplacePrefixPlugin;
|
package/package.json
CHANGED
|
@@ -1,37 +1,37 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@moluoxixi/vite-config",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.35",
|
|
4
4
|
"description": "ViteConfig 组件",
|
|
5
5
|
"sideEffects": [
|
|
6
6
|
"*.css",
|
|
7
7
|
"*.scss"
|
|
8
8
|
],
|
|
9
9
|
"peerDependencies": {
|
|
10
|
-
"vite": "6.2.4",
|
|
11
|
-
"vue": "3.5.18"
|
|
10
|
+
"vite": "^6.2.4",
|
|
11
|
+
"vue": "^3.5.18"
|
|
12
12
|
},
|
|
13
13
|
"dependencies": {
|
|
14
|
-
"dotenv": "16.6.1",
|
|
15
|
-
"@tailwindcss/postcss": "4.1.11",
|
|
16
|
-
"@vitejs/plugin-react": "4.7.0",
|
|
17
|
-
"@vitejs/plugin-vue": "5.2.4",
|
|
18
|
-
"@vitejs/plugin-vue-jsx": "4.2.0",
|
|
19
|
-
"autoprefixer": "10.4.21",
|
|
20
|
-
"code-inspector-plugin": "1.3.3",
|
|
21
|
-
"rollup-plugin-visualizer": "5.14.0",
|
|
22
|
-
"unplugin-auto-import": "19.3.0",
|
|
23
|
-
"unplugin-vue-components": "28.8.0",
|
|
24
|
-
"vite-plugin-cdn-import": "1.0.1",
|
|
25
|
-
"vite-plugin-compression": "0.5.1",
|
|
26
|
-
"vite-plugin-html": "3.2.2",
|
|
27
|
-
"vite-plugin-imagemin": "0.6.1",
|
|
28
|
-
"vite-plugin-pages": "0.33.2",
|
|
29
|
-
"vite-plugin-pwa": "1.2.0",
|
|
30
|
-
"vite-plugin-qiankun": "1.0.15",
|
|
31
|
-
"vite-plugin-vue-devtools": "7.7.7",
|
|
32
|
-
"lodash-es": "4.17.21",
|
|
33
|
-
"postcss": "8.5.6",
|
|
34
|
-
"postcss-selector-parser": "7.1.0"
|
|
14
|
+
"dotenv": "^16.6.1",
|
|
15
|
+
"@tailwindcss/postcss": "^4.1.11",
|
|
16
|
+
"@vitejs/plugin-react": "^4.7.0",
|
|
17
|
+
"@vitejs/plugin-vue": "^5.2.4",
|
|
18
|
+
"@vitejs/plugin-vue-jsx": "^4.2.0",
|
|
19
|
+
"autoprefixer": "^10.4.21",
|
|
20
|
+
"code-inspector-plugin": "^1.3.3",
|
|
21
|
+
"rollup-plugin-visualizer": "^5.14.0",
|
|
22
|
+
"unplugin-auto-import": "^19.3.0",
|
|
23
|
+
"unplugin-vue-components": "^28.8.0",
|
|
24
|
+
"vite-plugin-cdn-import": "^1.0.1",
|
|
25
|
+
"vite-plugin-compression": "^0.5.1",
|
|
26
|
+
"vite-plugin-html": "^3.2.2",
|
|
27
|
+
"vite-plugin-imagemin": "^0.6.1",
|
|
28
|
+
"vite-plugin-pages": "^0.33.2",
|
|
29
|
+
"vite-plugin-pwa": "^1.2.0",
|
|
30
|
+
"vite-plugin-qiankun": "^1.0.15",
|
|
31
|
+
"vite-plugin-vue-devtools": "^7.7.7",
|
|
32
|
+
"lodash-es": "^4.17.21",
|
|
33
|
+
"postcss": "^8.5.6",
|
|
34
|
+
"postcss-selector-parser": "^7.1.0"
|
|
35
35
|
},
|
|
36
36
|
"publishConfig": {
|
|
37
37
|
"access": "public"
|