diff --git a/src/Transformer.ts b/src/Transformer.ts index d5b76f7..f134744 100644 --- a/src/Transformer.ts +++ b/src/Transformer.ts @@ -1,3 +1,5 @@ +import { Platform } from 'react-native'; + export type Selection = { start: number; end: number }; export type TransformerWorklet = (input: { @@ -17,11 +19,17 @@ export class Transformer { private readonly _worklet: TransformerWorklet; constructor(worklet: TransformerWorklet) { - const workletHash = (worklet as { __workletHash?: number }).__workletHash; - if (workletHash == null) { - throw new Error( - '[rntti] Transformer must be a worklet. Did you forget to add the "worklet" directive?', - ); + // On web there is no UI runtime: the web TransformerTextInput runs the + // worklet synchronously in JS, so it doesn't need to be compiled by the + // worklets plugin. Native runs it on the UI thread and requires a real + // worklet, so keep enforcing the directive there. + if (Platform.OS !== 'web') { + const workletHash = (worklet as { __workletHash?: number }).__workletHash; + if (workletHash == null) { + throw new Error( + '[rntti] Transformer must be a worklet. Did you forget to add the "worklet" directive?', + ); + } } this._worklet = worklet; } diff --git a/src/__tests__/Transformer.test.ts b/src/__tests__/Transformer.test.ts index 59fd56c..965d24e 100644 --- a/src/__tests__/Transformer.test.ts +++ b/src/__tests__/Transformer.test.ts @@ -1,3 +1,5 @@ +import { Platform } from 'react-native'; + import { Transformer } from '../Transformer'; describe('Transformer', () => { @@ -10,6 +12,18 @@ describe('Transformer', () => { ).toThrow(/Transformer must be a worklet/i); }); + it('does not require a compiled worklet on web', () => { + // On web the transformer runs synchronously in JS, so a plain function + // (no worklets plugin) is accepted. + jest.replaceProperty(Platform, 'OS', 'web'); + expect( + () => + new Transformer(({ value }) => { + return { value }; + }), + ).not.toThrow(); + }); + it('accepts a worklet transformer', () => { expect( () =>