如何让 Metro (React Native packager) 忽略某些目录

How to make Metro (React Native packager) ignore certain directories

问题:

我的项目在尝试从命令行 运行 react-native run-ios 时有一个 @providesModule naming collision。它与由另一个 npm 包 esdoc 创建的自动生成的目录 dist/ 冲突。我希望能够保留这个自动生成的目录,只让本机打包程序忽略 dist/ 目录。

错误信息:

[01/23/2017, 13:17:07] <START> Building Haste Map
    Failed to build DependencyGraph: @providesModule naming collision:
      Duplicate module name: ann
      Paths: /Users/thurt/projects/example/package.json collides with /Users/thurt/projects/example/dist/esdoc/package.json

This error is caused by a @providesModule declaration with the same name across two different files.
Error: @providesModule naming collision:
  Duplicate module name: ann
  Paths: /Users/thurt/projects/example/package.json collides with /Users/thurt/projects/example/dist/esdoc/package.json

This error is caused by a @providesModule declaration with the same name across two different files.
    at HasteMap._updateHasteMap (/Users/thurt/projects/example/node_modules/react-native/packager/react-packager/src/node-haste/DependencyGraph/HasteMap.js:158:13)
    at p.getName.then.name (/Users/thurt/projects/example/node_modules/react-native/packager/react-packager/src/node-haste/DependencyGraph/HasteMap.js:133:31)

这里的配置有在 RN 版本之间变化的习惯。有关创建配置文件、加载配置文件和清除缓存的 version-specific 说明,请参阅下文。

对于 React Native >= 0.64(?)

这是推测性的,因为 0.64 尚未发布,but a rename from blacklist to exclusionList made in metro 0.60 is due to land 在 0.64 的 RN 中。

在您的项目根目录中创建 metro.config.js,内容如下:

const exclusionList = require('metro-config/src/defaults/exclusionList');

// exclusionList is a function that takes an array of regexes and combines
// them with the default exclusions to return a single regex.

module.exports = {
  resolver: {
    blacklistRE: exclusionList([/dist\/.*/])
  }
};

对于 React Native >= 0.59, < 0.64

在您的项目根目录中创建 metro.config.js,内容如下:

const blacklist = require('metro-config/src/defaults/blacklist');

// blacklist is a function that takes an array of regexes and combines
// them with the default blacklist to return a single regex.

module.exports = {
  resolver: {
    blacklistRE: blacklist([/dist\/.*/])
  }
};

对于 React Native >= 0.57, < 0.59

在您的项目根目录中创建 rn-cli.config.js,内容如下:

const blacklist = require('metro-config/src/defaults/blacklist');

// blacklist is a function that takes an array of regexes and combines
// them with the default blacklist to return a single regex.

module.exports = {
  resolver: {
    blacklistRE: blacklist([/dist\/.*/])
  }
};

对于 React Native >= 0.52, < 0.57

在您的项目根目录中创建 rn-cli.config.js,内容如下:

const blacklist = require('metro').createBlacklist;

module.exports = {
  getBlacklistRE: function() {
    return blacklist([/dist\/.*/]);
  }
};

对于 React Native >= 0.46, < 0.52.

在您的项目根目录中创建 rn-cli.config.js,内容如下:

const blacklist = require('metro-bundler').createBlacklist;

module.exports = {
  getBlacklistRE: function() {
    return blacklist([/dist\/.*/]);
  }
};

对于 React Native < 0.46

在您的项目根目录中创建 rn-cli.config.js,内容如下:

const blacklist = require('react-native/packager/blacklist');

module.exports = {
  getBlacklistRE: function() {
    return blacklist([/dist\/.*/]);
  }
};

所有版本 < 0.59

通过传递 --config 选项让您的 CLI 命令使用此配置:

react-native run-ios --config=rn-cli.config.js

(配置文件应该被 RN >= 0.59 自动获取,因为它被重命名了 metro.config.js

所有版本:关于缓存的注意事项

请注意,您的列入黑名单的项目可能已被打包程序包含在缓存中,在这种情况下,您第一次 运行 带有黑名单的打包程序时,您可能需要使用 [=27] 重置缓存=]