Skip to content

Commit

Permalink
Update swc (#33063)
Browse files Browse the repository at this point in the history
  • Loading branch information
kdy1 committed Jan 10, 2022
1 parent 320986a commit 87dbd03
Show file tree
Hide file tree
Showing 10 changed files with 378 additions and 568 deletions.
522 changes: 152 additions & 370 deletions packages/next-swc/Cargo.lock

Large diffs are not rendered by default.

18 changes: 9 additions & 9 deletions packages/next-swc/crates/core/Cargo.toml
Expand Up @@ -14,21 +14,21 @@ fxhash = "0.2.1"
pathdiff = "0.2.0"
serde = "1"
serde_json = "1"
styled_components = "0.6.0"
swc = "0.98.0"
styled_components = "0.9.0"
swc = "0.110.0"
swc_atoms = "0.2.7"
swc_common = { version = "0.15.0", features = ["concurrent", "sourcemap"] }
swc_css = "0.44.0"
swc_ecma_loader = { version = "0.25.0", features = ["node", "lru"] }
swc_ecmascript = { version = "0.98.0", features = ["codegen", "minifier", "optimization", "parser", "react", "transforms", "typescript", "utils", "visit"] }
swc_common = { version = "0.16.0", features = ["concurrent", "sourcemap"] }
swc_css = "0.45.0"
swc_ecma_loader = { version = "0.26.0", features = ["node", "lru"] }
swc_ecmascript = { version = "0.105.0", features = ["codegen", "minifier", "optimization", "parser", "react", "transforms", "typescript", "utils", "visit"] }
swc_node_base = "0.5.1"
swc_stylis = "0.41.1"
swc_stylis = "0.42.0"
tracing = {version = "0.1.28", features = ["release_max_level_off"]}
regex = "1.5"

[dev-dependencies]
swc_ecma_transforms_testing = "0.51.0"
testing = "0.16.0"
swc_ecma_transforms_testing = "0.56.0"
testing = "0.17.0"
walkdir = "2.3.2"


44 changes: 25 additions & 19 deletions packages/next-swc/crates/core/src/next_ssg.rs
Expand Up @@ -11,15 +11,15 @@ use swc_ecmascript::{
visit::{noop_fold_type, Fold},
};

/// Note: This paths requires runnning `resolver` **before** running this.
/// Note: This paths requires running `resolver` **before** running this.
pub fn next_ssg() -> impl Fold {
Repeat::new(NextSsg {
state: Default::default(),
in_lhs_of_var: false,
})
}

/// State of the transforms. Shared by the anayzer and the tranform.
/// State of the transforms. Shared by the analyzer and the transform.
#[derive(Debug, Default)]
struct State {
/// Identifiers referenced by non-data function codes.
Expand All @@ -45,11 +45,7 @@ struct State {

impl State {
fn is_data_identifier(&mut self, i: &Ident) -> Result<bool, Error> {
let ssg_exports = &[
"getStaticProps",
"getStaticPaths",
"getServerSideProps",
];
let ssg_exports = &["getStaticProps", "getStaticPaths", "getServerSideProps"];

if ssg_exports.contains(&&*i.sym) {
if &*i.sym == "getServerSideProps" {
Expand Down Expand Up @@ -125,7 +121,9 @@ impl Fold for Analyzer<'_> {
}

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

s
}
Expand Down Expand Up @@ -189,7 +187,7 @@ impl Fold for Analyzer<'_> {
e
}

/// Drops [ExportDecl] if all speicifers are removed.
/// Drops [ExportDecl] if all specifiers are removed.
fn fold_module_item(&mut self, s: ModuleItem) -> ModuleItem {
match s {
ModuleItem::ModuleDecl(ModuleDecl::ExportNamed(e)) if !e.specifiers.is_empty() => {
Expand All @@ -211,8 +209,7 @@ impl Fold for Analyzer<'_> {
ModuleItem::ModuleDecl(ModuleDecl::ExportDecl(e)) => match &e.decl {
Decl::Fn(f) => {
// Drop getStaticProps.
if let Ok(is_data_identifier) = self.state.is_data_identifier(&f.ident)
{
if let Ok(is_data_identifier) = self.state.is_data_identifier(&f.ident) {
if is_data_identifier {
return ModuleItem::Stmt(Stmt::Empty(EmptyStmt { span: DUMMY_SP }));
}
Expand Down Expand Up @@ -493,29 +490,38 @@ impl Fold for NextSsg {

n.specifiers.retain(|s| {
let preserve = match s {
ExportSpecifier::Namespace(ExportNamespaceSpecifier { name: exported, .. })
ExportSpecifier::Namespace(ExportNamespaceSpecifier {
name: ModuleExportName::Ident(exported),
..
})
| ExportSpecifier::Default(ExportDefaultSpecifier { exported, .. })
| ExportSpecifier::Named(ExportNamedSpecifier {
exported: Some(exported),
exported: Some(ModuleExportName::Ident(exported)),
..
}) => self
.state
.is_data_identifier(&exported)
.map(|is_data_identifier| !is_data_identifier),
ExportSpecifier::Named(s) => self
ExportSpecifier::Named(ExportNamedSpecifier {
orig: ModuleExportName::Ident(orig),
..
}) => self
.state
.is_data_identifier(&s.orig)
.is_data_identifier(&orig)
.map(|is_data_identifier| !is_data_identifier),

_ => Ok(true),
};

match preserve {
Ok(false) => {
tracing::trace!(
"Dropping a export specifier because it's a data identifier"
);
tracing::trace!("Dropping a export specifier because it's a data identifier");

match s {
ExportSpecifier::Named(ExportNamedSpecifier { orig, .. }) => {
ExportSpecifier::Named(ExportNamedSpecifier {
orig: ModuleExportName::Ident(orig),
..
}) => {
self.state.should_run_again = true;
self.state.refs_from_data_fn.insert(orig.to_id());
}
Expand Down

0 comments on commit 87dbd03

Please sign in to comment.