Skip to content

Commit

Permalink
fix: assets import option
Browse files Browse the repository at this point in the history
  • Loading branch information
ClarkXia committed Oct 26, 2023
1 parent 0c07ac5 commit ae7e593
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
2 changes: 1 addition & 1 deletion packages/react-server-component/package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
16 changes: 10 additions & 6 deletions packages/react-server-component/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,14 @@ struct ModuleImports {
specifiers: Vec<(JsWord, Span)>,
}

pub fn react_server_component<C>(file_name: FileName, is_server: bool, comments: C) -> impl Fold + VisitMut
pub fn react_server_component<C>(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"),
Expand Down Expand Up @@ -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<C: Comments> {
filepath: String,
is_server: bool,
assert_imports: bool,
comments: C,
export_names: Vec<String>,
invalid_server_imports: Vec<JsWord>,
Expand Down Expand Up @@ -374,15 +377,16 @@ impl <C: Comments> VisitMut for ReactServerComponent<C> {
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 {
Expand All @@ -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))
}

0 comments on commit ae7e593

Please sign in to comment.