config.prod.ts 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. // https://umijs.org/config/
  2. import { defineConfig } from 'umi';
  3. const assetDir = "static"
  4. export default defineConfig({
  5. history: { type: 'hash' }, // 默认是 browse
  6. // devtool: 'source-map',//正式坏境查看报错使用,假如正式版稳定请关闭
  7. // map关闭
  8. devtool: false,
  9. //新增
  10. nodeModulesTransform: {
  11. type: 'none',
  12. exclude: [],
  13. },
  14. chunks: ['vendors', 'umi'],
  15. chainWebpack: function (config, { env, webpack, createCSSRule }) {
  16. // 修改js,js chunk文件输出目录
  17. config.output
  18. .filename(assetDir + "/js/[name].[contenthash:8].js")
  19. .chunkFilename(assetDir + "/js/[name].[contenthash:8].chunk.js");
  20. // 修改css输出目录
  21. config.plugin("extract-css").tap(() => [
  22. {
  23. filename: `${assetDir}/css/[name].[contenthash:8].css`,
  24. chunkFilename: `${assetDir}/css/[name].[contenthash:8].chunk.css`,
  25. ignoreOrder: true,
  26. },
  27. ]);
  28. // 修改图片输出目录
  29. config.module
  30. .rule("images")
  31. .test(/\.(png|jpe?g|gif|webp|ico)(\?.*)?$/)
  32. .use("url-loader")
  33. .loader(require.resolve("url-loader"))
  34. .tap((options) => {
  35. const newOptions = {
  36. ...options,
  37. name: assetDir + "/img/[name].[hash:8].[ext]",
  38. fallback: {
  39. ...options.fallback,
  40. options: {
  41. name: assetDir + "/img/[name].[hash:8].[ext]",
  42. esModule: false,
  43. },
  44. },
  45. };
  46. return newOptions;
  47. });
  48. // 修改svg输出目录
  49. // config.module
  50. // .rule("svg")
  51. // .test(/\.(svg)(\?.*)?$/)
  52. // .use("file-loader")
  53. // .loader(require.resolve("file-loader"))
  54. // .tap((options) => ({
  55. // ...options,
  56. // name: assetDir + "/img/[name].[hash:8].[ext]",
  57. // }));
  58. // 修改fonts输出目录
  59. // config.module
  60. // .rule("fonts")
  61. // .test(/\.(eot|woff|woff2|ttf)(\?.*)?$/)
  62. // .use("file-loader")
  63. // .loader(require.resolve("file-loader"))
  64. // .tap((options) => ({
  65. // ...options,
  66. // name: assetDir + "/fonts/[name].[hash:8].[ext]",
  67. // fallback: {
  68. // ...options.fallback,
  69. // options: {
  70. // name: assetDir + "/img/[name].[hash:8].[ext]",
  71. // esModule: false,
  72. // },
  73. // },
  74. // }));
  75. // // 添加gzip压缩
  76. // config.when(true, (config) => {
  77. // config
  78. // .plugin("compression-webpack-plugin")
  79. // .use(CompressionWebpackPlugin, [
  80. // {
  81. // filename: "[path][base].gz",
  82. // algorithm: "gzip",
  83. // test: new RegExp("\\.(js|css)$"),
  84. // threshold: 10240,
  85. // minRatio: 0.8,
  86. // // deleteOriginalAssets: true//删除源文件
  87. // },
  88. // ]);
  89. // });
  90. //通用插件合并
  91. config.merge({
  92. optimization: {
  93. splitChunks: {
  94. chunks: 'all',
  95. minSize: 30000,
  96. minChunks: 3,
  97. automaticNameDelimiter: '.',
  98. cacheGroups: {
  99. vendor: {
  100. name: 'vendors',
  101. test({ resource }: any) {
  102. return /[\\/]node_modules[\\/]/.test(resource);
  103. },
  104. priority: 10,
  105. },
  106. },
  107. },
  108. }
  109. });
  110. },
  111. // 出去console.log
  112. terserOptions: {
  113. compress: {
  114. drop_console: true,
  115. },
  116. },
  117. //替换压缩器为 esbuild
  118. esbuild: {},
  119. // 不打包组件使用cdn
  120. externals: {
  121. // react: 'window.React',
  122. // 'react-dom': 'window.ReactDOM',
  123. // antd: 'window.antd',
  124. // echarts: 'window.echarts',
  125. // moment: 'window.moment'
  126. },
  127. scripts: [
  128. // 'https://unpkg.com/react@17/umd/react.production.min.js',
  129. // "https://unpkg.com/react-dom@17/umd/react-dom.production.min.js",
  130. // 'https://cdn.bootcdn.net/ajax/libs/moment.js/2.29.1/moment.min.js',
  131. // 'https://lib.baomitu.com/antd/4.16.2/antd-with-locales.min.js',
  132. // 'https://lib.baomitu.com/antd/4.16.12/antd.min.js',
  133. // 'https://cdn.bootcdn.net/ajax/libs/echarts/5.1.2/echarts.min.js',
  134. ],
  135. extraBabelPlugins:['transform-remove-console']
  136. });