@@ -9,6 +9,7 @@ use farmfe_core::{
99 plugin:: { Plugin , PluginLoadHookResult , PluginResolveHookResult } ,
1010 serde_json,
1111} ;
12+ use farmfe_toolkit:: script:: module_type_from_id;
1213use std:: { collections:: HashMap , path:: Path , sync:: Arc } ;
1314use thiserror:: Error ;
1415use utils:: { normalize_path, path_join} ;
@@ -30,13 +31,23 @@ pub enum VirtualModuleError {
3031#[ derive( Debug ) ]
3132#[ farm_plugin]
3233pub struct FarmPluginVirtualModule {
33- virtual_modules : HashMap < String , String > ,
34- resolved_paths : HashMap < String , String > ,
34+ virtual_modules : HashMap < String , Value > ,
35+ resolved_paths : HashMap < String , Value > ,
36+ }
37+
38+ #[ derive( Debug , Clone , serde:: Deserialize ) ]
39+ #[ serde( untagged) ]
40+ enum Value {
41+ Struct {
42+ raw : String ,
43+ module_type : Option < ModuleType > ,
44+ } ,
45+ Str ( String ) ,
3546}
3647
3748impl FarmPluginVirtualModule {
3849 fn new ( _: & Config , options : String ) -> Self {
39- let virtual_modules: HashMap < String , String > =
50+ let virtual_modules: HashMap < String , Value > =
4051 serde_json:: from_str ( & options) . expect ( "Failed to parse virtual module options" ) ;
4152
4253 let mut resolved_paths = HashMap :: new ( ) ;
@@ -104,9 +115,17 @@ impl FarmPluginVirtualModule {
104115 . virtual_modules
105116 . get ( id)
106117 . or_else ( || self . resolved_paths . get ( id) )
107- . map ( |content| PluginLoadHookResult {
108- content : content. clone ( ) ,
109- module_type : ModuleType :: Js ,
118+ . map ( |value| PluginLoadHookResult {
119+ content : match value {
120+ Value :: Struct { raw, .. } => raw. clone ( ) ,
121+ Value :: Str ( s) => s. clone ( ) ,
122+ } ,
123+ module_type : match value {
124+ Value :: Struct { module_type, .. } => module_type
125+ . clone ( )
126+ . unwrap_or ( module_type_from_id ( id) . unwrap_or ( ModuleType :: Js ) ) ,
127+ Value :: Str ( _) => module_type_from_id ( id) . unwrap_or ( ModuleType :: Js ) ,
128+ } ,
110129 source_map : None ,
111130 } )
112131 }
@@ -159,7 +178,6 @@ impl Plugin for FarmPluginVirtualModule {
159178#[ cfg( test) ]
160179mod tests {
161180 use super :: * ;
162- use std:: path:: PathBuf ;
163181
164182 #[ test]
165183 fn test_virtual_module_creation ( ) {
0 commit comments