"react-native link" silently fails to link non-font assets
See original GitHub issueIs this a bug report?
Yes
Have you read the Contributing Guidelines?
Yes
Environment
Environment: OS: macOS Sierra 10.12.6 Node: 7.10.0 Yarn: 0.27.5 npm: 4.2.0 Watchman: 4.7.0 Xcode: Xcode 9.0.1 Build version 9A1004 Android Studio: 2.3 AI-162.4069837
Packages: (wanted => installed) react: 16.0.0-alpha.12 => 16.0.0-alpha.12 react-native: 0.48.4 => 0.48.4
Steps to Reproduce
- Put font and non-font assets into a project directory (e.g.
./assets/fonts/file.ttf
./assets/text/file.txt
). - Add the following section to
package.json
:
"rnpm": {
"assets": [
"assets/text",
"assets/fonts"
]
},
- Run
react-native link
.
Expected Behavior
All asset files are copied to iOS and Android projects. If that’s not possible, an error or warning is reported in console. If for some reason that’s not possible either, this behavior is documented.
Actual Behavior
file.ttf
is copied as expected. file.txt
is not copied, console reports that linking was successful, there are no errors or warnings.
Reproducible Demo
https://github.com/andriichernenko/rnpm-non-font-assets-repro
Here’s the code in /node_modules/react-native/local-cli/link/ios/copyAssets.js
that copies the assets:
/**
* Copies each file from an array of assets provided to targetPath directory
*
* For now, the only types of files that are handled are:
* - Fonts (otf, ttf) - copied to targetPath/fonts under original name
*/
module.exports = function copyAssetsAndroid(files, targetPath) {
const assets = groupFilesByType(files);
(assets.font || []).forEach(asset =>
fs.copySync(asset, path.join(targetPath, 'fonts', path.basename(asset)))
);
};
As the comment says, “the only types of files that are handled are fonts”. One would think that other types are handled somewhere else, but that’s not the case.
Seems like this has been reported before (see https://github.com/facebook/react-native/issues/14468). There’s also a related PR: https://github.com/facebook/react-native/pull/12047, but it wasn’t merged.
Are there any plans to implement handling of resources of all types?
Issue Analytics
- State:
- Created 6 years ago
- Reactions:1
- Comments:13 (5 by maintainers)
I wrote react-native-asset which solves just that.
It also unlinks old files automatically, which I find pretty helpful.
Currently supports only fonts and sound files, but just finished writing it so might add more in the future.Added support for any file extension.I think yeah, shouldn’t be an issue. Just submit PR adding it if you think it’s a reasonable default. I think it would be cool to be able to import
txt
file into a string, but that’s against Node behavior AFAIK.I don’t have time to dig into it, but look how
Image
module handles it - it works the same way. I belive given thatid
of an asset, you should be able to parse it.Let me know if you need any further assistance, happy to help. As I said, I don’t have enough time to dig into it myself.