@parallax-rn/babel-plugin
v0.1.0-beta.17
Published
Babel plugin for Parallax for React Native: per-file source tables in dev builds (opt-in testID injection, dev-only safeguards) and the parallax-bundle-check command. Tested on React Native 0.87.1 (no third-party runtime dependencies).
Readme
@parallax-rn/babel-plugin
Build time part of Parallax. In development builds the plugin writes, for every file with JSX, a table of its elements (where each is used, which component it is in, what it was imported from, the testID the author wrote) and registers it at run time on the documented global __PARALLAX_RN__.sources, so a tool can map what is on screen to code. It never changes the code you wrote: it appends one statement. Optionally it also gives react-native primitives a testID that the accessibility tree shows (off by default, decision 0008).
Zero third-party runtime dependencies; the only dependency is @parallax-rn/protocol at the exact same version.
Install
- Add the package to the project (
npm i -D @parallax-rn/babel-plugin, or your package manager's equivalent). - Babel config: list the plugin before
react-native-worklets/pluginif the project uses it (that plugin must be last).// babel.config.js module.exports = { presets: ['module:@react-native/babel-preset'], plugins: ['@parallax-rn/babel-plugin', 'react-native-worklets/plugin'], }; - Restart Metro with
--reset-cache. Metro's transform cache does not notice a change ofbabel.config.jsor of the plugin (measured in S10: with the same cache the tables are missing after adding the plugin, and the old ones stay after updating it). - Check with the Parallax
doctortool: it reads the tables from the running app.
Update the plugin
Update the package, then restart Metro with --reset-cache. This one matters more than at install: until the cache is reset, tables written by the older version stay in the bundle and silently point to wrong files and lines. Every table carries the version of the plugin that wrote it (plugin), and doctor and status warn "source tables are stale" when it differs from the installed version.
Options
['@parallax-rn/babel-plugin', { injectTestIDs: false, root: process.cwd(), exclude: ['/stories/'] }]| Option | Default | |
|---|---|---|
| injectTestIDs | false | Give react-native primitives without a testID (and without a spread) one named prn:<file>::<n>. It adds nodes to the accessibility tree (95 to 115 on one screen in spike 0004); its effect on E2E suites and VoiceOver is not measured. Elements that have a testID are never touched. |
| root | Babel's working directory (Metro's project root) | File paths in the tables are relative to it. A file outside root gets no table. For sources outside the project (a monorepo package) set root to a directory that contains them; the keys are then relative to that directory, not to the project root. Each table records the root relative to the directory Babel ran in (root: "../.."; empty when they are the same, never absolute), and the Parallax tools use it to show paths relative to the project root. Sources under the project root are what stage 1 supports. |
| exclude | [] | Regular expressions (strings) matched against the relative path; node_modules is always excluded. |
Unknown options are an error. There is no option to force the plugin on in a release build, on purpose.
Files with no JSX, files in node_modules, files marked @generated or codegen in their first three comments, and files outside root get no table.
Dev-only safeguards (decision 0009)
- Active only when Babel's environment is
development(Metro sets it only for dev bundles). Otherwise the output is exactly what it would be without the plugin (tested; a release bundle built with the plugin configured is byte-identical to one built without it). - A build error when the Babel environment is
developmentbutNODE_ENVisproduction(a release pipeline told to build a dev bundle): "Fix: build releases with dev=false...". - The emitted code is guarded by
__DEV__. parallax-bundle-check <bundle> [--json], for CI: searches a release bundle, or the Hermes bytecode made from it, for what the plugin and the SDK leave behind (__PARALLAX_RN__,__PARALLAX_RN_PUSH__, injectedprn:…::nids). Exit code 0 clean, 1 found (listed with counts), 2 usage or file error.npx parallax-bundle-check ios/build/main.jsbundle
What a table looks like
{ p: [0, 3], file: 'src/Home.tsx', injected: false, plugin: '0.1.0', root: '', comps: [['HomeScreen', 6, 17]], els: [[0, 8, 5, 'View', 'HomeScreen', 6, 17, false, 'home/root', 'react-native'], …] }: element number, line, column (1-based, at the <), tag, enclosing component and where it is declared, whether a testID was injected, the author's testID (a literal, a pattern with * for the dynamic parts, or <expr>), and the module the tag was imported from. The contract is in @parallax-rn/protocol.
Cost (spike 0011, a real application)
Dev bundle +0.6% (+0.7% with injection), build time unchanged (4.2 s cold, 1.0 s warm); release bundle byte-identical; about 16 KB of tables are loaded for one screen.
Checks that need a React Native toolchain (not in CI)
scripts/verify-plugin-bundles.mjs <rn project dir> (throwaway project: dev and release bundles, the forgotten gate, Metro's cache), scripts/analyze-app.mjs <app dir> (runs the plugin over an app's sources, read only), scripts/measure-bundles.mjs <app dir> (offline bundle timings with an isolated cache), scripts/hermes-check.mjs (the check on Hermes bytecode).
License
Proprietary and free to use (decision 0014): see LICENSE in the package. The text is the beta terms, version 1. Tested on React Native 0.87.1.
