Skip to content

Commit

Permalink
Exclude $projectRoot/package.json from assets (#420)
Browse files Browse the repository at this point in the history
Summary:
**Summary**

From dcb41e3#diff-235a3e5d21175615e1cc254dd8b17eb2, files with `json` extension will be considered as assets and returned as `outputAssets` for `react-native-community/cli`.

This means `cli` will copy files with `json` extension to resource dir, in Android's case, `android/app/build/res`.

Here comes a problem. If `package.json` at the project root gets copied, it won't be renamed as other `package.json` in `node_modules` (e.g. `raw/node_modules_reactnativecodepush_package.json`). Instead, it will reside in `res` as `package.json`, which causes the error:

facebook/react-native#25325

```
android/app/src/main/res/raw/package.json: Error: package is not a valid resource name (reserved Java keyword)
```

This regression (I believe ;)) was introduced to React Native 0.60.0-rc with metro bumping from 0.51 to 0.54.

**Possible impact**

1. Typical usage concerning `package.json` at the root will be something like `import packageJson from "./package.json"` in JS files to get some project configurations and those content will be bundled into `index.android.bundle` no matter what. So removing `$projectRoot/package.json` won't have too much impact.

2. This new (or bad?) behavior has only been in React Native 0.60-rc so it won't affect current projects running 0.59. It should be fixed before the final release of 0.60, I assume.

**Test plan**

1. `react-native init resoucesRepro --version react-native@next` (so it uses 0.60.0-rc)
1. Add `import packageJson from './package.json'` to `App.js`.
1. `cd android && ./gradlew assembleRelease`
1. Get the error.
1. Modify `node_modules/metro/src/DeltaBundler/Serializers/getAssets.js` to match this commit.
1. `cd android && ./gradlew assembleRelease`
1. It now works!
Pull Request resolved: #420

Differential Revision: D16646741

Pulled By: cpojer

fbshipit-source-id: fc5065cce3e95529c678a8efa5b86c0148d71724
  • Loading branch information
robertying authored and facebook-github-bot committed Aug 5, 2019
1 parent 31f7f7a commit f3c9862
Showing 1 changed file with 2 additions and 1 deletion.
3 changes: 2 additions & 1 deletion packages/metro/src/DeltaBundler/Serializers/getAssets.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ async function getAssets(
if (
isJsModule(module) &&
processModuleFilter(module) &&
getJsOutput(module).type === 'js/module/asset'
getJsOutput(module).type === 'js/module/asset' &&
path.relative(options.projectRoot, module.path) !== 'package.json'
) {
promises.push(
getAssetData(
Expand Down

0 comments on commit f3c9862

Please sign in to comment.