Skip to content

Commit 9f6ee15

Browse files
committed
fix: farmfe_plugin_virtual options
1 parent 845e77b commit 9f6ee15

File tree

5 files changed

+58
-9
lines changed

5 files changed

+58
-9
lines changed

Cargo.lock

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rust-plugins/virtual/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,6 @@ crate-type = ["cdylib", "rlib"]
1010
farmfe_toolkit_plugin_types = { workspace = true }
1111
farmfe_macro_plugin = { workspace = true }
1212
farmfe_core = { workspace = true }
13+
farmfe_toolkit = { workspace = true }
1314
thiserror = "2.0.3"
15+
serde = { version = "1.0.197", features = ["derive"] }

rust-plugins/virtual/options.d.ts

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,29 @@
1-
export type IPluginOptions = Record<string, string>;
1+
/**
2+
* The value can be a string or an object with raw content and module type.
3+
* @example
4+
* // As a string
5+
* const options: IPluginOptions = {
6+
* 'virtual-module.js': "export default 'Hello, world!';"
7+
* };
8+
*
9+
* // As an object
10+
* const options: IPluginOptions = {
11+
* 'virtual-module.js': {
12+
* raw: "export default 'Hello, world!';",
13+
* moduleType: "js"
14+
* }
15+
* };
16+
*/
17+
type Value = string | {
18+
/**
19+
* The raw content of the module.
20+
* @example "export default 'Hello, world!';"
21+
*/
22+
raw: string;
23+
/**
24+
* The type of the module.
25+
* @default "js"
26+
*/
27+
moduleType: "js" | "ts" | "jsx" | "tsx" | 'css' | 'html' | 'json' | 'asset';
28+
};
29+
export type IPluginOptions = Record<string, Value>;

rust-plugins/virtual/src/lib.rs

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -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;
1213
use std::{collections::HashMap, path::Path, sync::Arc};
1314
use thiserror::Error;
1415
use utils::{normalize_path, path_join};
@@ -30,13 +31,23 @@ pub enum VirtualModuleError {
3031
#[derive(Debug)]
3132
#[farm_plugin]
3233
pub 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

3748
impl 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)]
160179
mod tests {
161180
use super::*;
162-
use std::path::PathBuf;
163181

164182
#[test]
165183
fn test_virtual_module_creation() {

rust-plugins/virtual/src/utils.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,6 @@ pub fn normalize_path<P: AsRef<Path>>(path: P) -> PathBuf {
8282
#[cfg(test)]
8383
mod tests {
8484
use super::*;
85-
use std::env;
8685

8786
#[test]
8887
fn test_normalize_path() {

0 commit comments

Comments
 (0)