From 9951da982336142bd00b1fba76632dbbd2a837e6 Mon Sep 17 00:00:00 2001 From: Yiming Li Date: Tue, 4 Mar 2025 19:02:44 +0800 Subject: [PATCH 1/2] feat: add compiler only version of addComponentElement --- .changeset/gentle-buses-ring.md | 15 ++ .../react/transform/__test__/fixture.spec.js | 30 +++ packages/react/transform/index.d.ts | 6 +- .../transform/src/swc_plugin_compat/mod.rs | 244 +++++++++++++----- ...uld_add_component_element_compiler_only.js | 15 ++ ...omponent_element_embedded_compiler_only.js | 1 + 6 files changed, 251 insertions(+), 60 deletions(-) create mode 100644 .changeset/gentle-buses-ring.md create mode 100644 packages/react/transform/tests/__swc_snapshots__/src/swc_plugin_compat/mod.rs/should_add_component_element_compiler_only.js create mode 100644 packages/react/transform/tests/__swc_snapshots__/src/swc_plugin_compat/mod.rs/should_add_component_element_embedded_compiler_only.js diff --git a/.changeset/gentle-buses-ring.md b/.changeset/gentle-buses-ring.md new file mode 100644 index 0000000000..1a576c2dae --- /dev/null +++ b/.changeset/gentle-buses-ring.md @@ -0,0 +1,15 @@ +--- +"@lynx-dev/react": patch +--- + +feat: add compiler only version of addComponentElement, it does not support spread props but have no runtime overhead, use it by: + +```js +pluginReactLynx({ + compat: { + addComponentElement: { + compilerOnly: true, + }, + }, +}); +``` diff --git a/packages/react/transform/__test__/fixture.spec.js b/packages/react/transform/__test__/fixture.spec.js index d5bdd06520..3e52ce3038 100644 --- a/packages/react/transform/__test__/fixture.spec.js +++ b/packages/react/transform/__test__/fixture.spec.js @@ -286,6 +286,36 @@ Component, View ).toMatchInlineSnapshot(`[]`); } + { + cfg.compat.addComponentElement = { + compilerOnly: true, + }; + const result = await transformReactLynx(`;`, cfg); + expect( + await formatMessages(result.warnings, { + kind: 'warning', + color: false, + }), + ).toMatchInlineSnapshot(` + [ + "▲ [WARNING] addComponentElement: component with JSXSpread is ignored to avoid badcase, you can switch addComponentElement.compilerOnly to false to enable JSXSpread support + + :1:7: + 1 │ ; + ╵ ~~~ + + ", + ] + `); + expect(result.code).toMatchInlineSnapshot(` + "/*#__PURE__*/ import { jsx as _jsx } from "@lynx-js/react/jsx-runtime"; + _jsx(Comp, { + ...s + }); + " + `); + } + { cfg.compat.addComponentElement = true; const result = await transformReactLynx(`;`, cfg); diff --git a/packages/react/transform/index.d.ts b/packages/react/transform/index.d.ts index a123b09016..8896521be5 100644 --- a/packages/react/transform/index.d.ts +++ b/packages/react/transform/index.d.ts @@ -48,6 +48,10 @@ export interface DarkModeConfig { /** @public */ themeExpr: string } +export interface AddComponentElementConfig { + /** @public */ + compilerOnly: boolean +} export interface CompatVisitorConfig { /** @internal */ target: 'LEPUS' | 'JS' | 'MIXED' @@ -60,7 +64,7 @@ export interface CompatVisitorConfig { /** @public */ additionalComponentAttributes: Array /** @public */ - addComponentElement: boolean + addComponentElement: boolean | AddComponentElementConfig /** * @public * @deprecated diff --git a/packages/react/transform/src/swc_plugin_compat/mod.rs b/packages/react/transform/src/swc_plugin_compat/mod.rs index 494a0b90b5..4529a61df7 100644 --- a/packages/react/transform/src/swc_plugin_compat/mod.rs +++ b/packages/react/transform/src/swc_plugin_compat/mod.rs @@ -63,6 +63,13 @@ pub struct DarkModeConfig { pub theme_expr: String, } +#[napi(object)] +#[derive(Clone, Debug)] +pub struct AddComponentElementConfig { + /// @public + pub compiler_only: bool, +} + #[napi(object)] #[derive(Clone, Debug)] pub struct CompatVisitorConfig { @@ -78,7 +85,7 @@ pub struct CompatVisitorConfig { /// @public pub additional_component_attributes: Vec, /// @public - pub add_component_element: bool, + pub add_component_element: Either, /// @public /// @deprecated pub simplify_ctor_like_react_lynx_2: bool, @@ -101,7 +108,7 @@ impl Default for CompatVisitorConfig { old_runtime_pkg: vec!["@lynx-js/react-runtime".into()], new_runtime_pkg: "@lynx-js/react".into(), additional_component_attributes: vec![], - add_component_element: false, + add_component_element: Either::A(false), simplify_ctor_like_react_lynx_2: false, remove_component_attr_regex: None, disable_deprecated_warning: false, @@ -298,23 +305,28 @@ where fn visit_mut_expr(&mut self, n: &mut Expr) { match n { - Expr::JSXElement(_) => { - let state = (vec![], JSXElement::dummy(), false, false); - self.add_component_element_state.push(state); - n.visit_mut_children_with(self); + Expr::JSXElement(_) => match self.opts.add_component_element { + Either::A(true) => { + let state = (vec![], JSXElement::dummy(), false, false); + self.add_component_element_state.push(state); + n.visit_mut_children_with(self); - let (primitive_attrs, component_jsx, has_spread, should_handle) = - self.add_component_element_state.pop().unwrap(); + let (primitive_attrs, component_jsx, has_spread, should_handle) = + self.add_component_element_state.pop().unwrap(); - if should_handle { - *n = self.wrap_with_lynx_component_helper(( - primitive_attrs, - component_jsx, - has_spread, - should_handle, - )) + if should_handle { + *n = self.wrap_with_lynx_component_helper(( + primitive_attrs, + component_jsx, + has_spread, + should_handle, + )) + } } - } + _ => { + n.visit_mut_children_with(self); + } + }, _ => { n.visit_mut_children_with(self); } @@ -323,26 +335,31 @@ where fn visit_mut_jsx_element_child(&mut self, child: &mut JSXElementChild) { match child { - JSXElementChild::JSXElement(_) => { - let state = (vec![], JSXElement::dummy(), false, false); - self.add_component_element_state.push(state); - child.visit_mut_children_with(self); + JSXElementChild::JSXElement(_) => match self.opts.add_component_element { + Either::A(true) => { + let state = (vec![], JSXElement::dummy(), false, false); + self.add_component_element_state.push(state); + child.visit_mut_children_with(self); - let (primitive_attrs, component_jsx, has_spread, should_handle) = - self.add_component_element_state.pop().unwrap(); + let (primitive_attrs, component_jsx, has_spread, should_handle) = + self.add_component_element_state.pop().unwrap(); - if should_handle { - *child = JSXElementChild::JSXExprContainer(JSXExprContainer { - span: DUMMY_SP, - expr: JSXExpr::Expr(Box::new(self.wrap_with_lynx_component_helper(( - primitive_attrs, - component_jsx, - has_spread, - should_handle, - )))), - }); + if should_handle { + *child = JSXElementChild::JSXExprContainer(JSXExprContainer { + span: DUMMY_SP, + expr: JSXExpr::Expr(Box::new(self.wrap_with_lynx_component_helper(( + primitive_attrs, + component_jsx, + has_spread, + should_handle, + )))), + }); + } } - } + _ => { + child.visit_mut_children_with(self); + } + }, _ => { child.visit_mut_children_with(self); } @@ -458,7 +475,7 @@ where // ignore_this_jsx, default false, // if some attr is removeComponentElement={true}, ignore_this_jsx = true // if some attr is JSXSpread, ignore_this_jsx = true - if self.opts.add_component_element { + if !matches!(self.opts.add_component_element, Either::A(false)) { // we use `iter().rev()` because JSXAttr will override previous SpreadElement ignore_this_jsx = n.opening.attrs.iter().rev().any(|attr| match attr { JSXAttrOrSpread::JSXAttr(attr) => match (&attr.name, &attr.value) { @@ -480,13 +497,29 @@ where (JSXAttrName::JSXNamespacedName(_), None) => false, (JSXAttrName::JSXNamespacedName(_), Some(_)) => false, }, - JSXAttrOrSpread::SpreadElement(_) => { - has_spread = true; + JSXAttrOrSpread::SpreadElement(spread) => { + if matches!( + self.opts.add_component_element, + Either::B(AddComponentElementConfig { + compiler_only: true + }) + ) { + HANDLER.with(|handler| { + handler + .struct_span_warn( + spread.dot3_token, + "addComponentElement: component with JSXSpread is ignored to avoid badcase, you can switch addComponentElement.compilerOnly to false to enable JSXSpread support", + ) + .emit() + }); + } else { + has_spread = true; + } true } }); - if has_spread { + if matches!(self.opts.add_component_element, Either::A(true)) && has_spread { ignore_this_jsx = false; } } @@ -538,7 +571,10 @@ where let mut primitive_attrs = vec![]; - if self.opts.add_component_element && !ignore_this_jsx && !has_spread { + if !matches!(self.opts.add_component_element, Either::A(false)) + && !ignore_this_jsx + && !has_spread + { n.opening.attrs.retain(|a| match &a { JSXAttrOrSpread::JSXAttr(attr) => match &attr.name { JSXAttrName::Ident(ident) => { @@ -602,20 +638,45 @@ where n.visit_mut_children_with(self); self.is_target_jsx_element.pop(); - if self.opts.add_component_element && !ignore_this_jsx { + if !matches!(self.opts.add_component_element, Either::A(false)) && !ignore_this_jsx { // => + if matches!( + self.opts.add_component_element, + Either::B(AddComponentElementConfig { + compiler_only: true + }) + ) { + *n = JSXElement { + span: Default::default(), + opening: JSXOpeningElement { + span: Default::default(), + name: JSXElementName::Ident(IdentName::new("view".into(), Default::default()).into()), + self_closing: false, + attrs: primitive_attrs, + type_args: None, + }, + children: vec![JSXElementChild::JSXElement(Box::new(n.clone()))], + closing: Some(JSXClosingElement { + span: Default::default(), + name: JSXElementName::Ident(IdentName::new("view".into(), Default::default()).into()), + }), + }; - let ( - ref mut primitive_attrs_state, - ref mut component_jsx, - ref mut has_spread_state, - ref mut should_handle, - ) = self.add_component_element_state.last_mut().unwrap(); - - *primitive_attrs_state = primitive_attrs; - *component_jsx = n.take(); - *has_spread_state = has_spread; - *should_handle = true; + n.opening.visit_mut_children_with(self); + n.closing.visit_mut_children_with(self); + } else { + let ( + ref mut primitive_attrs_state, + ref mut component_jsx, + ref mut has_spread_state, + ref mut should_handle, + ) = self.add_component_element_state.last_mut().unwrap(); + + *primitive_attrs_state = primitive_attrs; + *component_jsx = n.take(); + *has_spread_state = has_spread; + *should_handle = true; + } } } else { self.is_target_jsx_element.push(is_target_jsx_element); @@ -942,6 +1003,7 @@ where #[cfg(test)] mod tests { + use napi::Either; use swc_core::{ common::{chain, comments::SingleThreadedComments, Mark}, ecma::{ @@ -954,8 +1016,8 @@ mod tests { }, }; - use crate::CompatVisitor; use crate::CompatVisitorConfig; + use crate::{swc_plugin_compat::AddComponentElementConfig, CompatVisitor}; test!( module, @@ -1124,7 +1186,7 @@ mod tests { resolver(Mark::new(), Mark::new(), true), as_folder(CompatVisitor::<&SingleThreadedComments>::new( CompatVisitorConfig { - add_component_element: true, + add_component_element: Either::A(true), ..Default::default() }, None @@ -1161,7 +1223,46 @@ mod tests { resolver(Mark::new(), Mark::new(), true), as_folder(CompatVisitor::<&SingleThreadedComments>::new( CompatVisitorConfig { - add_component_element: true, + add_component_element: Either::B(AddComponentElementConfig { + compiler_only: true + }), + ..Default::default() + }, + None + )), + hygiene_with_config(Default::default()), + ), + should_add_component_element_compiler_only, + r#" + ; + ; + ; + {}} id="1"/>; + {}} id="1"/>; + ; + ; + ; + ; + ; + ; + ; + ; + ; + ; + "# + ); + + test!( + module, + Syntax::Es(EsSyntax { + jsx: true, + ..Default::default() + }), + |_| chain!( + resolver(Mark::new(), Mark::new(), true), + as_folder(CompatVisitor::<&SingleThreadedComments>::new( + CompatVisitorConfig { + add_component_element: Either::A(true), ..Default::default() }, None @@ -1184,7 +1285,32 @@ mod tests { resolver(Mark::new(), Mark::new(), true), as_folder(CompatVisitor::<&SingleThreadedComments>::new( CompatVisitorConfig { - // add_component_element: true, + add_component_element: Either::B(AddComponentElementConfig { + compiler_only: true + }), + ..Default::default() + }, + None + )), + hygiene_with_config(Default::default()), + ), + should_add_component_element_embedded_compiler_only, + r#" + ; + "# + ); + + test!( + module, + Syntax::Es(EsSyntax { + jsx: true, + ..Default::default() + }), + |_| chain!( + resolver(Mark::new(), Mark::new(), true), + as_folder(CompatVisitor::<&SingleThreadedComments>::new( + CompatVisitorConfig { + // add_component_element: Either::A(true), ..Default::default() }, None @@ -1207,7 +1333,7 @@ mod tests { resolver(Mark::new(), Mark::new(), true), as_folder(CompatVisitor::<&SingleThreadedComments>::new( CompatVisitorConfig { - add_component_element: true, + add_component_element: Either::A(true), simplify_ctor_like_react_lynx_2: true, ..Default::default() }, @@ -1245,7 +1371,7 @@ mod tests { resolver(Mark::new(), Mark::new(), true), as_folder(CompatVisitor::<&SingleThreadedComments>::new( CompatVisitorConfig { - add_component_element: true, + add_component_element: Either::A(true), simplify_ctor_like_react_lynx_2: true, ..Default::default() }, @@ -1285,7 +1411,7 @@ mod tests { resolver(Mark::new(), Mark::new(), true), as_folder(CompatVisitor::<&SingleThreadedComments>::new( CompatVisitorConfig { - add_component_element: true, + add_component_element: Either::A(true), simplify_ctor_like_react_lynx_2: true, ..Default::default() }, @@ -1323,7 +1449,7 @@ mod tests { resolver(Mark::new(), Mark::new(), true), as_folder(CompatVisitor::<&SingleThreadedComments>::new( CompatVisitorConfig { - add_component_element: true, + add_component_element: Either::A(true), simplify_ctor_like_react_lynx_2: true, ..Default::default() }, @@ -1363,7 +1489,7 @@ mod tests { resolver(Mark::new(), Mark::new(), true), as_folder(CompatVisitor::<&SingleThreadedComments>::new( CompatVisitorConfig { - add_component_element: true, + add_component_element: Either::A(true), simplify_ctor_like_react_lynx_2: true, ..Default::default() }, diff --git a/packages/react/transform/tests/__swc_snapshots__/src/swc_plugin_compat/mod.rs/should_add_component_element_compiler_only.js b/packages/react/transform/tests/__swc_snapshots__/src/swc_plugin_compat/mod.rs/should_add_component_element_compiler_only.js new file mode 100644 index 0000000000..5e3391b4fa --- /dev/null +++ b/packages/react/transform/tests/__swc_snapshots__/src/swc_plugin_compat/mod.rs/should_add_component_element_compiler_only.js @@ -0,0 +1,15 @@ +; +; +; +{}} id="1">; +{}} id="1">; +; +; +; +; +; +; +; +; +; +; diff --git a/packages/react/transform/tests/__swc_snapshots__/src/swc_plugin_compat/mod.rs/should_add_component_element_embedded_compiler_only.js b/packages/react/transform/tests/__swc_snapshots__/src/swc_plugin_compat/mod.rs/should_add_component_element_embedded_compiler_only.js new file mode 100644 index 0000000000..86aa0e651a --- /dev/null +++ b/packages/react/transform/tests/__swc_snapshots__/src/swc_plugin_compat/mod.rs/should_add_component_element_embedded_compiler_only.js @@ -0,0 +1 @@ +; From fb08e5e8cab89b921dbdb281a3a08e4c8eec18d6 Mon Sep 17 00:00:00 2001 From: Yiming Li Date: Tue, 4 Mar 2025 20:48:55 +0800 Subject: [PATCH 2/2] feat: support `MIXED` target for worklet --- .changeset/itchy-planes-cry.md | 5 + .../__test__/worklet/runOnBackground.test.js | 2 +- .../react/runtime/src/worklet/runWorklet.ts | 2 +- .../react/transform/__test__/fixture.spec.js | 140 +++++++++++------- .../src/swc_plugin_worklet/gen_stmt.rs | 12 +- .../transform/src/swc_plugin_worklet/mod.rs | 29 ++++ .../should_transform_lepus_general_mixed.js | 19 +++ 7 files changed, 155 insertions(+), 54 deletions(-) create mode 100644 .changeset/itchy-planes-cry.md create mode 100644 packages/react/transform/tests/__swc_snapshots__/src/swc_plugin_worklet/mod.rs/should_transform_lepus_general_mixed.js diff --git a/.changeset/itchy-planes-cry.md b/.changeset/itchy-planes-cry.md new file mode 100644 index 0000000000..12bd33628e --- /dev/null +++ b/.changeset/itchy-planes-cry.md @@ -0,0 +1,5 @@ +--- +"@lynx-js/react": patch +--- + +Support `MIXED` target for worklet, it will be used by unit testing frameworks, etc. diff --git a/packages/react/runtime/__test__/worklet/runOnBackground.test.js b/packages/react/runtime/__test__/worklet/runOnBackground.test.js index 4fcb7f14d5..1e97295029 100644 --- a/packages/react/runtime/__test__/worklet/runOnBackground.test.js +++ b/packages/react/runtime/__test__/worklet/runOnBackground.test.js @@ -42,7 +42,7 @@ describe('runOnBackground', () => { }; expect(() => { runOnBackground(worklet)(1, ['args']); - }).toThrowError('runOnBackground can not be used on the main thread.'); + }).toThrowError('runOnBackground can only be used on the main thread.'); }); it('should throw when native capabilities not fulfilled', () => { diff --git a/packages/react/runtime/src/worklet/runWorklet.ts b/packages/react/runtime/src/worklet/runWorklet.ts index 83d80754fa..eae841395a 100644 --- a/packages/react/runtime/src/worklet/runWorklet.ts +++ b/packages/react/runtime/src/worklet/runWorklet.ts @@ -65,7 +65,7 @@ export function runOnBackground any>(f: Fn): (... throw new Error('runOnBackground requires Lynx sdk version 2.16.'); } if (__JS__) { - throw new Error('runOnBackground can not be used on the main thread.'); + throw new Error('runOnBackground can only be used on the main thread.'); } const obj = f as any as JsFnHandle; if (obj._error) { diff --git a/packages/react/transform/__test__/fixture.spec.js b/packages/react/transform/__test__/fixture.spec.js index 3e52ce3038..29c7c7bb0a 100644 --- a/packages/react/transform/__test__/fixture.spec.js +++ b/packages/react/transform/__test__/fixture.spec.js @@ -1306,60 +1306,100 @@ class X extends Component { }); describe('worklet', () => { - it('member expression', async () => { - const { code } = await transformReactLynx( - `\ -export function getCurrentDelta(event) { - "main thread"; - return foo.bar.baz; -} -`, - { - pluginName: '', - filename: '', - sourcemap: false, - cssScope: false, - jsx: false, - directiveDCE: true, - defineDCE: { - define: { - __LEPUS__: 'true', - __JS__: 'false', - }, - }, - shake: false, - compat: true, - refresh: false, - worklet: { - target: 'LEPUS', + for (const target of ['LEPUS', 'JS', 'MIXED']) { + it('member expression', async () => { + const { code } = await transformReactLynx( + `\ + export function getCurrentDelta(event) { + "main thread"; + return foo.bar.baz; + } + `, + { + pluginName: '', filename: '', - runtimePkg: '@lynx-js/react', + sourcemap: false, + cssScope: false, + jsx: false, + directiveDCE: true, + defineDCE: { + define: { + __LEPUS__: 'true', + __JS__: 'false', + }, + }, + shake: false, + compat: true, + refresh: false, + worklet: { + target, + filename: '', + runtimePkg: '@lynx-js/react', + }, }, - }, - ); + ); - expect(code).toMatchInlineSnapshot(` - "import { loadWorkletRuntime as __loadWorkletRuntime } from "@lynx-js/react"; - var loadWorkletRuntime = __loadWorkletRuntime; - export let getCurrentDelta = { - _c: { - foo: { - bar: { - baz: foo.bar.baz + if (target === 'LEPUS') { + expect(code).toMatchInlineSnapshot(` + "import { loadWorkletRuntime as __loadWorkletRuntime } from "@lynx-js/react"; + var loadWorkletRuntime = __loadWorkletRuntime; + export let getCurrentDelta = { + _c: { + foo: { + bar: { + baz: foo.bar.baz + } } - } - }, - _lepusWorkletHash: "da39:78ee8:1" - }; - loadWorkletRuntime(typeof globDynamicComponentEntry === 'undefined' ? undefined : globDynamicComponentEntry) && registerWorkletInternal("main-thread", "da39:78ee8:1", function(event) { - const getCurrentDelta = lynxWorkletImpl._workletMap["da39:78ee8:1"].bind(this); - let { foo } = this["_c"]; - "main thread"; - return foo.bar.baz; - }); - " - `); - }); + }, + _lepusWorkletHash: "da39:75a1b:1" + }; + loadWorkletRuntime(typeof globDynamicComponentEntry === 'undefined' ? undefined : globDynamicComponentEntry) && registerWorkletInternal("main-thread", "da39:75a1b:1", function(event) { + const getCurrentDelta = lynxWorkletImpl._workletMap["da39:75a1b:1"].bind(this); + let { foo } = this["_c"]; + "main thread"; + return foo.bar.baz; + }); + " + `); + } else if (target === 'JS') { + expect(code).toMatchInlineSnapshot(` + "export let getCurrentDelta = { + _c: { + foo: { + bar: { + baz: foo.bar.baz + } + } + }, + _wkltId: "da39:75a1b:1" + }; + " + `); + } else if (target === 'MIXED') { + expect(code).toMatchInlineSnapshot(` + "import { loadWorkletRuntime as __loadWorkletRuntime } from "@lynx-js/react"; + var loadWorkletRuntime = __loadWorkletRuntime; + export let getCurrentDelta = { + _c: { + foo: { + bar: { + baz: foo.bar.baz + } + } + }, + _wkltId: "da39:75a1b:1" + }; + loadWorkletRuntime(typeof globDynamicComponentEntry === 'undefined' ? undefined : globDynamicComponentEntry) && registerWorkletInternal("main-thread", "da39:75a1b:1", function(event) { + const getCurrentDelta = lynxWorkletImpl._workletMap["da39:75a1b:1"].bind(this); + let { foo } = this["_c"]; + "main thread"; + return foo.bar.baz; + }); + " + `); + } + }); + } it('member expression with multiple times', async () => { const { code } = await transformReactLynx( diff --git a/packages/react/transform/src/swc_plugin_worklet/gen_stmt.rs b/packages/react/transform/src/swc_plugin_worklet/gen_stmt.rs index 4677d6f9df..17e9c74dcf 100644 --- a/packages/react/transform/src/swc_plugin_worklet/gen_stmt.rs +++ b/packages/react/transform/src/swc_plugin_worklet/gen_stmt.rs @@ -29,7 +29,11 @@ impl StmtGen { ( StmtGen::gen_transformed_worklet_expr( - target, + if target == TransformTarget::MIXED { + TransformTarget::JS + } else { + target + }, extracted_value, ident_collector.take_this_expr(), extracted_js_fns.clone(), @@ -38,7 +42,11 @@ impl StmtGen { ), StmtGen::gen_register_worklet_stmt( mode, - target, + if target == TransformTarget::MIXED { + TransformTarget::LEPUS + } else { + target + }, worklet_type, function_name, function, diff --git a/packages/react/transform/src/swc_plugin_worklet/mod.rs b/packages/react/transform/src/swc_plugin_worklet/mod.rs index 30b954acf0..1379e3d110 100644 --- a/packages/react/transform/src/swc_plugin_worklet/mod.rs +++ b/packages/react/transform/src/swc_plugin_worklet/mod.rs @@ -514,6 +514,35 @@ function worklet(event: Event) { "# ); + test!( + module, + Syntax::Typescript(TsSyntax { + ..Default::default() + }), + |_| chain!( + resolver(Mark::new(), Mark::new(), true), + as_folder(WorkletVisitor::new( + TransformMode::Test, + WorkletVisitorConfig { + filename: "index.js".into(), + target: TransformTarget::MIXED, + custom_global_ident_names: None, + runtime_pkg: "@lynx-js/react".into(), + } + )), + hygiene() + ), + should_transform_lepus_general_mixed, + r#" +function worklet(event: Event) { + "main thread"; + console.log(y1); + console.log(this.y1); + let a: object = y1; +} + "# + ); + test!( module, Syntax::Typescript(TsSyntax { diff --git a/packages/react/transform/tests/__swc_snapshots__/src/swc_plugin_worklet/mod.rs/should_transform_lepus_general_mixed.js b/packages/react/transform/tests/__swc_snapshots__/src/swc_plugin_worklet/mod.rs/should_transform_lepus_general_mixed.js new file mode 100644 index 0000000000..272cac1462 --- /dev/null +++ b/packages/react/transform/tests/__swc_snapshots__/src/swc_plugin_worklet/mod.rs/should_transform_lepus_general_mixed.js @@ -0,0 +1,19 @@ +import { loadWorkletRuntime as __loadWorkletRuntime } from "@lynx-js/react"; +var loadWorkletRuntime = __loadWorkletRuntime; +let worklet = { + _c: { + y1 + }, + _wkltId: "a77b:test:1", + ...{ + y1: this.y1 + } +}; +loadWorkletRuntime(typeof globDynamicComponentEntry === 'undefined' ? undefined : globDynamicComponentEntry) && registerWorkletInternal("main-thread", "a77b:test:1", function(event: Event) { + const worklet = lynxWorkletImpl._workletMap["a77b:test:1"].bind(this); + let { y1 } = this["_c"]; + "main thread"; + console.log(y1); + console.log(this.y1); + let a: object = y1; +});