1+ import { createRequire } from 'node:module' ;
12import { PluginOptions , transformSync } from '@babel/core' ;
23import syntaxJSX from '@babel/plugin-syntax-jsx' ;
34import presetReact from '@babel/preset-react' ;
45import presetTypeScript from '@babel/preset-typescript' ;
6+ import * as swc from '@swc/core' ;
57import prettier from 'prettier-2' ;
68import fbt from '../index.tsx' ;
79
10+ const require = createRequire ( import . meta. url ) ;
11+
12+ function resolveSwcPluginPath ( ) {
13+ const tryPaths = [
14+ '../../../swc-plugin-fbtee/target/wasm32-wasip1/debug/swc_plugin_fbtee.wasm' ,
15+ '../../../swc-plugin-fbtee/target/wasm32-wasip1/release/swc_plugin_fbtee.wasm' ,
16+ ] ;
17+
18+ for ( const p of tryPaths ) {
19+ try {
20+ return require . resolve ( p ) ;
21+ } catch {
22+ // continue
23+ }
24+ }
25+
26+ throw new Error (
27+ 'Could not resolve swc-plugin-fbtee .wasm. Please build it (e.g. cargo build --target wasm32-wasi).' ,
28+ ) ;
29+ }
30+
31+ export const fbtSwcPlugin = resolveSwcPluginPath ( ) ;
32+
833export function transform ( source : string , pluginOptions ?: PluginOptions ) {
34+ const useSwc = process . env . USE_SWC === '1' ;
35+
36+ if ( ! useSwc ) {
37+ return (
38+ transformSync ( source , {
39+ ast : false ,
40+ filename : 'source.js' ,
41+ plugins : [ [ fbt , pluginOptions ] ] ,
42+ presets : [ presetTypeScript , presetReact ] ,
43+ sourceType : 'module' ,
44+ } ) ?. code || ''
45+ ) ;
46+ }
47+
948 return (
10- transformSync ( source , {
11- ast : false ,
49+ swc . transformSync ( source , {
50+ isModule : true ,
1251 filename : 'source.js' ,
13- plugins : [ [ fbt , pluginOptions ] ] ,
14- presets : [ presetTypeScript , presetReact ] ,
15- sourceType : 'module' ,
16- } ) ?. code || ''
52+ jsc : {
53+ target : 'es2020' ,
54+ parser : {
55+ syntax : 'ecmascript' ,
56+ jsx : true ,
57+ } ,
58+ transform : {
59+ react : {
60+ runtime : 'preserve' ,
61+ throwIfNamespace : false ,
62+ } ,
63+ } ,
64+ experimental : {
65+ plugins : [ [ fbtSwcPlugin , { } ] ] ,
66+ } ,
67+ } ,
68+ } ) . code || ''
1769 ) ;
1870}
1971
@@ -24,17 +76,46 @@ export function snapshotTransform(
2476 return transform ( source , pluginOptions ) ;
2577}
2678
27- const transformKeepJsx = ( source : string , pluginOptions ?: PluginOptions ) =>
28- prettier . format (
29- transformSync ( source , {
30- ast : false ,
79+ const transformKeepJsx = ( source : string , pluginOptions ?: PluginOptions ) => {
80+ const useSwc = process . env . USE_SWC === '1' ;
81+
82+ if ( ! useSwc ) {
83+ return prettier . format (
84+ transformSync ( source , {
85+ ast : false ,
86+ filename : 'source.js' ,
87+ plugins : [ syntaxJSX , [ fbt , pluginOptions ] ] ,
88+ presets : [ presetTypeScript ] ,
89+ sourceType : 'module' ,
90+ } ) ?. code || '' ,
91+ { parser : 'babel' } ,
92+ ) ;
93+ }
94+
95+ return prettier . format (
96+ swc . transformSync ( source , {
97+ isModule : true ,
3198 filename : 'source.js' ,
32- plugins : [ syntaxJSX , [ fbt , pluginOptions ] ] ,
33- presets : [ presetTypeScript ] ,
34- sourceType : 'module' ,
35- } ) ?. code || '' ,
36- { parser : 'babel' } ,
99+ jsc : {
100+ target : 'es2020' ,
101+ parser : {
102+ syntax : 'ecmascript' ,
103+ jsx : true ,
104+ } ,
105+ transform : {
106+ react : {
107+ runtime : 'preserve' ,
108+ throwIfNamespace : false ,
109+ } ,
110+ } ,
111+ experimental : {
112+ plugins : [ [ fbtSwcPlugin , { } ] ] ,
113+ } ,
114+ } ,
115+ } ) . code || '' ,
116+ { parser : 'swc' } ,
37117 ) ;
118+ } ;
38119
39120export const snapshotTransformKeepJsx = (
40121 source : string ,
0 commit comments