forked from PangHu-Code/EZ-Theme
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvue.config.js
More file actions
143 lines (124 loc) · 4.72 KB
/
vue.config.js
File metadata and controls
143 lines (124 loc) · 4.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
const { defineConfig } = require("@vue/cli-service");
const path = require("path");
const fs = require("fs");
const webpack = require("webpack");
const TerserPlugin = require("terser-webpack-plugin");
const JavaScriptObfuscator = require("javascript-obfuscator");
const isProd = process.env.NODE_ENV === "production";
const enableConfigJS = process.env.VUE_APP_CONFIGJS == "true";
const enableObfuscation = process.env.VUE_APP_OBFUSCATION == "true";
let extraScriptFileName = '';
const generateRandomFileName = (length = 8) => {
const chars = "abcdefghijklmnopqrstuvwxyz0123456789";
let name = "";
for (let i = 0; i < length; i++) {
name += chars.charAt(Math.floor(Math.random() * chars.length));
}
const randowNumber = Math.floor(Math.random() * 1000).toString().padStart(3, "0")
return `${randowNumber}.${name}.js`;
};
if (isProd && enableConfigJS) {
extraScriptFileName = generateRandomFileName();
}
module.exports = defineConfig({
publicPath: "./",
outputDir: "dist",
assetsDir: "static",
lintOnSave: false,
productionSourceMap: false,
configureWebpack: (config) => {
config.experiments = { ...config.experiments, asyncWebAssembly: true, syncWebAssembly: true };
config.resolve = { ...config.resolve, alias: { "@": path.resolve(__dirname, "src") } };
config.module.rules.push({
test: /\.mjs$/,
include: /node_modules/,
type: "javascript/auto"
});
config.plugins.push(
new webpack.DefinePlugin({
__VUE_OPTIONS_API__: JSON.stringify(true),
__VUE_PROD_DEVTOOLS__: JSON.stringify(false),
__VUE_PROD_HYDRATION_MISMATCH_DETAILS__: JSON.stringify(false),
})
);
if (isProd && enableConfigJS) {
config.plugins.push({
apply: (compiler) => {
compiler.hooks.afterEmit.tap("GenerateExtraConfigPlugin", () => {
const configPath = path.resolve(__dirname, "src/config/index.js");
const distPath = path.resolve(compiler.options.output.path, extraScriptFileName);
try {
let content = fs.readFileSync(configPath, "utf-8");
content = content.replace(/window\.EZ_CONFIG\s*=\s*config\s*;?/g, "");
content = content.replace(/export\s+const\s+config\s*=/, "window.EZ_CONFIG =");
const obfuscated = JavaScriptObfuscator.obfuscate(content, {
compact: true,
controlFlowFlattening: true,
controlFlowFlatteningThreshold: 0.75,
numbersToExpressions: true,
simplify: true,
stringArray: true,
stringArrayEncoding: ["rc4"],
stringArrayThreshold: 0.75,
transformObjectKeys: true,
unicodeEscapeSequence: true
}).getObfuscatedCode();
const fileContent = enableObfuscation ? obfuscated : content;
// 写入 dist
fs.writeFileSync(distPath, fileContent, "utf-8");
console.log(`生成混淆独立 JS 文件: ${extraScriptFileName}`);
} catch (err) {
console.warn("生成独立 JS 文件失败:", err);
}
});
},
});
}
if (isProd) {
config.optimization = {
...config.optimization,
splitChunks: {
chunks: "all",
cacheGroups: {
vendors: { name: "chunk-vendors", test: /[\\/]node_modules[\\/]/, priority: -10, chunks: "initial" },
common: { name: "chunk-common", minChunks: 2, priority: -20, chunks: "initial", reuseExistingChunk: true },
},
},
minimize: true,
minimizer: [
new TerserPlugin({
terserOptions: { compress: { drop_console: true, drop_debugger: true }, mangle: true, format: { comments: false, ascii_only: true } },
extractComments: false,
}),
],
};
}
},
chainWebpack: (config) => {
if (isProd) {
const pluginName = "html-index";
config.plugin(pluginName).tap((args) => {
args[0].templateParameters = {
...args[0].templateParameters,
injectCustomScript: `
${enableConfigJS ? `<script src="./${extraScriptFileName}"></script>` : ""}
`,
};
return args;
});
}
},
css: {
loaderOptions: {
sass: {
implementation: require("sass"),
sassOptions: { outputStyle: "expanded", fiber: false, indentedSyntax: false, includePaths: ["node_modules"] },
additionalData: `@use "@/assets/styles/base/variables.scss" as *;`,
},
},
},
pages: {
index: { entry: "src/main.js", template: "public/index.html", filename: "index.html", title: process.env.VUE_APP_TITLE },
},
devServer: { client: { overlay: false } },
});