如何使Metro(React Native打包程序)忽略某些目录

1wnzp6jl  于 2023-01-18  发布在  React
关注(0)|答案(1)|浏览(208)

问题:
我的项目在尝试从命令行运行react-native run-ios时有一个@providesModule naming collision。它与另一个npm包esdoc创建的自动生成的目录dist/冲突。我希望能够保留此自动生成的目录,并使react本机打包程序忽略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)
5jdjgkvh

5jdjgkvh1#

这个配置习惯于在RN版本之间改变。关于创建配置文件、加载配置文件和该高速缓存的版本特定说明,请参见下文。

对于React Native〉= 0.64至0.71(+?)

在项目根目录中创建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: {
    blockList: 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命令使用此配置:
x1米11米1x
(The RN〉= 0.59应自动拾取配置文件,因为它被重命名为metro.config.js

所有版本:关于缓存的说明

请注意,打包程序可能已将您的黑名单项目包含该高速缓存中,在这种情况下,您第一次运行包含黑名单的打包程序时,可能需要使用--reset-cache重置缓存

相关问题