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

fix(next-swc/ssg): less aggressive exports drop #36199

Merged
merged 5 commits into from Apr 19, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 22 additions & 4 deletions packages/next-swc/crates/core/src/next_ssg.rs
Expand Up @@ -13,6 +13,8 @@ use swc_ecmascript::{
visit::{noop_fold_type, Fold},
};

static SSG_EXPORTS: &[&str; 3] = &["getStaticProps", "getStaticPaths", "getServerSideProps"];

/// Note: This paths requires running `resolver` **before** running this.
pub fn next_ssg(eliminated_packages: Rc<RefCell<FxHashSet<String>>>) -> impl Fold {
Repeat::new(NextSsg {
Expand Down Expand Up @@ -55,9 +57,7 @@ struct State {
impl State {
#[allow(clippy::wrong_self_convention)]
fn is_data_identifier(&mut self, i: &Ident) -> Result<bool, Error> {
let ssg_exports = &["getStaticProps", "getStaticPaths", "getServerSideProps"];

if ssg_exports.contains(&&*i.sym) {
if SSG_EXPORTS.contains(&&*i.sym) {
if &*i.sym == "getServerSideProps" {
if self.is_prerenderer {
HANDLER.with(|handler| {
Expand Down Expand Up @@ -132,12 +132,30 @@ impl Fold for Analyzer<'_> {

fn fold_export_named_specifier(&mut self, s: ExportNamedSpecifier) -> ExportNamedSpecifier {
if let ModuleExportName::Ident(id) = &s.orig {
self.add_ref(id.to_id());
if !SSG_EXPORTS.contains(&&*id.sym) {
self.add_ref(id.to_id());
}
}

s
}

fn fold_export_decl(&mut self, s: ExportDecl) -> ExportDecl {
if let Decl::Var(d) = &s.decl {
if d.decls.is_empty() {
return s;
}

if let Pat::Ident(id) = &d.decls[0].name {
if !SSG_EXPORTS.contains(&&*id.id.sym) {
self.add_ref(id.to_id());
}
}
}

s.fold_children_with(self)
}

fn fold_expr(&mut self, e: Expr) -> Expr {
let e = e.fold_children_with(self);

Expand Down
@@ -0,0 +1,16 @@
export const revalidateInSeconds = 5 * 60;

export const getStaticProps = async () => {
return {
props: {},
revalidate: revalidateInSeconds,
};
};

export default function Home({}) {
return (
<div>
<p>Hello World</p>
</div>
)
}
@@ -0,0 +1,5 @@
export var __N_SSG = true;
export const revalidateInSeconds = 5 * 60;
export default function Home({}) {
return __jsx("div", null, __jsx("p", null, "Hello World"));
};