From 4e8e3d5199c478bfbe4578b7bcb45d3a7e5406f8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hanno=20J=2E=20G=C3=B6decke?= Date: Sun, 5 Apr 2026 18:50:18 +0200 Subject: [PATCH] allow react-native imports in bundle mode --- .../__tests__/plugin.test.ts | 46 +++++++++++++++++++ .../react-native-worklets/plugin/index.js | 23 ++++++++-- .../plugin/src/bundleMode.ts | 31 ++++++++++--- .../src/initializers/initializers.native.ts | 4 +- .../workletRuntimeEntry.native.ts | 1 + .../src/privateGlobals.d.ts | 1 + 6 files changed, 94 insertions(+), 12 deletions(-) diff --git a/packages/react-native-worklets/__tests__/plugin.test.ts b/packages/react-native-worklets/__tests__/plugin.test.ts index 868f29af34ac..73dc3d64d3e3 100644 --- a/packages/react-native-worklets/__tests__/plugin.test.ts +++ b/packages/react-native-worklets/__tests__/plugin.test.ts @@ -540,6 +540,52 @@ describe('babel plugin', () => { }); }); + describe('for bundle mode runtime entry', () => { + const workletRuntimeEntryFilename = + '/tmp/react-native-worklets/src/initializers/workletRuntimeEntry.native.ts'; + + test('enables bundle mode in worklet runtime entry', () => { + const input = html``; + + const { code } = runPlugin(input, undefined, { + bundleMode: true, + }, workletRuntimeEntryFilename); + + expect(code).toContain('globalThis._WORKLETS_BUNDLE_MODE_ENABLED = true;'); + }); + + test('allows react-native imports when explicitly configured', () => { + const input = html``; + + const { code } = runPlugin(input, undefined, { + bundleMode: true, + workletizableModules: ['react-native'], + }, workletRuntimeEntryFilename); + + expect(code).toContain( + 'globalThis._WORKLETS_REACT_NATIVE_IMPORTS_ALLOWED = true;' + ); + }); + + test("doesn't allow react-native imports by default", () => { + const input = html``; + + const { code } = runPlugin(input, undefined, { + bundleMode: true, + }, workletRuntimeEntryFilename); + + expect(code).toContain( + 'globalThis._WORKLETS_REACT_NATIVE_IMPORTS_ALLOWED = false;' + ); + }); + }); + describe('for explicit worklets', () => { test('workletizes FunctionDeclaration', () => { const input = html`