2021-03-09 18:10:43 +08:00
|
|
|
const HardSourceWebpackPlugin = require("hard-source-webpack-plugin");
|
2021-03-29 00:10:32 +08:00
|
|
|
const path = require("path");
|
|
|
|
|
|
|
|
function resolve(dir) {
|
|
|
|
return path.join(__dirname, dir);
|
|
|
|
}
|
|
|
|
|
2021-02-28 01:55:04 +08:00
|
|
|
const isProduction = process.env.NODE_ENV === "production";
|
|
|
|
|
2021-03-09 18:10:43 +08:00
|
|
|
// 代理列表
|
|
|
|
const PROXY_LIST = {
|
|
|
|
"/dev": {
|
|
|
|
target: "http://127.0.0.1:8001",
|
|
|
|
changeOrigin: true,
|
|
|
|
pathRewrite: {
|
|
|
|
"^/dev": ""
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
"/pro": {
|
|
|
|
target: "https://show.cool-admin.com",
|
|
|
|
changeOrigin: true,
|
|
|
|
pathRewrite: {
|
|
|
|
"^/pro": "/api"
|
|
|
|
}
|
|
|
|
}
|
2021-03-12 21:46:15 +08:00
|
|
|
};
|
2021-02-28 01:55:04 +08:00
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
publicPath: "/",
|
|
|
|
lintOnSave: true,
|
|
|
|
productionSourceMap: false,
|
|
|
|
parallel: require("os").cpus().length > 1,
|
|
|
|
|
|
|
|
css: {
|
|
|
|
extract: isProduction,
|
|
|
|
sourceMap: false,
|
|
|
|
loaderOptions: {
|
|
|
|
sass: {
|
|
|
|
prependData: `@import "@/assets/css/common.scss";`
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
devServer: {
|
|
|
|
port: 9000,
|
|
|
|
open: false,
|
|
|
|
compress: false,
|
|
|
|
overlay: {
|
|
|
|
warnings: false,
|
|
|
|
errors: true
|
|
|
|
},
|
|
|
|
disableHostCheck: true,
|
2021-03-09 18:10:43 +08:00
|
|
|
proxy: PROXY_LIST
|
2021-02-28 01:55:04 +08:00
|
|
|
},
|
|
|
|
|
2021-03-09 18:10:43 +08:00
|
|
|
chainWebpack: config => {
|
2021-03-11 16:04:52 +08:00
|
|
|
// 设置环境变量
|
2021-03-12 21:46:15 +08:00
|
|
|
config.plugin("define").tap(args => {
|
|
|
|
args[0]["process.env"].PROXY_LIST = JSON.stringify(PROXY_LIST);
|
|
|
|
return args;
|
|
|
|
});
|
2021-03-11 16:04:52 +08:00
|
|
|
|
|
|
|
// 设置 svg
|
2021-03-29 00:10:32 +08:00
|
|
|
const svgRule = config.module.rule("svg");
|
2021-02-28 01:55:04 +08:00
|
|
|
|
2021-03-29 00:10:32 +08:00
|
|
|
svgRule.uses.clear();
|
|
|
|
svgRule
|
2021-02-28 01:55:04 +08:00
|
|
|
.use("svg-sprite-loader")
|
|
|
|
.loader("svg-sprite-loader")
|
|
|
|
.options({
|
|
|
|
symbolId: "[name]"
|
2021-03-29 00:10:32 +08:00
|
|
|
})
|
|
|
|
.end();
|
2021-02-28 01:55:04 +08:00
|
|
|
|
2021-03-11 16:04:52 +08:00
|
|
|
// 生产模式下
|
|
|
|
if (isProduction) {
|
|
|
|
// 移除 prefetch 插件
|
|
|
|
config.plugins.delete("prefetch-index");
|
2021-02-28 01:55:04 +08:00
|
|
|
|
2021-03-11 16:04:52 +08:00
|
|
|
// 移除 preload 插件,避免加载多余的资源
|
|
|
|
config.plugins.delete("preload-index");
|
2021-02-28 01:55:04 +08:00
|
|
|
|
2021-03-12 21:46:15 +08:00
|
|
|
config.optimization.minimizer("terser").tap(args => {
|
2021-03-11 16:04:52 +08:00
|
|
|
// 去掉注释
|
|
|
|
args[0].terserOptions.output = {
|
|
|
|
comments: false
|
|
|
|
};
|
2021-03-12 21:46:15 +08:00
|
|
|
return args;
|
|
|
|
});
|
2021-02-28 01:55:04 +08:00
|
|
|
|
2021-03-11 16:04:52 +08:00
|
|
|
// 分割模块
|
2021-02-28 01:55:04 +08:00
|
|
|
config.optimization.splitChunks({
|
2021-03-12 21:46:15 +08:00
|
|
|
chunks: "all",
|
2021-03-11 16:04:52 +08:00
|
|
|
maxInitialRequests: Infinity,
|
|
|
|
minSize: 300000,
|
2021-03-12 21:46:15 +08:00
|
|
|
automaticNameDelimiter: "-",
|
2021-02-28 01:55:04 +08:00
|
|
|
cacheGroups: {
|
|
|
|
vendor: {
|
2021-03-11 16:04:52 +08:00
|
|
|
test: /[\\/]node_modules[\\/]/,
|
|
|
|
name(module) {
|
2021-03-12 21:46:15 +08:00
|
|
|
const packageName = module.context.match(
|
|
|
|
/[\\/]node_modules[\\/](.*?)([\\/]|$)/
|
|
|
|
)[1];
|
|
|
|
return `chunk.${packageName.replace("@", "")}`;
|
2021-03-11 16:04:52 +08:00
|
|
|
},
|
|
|
|
priority: 10
|
2021-02-28 01:55:04 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2021-03-09 18:10:43 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
configureWebpack: config => {
|
2021-03-11 16:04:52 +08:00
|
|
|
// 构建缓存
|
|
|
|
config.plugins.push(new HardSourceWebpackPlugin());
|
2021-02-28 01:55:04 +08:00
|
|
|
}
|
|
|
|
};
|