vue.config.js 4.1 KB

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