Unverified Commit deb82802 authored by OJ Kwon's avatar OJ Kwon Committed by GitHub

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`
parent 44c7fd79
...@@ -13,6 +13,8 @@ use swc_ecmascript::{ ...@@ -13,6 +13,8 @@ use swc_ecmascript::{
visit::{noop_fold_type, Fold}, visit::{noop_fold_type, Fold},
}; };
static SSG_EXPORTS: &[&str; 3] = &["getStaticProps", "getStaticPaths", "getServerSideProps"];
/// Note: This paths requires running `resolver` **before** running this. /// Note: This paths requires running `resolver` **before** running this.
pub fn next_ssg(eliminated_packages: Rc<RefCell<FxHashSet<String>>>) -> impl Fold { pub fn next_ssg(eliminated_packages: Rc<RefCell<FxHashSet<String>>>) -> impl Fold {
Repeat::new(NextSsg { Repeat::new(NextSsg {
...@@ -55,9 +57,7 @@ struct State { ...@@ -55,9 +57,7 @@ struct State {
impl State { impl State {
#[allow(clippy::wrong_self_convention)] #[allow(clippy::wrong_self_convention)]
fn is_data_identifier(&mut self, i: &Ident) -> Result<bool, Error> { 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 &*i.sym == "getServerSideProps" {
if self.is_prerenderer { if self.is_prerenderer {
HANDLER.with(|handler| { HANDLER.with(|handler| {
...@@ -132,12 +132,30 @@ impl Fold for Analyzer<'_> { ...@@ -132,12 +132,30 @@ impl Fold for Analyzer<'_> {
fn fold_export_named_specifier(&mut self, s: ExportNamedSpecifier) -> ExportNamedSpecifier { fn fold_export_named_specifier(&mut self, s: ExportNamedSpecifier) -> ExportNamedSpecifier {
if let ModuleExportName::Ident(id) = &s.orig { if let ModuleExportName::Ident(id) = &s.orig {
if !SSG_EXPORTS.contains(&&*id.sym) {
self.add_ref(id.to_id()); self.add_ref(id.to_id());
} }
}
s 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 { fn fold_expr(&mut self, e: Expr) -> Expr {
let e = e.fold_children_with(self); let e = e.fold_children_with(self);
......
export const revalidateInSeconds = 5 * 60;
export const getStaticProps = async () => {
return {
props: {},
revalidate: revalidateInSeconds,
};
};
export default function Home({}) {
return (
<div>
<p>Hello World</p>
</div>
)
}
\ No newline at end of file
export var __N_SSG = true;
export const revalidateInSeconds = 5 * 60;
export default function Home({}) {
return __jsx("div", null, __jsx("p", null, "Hello World"));
};
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment