2020-04-28 02:16:41 +08:00
|
|
|
|
const path = require("path");
|
2020-05-03 15:10:01 +08:00
|
|
|
|
const cdnDependencies = require("./dependencies-cdn");
|
2020-05-06 20:45:18 +08:00
|
|
|
|
const BuildAppJSPlugin = require("./buildAppJSPlugin");
|
2020-05-31 01:02:42 +08:00
|
|
|
|
const CompressionWebpackPlugin = require("compression-webpack-plugin");
|
2020-06-29 22:45:58 +08:00
|
|
|
|
const { set } = require("lodash");
|
2020-04-24 19:43:00 +08:00
|
|
|
|
|
2020-04-28 02:16:41 +08:00
|
|
|
|
function resolve(dir) {
|
|
|
|
|
return path.join(__dirname, dir);
|
2020-04-24 19:43:00 +08:00
|
|
|
|
}
|
2020-04-28 19:02:48 +08:00
|
|
|
|
|
2020-05-14 03:24:35 +08:00
|
|
|
|
// 增加环境变量
|
2020-05-31 01:02:42 +08:00
|
|
|
|
process.env.VUE_APP_VERSION = require("./package.json").version;
|
|
|
|
|
process.env.VUE_APP_G2INDEX_VERSION = require("./package.json").g2index;
|
|
|
|
|
|
|
|
|
|
process.env.VUE_APP_CDN_PATH =
|
|
|
|
|
process.env.VUE_APP_CDN_PATH.replace(
|
|
|
|
|
"@master",
|
2020-05-31 02:03:23 +08:00
|
|
|
|
"@v" + process.env.VUE_APP_VERSION
|
2020-05-31 01:02:42 +08:00
|
|
|
|
) || "/";
|
2020-05-14 03:24:35 +08:00
|
|
|
|
|
2020-04-28 19:02:48 +08:00
|
|
|
|
// 基础路径 注意发布之前要先修改这里
|
2020-05-22 02:11:32 +08:00
|
|
|
|
let publicPath = process.env.VUE_APP_CDN_PATH || "/";
|
2020-05-31 01:02:42 +08:00
|
|
|
|
let cdnPath = process.env.VUE_APP_CDN_PATH;
|
|
|
|
|
const isProd = process.env.NODE_ENV === "production";
|
2020-04-28 19:02:48 +08:00
|
|
|
|
|
|
|
|
|
// 设置不参与构建的库
|
2020-05-03 15:10:01 +08:00
|
|
|
|
let externals = {};
|
|
|
|
|
cdnDependencies.forEach((item) => {
|
|
|
|
|
externals[item.name] = item.library;
|
|
|
|
|
});
|
2020-04-28 19:02:48 +08:00
|
|
|
|
|
|
|
|
|
// 引入文件的 cdn 链接
|
|
|
|
|
const cdn = {
|
2020-05-03 15:10:01 +08:00
|
|
|
|
css: cdnDependencies.map((e) => e.css).filter((e) => e),
|
|
|
|
|
js: cdnDependencies.map((e) => e.js).filter((e) => e),
|
|
|
|
|
};
|
2020-04-24 19:43:00 +08:00
|
|
|
|
module.exports = {
|
2020-04-28 19:02:48 +08:00
|
|
|
|
publicPath,
|
2020-04-28 02:16:41 +08:00
|
|
|
|
lintOnSave: true,
|
2020-05-31 01:02:42 +08:00
|
|
|
|
css: {
|
|
|
|
|
loaderOptions: {
|
|
|
|
|
// 设置 scss 公用变量文件
|
|
|
|
|
sass: {
|
|
|
|
|
prependData: `$cdnPath: "${isProd ? cdnPath : "/"}";`,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
2020-05-03 15:10:01 +08:00
|
|
|
|
configureWebpack: (config) => {
|
2020-05-31 01:02:42 +08:00
|
|
|
|
const configNew = {};
|
|
|
|
|
if (isProd) {
|
|
|
|
|
configNew.externals = externals;
|
2020-05-14 03:24:35 +08:00
|
|
|
|
configNew.plugins = [
|
|
|
|
|
// gzip
|
|
|
|
|
new CompressionWebpackPlugin({
|
2020-05-31 01:02:42 +08:00
|
|
|
|
filename: "[path].gz[query]",
|
|
|
|
|
test: new RegExp("\\.(" + ["js", "css"].join("|") + ")$"),
|
2020-05-14 03:24:35 +08:00
|
|
|
|
threshold: 10240,
|
|
|
|
|
minRatio: 0.8,
|
2020-05-31 01:02:42 +08:00
|
|
|
|
deleteOriginalAssets: false,
|
|
|
|
|
}),
|
|
|
|
|
];
|
2020-04-28 19:02:48 +08:00
|
|
|
|
}
|
2020-05-31 01:02:42 +08:00
|
|
|
|
return configNew;
|
2020-04-28 19:02:48 +08:00
|
|
|
|
},
|
2020-05-02 20:54:21 +08:00
|
|
|
|
|
2020-04-28 02:16:41 +08:00
|
|
|
|
chainWebpack: (config) => {
|
2020-05-06 20:45:18 +08:00
|
|
|
|
config.plugin("BuildAppJSPlugin").use(BuildAppJSPlugin);
|
2020-04-28 19:02:48 +08:00
|
|
|
|
/**
|
|
|
|
|
* 添加 CDN 参数到 htmlWebpackPlugin 配置中
|
|
|
|
|
*/
|
2020-06-29 22:45:58 +08:00
|
|
|
|
config.plugin("html").tap((options) => {
|
2020-05-31 01:02:42 +08:00
|
|
|
|
if (isProd) {
|
2020-06-29 22:45:58 +08:00
|
|
|
|
set(options, "[0].cdn", cdn);
|
2020-04-28 19:02:48 +08:00
|
|
|
|
} else {
|
2020-06-29 22:45:58 +08:00
|
|
|
|
set(options, "[0].cdn", {
|
2020-05-14 03:24:35 +08:00
|
|
|
|
js: cdnDependencies.filter((e) => e.name === "").map((e) => e.js),
|
2020-05-06 20:45:18 +08:00
|
|
|
|
css: cdnDependencies.filter((e) => e.name === "").map((e) => e.css),
|
2020-06-29 22:45:58 +08:00
|
|
|
|
});
|
2020-04-28 19:02:48 +08:00
|
|
|
|
}
|
2020-06-29 22:45:58 +08:00
|
|
|
|
set(options, "[0].inject", false);
|
|
|
|
|
return options;
|
2020-05-03 15:10:01 +08:00
|
|
|
|
});
|
2020-06-29 22:45:58 +08:00
|
|
|
|
/**
|
|
|
|
|
* 删除懒加载模块的 prefetch preload,降低带宽压力
|
|
|
|
|
* https://cli.vuejs.org/zh/guide/html-and-static-assets.html#prefetch
|
|
|
|
|
* https://cli.vuejs.org/zh/guide/html-and-static-assets.html#preload
|
|
|
|
|
* 而且预渲染时生成的 prefetch 标签是 modern 版本的,低版本浏览器是不需要的
|
|
|
|
|
*/
|
|
|
|
|
if (isProd) {
|
|
|
|
|
config.plugins.delete("prefetch").delete("preload");
|
|
|
|
|
}
|
2020-05-14 03:24:35 +08:00
|
|
|
|
// 解决 cli3 热更新失效 https://github.com/vuejs/vue-cli/issues/1559
|
2020-05-31 01:02:42 +08:00
|
|
|
|
config.resolve.symlinks(true);
|
2020-04-28 02:16:41 +08:00
|
|
|
|
config.resolve.alias
|
|
|
|
|
.set("@", resolve("src"))
|
|
|
|
|
.set("@assets", resolve("src/assets"))
|
|
|
|
|
.set("@utils", resolve("src/utils"))
|
2020-05-30 19:28:20 +08:00
|
|
|
|
.set("@api", resolve("src/api"))
|
2020-04-28 02:16:41 +08:00
|
|
|
|
.set("@node_modules", resolve("node_modules"));
|
2020-04-28 19:02:48 +08:00
|
|
|
|
|
|
|
|
|
// 分析工具
|
|
|
|
|
if (process.env.npm_config_report) {
|
|
|
|
|
config
|
2020-05-03 15:10:01 +08:00
|
|
|
|
.plugin("webpack-bundle-analyzer")
|
|
|
|
|
.use(require("webpack-bundle-analyzer").BundleAnalyzerPlugin);
|
2020-04-28 19:02:48 +08:00
|
|
|
|
}
|
2020-04-28 02:16:41 +08:00
|
|
|
|
},
|
2020-05-02 20:54:21 +08:00
|
|
|
|
|
2020-04-28 19:02:48 +08:00
|
|
|
|
// 不输出 map 文件
|
|
|
|
|
productionSourceMap: false,
|
2020-05-02 20:54:21 +08:00
|
|
|
|
|
2020-04-28 02:16:41 +08:00
|
|
|
|
devServer: {
|
2020-04-28 19:02:48 +08:00
|
|
|
|
publicPath,
|
2020-04-28 02:16:41 +08:00
|
|
|
|
proxy: {
|
|
|
|
|
"/api": {
|
|
|
|
|
target: "https://ossdev.achirou.workers.dev/",
|
|
|
|
|
ws: true,
|
|
|
|
|
changeOrigin: true,
|
|
|
|
|
pathRewrite: {
|
|
|
|
|
"^/api": "",
|
|
|
|
|
},
|
|
|
|
|
},
|
2020-04-24 19:43:00 +08:00
|
|
|
|
},
|
2020-04-28 02:16:41 +08:00
|
|
|
|
},
|
2020-05-02 20:54:21 +08:00
|
|
|
|
|
|
|
|
|
pluginOptions: {
|
|
|
|
|
i18n: {
|
2020-05-03 15:10:01 +08:00
|
|
|
|
locale: "zh-chs",
|
|
|
|
|
fallbackLocale: "en",
|
|
|
|
|
localeDir: "locales",
|
|
|
|
|
enableInSFC: true,
|
|
|
|
|
},
|
|
|
|
|
},
|
2020-05-02 20:54:21 +08:00
|
|
|
|
};
|