vue.config.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. "use strict";
  2. const path = require("path");
  3. require("babel-polyfill");
  4. const productionGzipExtensions = ["js", "css"];
  5. const UglifyJsPlugin = require("uglifyjs-webpack-plugin");
  6. const CompressionWebpackPlugin = require("compression-webpack-plugin");
  7. const defaultSettings = require("./src/settings.js");
  8. const isProduction = process.env.NODE_ENV === "production";
  9. function resolve(dir) {
  10. return path.join(__dirname, dir);
  11. }
  12. const name = defaultSettings.title || "采销平台"; // page title
  13. // If your port is set to 80,
  14. // use administrator privileges to execute the command line.
  15. // For example, Mac: sudo npm run
  16. // You can change the port by the following method:
  17. // port = 8080 npm run dev OR npm run dev --port = 8080
  18. const port = process.env.port || process.env.npm_config_port || 8080; // dev port
  19. // All configuration item explanations can be find in https://cli.vuejs.org/config/
  20. module.exports = {
  21. /**
  22. * You will need to set publicPath if you plan to deploy your site under a sub path,
  23. * for example GitHub Pages. If you plan to deploy your site to https://foo.github.io/bar/,
  24. * then publicPath should be set to "/bar/".
  25. * In most cases please use '/' !!!
  26. * Detail: https://cli.vuejs.org/config/#publicpath
  27. */
  28. publicPath: "/",
  29. outputDir: "dist",
  30. assetsDir: "static",
  31. lintOnSave: false, //process.env.NODE_ENV === 'development'
  32. productionSourceMap: false,
  33. devServer: {
  34. // proxy: {
  35. // "/admin": {
  36. // target: process.env.NODE_ENV,
  37. // ws: true,
  38. // secure: false, // 如果是https接口,需要配置这个参数
  39. // changeOrigin: true, // 是否跨域
  40. // // pathRewrite: {
  41. // // // 路径重写
  42. // // "/admin": "/admin" // 这个意思就是以api开头的,定向到哪里, 如果你的后边还有路径的话, 会自动拼接上
  43. // // }
  44. // }
  45. // },
  46. port: port,
  47. open: true,
  48. overlay: {
  49. warnings: false,
  50. errors: true,
  51. },
  52. // before: require('./mock/mock-server.js')
  53. },
  54. configureWebpack: (config) => {
  55. // 入口文件
  56. config.entry.app = ["babel-polyfill", "./src/main.js"];
  57. config.externals = {
  58. vue: "Vue",
  59. // 'vuex': 'Vuex',
  60. axios: "axios",
  61. "vue-router": "VueRouter",
  62. };
  63. if (isProduction) {
  64. config.plugins.push(
  65. new CompressionWebpackPlugin({
  66. algorithm: "gzip",
  67. test: new RegExp("\\.(" + productionGzipExtensions.join("|") + ")$"),
  68. threshold: 10240,
  69. // deleteOriginalAssets:true, //删除源文件,不建议
  70. minRatio: 0.7,
  71. })
  72. );
  73. }
  74. // 删除console插件
  75. let plugins = [
  76. new UglifyJsPlugin({
  77. uglifyOptions: {
  78. compress: {
  79. // warnings: false, // `warnings` is not a supported option
  80. drop_console: true,
  81. drop_debugger: true,
  82. },
  83. output: {
  84. // 去掉注释内容
  85. comments: true,
  86. },
  87. },
  88. cache: true, //启用/禁用文件缓存(类型可布尔也可是字符串)
  89. sourceMap: false,
  90. parallel: true,
  91. }),
  92. ];
  93. // 只有打包生产环境才需要将console删除
  94. if (isProduction) {
  95. config.plugins = [...config.plugins, ...plugins];
  96. }
  97. },
  98. chainWebpack(config) {
  99. // it can improve the speed of the first screen, it is recommended to turn on preload
  100. // it can improve the speed of the first screen, it is recommended to turn on preload
  101. config.plugin("preload").tap(() => [
  102. {
  103. rel: "preload",
  104. // to ignore runtime.js
  105. // https://github.com/vuejs/vue-cli/blob/dev/packages/@vue/cli-service/lib/config/app.js#L171
  106. fileBlacklist: [/\.map$/, /hot-update\.js$/, /runtime\..*\.js$/],
  107. include: "initial",
  108. },
  109. ]);
  110. // when there are many pages, it will cause too many meaningless requests
  111. config.plugins.delete("prefetch");
  112. // set svg-sprite-loader
  113. config.module.rule("svg").exclude.add(resolve("src/icons")).end();
  114. config.module
  115. .rule("icons")
  116. .test(/\.svg$/)
  117. .include.add(resolve("src/icons"))
  118. .end()
  119. .use("svg-sprite-loader")
  120. .loader("svg-sprite-loader")
  121. .options({
  122. symbolId: "icon-[name]",
  123. })
  124. .end();
  125. //process.env.NODE_ENV !== 'development'
  126. config.when(true, (config) => {
  127. config
  128. .plugin("ScriptExtHtmlWebpackPlugin")
  129. .after("html")
  130. .use("script-ext-html-webpack-plugin", [
  131. {
  132. // `runtime` must same as runtimeChunk name. default is `runtime`
  133. inline: /runtime\..*\.js$/,
  134. },
  135. ])
  136. .end();
  137. config.optimization.splitChunks({
  138. chunks: "all",
  139. cacheGroups: {
  140. libs: {
  141. name: "chunk-libs",
  142. test: /[\\/]node_modules[\\/]/,
  143. priority: 10,
  144. chunks: "initial", // only package third parties that are initially dependent
  145. },
  146. elementUI: {
  147. name: "chunk-elementUI", // split elementUI into a single package
  148. priority: 20, // the weight needs to be larger than libs and app or it will be packaged into libs or app
  149. test: /[\\/]node_modules[\\/]_?element-ui(.*)/, // in order to adapt to cnpm
  150. },
  151. commons: {
  152. name: "chunk-commons",
  153. test: resolve("src/components"), // can customize your rules
  154. minChunks: 3, // minimum common number
  155. priority: 5,
  156. reuseExistingChunk: true,
  157. },
  158. },
  159. });
  160. // https:// webpack.js.org/configuration/optimization/#optimizationruntimechunk
  161. config.optimization.runtimeChunk("single");
  162. });
  163. },
  164. };