vue-study-template/vue.config.js

133 lines
3.7 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

const path = require("path");
function resolve(dir) {
return path.join(__dirname, dir);
}
const isProduction = process.env.NODE_ENV === "production";
const EXTERNALS = {
vue: "Vue",
axios: "axios",
"element-ui": "ELEMENT",
"vue-router": "VueRouter",
vuex: "Vuex",
vant: "vant",
// "vuex-persistedstate": "createPersistedState",
lodash: "_",
dayjs: "dayjs",
echarts: "echarts",
};
const CDN_LIST = {
css: ["https://cdn.staticfile.org/element-ui/2.15.1/theme-chalk/index.min.css", "https://cdn.staticfile.org/vant/2.12.13/index.min.css"],
js: [
"https://cdn.staticfile.org/vue/2.6.12/vue.min.js",
"https://cdn.staticfile.org/vue-router/3.5.1/vue-router.min.js",
"https://cdn.staticfile.org/vuex/3.6.2/vuex.min.js",
"https://cdn.staticfile.org/axios/0.21.1/axios.min.js",
"https://cdn.staticfile.org/element-ui/2.15.1/index.min.js",
"https://cdn.staticfile.org/vant/2.12.13/vant.min.js",
// "https://cdn.staticfile.org/vuex-persistedstate/4.0.0-beta.3/vuex-persistedstate.min.js",
"https://cdn.staticfile.org/lodash.js/4.17.21/lodash.min.js",
"https://cdn.staticfile.org/dayjs/1.10.4/dayjs.min.js",
"https://cdn.staticfile.org/echarts/5.1.2/echarts.min.js",
],
};
module.exports = {
publicPath: process.env.VUE_APP_PUBLIC_PATH || "/",
devServer: {
proxy: {
"/apis": {
target: "https://api.yyjishu.com/antifraud",
changeOrigin: true,
pathRewrite: {
"^/apis": "",
},
},
},
publicPath: process.env.VUE_APP_PUBLIC_PATH || "/",
disableHostCheck: !isProduction, // 关闭 host check方便使用 ngrok 之类的内网转发工具
},
chainWebpack: (config) => {
// 设置一些常用alias
config.resolve.alias
.set("@", resolve("src"))
.set("@assets", resolve("./src/assets"))
.set("@components", resolve("src/components"))
.set("@styles", resolve("src/styles"))
.set("@layout", resolve("src/components/common/layout"))
.set("@views", resolve("src/views"));
config.plugins.delete("prefetch");
// 压缩图片
config.module
.rule("images")
.use("image-webpack-loader")
.loader("image-webpack-loader")
.options({ bypassOnDebug: true })
.end();
if (isProduction) {
// 通过 html-webpack-plugin 将 cdn 注入到 index.html 之中
config.externals(EXTERNALS);
config.plugin("html").tap((args) => {
args[0].cdn = CDN_LIST;
return args;
});
}
},
configureWebpack: (config) => {
if (isProduction) {
// 公共代码抽离
config.optimization = {
splitChunks: {
cacheGroups: {
vendor: {
chunks: "all",
test: /node_modules/,
name: "vendor",
minChunks: 1,
maxInitialRequests: 5,
minSize: 0,
priority: 100,
},
common: {
chunks: "all",
test: /[\\/]src[\\/]js[\\/]/,
name: "common",
minChunks: 2,
maxInitialRequests: 5,
minSize: 0,
priority: 60,
},
styles: {
name: "styles",
test: /\.(sa|sc|c)ss$/,
chunks: "all",
enforce: true,
},
runtimeChunk: {
name: "manifest",
},
},
},
};
}
},
css: {
loaderOptions: {
scss: {
additionalData: `
@import "./src/styles/mixin";
@import "./src/styles/_var";
@import "./src/styles/all.scss";`,
},
postcss: {
plugins: [require("postcss-px2rem")({ remUnit: 32 })],
},
},
},
productionSourceMap: false,
};