-
Notifications
You must be signed in to change notification settings - Fork 118
docs: improve tree shaking docs #4155
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -38,6 +38,54 @@ Native below 0.64. | |
|
|
||
| ## Usage | ||
|
|
||
| If you use `react-native rnx-bundle`, enable tree shaking in your rnx-kit | ||
| configuration: | ||
|
|
||
| ```json | ||
| { | ||
| "rnx-kit": { | ||
| "bundle": { | ||
| "entryFile": "index.js", | ||
| "targets": ["android", "ios"], | ||
| "treeShake": true | ||
| } | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| Then create a production bundle: | ||
|
|
||
| ```sh | ||
| react-native rnx-bundle --platform ios --dev false | ||
| ``` | ||
|
|
||
| The `treeShake` property can also be an options object for this serializer: | ||
|
|
||
| ```json | ||
| { | ||
| "rnx-kit": { | ||
| "bundle": { | ||
| "treeShake": { | ||
| "analyze": true, | ||
| "metafile": "dist/esbuild-meta.json", | ||
| "drop": ["debugger"] | ||
| } | ||
| } | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| For one-off validation, use the command-line override: | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should include that users can refer to the Options section further down for available options. |
||
|
|
||
| ```sh | ||
| react-native rnx-bundle --platform ios --dev false --tree-shake true | ||
| ``` | ||
|
|
||
| Tree shaking only runs for production bundles. If `--dev` is true, it is turned | ||
| off even when `treeShake` is enabled. | ||
|
|
||
| ### Manual Metro Setup | ||
|
|
||
| esbuild works best when we pass it ES6 modules. The first thing we must do is to | ||
| disable import/export transformation by enabling `disableImportExportTransform` | ||
| in `babel.config.js`: | ||
|
|
@@ -99,6 +147,21 @@ We can now create a bundle as usual, e.g.: | |
| react-native bundle --entry-file index.js --platform ios --dev false ... | ||
| ``` | ||
|
|
||
| ## Checking the Result | ||
|
|
||
| Build once without tree shaking and once with it, then compare bundle sizes: | ||
|
|
||
| ```sh | ||
| react-native rnx-bundle --platform ios --dev false --tree-shake false | ||
| mv index.ios.jsbundle index.ios.baseline.jsbundle | ||
|
|
||
| react-native rnx-bundle --platform ios --dev false --tree-shake true | ||
| ls -lh index.ios.baseline.jsbundle index.ios.jsbundle | ||
| ``` | ||
|
Comment on lines
+152
to
+160
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think we need this section. It's not platform agnostic and people should know how to do this already. I would maybe expand the section about the analyzer a bit and include a link to it: https://esbuild.github.io/analyze/ |
||
|
|
||
| For deeper inspection, enable `metafile` and load the generated JSON in an | ||
| esbuild metafile analyzer. | ||
|
|
||
| ## Options | ||
|
|
||
| Similar to | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should mention
@rnx-kit/cliand thernx-bundlecommand in a parenthesis e.g., "if you use@rnx-kit/cli(orreact-native rnx-bundle)…