vue.config.js 4.4 KB

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