一、基础配置

  1. let { version, openGzip } = require("./package.json"); // 引入版本号和是否开启Gzip压缩
  2. const path = require("path"); // 引入path路径
  3. module.exports = {
  4. publicPath: "./", // 输出基本路径
  5. outputDir: "dist", // 输出文件目录
  6. indexPath: "index.html", // index.html 的输出路径 (相对于 outputDir)
  7. lintOnSave: false, // eslint-loader 是否在保存的时候检查
  8. // webpack配置
  9. chainWebpack: (config) => {},
  10. configureWebpack: (config) => {
  11. if (process.env.NODE_ENV === "production") {
  12. // 为生产环境修改配置...
  13. config.mode = "production";
  14. // 利用splitChunks将每个依赖包单独打包,在生产环境下配置
  15. // npm install --save-dev add-asset-html-webpack-plugin
  16. config.optimization = {
  17. runtimeChunk: "single",
  18. splitChunks: {
  19. chunks: "all",
  20. maxInitialRequests: Infinity,
  21. minSize: 20000, // 依赖包超过20000bit将被单独打包
  22. cacheGroups: {
  23. vendor: {
  24. test: /[\\/]node_modules[\\/]/,
  25. name(module) {
  26. // get the name. E.g. node_modules/packageName/not/this/part.js or node_modules/packageName
  27. const packageName = module.context.match(
  28. /[\\/]node_modules[\\/](.*?)([\\/]|$)/
  29. )[1];
  30. // npm package names are URL-safe, but some servers don't like @ symbols
  31. return `npm.${packageName.replace("@", "")}`;
  32. },
  33. },
  34. },
  35. },
  36. minimizer: [
  37. new UglifyPlugin({
  38. uglifyOptions: {
  39. warnings: false,
  40. compress: {
  41. drop_console: true, // console
  42. drop_debugger: false,
  43. pure_funcs: ["console.log"], // 移除console
  44. },
  45. },
  46. }),
  47. ],
  48. };
  49. Object.assign(config, {
  50. // 配置版本号
  51. output: {
  52. ...config.output,
  53. filename: `static/js/[name].[chunkhash].${version}.js`,
  54. chunkFilename: `static/js/[name].[chunkhash].${version}.js`,
  55. },
  56. // 开发生产共同配置
  57. resolve: {
  58. alias: {
  59. "@": path.resolve(__dirname, "./src"),
  60. "@c": path.resolve(__dirname, "./src/components"),
  61. "@p": path.resolve(__dirname, "./src/pages"),
  62. }, // 别名配置
  63. },
  64. });
  65. // 安装gzip压缩 npm install -D compression-webpack-plugin
  66. if (openGzip) {
  67. config.plugins = [
  68. ...config.plugins,
  69. new CompressionPlugin({
  70. test: /\.js$|\.html$|.\css/, //匹配文件名
  71. threshold: 10240, //对超过10k的数据压缩
  72. deleteOriginalAssets: false, //不删除源文件
  73. }),
  74. ];
  75. }
  76. // 外部扩展引入,同时需要在index.html中通过<script>引入对应的cdn链接
  77. config.externals = {
  78. AMap: "AMap",
  79. vue: "Vue",
  80. "vue-router": "VueRouter",
  81. axios: "axios",
  82. };
  83. } else {
  84. // 为开发环境修改配置...
  85. config.mode = "development";
  86. }
  87. },
  88. productionSourceMap: false, // 生产环境是否生成 sourceMap 文件
  89. // css相关配置
  90. css: {
  91. extract: true, // 是否使用css分离插件 ExtractTextPlugin
  92. sourceMap: false, // 开启 CSS source maps?
  93. loaderOptions: {
  94. css: {}, // 这里的选项会传递给 css-loader
  95. postcss: {
  96. // 配置pxtorem,需要npm安装pxtorem
  97. plugins: [
  98. require("postcss-pxtorem")({
  99. //这里是配置项,详见官方文档
  100. rootValue: 100, // 换算的基数
  101. selectorBlackList: ["weui", "chart"], // 忽略转换正则匹配项,以这些开头的clss类名不会转化
  102. propList: ["*"],
  103. exclude: /node_modules, /, //对该路径下的不去替换
  104. }),
  105. ],
  106. }, // 这里的选项会传递给 postcss-loader
  107. }, // css预设器配置项 详见https://cli.vuejs.org/zh/config/#css-loaderoptions
  108. modules: false, // 启用 CSS modules for all css / pre-processor files.
  109. },
  110. parallel: require("os").cpus().length > 1, // 是否为 Babel 或 TypeScript 使用 thread-loader。该选项在系统的 CPU 有多于一个内核时自动启用,仅作用于生产构建。
  111. // pwa: {}, // PWA 插件相关配置 see https://github.com/vuejs/vue-cli/tree/dev/packages/%40vue/cli-plugin-pwa
  112. // webpack-dev-server 相关配置
  113. devServer: {
  114. open: process.platform === "darwin",
  115. host: "0.0.0.0", // 允许外部ip访问
  116. port: 8022, // 端口
  117. https: false, // 启用https
  118. // 错误、警告在页面弹出
  119. overlay: {
  120. warnings: true,
  121. errors: true,
  122. },
  123. // 代理转发配置,用于调试环境
  124. proxy: {
  125. "/api": {
  126. target: "http://www.baidu.com/api",
  127. changeOrigin: true, // 允许websockets跨域
  128. // ws: true,
  129. pathRewrite: {
  130. "^/api": "",
  131. },
  132. },
  133. },
  134. },
  135. // 第三方插件配置
  136. pluginOptions: {},
  137. };