vue.config.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. "use strict";
  2. const path = require("path");
  3. function resolve(dir) {
  4. return path.join(__dirname, dir);
  5. }
  6. const CompressionPlugin = require("compression-webpack-plugin");
  7. const UglifyJsPlugin = require("uglifyjs-webpack-plugin");
  8. const port = process.env.port || process.env.npm_config_port || 80; // 端口
  9. // vue.config.js 配置说明
  10. module.exports = {
  11. // 部署生产环境和开发环境下的URL。
  12. // 默认情况下,Vue CLI 会假设你的应用是被部署在一个域名的根路径上
  13. publicPath: process.env.NODE_ENV === "production" ? "/" : "/",
  14. // 在npm run build 或 yarn build 时 ,生成文件的目录名称(要和baseUrl的生产环境路径一致)(默认dist)
  15. outputDir: "dist",
  16. // 用于放置生成的静态资源 (js、css、img、fonts) 的;(项目打包之后,静态资源会放在这个文件夹下)
  17. assetsDir: "static",
  18. // 是否开启eslint保存检测,有效值:ture | false | 'error'
  19. lintOnSave: process.env.NODE_ENV === "development",
  20. // 如果你不需要生产环境的 source map,可以将其设置为 false 以加速生产环境构建。
  21. productionSourceMap: false,
  22. // webpack-dev-server 相关配置
  23. devServer: {
  24. host: "0.0.0.0",
  25. port: port,
  26. open: true,
  27. disableHostCheck: true,
  28. },
  29. css: {
  30. loaderOptions: {
  31. sass: {
  32. sassOptions: { outputStyle: "expanded" },
  33. },
  34. },
  35. },
  36. configureWebpack: {
  37. resolve: {
  38. alias: {
  39. "@": resolve("src"),
  40. },
  41. },
  42. plugins: [
  43. // new CompressionPlugin({
  44. // cache: false, // 不启用文件缓存
  45. // test: /\.(js|css|html)?$/i, // 压缩文件格式
  46. // filename: "[path].gz[query]", // 压缩后的文件名
  47. // algorithm: "gzip", // 使用gzip压缩
  48. // minRatio: 0.8, // 压缩率小于1才会压缩
  49. // }),
  50. process.env.NODE_ENV === "production"
  51. ? new UglifyJsPlugin({
  52. uglifyOptions: {
  53. output: {
  54. comments: false, // 去掉注释
  55. },
  56. warnings: false, // 禁用警告
  57. compress: {
  58. drop_console: process.env.NODE_ENV === "production",
  59. drop_debugger: true,
  60. // pure_funcs: ["console.log"], // 移除console
  61. },
  62. },
  63. })
  64. : null,
  65. ].filter(Boolean),
  66. },
  67. chainWebpack(config) {
  68. config.plugins.delete("preload"); // TODO: need test
  69. config.plugins.delete("prefetch"); // TODO: need test
  70. // set svg-sprite-loader
  71. config.module.rule("svg").exclude.add(resolve("src/assets/icons")).end();
  72. config.module
  73. .rule("icons")
  74. .test(/\.svg$/)
  75. .include.add(resolve("src/assets/icons"))
  76. .end()
  77. .use("svg-sprite-loader")
  78. .loader("svg-sprite-loader")
  79. .options({
  80. symbolId: "icon-[name]",
  81. })
  82. .end();
  83. // html解析
  84. config.plugin("html").tap((args) => {
  85. args[0].title = process.env.VUE_APP_TITLE;
  86. args[0].platform = process.env.VUE_APP_ENV;
  87. return args;
  88. });
  89. config.when(process.env.NODE_ENV === "production", (config) => {
  90. // 生产配置
  91. config
  92. .plugin("ScriptExtHtmlWebpackPlugin")
  93. .after("html")
  94. .use("script-ext-html-webpack-plugin", [
  95. {
  96. // `runtime` must same as runtimeChunk name. default is `runtime`
  97. inline: /runtime\..*\.js$/,
  98. },
  99. ])
  100. .end();
  101. config.optimization.splitChunks({
  102. chunks: "all",
  103. cacheGroups: {
  104. libs: {
  105. name: "chunk-libs",
  106. test: /[\\/]node_modules[\\/]/,
  107. priority: 10,
  108. chunks: "initial", // only package third parties that are initially dependent
  109. },
  110. elementUI: {
  111. name: "chunk-elementUI", // split elementUI into a single package
  112. test: /[\\/]node_modules[\\/]_?element-ui(.*)/, // in order to adapt to cnpm
  113. priority: 20, // the weight needs to be larger than libs and app or it will be packaged into libs or app
  114. },
  115. commons: {
  116. name: "chunk-commons",
  117. test: resolve("src/components"), // can customize your rules
  118. minChunks: 3, // minimum common number
  119. priority: 5,
  120. reuseExistingChunk: true,
  121. },
  122. },
  123. });
  124. config.optimization.runtimeChunk("single"),
  125. {
  126. from: path.resolve(__dirname, "./public/robots.txt"), //防爬虫文件
  127. to: "./", //到根目录下
  128. };
  129. });
  130. },
  131. };