Skip to content

fix: allow web transformer to run without a compiled worklet#13

Merged
janicduplessis merged 1 commit into
mainfrom
@janic/web-no-compiled-worklet
Jun 19, 2026
Merged

fix: allow web transformer to run without a compiled worklet#13
janicduplessis merged 1 commit into
mainfrom
@janic/web-no-compiled-worklet

Conversation

@janicduplessis

@janicduplessis janicduplessis commented Jun 19, 2026

Copy link
Copy Markdown
Contributor

Problem

The web TransformerTextInput added in #12 crashes on construction in a real web app:

[rntti] Transformer must be a worklet. Did you forget to add the "worklet" directive?

The Transformer constructor checks for __workletHash to confirm the worklet was compiled. That hash is only added by the react-native-worklets Babel plugin — which Metro runs over node_modules on native, but web bundlers (vite/esbuild) don't when pre-bundling the prebuilt lib/. So on web the function reaches the constructor uncompiled and throws, making any worklet-based transformer (e.g. PhoneNumberTransformer) unusable.

Fix

Web has no UI runtime: the web TransformerTextInput already runs the worklet synchronously in JS (transformer.worklet(...)), so it doesn't need a compiled worklet at all. Skip the __workletHash assertion when Platform.OS === 'web'; native keeps enforcing it so a missing "worklet" directive is still caught there.

Test plan

  • Added a unit test for the web branch (a plain, non-worklet function constructs without throwing on web).
  • Verified in a real web consumer — an app bundling rntti through vite (react-native-web, via react-cosmos). Typing into a PhoneNumberTransformer({ international: true }) input formats live, derives E.164, and resolves the country flag from the area code. Without this fix the same setup throws on mount.
  • yarn test (123 passing), tsc, and eslint clean.

The Transformer constructor required a __workletHash, which is only set when
the worklets babel plugin transforms the code. Web bundlers (vite/esbuild)
don't run that plugin over node_modules, so constructing a transformer threw
on web. The web TransformerTextInput runs the worklet synchronously in JS and
doesn't need a compiled worklet, so skip the check when Platform.OS === 'web'.
@janicduplessis janicduplessis merged commit f85bffb into main Jun 19, 2026
5 checks passed
@janicduplessis janicduplessis deleted the @janic/web-no-compiled-worklet branch June 19, 2026 05:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant