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 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 15 additions & 0 deletions packages/next-swc/crates/core/src/next_ssg.rs
Expand Up @@ -143,6 +143,21 @@ impl Fold for Analyzer<'_> {
e
}

fn fold_jsx_element(&mut self, jsx: JSXElement) -> JSXElement {
match &jsx.opening.name {
JSXElementName::Ident(i) => {
self.add_ref(i.to_id());
}
_ => {}
kdy1 marked this conversation as resolved.
Show resolved Hide resolved
}

jsx.fold_children_with(self)
}

fn fold_export_default_decl(&mut self, export: ExportDefaultDecl) -> ExportDefaultDecl {
pveyes marked this conversation as resolved.
Show resolved Hide resolved
export.fold_children_with(self)
}

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

Expand Down
@@ -0,0 +1,36 @@
import { useState, useEffect } from 'react'
import { Root, Children, 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 />} />
</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,
// this import reference should be removed
UnusedInRender.value,
],
}
}
}
@@ -0,0 +1,15 @@
import { useState, useEffect } from "react";
import { Root, Children, 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)
})));
}