React-native 打包器配置 - 如何在包中包含 .zip 文件? [英] React-native packager configuration - How to include .zip file in bundle?
问题描述
我的问题:
- 我有一个 zip 文件,其中包含我公司设备的固件更新
- 我希望能够使用 react-native-fs 和下面的代码访问它
.
export function readAssetFile(name) {
if(Platform.OS === 'ios') {
return RNFS.readFile(`${RNFS.MainBundlePath}/assets/data/${name}`);
} else {
return RNFS.readFileAssets(`raw/${name}`, 'base64');
}
}
我的项目结构如下:
ProjectDir
android
data
image1.png
image2.png
firmwarefile.zip
ios
android 分支有效,因为我在我的 .gradle 中添加了一个构建步骤,将固件文件.zip 复制到 ProjectDir/android/app/src/main/assets/raw.所以我可以调用 readAssetFile('firmwarefile.zip'),它返回数据.
The android branch works, because I added a build step in my .gradle to copy firmwarefile.zip into ProjectDir/android/app/src/main/assets/raw. So I can call readAssetFile('firmwarefile.zip'), and it returns the data.
在 iOS 上,所有图像文件(Image1.png、Image2.png)都包含在 MyProject.app/assets/data/中,我无需执行任何操作,但它们旁边的 zip 文件却没有.
On iOS, all the image files (Image1.png, Image2.png) are included in MyProject.app/assets/data/ without me having to do anything, but the zip file that sits beside them is not.
查看实际的打包程序代码(来自 metro 项目),似乎(基于在 Metro/src/defaults.js) 中,打包器默认不包含 zip 文件,但打包器可以配置为包含其他文件类型.但是我找不到任何文档来说明我将如何进行配置.
Looking into the actual packager code (from the metro project), it seems (based on metro/src/defaults.js) that zip files aren't included by default by the packager, but the packager can be configured to include other file types. But I can't find any documentation for how I'd go about doing that configuring.
抱歉,我问了一个非常简单的问题,但我一直在尝试将这个 zip 包含在我的包中大约 4 个小时.我正在求助于手动输入 console.logs 和 error-throws 来跟踪 Metro 内部的内容,以尝试找到应该在我的配置中发送的位置.
Sorry for what feels like a really simple question, but I've been trying to get this zip included in my bundle for ~4 hours now. I'm resorting to manually putting in console.logs and error-throws to trace things inside metro to try and find where I should be sending in my config.
版本:反应原生:0.55.3地铁:0.30.2
Versions: React-native: 0.55.3 Metro: 0.30.2
推荐答案
这是一个 hack,但它完成了:
This is a hack, but it gets it done:
- 将您的 zip 二进制文件转换为 base64 字符串
- 将其粘贴在 .js 文件中,例如
module.exports = "<您的 base64 数据在此处>"
- 在需要 zip 文件的文件中,使用
import myZipFileAsBase64 from './hacky-base64-file.js';
这是制作 base64 文件的快速脚本:
Here's a quick script to make your base64 files:
var fs = require('fs');
function prepareZip(file, outJs) {
const b64 = fs.readFileSync(file, 'base64');
fs.writeFileSync(outJs, `module.exports = ${JSON.stringify(b64)};`);
}
prepareZip('./data/myFirmware.zip', './hacky-base64-file.js');
这篇关于React-native 打包器配置 - 如何在包中包含 .zip 文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!