Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions packages/react-native-worklets/__tests__/plugin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`<script>
globalThis._WORKLETS_BUNDLE_MODE_ENABLED = false;
</script>`;

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`<script>
globalThis._WORKLETS_REACT_NATIVE_IMPORTS_ALLOWED = false;
</script>`;

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`<script>
globalThis._WORKLETS_REACT_NATIVE_IMPORTS_ALLOWED = false;
</script>`;

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`<script>
Expand Down
23 changes: 18 additions & 5 deletions packages/react-native-worklets/plugin/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 25 additions & 6 deletions packages/react-native-worklets/plugin/src/bundleMode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,28 @@ export function toggleBundleMode(
return;
}

const propertyName = getGlobalPropertyName(expressionPath);

if (propertyName === '_WORKLETS_BUNDLE_MODE_ENABLED') {
expressionPath.get('right').replaceWith(booleanLiteral(true));
return;
}

if (
propertyName === '_WORKLETS_REACT_NATIVE_IMPORTS_ALLOWED' &&
areReactNativeImportsAllowed(state)
) {
expressionPath.get('right').replaceWith(booleanLiteral(true));
}
}

function getGlobalPropertyName(
expressionPath: NodePath<ExpressionStatement['expression']>
) {
const left = expressionPath.get('left');

if (!left.isMemberExpression()) {
return;
return null;
}

const object = left.get('object');
Expand All @@ -42,13 +60,14 @@ export function toggleBundleMode(
if (
!object.isIdentifier() ||
object.node.name !== 'globalThis' ||
!property.isIdentifier() ||
property.node.name !== '_WORKLETS_BUNDLE_MODE_ENABLED'
!property.isIdentifier()
) {
return;
return null;
}

const right = expressionPath.get('right');
return property.node.name;
}

right.replaceWith(booleanLiteral(true));
function areReactNativeImportsAllowed(state: WorkletsPluginPass) {
return state.opts.workletizableModules?.includes('react-native') ?? false;
}
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,9 @@ function initializeWorkletRuntime() {

if (__DEV__) {
silenceHMRWarnings();
disallowRNImports();
if (!globalThis._WORKLETS_REACT_NATIVE_IMPORTS_ALLOWED) {
disallowRNImports();
}
}

if (getStaticFeatureFlag('FETCH_PREVIEW_ENABLED')) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export function bundleModeInit() {
// Worklets Babel Plugin replaces `false` with `true` here
// when Bundle Mode is enabled.
globalThis._WORKLETS_BUNDLE_MODE_ENABLED = false;
globalThis._WORKLETS_REACT_NATIVE_IMPORTS_ALLOWED = false;

if (!globalThis._WORKLETS_BUNDLE_MODE_ENABLED) {
return;
Expand Down
1 change: 1 addition & 0 deletions packages/react-native-worklets/src/privateGlobals.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ declare global {
var _toString: (value: unknown) => string;
var __workletsModuleProxy: WorkletsModuleProxy;
var _WORKLETS_BUNDLE_MODE_ENABLED: boolean | undefined;
var _WORKLETS_REACT_NATIVE_IMPORTS_ALLOWED: boolean | undefined;
var _WORKLETS_VERSION_CPP: string | undefined;
var _WORKLETS_VERSION_JS: string | undefined;
var _createSerializable: <T>(
Expand Down
Loading