prod.config.js 3.2 KB

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