diff --git a/packages/next-swc/crates/core/src/next_ssg.rs b/packages/next-swc/crates/core/src/next_ssg.rs index 7e36c32e12bde..3cc8c6e130261 100644 --- a/packages/next-swc/crates/core/src/next_ssg.rs +++ b/packages/next-swc/crates/core/src/next_ssg.rs @@ -143,6 +143,31 @@ impl Fold for Analyzer<'_> { e } + fn fold_jsx_element(&mut self, jsx: JSXElement) -> JSXElement { + fn get_leftmost_id_member_expr(e: &JSXMemberExpr) -> Id { + match &e.obj { + JSXObject::Ident(i) => { + i.to_id() + } + JSXObject::JSXMemberExpr(e) => { + get_leftmost_id_member_expr(e) + } + } + } + + match &jsx.opening.name { + JSXElementName::Ident(i) => { + self.add_ref(i.to_id()); + } + JSXElementName::JSXMemberExpr(e) => { + self.add_ref(get_leftmost_id_member_expr(e)); + } + _ => {} + } + + jsx.fold_children_with(self) + } + fn fold_fn_decl(&mut self, f: FnDecl) -> FnDecl { let old_in_data = self.in_data_fn; diff --git a/packages/next-swc/crates/core/tests/fixture/ssg/getStaticProps/should-not-remove-import-used-in-render/input.js b/packages/next-swc/crates/core/tests/fixture/ssg/getStaticProps/should-not-remove-import-used-in-render/input.js new file mode 100644 index 0000000000000..196f6585af0a4 --- /dev/null +++ b/packages/next-swc/crates/core/tests/fixture/ssg/getStaticProps/should-not-remove-import-used-in-render/input.js @@ -0,0 +1,38 @@ +import { useState, useEffect } from 'react' +import { Root, Children, JSXMemberExpression, AttributeValue, AttributeJSX, ValueInRender, ValueInEffect, UnusedInRender } from '../' + +export default function Test() { + const [x, setX] = useState(ValueInRender.value) + useEffect(() => { + setX(ValueInEffect.value) + }, []) + + return ( + +
+ } /> + +
+
+ ) +} + +export async function getStaticProps() { + return { + props: { + // simulate import usage inside getStaticProps + used: [ + // these import references should not be removed + Root.value, + Children.value, + AttributeValue.value, + AttributeJSX.value, + ValueInRender.value, + ValueInEffect.value, + JSXMemberExpression.value, + // this import reference should be removed + UnusedInRender.value, + ], + } + } +} diff --git a/packages/next-swc/crates/core/tests/fixture/ssg/getStaticProps/should-not-remove-import-used-in-render/output.js b/packages/next-swc/crates/core/tests/fixture/ssg/getStaticProps/should-not-remove-import-used-in-render/output.js new file mode 100644 index 0000000000000..06e38e50c3b90 --- /dev/null +++ b/packages/next-swc/crates/core/tests/fixture/ssg/getStaticProps/should-not-remove-import-used-in-render/output.js @@ -0,0 +1,15 @@ +import { useState, useEffect } from "react"; +import { Root, Children, JSXMemberExpression, AttributeValue, AttributeJSX, ValueInRender, ValueInEffect } from "../"; +export var __N_SSG = true; +export default function Test() { + const [x, setX] = useState(ValueInRender.value); + useEffect(() => { + setX(ValueInEffect.value); + }, []) + return __jsx(Root, { + x: x + }, __jsx('div', null, __jsx(Children, { + attr: AttributeValue, + jsx: __jsx(AttributeJSX, null) + }), __jsx(JSXMemberExpression.Deep.Property, null))); +}