diff --git a/packages/loadable-components/src/lib.rs b/packages/loadable-components/src/lib.rs index 636067d96..e55c85c51 100644 --- a/packages/loadable-components/src/lib.rs +++ b/packages/loadable-components/src/lib.rs @@ -127,7 +127,43 @@ where false } + fn has_ssr_false(&self, call: &CallExpr) -> bool { + let options = match call.args.get(1) { + Some(arg) => &arg.expr, + None => return false, + }; + + let obj = match options.as_ref() { + Expr::Object(obj) => obj, + _ => return false, + }; + + obj.props.iter().any(|prop| match prop { + PropOrSpread::Prop(prop) => match prop.as_ref() { + Prop::KeyValue(kv) => { + let is_ssr_key = match &kv.key { + PropName::Ident(i) => &*i.sym == "ssr", + PropName::Str(s) => s.value == "ssr", + _ => false, + }; + is_ssr_key + && matches!( + kv.value.as_ref(), + Expr::Lit(Lit::Bool(Bool { value: false, .. })) + ) + } + _ => false, + }, + _ => false, + }) + } + fn transform_import_expr(&mut self, call: &mut CallExpr) { + // Skip transformation if ssr: false is specified in the options + if self.has_ssr_false(call) { + return; + } + let import = { let mut v = ImportFinder::default(); call.visit_with(&mut v); diff --git a/packages/loadable-components/tests/fixture/simple import/should not transform when ssr is false/input.js b/packages/loadable-components/tests/fixture/simple import/should not transform when ssr is false/input.js new file mode 100644 index 000000000..f27e9be13 --- /dev/null +++ b/packages/loadable-components/tests/fixture/simple import/should not transform when ssr is false/input.js @@ -0,0 +1,2 @@ +import loadable from "@loadable/component"; +loadable(() => import("./OtherComponent"), { ssr: false }); diff --git a/packages/loadable-components/tests/fixture/simple import/should not transform when ssr is false/output.js b/packages/loadable-components/tests/fixture/simple import/should not transform when ssr is false/output.js new file mode 100644 index 000000000..539f69fe8 --- /dev/null +++ b/packages/loadable-components/tests/fixture/simple import/should not transform when ssr is false/output.js @@ -0,0 +1,4 @@ +import loadable from "@loadable/component"; +loadable(()=>import("./OtherComponent"), { + ssr: false +});