Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

next-swc: fix ssg code elimination when used in render #32709

Merged
merged 5 commits into from
Jan 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions packages/next-swc/crates/core/src/next_ssg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
_ => {}
kdy1 marked this conversation as resolved.
Show resolved Hide resolved
}

jsx.fold_children_with(self)
}

fn fold_fn_decl(&mut self, f: FnDecl) -> FnDecl {
let old_in_data = self.in_data_fn;

Expand Down
Original file line number Diff line number Diff line change
@@ -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 (
<Root x={x}>
<div>
<Children attr={AttributeValue} jsx={<AttributeJSX />} />
<JSXMemberExpression.Deep.Property />
</div>
</Root>
)
}

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,
],
}
}
}
Original file line number Diff line number Diff line change
@@ -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)));
}