From ae7e593f00a9c0591580223a8db915aad4fa1f4d Mon Sep 17 00:00:00 2001 From: ClarkXia Date: Thu, 26 Oct 2023 16:15:44 +0800 Subject: [PATCH] fix: assets import option --- packages/react-server-component/package.json | 2 +- packages/react-server-component/src/lib.rs | 16 ++++++++++------ 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/packages/react-server-component/package.json b/packages/react-server-component/package.json index 36043c1..4ca7edf 100644 --- a/packages/react-server-component/package.json +++ b/packages/react-server-component/package.json @@ -1,6 +1,6 @@ { "name": "@ice/swc-plugin-react-server-component", - "version": "0.1.0", + "version": "0.1.1", "license": "MIT", "keywords": ["swc-plugin"], "main": "swc_plugin_react_server_component.wasm", diff --git a/packages/react-server-component/src/lib.rs b/packages/react-server-component/src/lib.rs index 9d26598..9d2e04b 100644 --- a/packages/react-server-component/src/lib.rs +++ b/packages/react-server-component/src/lib.rs @@ -20,13 +20,14 @@ struct ModuleImports { specifiers: Vec<(JsWord, Span)>, } -pub fn react_server_component(file_name: FileName, is_server: bool, comments: C) -> impl Fold + VisitMut +pub fn react_server_component(file_name: FileName, is_server: bool, assert_imports: bool, comments: C) -> impl Fold + VisitMut where C: Comments, { as_folder(ReactServerComponent { comments, filepath: file_name.to_string(), is_server, + assert_imports, export_names: vec![], invalid_server_imports: vec![ JsWord::from("client-only"), @@ -60,12 +61,14 @@ where C: Comments, #[derive(Debug, Default, Clone, Deserialize)] #[serde(rename_all = "camelCase", deny_unknown_fields)] struct Config { - pub is_server: bool + pub is_server: bool, + pub assert_imports: bool } struct ReactServerComponent { filepath: String, is_server: bool, + assert_imports: bool, comments: C, export_names: Vec, invalid_server_imports: Vec, @@ -374,15 +377,16 @@ impl VisitMut for ReactServerComponent { fn visit_mut_module(&mut self, module: &mut Module) { let (is_client_entry, is_action_file, imports) = self.collect_top_level_directives_and_imports(module); if self.is_server { - if !is_client_entry { + if !is_client_entry && self.assert_imports { self.assert_server_import(&imports); - } else { + } + if is_client_entry { // Proxy client module. self.create_module_proxy(module); return; } } else { - if !is_action_file { + if !is_action_file && self.assert_imports { self.assert_client_import(&imports); } if is_client_entry { @@ -405,5 +409,5 @@ pub fn process_transform(program: Program, _metadata: TransformPluginProgramMeta Some(s) => FileName::Real(s.into()), None => FileName::Anon, }; - program.fold_with(&mut react_server_component(file_name, config.is_server, PluginCommentsProxy)) + program.fold_with(&mut react_server_component(file_name, config.is_server, config.assert_imports, PluginCommentsProxy)) } \ No newline at end of file