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. //新增
  8. nodeModulesTransform: {
  9. type: 'none',
  10. exclude: [],
  11. },
  12. chunks: ['vendors', 'umi'],
  13. chainWebpack: function (config, { env, webpack, createCSSRule }) {
  14. // 修改js,js chunk文件输出目录
  15. config.output
  16. .filename(assetDir + "/js/[name].[contenthash:8].js")
  17. .chunkFilename(assetDir + "/js/[name].[contenthash:8].chunk.js");
  18. // 修改css输出目录
  19. config.plugin("extract-css").tap(() => [
  20. {
  21. filename: `${assetDir}/css/[name].[contenthash:8].css`,
  22. chunkFilename: `${assetDir}/css/[name].[contenthash:8].chunk.css`,
  23. ignoreOrder: true,
  24. },
  25. ]);
  26. // 修改图片输出目录
  27. config.module
  28. .rule("images")
  29. .test(/\.(png|jpe?g|gif|webp|ico)(\?.*)?$/)
  30. .use("url-loader")
  31. .loader(require.resolve("url-loader"))
  32. .tap((options) => {
  33. const newOptions = {
  34. ...options,
  35. name: assetDir + "/img/[name].[hash:8].[ext]",
  36. fallback: {
  37. ...options.fallback,
  38. options: {
  39. name: assetDir + "/img/[name].[hash:8].[ext]",
  40. esModule: false,
  41. },
  42. },
  43. };
  44. return newOptions;
  45. });
  46. // 修改svg输出目录
  47. // config.module
  48. // .rule("svg")
  49. // .test(/\.(svg)(\?.*)?$/)
  50. // .use("file-loader")
  51. // .loader(require.resolve("file-loader"))
  52. // .tap((options) => ({
  53. // ...options,
  54. // name: assetDir + "/img/[name].[hash:8].[ext]",
  55. // }));
  56. // 修改fonts输出目录
  57. // config.module
  58. // .rule("fonts")
  59. // .test(/\.(eot|woff|woff2|ttf)(\?.*)?$/)
  60. // .use("file-loader")
  61. // .loader(require.resolve("file-loader"))
  62. // .tap((options) => ({
  63. // ...options,
  64. // name: assetDir + "/fonts/[name].[hash:8].[ext]",
  65. // fallback: {
  66. // ...options.fallback,
  67. // options: {
  68. // name: assetDir + "/img/[name].[hash:8].[ext]",
  69. // esModule: false,
  70. // },
  71. // },
  72. // }));
  73. // // 添加gzip压缩
  74. // config.when(true, (config) => {
  75. // config
  76. // .plugin("compression-webpack-plugin")
  77. // .use(CompressionWebpackPlugin, [
  78. // {
  79. // filename: "[path][base].gz",
  80. // algorithm: "gzip",
  81. // test: new RegExp("\\.(js|css)$"),
  82. // threshold: 10240,
  83. // minRatio: 0.8,
  84. // // deleteOriginalAssets: true//删除源文件
  85. // },
  86. // ]);
  87. // });
  88. //通用插件合并
  89. config.merge({
  90. optimization: {
  91. splitChunks: {
  92. chunks: 'all',
  93. minSize: 30000,
  94. minChunks: 3,
  95. automaticNameDelimiter: '.',
  96. cacheGroups: {
  97. vendor: {
  98. name: 'vendors',
  99. test({ resource }: any) {
  100. return /[\\/]node_modules[\\/]/.test(resource);
  101. },
  102. priority: 10,
  103. },
  104. },
  105. },
  106. }
  107. });
  108. },
  109. // 出去console.log
  110. terserOptions: {
  111. compress: {
  112. drop_console: true,
  113. },
  114. },
  115. // map关闭
  116. devtool: false,
  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. });