我有一个库,我想在已被浏览的React Native项目中使用。当我需要库时,所有内部require()调用都会被劫持,而不是解决文件内部的依赖性问题,包括React Native试图解决它们,从而导致它中断。我想知道在React Native项目中包含browserified库的正确方法是什么。如何在React Native中使用browserified库
回答
我想出的最佳解决方案是切换到webpack。正如在一个评论中暗示的那样,图书馆需要通过诸如浏览器或网络包之类的东西进行处理,因为它具有诸如“域”之类的节点内建命令的优势。问题在于browserify声明了一个require()方法,该方法在React Native里面并不好,它也有一个require()方法。切换到webpack解决了这个问题,因为它们以不同的方式命名它们的require()__webpack_require(),这使得处理后的版本能够在React Native内正确工作。
恕我直言比browserifying节点库中的更好的方法是
- 使用节点内置插件一样
crypto
,stream
,等browserify polyfills, - a small Babel transform到
require()
电话从crypto
重写crypto-browserify
等, - 和React Native打包程序本身将其打包。
ReactNativify回购示范如何做到这一点。
如果您决定使用由ReactNativify通知的方法,通过Philipp von Weitershausen的建议,那么你应该知道的一些注意事项和技巧(原贴here):
1)我跟着我的发现建议问题列表和分裂transformer.js
分为两个部分:
transformers.js(在/config
和调用从rn-cli.config.js
):
const babelTransformer = require('./babel-transformer');
module.exports.transform = function(src, filename, options) {
const extension = String(filename.slice(filename.lastIndexOf('.')));
let result;
try {
result = babelTransformer(src, filename);
} catch (e) {
throw new Error(e);
return;
}
return {
ast: result.ast,
code: result.code,
map: result.map,
filename
};
};
巴贝尔-transformer.js(也在/config
):
'use strict'
const babel = require('babel-core');
/**
* This is your `.babelrc` equivalent.
*/
const babelRC = {
presets: ['react-native'],
plugins: [
// The following plugin will rewrite imports. Reimplementations of node
// libraries such as `assert`, `buffer`, etc. will be picked up
// automatically by the React Native packager. All other built-in node
// libraries get rewritten to their browserify counterpart.
[require('babel-plugin-rewrite-require'), {
aliases: {
constants: 'constants-browserify',
crypto: 'react-native-crypto',
dns: 'mock/dns',
domain: 'domain-browser',
fs: 'mock/empty',
http: 'stream-http',
https: 'https-browserify',
net: 'mock/net',
os: 'os-browserify/browser',
path: 'path-browserify',
pbkdf2: 'react-native-pbkdf2-shim',
process: 'process/browser',
querystring: 'querystring-es3',
stream: 'stream-browserify',
_stream_duplex: 'readable-stream/duplex',
_stream_passthrough: 'readable-stream/passthrough',
_stream_readable: 'readable-stream/readable',
_stream_transform: 'readable-stream/transform',
_stream_writable: 'readable-stream/writable',
sys: 'util',
timers: 'timers-browserify',
tls: 'mock/tls',
tty: 'tty-browserify',
vm: 'vm-browserify',
zlib: 'browserify-zlib'
},
throwForNonStringLiteral: true
}],
// Instead of the above you could also do the rewriting like this:
["module-resolver", {
"alias": {
"mock": "./config/mock",
"sodium-universal": "libsodium"
}
}]
]
};
module.exports = (src, filename) => {
const babelConfig = Object.assign({}, babelRC, {
filename,
sourceFileName: filename
});
const result = babel.transform(src, babelConfig);
return {
ast: result.ast,
code: result.code,
map: result.map,
filename
};
}
2)由于可以在上面的代码中看到的,我也使用babel-plugin-module-resolver
证实。
注意,我将使用此插件而不是一个ReactNative使用。它可以让你引用本地文件,并用适当的引号书面允许像非JS兼容的名字“钠盐万能”
注2或去.babelrc
解决方案(也许干净)如本意见所列:https://github.com/philikon/ReactNativify/issues/4#issuecomment-312136794
3)我发现我仍然需要在我的项目的根目录中使用.babelrc
以使我的Jest测试正常工作。有关详细信息,请参阅此问题:https://github.com/philikon/ReactNativify/issues/8
谢谢,我按你的要求做了。如果您批准,您可以删除递减投票。 –
建议改善,然后向下投票和离开是有点粗鲁@loki .. –
- 1. 如何使用redux在react-native-router-flux中实现react-native-drawer?
- 2. react-native:如何在rocksdb中使用asyncstorage?
- 3. 如何在React Native ART中使用LinearGradient?
- 4. 如何在React-native中使用回调?
- 5. 如何在MobX中使用React Native ListView?
- 6. 如何在使用Podfiles集成react-native时集成react-native-video?
- 7. 如何使用React Native DatePickerAndroid
- 8. 如何使用RCT_EXPORT_MODULE React Native
- 9. React Native:如何使用Webview?
- 10. 如何在我的react-native库项目中使用.arr sdk
- 11. 我可以使用react-native-i18n库和react-native-web-boilerplate
- 12. 如何在react-native-navigation中使用native-base图标
- 13. react-native - 如何使用react-native-maps显示地图
- 14. react-native react-native-vector-icons:如何使用字体真棒图标
- 15. 如何使用react-native-router-flux在react-native-drawer中进行导航
- 16. 如何使用REACT-NATIVE-IMAGE-PICKER从库中选择视频?
- 17. 库中的更改(React-Native)
- 18. 图形库 - React Native
- 19. React Native使用Cocoapods
- 20. 在React-Native中使用键或引用
- 21. 如何创建React Native组件库?
- 22. 如何将Javascript库移植到React Native?
- 23. 如何使用React Native导航页面
- 24. 如何使用react-native的PushNotificationIOS.getInitialNotification
- 25. 如何使用React Native下载文件?
- 26. 如何使用React Native的WebSocket和Cookie
- 27. 使用react-navigation如何触发在react - native应用中从Actions中导航?
- 28. 可以在React-Native中同时使用react-router和react-navigation?
- 29. 在react-native中使用CSS过滤器
- 30. 在React Native中使用上下文
为什么不使用npm版本? –
有问题的库取决于像“事件”或“域”这样的内置节点库,虽然像“事件”这样的东西可以作为依赖添加并且正常工作,但“域”实际上是“域浏览器”,因此需要雄蕊需要被诸如browserify之类的东西取代。 – stanlemon