Skip to content

Commit

Permalink
fix(next-swc/ssg): less aggressive exports drop (#36199)
Browse files Browse the repository at this point in the history
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`
  • Loading branch information
kwonoj committed Apr 19, 2022
1 parent 44c7fd7 commit deb8280
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 4 deletions.
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"));
};

0 comments on commit deb8280

Please sign in to comment.