vue.config.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. const Setting = require("./src/setting.env");
  2. // 引入打包分析文件
  3. const { BundleAnalyzerPlugin } = require("webpack-bundle-analyzer");
  4. // 引入Gzip压缩文件
  5. const CompressionPlugin = require("compression-webpack-plugin");
  6. // 引入js打包工具
  7. const UglifyJsPlugin = require("uglifyjs-webpack-plugin");
  8. // 拼接路径
  9. const resolve = (dir) => require("path").join(__dirname, dir);
  10. // 增加环境变量
  11. process.env.VUE_APP_VERSION = require("./package.json").version;
  12. process.env.VUE_APP_BUILD_TIME = require("dayjs")().format("YYYY-M-D HH:mm:ss");
  13. module.exports = {
  14. publicPath: Setting.publicPath,
  15. lintOnSave: Setting.lintOnSave,
  16. outputDir: Setting.outputDir,
  17. assetsDir: Setting.assetsDir,
  18. runtimeCompiler: true,
  19. productionSourceMap: false, //关闭生产环境下的SourceMap映射文件
  20. devServer: {
  21. publicPath: Setting.publicPath,
  22. },
  23. // 打包优化
  24. configureWebpack: (config) => {
  25. const pluginsPro = [
  26. new BundleAnalyzerPlugin()
  27. ];
  28. pluginsPro.push(
  29. new CompressionPlugin({
  30. algorithm: "gzip",
  31. test: /\.js$|\.html$|\.css$/, // 匹配文件名
  32. minRatio: 0.8, // 压缩率小于1才会压缩
  33. threshold: 10240, // 对超过10k的数据压缩
  34. deleteOriginalAssets: false // 是否删除未压缩的源文件,谨慎设置,如果希望提供非gzip的资源,可不设置或者设置为false(比如删除打包后的gz后还可以加载到原始资源文件)
  35. })
  36. );
  37. pluginsPro.push(
  38. // js文件压缩
  39. new UglifyJsPlugin({
  40. uglifyOptions: {
  41. compress: {
  42. drop_debugger: true,
  43. drop_console: true, //生产环境自动删除console
  44. pure_funcs: ["console.log"] //移除console
  45. },
  46. },
  47. sourceMap: false,
  48. parallel: true //使用多进程并行运行来提高构建速度。默认并发运行数:os.cpus().length - 1。
  49. })
  50. );
  51. if (process.env.NODE_ENV === "production") {
  52. config.plugins = [...config.plugins, ...pluginsPro];
  53. }
  54. },
  55. css: {
  56. sourceMap: false, // css sourceMap 配置
  57. loaderOptions: {
  58. less: {},
  59. },
  60. },
  61. transpileDependencies: ["view-design", "iview", "vuedraggable"],
  62. // 默认设置: https://github.com/vuejs/vue-cli/tree/dev/packages/@vue/cli-service/lib/config/base.js
  63. chainWebpack: (config) => {
  64. /**
  65. * 删除懒加载模块的 prefetch preload,降低带宽压力
  66. * https://cli.vuejs.org/zh/guide/html-and-static-assets.html#prefetch
  67. * https://cli.vuejs.org/zh/guide/html-and-static-assets.html#preload
  68. * 而且预渲染时生成的 prefetch 标签是 modern 版本的,低版本浏览器是不需要的
  69. */
  70. config.plugins.delete("prefetch").delete("preload");
  71. // 解决 cli3 热更新失效 https://github.com/vuejs/vue-cli/issues/1559
  72. config.resolve.symlinks(true);
  73. config
  74. // 开发环境
  75. .when(
  76. process.env.NODE_ENV === "development",
  77. // sourcemap不包含列信息
  78. (config) => config.devtool("cheap-source-map")
  79. )
  80. // 非开发环境
  81. .when(process.env.NODE_ENV !== "development", (config) => {});
  82. // 不编译 iView Pro
  83. config.module
  84. .rule("js")
  85. .test(/\.jsx?$/)
  86. .exclude.add(resolve("src/libs/iview-pro"))
  87. .end();
  88. // 使用 iView Loader
  89. config.module
  90. .rule("vue")
  91. .test(/\.vue$/)
  92. .use("iview-loader")
  93. .loader("iview-loader")
  94. .tap(() => {
  95. return Setting.iviewLoaderOptions;
  96. })
  97. .end();
  98. // markdown
  99. config.module
  100. .rule("md")
  101. .test(/\.md$/)
  102. .use("text-loader")
  103. .loader("text-loader")
  104. .end();
  105. // i18n
  106. config.module
  107. .rule("i18n")
  108. .resourceQuery(/blockType=i18n/)
  109. .use("i18n")
  110. .loader("@kazupon/vue-i18n-loader")
  111. .end();
  112. // image exclude
  113. const imagesRule = config.module.rule("images");
  114. imagesRule
  115. .test(/\.(png|jpe?g|gif|webp|svg)(\?.*)?$/)
  116. .exclude.add(resolve("src/assets/svg"))
  117. .end();
  118. // 重新设置 alias
  119. config.resolve.alias.set("@api", resolve("src/api"));
  120. // node
  121. config.node.set("__dirname", true).set("__filename", true);
  122. // 判断是否需要加入模拟数据
  123. const entry = config.entry("app");
  124. if (Setting.isMock) {
  125. entry.add("@/mock").end();
  126. }
  127. },
  128. };