facebook / metro

🚇 The JavaScript bundler for React Native
https://metrobundler.dev
MIT License
5.24k stars 626 forks source link

Allow .asset.json files to be treated as assets #1391

Open bradleyayers opened 4 days ago

bradleyayers commented 4 days ago

Summary

This patch in conjunction with adding asset.json to assetExts allows JSON files to be treated as assets. This is helpful in cases where the JSON file is large and would cause too much bloat in the main bundle.

Perhaps because json was previously in the assetExts array (removed in https://github.com/facebook/metro/pull/593) it was necessary to have a special code path for transforming .json files rather than letting standard asset transformation take priority. However this logic prevents large JSON files from being treated as assets (i.e. not included in the bundle) even when using a custom extension like .asset.json. There is a test for asset.json that hints this should be supported, but the transform prevents it from working in practice (see https://github.com/facebook/metro/blob/412771475c540b6f85d75d9dcd5a39a6e0753582/packages/metro-resolver/src/__tests__/assets-test.js#L67).

Edit: After experimenting with this more, I found that in my use-case using async imports like import("./foo.json") instead works and splits the .json file out into another bundle. With that strategy this PR isn't necessary, so perhaps it's better to just close this.

Changelog:

Test plan