Umi 配置splitChunks

这段时间花了点儿时间看了下umi的官网,而且下载了官网提供的例子,整理了下本身配置umi分包的配置,只是整理,但愿大佬们轻喷!
首先想吐槽下官网的例子,真的很“简洁”,通过度娘搜索,发现没有不少答案,大概的配置都是相似这种css

vendors: {
    name: 'vendors',
    chunks: 'all',
    test: /[\\/]node_modules[\\/](react|react-dom|react-router|react-router-dom|lodash|lodash-decorators|redux-saga|re-select|dva|moment)[\\/]/,
    priority: -10,
},

而后我以为这么配置没什么毛病,也就配置到本身的项目中,我这里是直接按着官网的步骤init项目,而后问题就来了,一直在报file ant.js don't exists in chunksMap这种错误,没有打包成功。第一想法多是我项目哪里配错了,而后回顾整个步骤发现并无什么大问题,提issues好像彷佛很卑微,也没有理我,最后捣鼓了很久,只剩下多是node包的问题,换了node的版本
image.png
从最新的换到了8的版本彷佛不能够,最后换到了10,惊奇的发现能够了,随后的版本应该还能够可是我没有试了,你们自行试一下。而后查看官网的例子,以为官网ant-pro的例子中的分包写法也能够使用,就加到本身的项目中node

import path from 'path';
import * as IWebpackChainConfig from 'webpack-chain';

function getModulePackageName(module: { context: string }) {
  if (!module.context) return null;
  const nodeModulesPath = path.join(__dirname, '../node_modules/');
  if (module.context.substring(0, nodeModulesPath.length) !== nodeModulesPath) {
    return null;
  }
  const moduleRelativePath = module.context.substring(nodeModulesPath.length);
  const [moduleDirName] = moduleRelativePath.split(path.sep);
  let packageName: string | null = moduleDirName;
  if (packageName && packageName.match('^_')) {
    // eslint-disable-next-line prefer-destructuring
    packageName = packageName.match(/^_(@?[^@]+)/)![1];
  }
  return packageName;
}
export const chainWebpack = (config: IWebpackChainConfig) => {
  config.optimization
    .runtimeChunk(false)
    .splitChunks({
      chunks: 'async',
      minSize: 0,
      cacheGroups: {
        vendors: {
          chunks: 'all',
          name: 'vendors',
          test: (module: { context: string }) => {
            const packageName = getModulePackageName(module) || '';
            if (packageName) {
              return [
                'react',
                'react-dom',
                'react-router',
                'react-router-dom',
                'lodash',
                'lodash-decorators',
                'redux-saga',
                're-select',
                'dva',
                'moment',
                'postcss-px2rem'
              ].includes(packageName);
            }
            return false;
          },
          priority: 10
        },
        antd: {
          name: "argrace",
          test: /[\\/]node_modules[\\/](@ant-design|antd|antd-mobile)[\\/]/,
          chunks: "all",
          priority: 9
        }
      }
    })
}

第一次发表文章,感谢你们支持!!!react

相关文章
相关标签/搜索