一、基础配置
let { version, openGzip } = require("./package.json"); // 引入版本号和是否开启Gzip压缩const path = require("path"); // 引入path路径module.exports = { publicPath: "./", // 输出基本路径 outputDir: "dist", // 输出文件目录 indexPath: "index.html", // index.html 的输出路径 (相对于 outputDir) lintOnSave: false, // eslint-loader 是否在保存的时候检查 // webpack配置 chainWebpack: (config) => {}, configureWebpack: (config) => { if (process.env.NODE_ENV === "production") { // 为生产环境修改配置... config.mode = "production"; // 利用splitChunks将每个依赖包单独打包,在生产环境下配置 // npm install --save-dev add-asset-html-webpack-plugin config.optimization = { runtimeChunk: "single", splitChunks: { chunks: "all", maxInitialRequests: Infinity, minSize: 20000, // 依赖包超过20000bit将被单独打包 cacheGroups: { vendor: { test: /[\\/]node_modules[\\/]/, name(module) { // get the name. E.g. node_modules/packageName/not/this/part.js or node_modules/packageName const packageName = module.context.match( /[\\/]node_modules[\\/](.*?)([\\/]|$)/ )[1]; // npm package names are URL-safe, but some servers don't like @ symbols return `npm.${packageName.replace("@", "")}`; }, }, }, }, minimizer: [ new UglifyPlugin({ uglifyOptions: { warnings: false, compress: { drop_console: true, // console drop_debugger: false, pure_funcs: ["console.log"], // 移除console }, }, }), ], }; Object.assign(config, { // 配置版本号 output: { ...config.output, filename: `static/js/[name].[chunkhash].${version}.js`, chunkFilename: `static/js/[name].[chunkhash].${version}.js`, }, // 开发生产共同配置 resolve: { alias: { "@": path.resolve(__dirname, "./src"), "@c": path.resolve(__dirname, "./src/components"), "@p": path.resolve(__dirname, "./src/pages"), }, // 别名配置 }, }); // 安装gzip压缩 npm install -D compression-webpack-plugin if (openGzip) { config.plugins = [ ...config.plugins, new CompressionPlugin({ test: /\.js$|\.html$|.\css/, //匹配文件名 threshold: 10240, //对超过10k的数据压缩 deleteOriginalAssets: false, //不删除源文件 }), ]; } // 外部扩展引入,同时需要在index.html中通过<script>引入对应的cdn链接 config.externals = { AMap: "AMap", vue: "Vue", "vue-router": "VueRouter", axios: "axios", }; } else { // 为开发环境修改配置... config.mode = "development"; } }, productionSourceMap: false, // 生产环境是否生成 sourceMap 文件 // css相关配置 css: { extract: true, // 是否使用css分离插件 ExtractTextPlugin sourceMap: false, // 开启 CSS source maps? loaderOptions: { css: {}, // 这里的选项会传递给 css-loader postcss: { // 配置pxtorem,需要npm安装pxtorem plugins: [ require("postcss-pxtorem")({ //这里是配置项,详见官方文档 rootValue: 100, // 换算的基数 selectorBlackList: ["weui", "chart"], // 忽略转换正则匹配项,以这些开头的clss类名不会转化 propList: ["*"], exclude: /node_modules, /, //对该路径下的不去替换 }), ], }, // 这里的选项会传递给 postcss-loader }, // css预设器配置项 详见https://cli.vuejs.org/zh/config/#css-loaderoptions modules: false, // 启用 CSS modules for all css / pre-processor files. }, parallel: require("os").cpus().length > 1, // 是否为 Babel 或 TypeScript 使用 thread-loader。该选项在系统的 CPU 有多于一个内核时自动启用,仅作用于生产构建。 // pwa: {}, // PWA 插件相关配置 see https://github.com/vuejs/vue-cli/tree/dev/packages/%40vue/cli-plugin-pwa // webpack-dev-server 相关配置 devServer: { open: process.platform === "darwin", host: "0.0.0.0", // 允许外部ip访问 port: 8022, // 端口 https: false, // 启用https // 错误、警告在页面弹出 overlay: { warnings: true, errors: true, }, // 代理转发配置,用于调试环境 proxy: { "/api": { target: "http://www.baidu.com/api", changeOrigin: true, // 允许websockets跨域 // ws: true, pathRewrite: { "^/api": "", }, }, }, }, // 第三方插件配置 pluginOptions: {},};