prod.config.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. const webpack = require("webpack");
  2. const UglifyJsPlugin = require("uglifyjs-webpack-plugin");
  3. const isProd = process.env.NODE_ENV === "production";
  4. // * 避免打包项
  5. const externals = isProd
  6. ? {
  7. vue: "Vue",
  8. vuex: "Vuex",
  9. "vue-router": "VueRouter",
  10. axios: "axios",
  11. //clipboard: "Clipboard",
  12. }
  13. : {};
  14. // * 资源地址
  15. const assetsDir = isProd
  16. ? "static" + new Date().toLocaleDateString().replace(/\//g, "-")
  17. : "static";
  18. // * 公共代码抽离
  19. const optimization = {
  20. splitChunks: {
  21. cacheGroups: {
  22. vendors: {
  23. name: "chunk-vendors",
  24. test: /[\\/]node_modules[\\/]/,
  25. priority: 100,
  26. chunks: "all",
  27. minChunks: 1,
  28. maxInitialRequests: 5,
  29. minSize: 0,
  30. },
  31. common: {
  32. name: "chunk-common",
  33. test: /[\\/]src[\\/]ts[\\/]/,
  34. minChunks: 2,
  35. maxInitialRequests: 5,
  36. minSize: 0,
  37. priority: 60,
  38. chunks: "all",
  39. reuseExistingChunk: true,
  40. },
  41. antDesignVue: {
  42. name: "chunk-antdv",
  43. test: /[\\/]node_modules[\\/]ant-design-vue[\\/]/,
  44. chunks: "initial",
  45. priority: 120,
  46. reuseExistingChunk: true,
  47. enforce: true,
  48. },
  49. styles: {
  50. name: "styles",
  51. test: /\.(sa|sc|c)ss$/,
  52. chunks: "all",
  53. enforce: true,
  54. },
  55. runtimeChunk: {
  56. name: "manifest",
  57. },
  58. },
  59. },
  60. };
  61. const ossCDN = "https://firemanage.oss-cn-hangzhou.aliyuncs.com/FE-resource";
  62. // * oss config
  63. const ossConfig = {
  64. buildPath: "/",
  65. region: "oss-cn-hangzhou",
  66. ak: "LTAIowrHAk6HHxb8",
  67. sk: "vhrLQEn1WW8WQphOPBfcDE8zwx7nel",
  68. bucket: "firemanage",
  69. };
  70. // * 资源配置
  71. const cdns = {
  72. dev: {},
  73. build: {
  74. css: [],
  75. js: [
  76. `${ossCDN}/library/vue.next.min.js`,
  77. `${ossCDN}/library/vuex.next.min.js`,
  78. `${ossCDN}/library/vue-router.next.min.js`,
  79. `${ossCDN}/library/axios.min.js`,
  80. //`${ossCDN}/library/clipboard.min.js`,
  81. ],
  82. },
  83. };
  84. // * 打包后资源上传oss
  85. const uploadAssetsToOSS = (config) => {
  86. config
  87. .plugin("webpack-aliyun-oss-plugin")
  88. .use(require("webpack-aliyun-oss-plugin"), [
  89. {
  90. buildPath: ossConfig.buildPath,
  91. region: ossConfig.region,
  92. ak: ossConfig.ak,
  93. sk: ossConfig.sk,
  94. bucket: ossConfig.bucket,
  95. filter: function(assets) {
  96. return !/\.html$/.test(assets);
  97. },
  98. },
  99. ]);
  100. };
  101. // * 打包gzip
  102. const assetsGzip = (config) => {
  103. config
  104. .plugin("compression-webpack-plugin")
  105. .use(require("compression-webpack-plugin"), [
  106. {
  107. filename: "[path].gz[query]",
  108. algorithm: "gzip",
  109. test: /\.js$|\.html$|\.json$|\.css/,
  110. threshold: 10240, // 只有大小大于该值的资源会被处理 10240
  111. minRatio: 0.8, // 只有压缩率小于这个值的资源才会被处理
  112. deleteOriginalAssets: true, // 删除原文件
  113. },
  114. ]);
  115. };
  116. // * 代码压缩
  117. const codeUglifyConfig = {
  118. uglifyOptions: {
  119. //生产环境自动删除console
  120. compress: {
  121. drop_debugger: true,
  122. drop_console: false,
  123. pure_funcs: ["console.log"],
  124. },
  125. },
  126. sourceMap: false,
  127. parallel: true,
  128. };
  129. const plugins = isProd
  130. ? [
  131. new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/),
  132. new webpack.HashedModuleIdsPlugin(),
  133. new UglifyJsPlugin(codeUglifyConfig),
  134. ]
  135. : [];
  136. module.exports = {
  137. uploadAssetsToOSS,
  138. assetsGzip,
  139. plugins,
  140. externals,
  141. optimization,
  142. cdns,
  143. assetsDir,
  144. };