diff --git a/__tests__/__snapshots__/plugin.test.ts.snap b/__tests__/__snapshots__/plugin.test.ts.snap index c281a83b3ab..4b2ee071d42 100644 --- a/__tests__/__snapshots__/plugin.test.ts.snap +++ b/__tests__/__snapshots__/plugin.test.ts.snap @@ -1620,6 +1620,40 @@ function App() { }" `; +exports[`babel plugin for web configuration doesn't substitute isWeb and shouldBeUseWeb in worklets 1`] = ` +"var _worklet_8641000714901_init_data = { + code: "function foo(){const{isWeb,shouldBeUseWeb}=this.__closure;const x=isWeb();const y=shouldBeUseWeb();}", + location: "/dev/null", + sourceMap: "\\"mock source map\\"", + version: "x.y.z" +}; +var foo = function () { + var _e = [new global.Error(), -3, -27]; + var foo = function foo() { + var x = true; + var y = true; + }; + foo.__closure = { + isWeb: isWeb, + shouldBeUseWeb: shouldBeUseWeb + }; + foo.__workletHash = 8641000714901; + foo.__initData = _worklet_8641000714901_init_data; + foo.__stackDetails = _e; + return foo; +}();" +`; + +exports[`babel plugin for web configuration doesn't substitute isWeb and shouldBeUseWeb with true when substituteWebPlatformChecks option is set to false 1`] = ` +"var x = isWeb(); +var y = shouldBeUseWeb();" +`; + +exports[`babel plugin for web configuration doesn't substitute isWeb and shouldBeUseWeb with true when substituteWebPlatformChecks option is undefined 1`] = ` +"var x = isWeb(); +var y = shouldBeUseWeb();" +`; + exports[`babel plugin for web configuration includes initData when omitNativeOnlyData option is set to false 1`] = ` "var _worklet_17472070642672_init_data = { code: "function foo(){var foo='bar';}", @@ -1653,6 +1687,11 @@ exports[`babel plugin for web configuration skips initData when omitNativeOnlyDa }();" `; +exports[`babel plugin for web configuration substitutes isWeb and shouldBeUseWeb with true when substituteWebPlatformChecks option is set to true 1`] = ` +"var x = true; +var y = true;" +`; + exports[`babel plugin for worklet nesting doesn't process nested worklets when disabled 1`] = ` "var _worklet_1678749606628_init_data = { code: "function foo(x){function bar(x){'worklet';return x+2;}return bar(x)+1;}", diff --git a/__tests__/plugin.test.ts b/__tests__/plugin.test.ts index 3a3a261ef7e..f8df1a8ce8d 100644 --- a/__tests__/plugin.test.ts +++ b/__tests__/plugin.test.ts @@ -1609,5 +1609,68 @@ describe('babel plugin', () => { expect(code).toHaveWorkletData(1); expect(code).toMatchSnapshot(); }); + + it('substitutes isWeb and shouldBeUseWeb with true when substituteWebPlatformChecks option is set to true', () => { + const input = html``; + + const { code } = runPlugin( + input, + {}, + { substituteWebPlatformChecks: true } + ); + expect(code).toContain('var x = true;'); + expect(code).toContain('var y = true;'); + expect(code).toMatchSnapshot(); + }); + + it("doesn't substitute isWeb and shouldBeUseWeb with true when substituteWebPlatformChecks option is set to false", () => { + const input = html``; + + const { code } = runPlugin( + input, + {}, + { substituteWebPlatformChecks: false } + ); + expect(code).toContain('var x = isWeb();'); + expect(code).toContain('var y = shouldBeUseWeb();'); + expect(code).toMatchSnapshot(); + }); + + it("doesn't substitute isWeb and shouldBeUseWeb with true when substituteWebPlatformChecks option is undefined", () => { + const input = html``; + + const { code } = runPlugin(input); + expect(code).toContain('var x = isWeb();'); + expect(code).toContain('var y = shouldBeUseWeb();'); + expect(code).toMatchSnapshot(); + }); + + it("doesn't substitute isWeb and shouldBeUseWeb in worklets", () => { + const input = html``; + + const { code } = runPlugin( + input, + {}, + { substituteWebPlatformChecks: true } + ); + expect(code).toContain('const x=isWeb();'); + expect(code).toContain('const y=shouldBeUseWeb();'); + expect(code).toMatchSnapshot(); + }); }); }); diff --git a/plugin/build/plugin.js b/plugin/build/plugin.js index 27ea4849a1a..5d735485608 100644 --- a/plugin/build/plugin.js +++ b/plugin/build/plugin.js @@ -978,6 +978,26 @@ var require_addCustomGlobals = __commonJS({ } }); +// lib/substituteWebCallExpression.js +var require_substituteWebCallExpression = __commonJS({ + "lib/substituteWebCallExpression.js"(exports2) { + "use strict"; + Object.defineProperty(exports2, "__esModule", { value: true }); + exports2.substituteWebCallExpression = void 0; + var types_1 = require("@babel/types"); + function substituteWebCallExpression(path) { + const callee = path.node.callee; + if ((0, types_1.isIdentifier)(callee)) { + const name = callee.name; + if (name === "isWeb" || name === "shouldBeUseWeb") { + path.replaceWith((0, types_1.booleanLiteral)(true)); + } + } + } + exports2.substituteWebCallExpression = substituteWebCallExpression; + } +}); + // lib/plugin.js Object.defineProperty(exports, "__esModule", { value: true }); var processForCalleesWorklets_1 = require_processForCalleesWorklets(); @@ -986,6 +1006,7 @@ var processInlineStylesWarning_1 = require_processInlineStylesWarning(); var processIfCallback_1 = require_processIfCallback(); var addCustomGlobals_1 = require_addCustomGlobals(); var globals_1 = require_globals(); +var substituteWebCallExpression_1 = require_substituteWebCallExpression(); module.exports = function() { function runWithTaggedExceptions(fun) { try { @@ -1004,7 +1025,12 @@ module.exports = function() { visitor: { CallExpression: { enter(path, state) { - runWithTaggedExceptions(() => (0, processForCalleesWorklets_1.processForCalleesWorklets)(path, state)); + runWithTaggedExceptions(() => { + (0, processForCalleesWorklets_1.processForCalleesWorklets)(path, state); + if (state.opts.substituteWebPlatformChecks) { + (0, substituteWebCallExpression_1.substituteWebCallExpression)(path); + } + }); } }, "FunctionDeclaration|FunctionExpression|ArrowFunctionExpression": { diff --git a/plugin/src/plugin.ts b/plugin/src/plugin.ts index 3a00944298e..431675b8b6a 100644 --- a/plugin/src/plugin.ts +++ b/plugin/src/plugin.ts @@ -7,6 +7,7 @@ import { processInlineStylesWarning } from './processInlineStylesWarning'; import { processIfCallback } from './processIfCallback'; import { addCustomGlobals } from './addCustomGlobals'; import { initializeGlobals } from './globals'; +import { substituteWebCallExpression } from './substituteWebCallExpression'; module.exports = function (): PluginItem { function runWithTaggedExceptions(fun: () => void) { @@ -27,7 +28,12 @@ module.exports = function (): PluginItem { visitor: { CallExpression: { enter(path: NodePath, state: ReanimatedPluginPass) { - runWithTaggedExceptions(() => processForCalleesWorklets(path, state)); + runWithTaggedExceptions(() => { + processForCalleesWorklets(path, state); + if (state.opts.substituteWebPlatformChecks) { + substituteWebCallExpression(path); + } + }); }, }, 'FunctionDeclaration|FunctionExpression|ArrowFunctionExpression': { diff --git a/plugin/src/substituteWebCallExpression.ts b/plugin/src/substituteWebCallExpression.ts new file mode 100644 index 00000000000..87e5ccb1f82 --- /dev/null +++ b/plugin/src/substituteWebCallExpression.ts @@ -0,0 +1,13 @@ +import type { NodePath } from '@babel/core'; +import { booleanLiteral, isIdentifier } from '@babel/types'; +import type { CallExpression } from '@babel/types'; + +export function substituteWebCallExpression(path: NodePath) { + const callee = path.node.callee; + if (isIdentifier(callee)) { + const name = callee.name; + if (name === 'isWeb' || name === 'shouldBeUseWeb') { + path.replaceWith(booleanLiteral(true)); + } + } +} diff --git a/plugin/src/types.ts b/plugin/src/types.ts index 50c6cd0dd8a..2ea1c613e93 100644 --- a/plugin/src/types.ts +++ b/plugin/src/types.ts @@ -12,6 +12,7 @@ export interface ReanimatedPluginOptions { processNestedWorklets?: boolean; omitNativeOnlyData?: boolean; globals?: string[]; + substituteWebPlatformChecks?: boolean; } export interface ReanimatedPluginPass {