vue.config.js 4.5 KB

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