@@ -11,10 +11,11 @@ use swc_ecma_visit::noop_visit_mut_type;
1111use swc_ecma_visit:: VisitMut ;
1212use swc_ecma_visit:: VisitMutWith ;
1313
14+ #[ derive( Debug , Default ) ]
1415pub struct JsxPrecompile {
1516 // The import path to import the jsx runtime from. Will be
1617 // `<import_source>/jsx-runtime`.
17- import_source : String ,
18+ import_source : Option < String > ,
1819 // List of HTML elements which should not be serialized
1920 skip_serialize : Option < Vec < String > > ,
2021 // List of props/attributes that should not be serialized and
@@ -38,25 +39,9 @@ pub struct JsxPrecompile {
3839 import_jsx_escape : Option < Ident > ,
3940}
4041
41- impl Default for JsxPrecompile {
42- fn default ( ) -> Self {
43- Self {
44- next_index : 0 ,
45- templates : vec ! [ ] ,
46- import_source : "react" . to_string ( ) ,
47- skip_serialize : None ,
48- skip_prop_serialize : None ,
49- import_jsx : None ,
50- import_jsx_ssr : None ,
51- import_jsx_attr : None ,
52- import_jsx_escape : None ,
53- }
54- }
55- }
56-
5742impl JsxPrecompile {
5843 pub fn new (
59- import_source : String ,
44+ import_source : Option < String > ,
6045 skip_serialize : Option < Vec < String > > ,
6146 skip_prop_serialize : Option < Vec < String > > ,
6247 ) -> Self {
@@ -1461,7 +1446,10 @@ impl JsxPrecompile {
14611446 }
14621447
14631448 if !imports. is_empty ( ) {
1464- let src = format ! ( "{}/jsx-runtime" , self . import_source) ;
1449+ let src = format ! (
1450+ "{}/jsx-runtime" ,
1451+ self . import_source. as_deref( ) . unwrap_or( "react" )
1452+ ) ;
14651453
14661454 let specifiers = imports
14671455 . into_iter ( )
@@ -2238,8 +2226,8 @@ const a = _jsxTemplate($$_tpl_1);"#,
22382226 JsxPrecompile :: default ( ) ,
22392227 // force tab indentation
22402228 r#"const result = <div>
2241- foo
2242- bar
2229+ foo
2230+ bar
22432231</div>;"# ,
22442232 r#"import { jsxTemplate as _jsxTemplate } from "react/jsx-runtime";
22452233const $$_tpl_1 = [
@@ -2252,8 +2240,8 @@ const result = _jsxTemplate($$_tpl_1);"#,
22522240 JsxPrecompile :: default ( ) ,
22532241 // force space indentation
22542242 r#"const result = <div>
2255- foo
2256- bar
2243+ foo
2244+ bar
22572245</div>;"# ,
22582246 r#"import { jsxTemplate as _jsxTemplate } from "react/jsx-runtime";
22592247const $$_tpl_1 = [
@@ -2659,7 +2647,7 @@ const a = _jsx(MyIsland.Foo, {
26592647 #[ test]
26602648 fn import_source_option_test ( ) {
26612649 test_transform (
2662- JsxPrecompile :: new ( "foobar" . to_string ( ) , None , None ) ,
2650+ JsxPrecompile :: new ( Some ( "foobar" . to_string ( ) ) , None , None ) ,
26632651 r#"const a = <div>foo</div>;"# ,
26642652 r#"import { jsxTemplate as _jsxTemplate } from "foobar/jsx-runtime";
26652653const $$_tpl_1 = [
@@ -2807,7 +2795,7 @@ const a = _jsxTemplate($$_tpl_1);"#,
28072795 fn skip_serialization_test ( ) {
28082796 test_transform (
28092797 JsxPrecompile :: new (
2810- "react" . to_string ( ) ,
2798+ None ,
28112799 Some ( vec ! [ "a" . to_string( ) , "img" . to_string( ) ] ) ,
28122800 None ,
28132801 ) ,
@@ -2831,7 +2819,7 @@ const a = _jsxTemplate($$_tpl_1, _jsx("img", {
28312819 fn skip_prop_serialization_test ( ) {
28322820 test_transform (
28332821 JsxPrecompile :: new (
2834- "react" . to_string ( ) ,
2822+ None ,
28352823 None ,
28362824 Some ( vec ! [ "class" . to_string( ) , "className" . to_string( ) ] ) ,
28372825 ) ,
@@ -3010,11 +2998,7 @@ const a = _jsxTemplate($$_tpl_1, _jsxAttr("class", "foo"), _jsxAttr("className",
30102998 "const a = _jsxTemplate($$_tpl_1);" ,
30112999 ]
30123000 . join ( "\n " ) ;
3013- test_transform (
3014- JsxPrecompile :: new ( "react" . to_string ( ) , None , None ) ,
3015- & input,
3016- & expected,
3017- ) ;
3001+ test_transform ( JsxPrecompile :: new ( None , None , None ) , & input, & expected) ;
30183002 }
30193003 }
30203004
0 commit comments