vue.config.js 4.6 KB

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