@@ -12,12 +12,16 @@ use rustc_hash::FxHashMap;
12
12
13
13
use oxc:: {
14
14
CompilerInterface ,
15
- codegen:: CodegenReturn ,
15
+ allocator:: Allocator ,
16
+ codegen:: { CodeGenerator , CodegenOptions , CodegenReturn } ,
16
17
diagnostics:: OxcDiagnostic ,
18
+ parser:: Parser ,
19
+ semantic:: { SemanticBuilder , SemanticBuilderReturn } ,
17
20
span:: SourceType ,
18
21
transformer:: {
19
22
EnvOptions , HelperLoaderMode , HelperLoaderOptions , InjectGlobalVariablesConfig ,
20
- InjectImport , JsxRuntime , ReplaceGlobalDefinesConfig , RewriteExtensionsMode ,
23
+ InjectImport , JsxRuntime , ModuleRunnerTransform , ReplaceGlobalDefinesConfig ,
24
+ RewriteExtensionsMode ,
21
25
} ,
22
26
} ;
23
27
use oxc_napi:: OxcError ;
@@ -713,3 +717,106 @@ pub fn transform(
713
717
errors : compiler. errors . into_iter ( ) . map ( OxcError :: from) . collect ( ) ,
714
718
}
715
719
}
720
+
721
+ #[ derive( Default ) ]
722
+ #[ napi( object) ]
723
+ pub struct ModuleRunnerTransformOptions {
724
+ /// Enable source map generation.
725
+ ///
726
+ /// When `true`, the `sourceMap` field of transform result objects will be populated.
727
+ ///
728
+ /// @default false
729
+ ///
730
+ /// @see {@link SourceMap}
731
+ pub sourcemap : Option < bool > ,
732
+ }
733
+
734
+ #[ derive( Default ) ]
735
+ #[ napi( object) ]
736
+ pub struct ModuleRunnerTransformResult {
737
+ /// The transformed code.
738
+ ///
739
+ /// If parsing failed, this will be an empty string.
740
+ pub code : String ,
741
+
742
+ /// The source map for the transformed code.
743
+ ///
744
+ /// This will be set if {@link TransformOptions#sourcemap} is `true`.
745
+ pub map : Option < SourceMap > ,
746
+
747
+ // Import sources collected during transformation.
748
+ pub deps : Vec < String > ,
749
+
750
+ // Dynamic import sources collected during transformation.
751
+ pub dynamic_deps : Vec < String > ,
752
+
753
+ /// Parse and transformation errors.
754
+ ///
755
+ /// Oxc's parser recovers from common syntax errors, meaning that
756
+ /// transformed code may still be available even if there are errors in this
757
+ /// list.
758
+ pub errors : Vec < OxcError > ,
759
+ }
760
+
761
+ /// Transform JavaScript code to a Vite Node runnable module.
762
+ ///
763
+ /// @param filename The name of the file being transformed.
764
+ /// @param sourceText the source code itself
765
+ /// @param options The options for the transformation. See {@link
766
+ /// ModuleRunnerTransformOptions} for more information.
767
+ ///
768
+ /// @returns an object containing the transformed code, source maps, and any
769
+ /// errors that occurred during parsing or transformation.
770
+ ///
771
+ /// @deprecated Only works for Vite.
772
+ #[ allow( clippy:: needless_pass_by_value, clippy:: allow_attributes) ]
773
+ #[ napi]
774
+ pub fn module_runner_transform (
775
+ filename : String ,
776
+ source_text : String ,
777
+ options : Option < ModuleRunnerTransformOptions > ,
778
+ ) -> ModuleRunnerTransformResult {
779
+ let file_path = Path :: new ( & filename) ;
780
+ let source_type = SourceType :: from_path ( file_path) ;
781
+ let source_type = match source_type {
782
+ Ok ( s) => s,
783
+ Err ( err) => {
784
+ return ModuleRunnerTransformResult {
785
+ code : String :: default ( ) ,
786
+ map : None ,
787
+ deps : vec ! [ ] ,
788
+ dynamic_deps : vec ! [ ] ,
789
+ errors : vec ! [ OxcError :: new( err. to_string( ) ) ] ,
790
+ } ;
791
+ }
792
+ } ;
793
+
794
+ let allocator = Allocator :: default ( ) ;
795
+ let mut parser_ret = Parser :: new ( & allocator, & source_text, source_type) . parse ( ) ;
796
+ let mut program = parser_ret. program ;
797
+
798
+ let SemanticBuilderReturn { semantic, errors } =
799
+ SemanticBuilder :: new ( ) . with_check_syntax_error ( true ) . build ( & program) ;
800
+ parser_ret. errors . extend ( errors) ;
801
+
802
+ let ( symbols, scopes) = semantic. into_symbol_table_and_scope_tree ( ) ;
803
+ let ( deps, dynamic_deps) =
804
+ ModuleRunnerTransform :: default ( ) . transform ( & allocator, & mut program, symbols, scopes) ;
805
+
806
+ let CodegenReturn { code, map, .. } = CodeGenerator :: new ( )
807
+ . with_options ( CodegenOptions {
808
+ source_map_path : options. and_then ( |opts| {
809
+ opts. sourcemap . as_ref ( ) . and_then ( |s| s. then ( || file_path. to_path_buf ( ) ) )
810
+ } ) ,
811
+ ..Default :: default ( )
812
+ } )
813
+ . build ( & program) ;
814
+
815
+ ModuleRunnerTransformResult {
816
+ code,
817
+ map : map. map ( Into :: into) ,
818
+ deps,
819
+ dynamic_deps,
820
+ errors : parser_ret. errors . into_iter ( ) . map ( OxcError :: from) . collect ( ) ,
821
+ }
822
+ }
0 commit comments