From deb82802a4d112079ca6fcaa4bca4054d25cec3b Mon Sep 17 00:00:00 2001 From: OJ Kwon Date: Tue, 19 Apr 2022 05:13:57 -0700 Subject: [PATCH] fix(next-swc/ssg): less aggressive exports drop (#36199) This PR attempts to fix #31855, by loosening conditions to determine what to drop when swc runs transform. Currently, it drops all the export declaration if it's being referenced only in getstatic* in local scope. But as `export` implies, there's no guarantee given export will be used in other modules even if it's not being used in local scope. PR tries to not to drop exports declarations as much if it's not being used locally other than getstatic*. This makes dropping bit ineffecient, but as long as we can't cross-ref across modules that's something unavoidable in my opinion. I don't think implementation itself is quite acceptable: probably need review & revise to the logics. ## Bug - Attempt to close #31855 - [x] Related issues linked using `fixes #number` - [ ] Integration tests added - [ ] Errors have helpful link attached, see `contributing.md` ## Feature - [ ] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR. - [ ] Related issues linked using `fixes #number` - [ ] Integration tests added - [ ] Documentation added - [ ] Telemetry added. In case of a feature if it's used or not. - [ ] Errors have helpful link attached, see `contributing.md` ## Documentation / Examples - [ ] Make sure the linting passes by running `yarn lint` --- packages/next-swc/crates/core/src/next_ssg.rs | 26 ++++++++++++++++--- .../ssg/getStaticProps/issue-31855/input.js | 16 ++++++++++++ .../ssg/getStaticProps/issue-31855/output.js | 5 ++++ 3 files changed, 43 insertions(+), 4 deletions(-) create mode 100644 packages/next-swc/crates/core/tests/fixture/ssg/getStaticProps/issue-31855/input.js create mode 100644 packages/next-swc/crates/core/tests/fixture/ssg/getStaticProps/issue-31855/output.js diff --git a/packages/next-swc/crates/core/src/next_ssg.rs b/packages/next-swc/crates/core/src/next_ssg.rs index d25891c1700cd..2f8987ed21396 100644 --- a/packages/next-swc/crates/core/src/next_ssg.rs +++ b/packages/next-swc/crates/core/src/next_ssg.rs @@ -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>>) -> impl Fold { Repeat::new(NextSsg { @@ -55,9 +57,7 @@ struct State { impl State { #[allow(clippy::wrong_self_convention)] fn is_data_identifier(&mut self, i: &Ident) -> Result { - 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| { @@ -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); diff --git a/packages/next-swc/crates/core/tests/fixture/ssg/getStaticProps/issue-31855/input.js b/packages/next-swc/crates/core/tests/fixture/ssg/getStaticProps/issue-31855/input.js new file mode 100644 index 0000000000000..4d2c4d7005d0d --- /dev/null +++ b/packages/next-swc/crates/core/tests/fixture/ssg/getStaticProps/issue-31855/input.js @@ -0,0 +1,16 @@ +export const revalidateInSeconds = 5 * 60; + +export const getStaticProps = async () => { + return { + props: {}, + revalidate: revalidateInSeconds, + }; +}; + +export default function Home({}) { + return ( +
+

Hello World

+
+ ) +} \ No newline at end of file diff --git a/packages/next-swc/crates/core/tests/fixture/ssg/getStaticProps/issue-31855/output.js b/packages/next-swc/crates/core/tests/fixture/ssg/getStaticProps/issue-31855/output.js new file mode 100644 index 0000000000000..64a7370fbd4bf --- /dev/null +++ b/packages/next-swc/crates/core/tests/fixture/ssg/getStaticProps/issue-31855/output.js @@ -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")); +};