当前位置:  开发笔记 > 前端 > 正文

如何使native native packager忽略某些目录

如何解决《如何使nativenativepackager忽略某些目录》经验,为你挑选了1个好方法。

问题:

我的项目有一个@providesModule naming collision尝试从命令行运行react-native run-ios.它与自动生成的dir冲突,dist/后者是由另一个npm包esdoc创建的.我希望能够保留这个自动生成的目录,并让反应原生包装器忽略该dist/目录.

错误信息:

[01/23/2017, 13:17:07]  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)

Rob Hogan.. 32

你几乎在https://github.com/facebook/react-native/issues/12131

您可以在项目根目录中创建一个metro.config.js使用(特定于版本)内容调用的文件,如下所示:

对于反应原生> = 0.57

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\/.*/])
  }
};

对于反应原生> = 0.52,<0.57

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.46,<0.52.

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

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

对于React Native <0.46.

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

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

所有版本:

让CLI命令通过传递rn-cli.config.js选项使用此配置:

rn-cli.config.js

(NB可能是您需要传递rn-cli.config.js参数的错误,此位置可能应该自动加载但rn-cli.config.js优先级)

请注意,您的--config文件夹可能已被包装程序缓存,在这种情况下,您第一次运行它时可能需要重置缓存:

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



1> Rob Hogan..:

你几乎在https://github.com/facebook/react-native/issues/12131

您可以在项目根目录中创建一个metro.config.js使用(特定于版本)内容调用的文件,如下所示:

对于反应原生> = 0.57

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\/.*/])
  }
};

对于反应原生> = 0.52,<0.57

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.46,<0.52.

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

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

对于React Native <0.46.

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

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

所有版本:

让CLI命令通过传递rn-cli.config.js选项使用此配置:

rn-cli.config.js

(NB可能是您需要传递rn-cli.config.js参数的错误,此位置可能应该自动加载但rn-cli.config.js优先级)

请注意,您的--config文件夹可能已被包装程序缓存,在这种情况下,您第一次运行它时可能需要重置缓存:

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


这里没有说的是匹配被锚定到输入的末尾(数组中的正则表达式被构建到一个由$终止的最终正则表达式中)...所以确保在末尾放置一个.*您想要匹配的正则表达式.另外,另一个有用的提示是所有'/'正斜杠都会自动替换为当前的OS路径分隔符.
从(至少)RN 0.52开始,导入需要是:`const blacklist = require('metro').createBlacklist;`
推荐阅读
农大军乐团_697
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有