metro.main.config.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. const { hasBuildInfo, getCacheFile, isPwdFile } = require('./build.js');
  2. const bundleBuInfo = require('./config/bundleBuInfo.json');
  3. const {mergeConfig, getDefaultConfig} = require('@react-native/metro-config');
  4. function postProcessModulesFilter(module) {
  5. if (
  6. module.path.indexOf('__prelude__') >= 0 ||
  7. module.path.indexOf('polyfills') >= 0
  8. ) {
  9. return false;
  10. }
  11. return !hasBuildInfo('./config/bundleCommonInfo.json', module.path);
  12. }
  13. // 不要使用 string 会导致 bundle 体积陡增
  14. function createModuleIdFactory() {
  15. // 如果是业务 模块请以 10000000 来自增命名
  16. const fileToIdMap = new Map();
  17. let nextId = 10000000;
  18. let isFirst = false;
  19. return (path) => {
  20. if (getCacheFile('./config/bundleCommonInfo.json', path)) {
  21. return getCacheFile('./config/bundleCommonInfo.json', path);
  22. }
  23. if (!isFirst && isPwdFile(path)) {
  24. nextId = bundleBuInfo[isPwdFile(path)];
  25. isFirst = true;
  26. }
  27. let id = fileToIdMap.get(path);
  28. if (typeof id !== 'number') {
  29. id = nextId++;
  30. fileToIdMap.set(path, id);
  31. }
  32. return id;
  33. };
  34. }
  35. const config = {
  36. serializer: {
  37. createModuleIdFactory: createModuleIdFactory, // 给 bundle 一个id 避免冲突 cli 源码中这个id 是从1 开始 自增的
  38. processModuleFilter: postProcessModulesFilter, // 返回false 就不会build 进去
  39. },
  40. };
  41. module.exports = mergeConfig(getDefaultConfig(__dirname), config);