Skip to content

Commit

Permalink
Fix bundler (#1247)
Browse files Browse the repository at this point in the history
swc_bundler:
 - Handle indirect wrapped es modules. (denoland/deno#8597, denoland/deno#8625)
 - Respect `export { foo }`. (denoland/deno#8626)

swc_ecma_parser:
- Allow `??=`, `||=`, `??=` in non-ts modules. (denoland/deno#8627)

swc_ecma_transforms:
 - Make `hygiene` check if a variable with expanded name exists. (denoland/deno#8620)
 - Handle `??=` correctly.
  • Loading branch information
kdy1 authored Dec 9, 2020
1 parent 8ba2ae9 commit 03f9b93
Show file tree
Hide file tree
Showing 24 changed files with 761 additions and 87 deletions.
2 changes: 1 addition & 1 deletion bundler/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ edition = "2018"
license = "Apache-2.0/MIT"
name = "swc_bundler"
repository = "https://github.com/swc-project/swc.git"
version = "0.17.5"
version = "0.17.6"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[features]
Expand Down
3 changes: 2 additions & 1 deletion bundler/scripts/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
set -eux

cargo test --test fixture
cargo test --test deno $@
(cd ../spack && cargo test --test fixture)
cargo test --test deno $@ -- --nocapture
11 changes: 6 additions & 5 deletions bundler/src/bundler/chunk/computed_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,10 +184,6 @@ impl Fold for ExportToReturn {
Some(Stmt::Decl(export.decl))
}

// Ignore export {} specified by user.
ModuleDecl::ExportNamed(NamedExport {
span, src: None, ..
}) if span.ctxt != self.synthesized_ctxt => None,
ModuleDecl::ExportDefaultDecl(export) => match export.decl {
DefaultDecl::Class(expr) => {
let ident = expr.ident;
Expand Down Expand Up @@ -248,7 +244,12 @@ impl Fold for ExportToReturn {
}
}

return ModuleItem::ModuleDecl(ModuleDecl::ExportNamed(named));
// Ignore export {} specified by user.
if named.src.is_none() && named.span.ctxt != self.synthesized_ctxt {
None
} else {
return ModuleItem::ModuleDecl(ModuleDecl::ExportNamed(named));
}
}
ModuleDecl::TsImportEquals(_) => None,
ModuleDecl::TsExportAssignment(_) => None,
Expand Down
57 changes: 38 additions & 19 deletions bundler/src/bundler/chunk/merge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,24 +116,23 @@ where
module = self.wrap_esm(ctx, dep_id, module)?;

// Inject local_name = wrapped_esm_module_name
let module_ident = specifiers
.iter()
.find_map(|specifier| match specifier {
Specifier::Namespace {
local, all: true, ..
} => Some(local.clone()),
_ => None,
})
.unwrap();
let module_ident = specifiers.iter().find_map(|specifier| match specifier {
Specifier::Namespace {
local, all: true, ..
} => Some(local.clone()),
_ => None,
});

let esm_id = self.scope.wrapped_esm_id(dep_id).unwrap();

module.body.push(
esm_id
.clone()
.assign_to(module_ident.clone())
.into_module_item("merge_direct_import"),
);
if let Some(module_ident) = &module_ident {
module.body.push(
esm_id
.clone()
.assign_to(module_ident.clone())
.into_module_item("merge_direct_import"),
);
}

let plan = ctx.plan.normal.get(&dep_id);
let default_plan;
Expand Down Expand Up @@ -760,7 +759,26 @@ where
continue;
}
}
ImportSpecifier::Namespace(_) => {}
ImportSpecifier::Namespace(s) => {
if let Some((src, _)) = info
.imports
.specifiers
.iter()
.find(|s| s.0.src.value == import.src.value)
{
let esm_id = self.scope.wrapped_esm_id(src.module_id).expect(
"If a namespace impoet specifier is preserved, it means \
failutre of deblobbing and as a result module should be \
marked as wrpaped esm",
);
new.push(
esm_id.clone().assign_to(s.local.clone()).into_module_item(
"from_replace_import_specifiers: namespaced",
),
);
continue;
}
}
}
}

Expand Down Expand Up @@ -828,9 +846,10 @@ where
.orig
.clone()
.assign_to(lhs)
.into_module_item(
"import_deps_named_alias",
),
.into_module_item(&format!(
"import_deps_named_alias of {}",
info.fm.name
)),
);
}
}
Expand Down
2 changes: 1 addition & 1 deletion bundler/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use swc_ecma_utils::ident::IdentLike;
use swc_ecma_visit::{noop_visit_mut_type, VisitMut};

pub(crate) trait VarDeclaratorExt: Into<VarDeclarator> {
fn into_module_item(self, _name: &'static str) -> ModuleItem {
fn into_module_item(self, _name: &str) -> ModuleItem {
ModuleItem::Stmt(Stmt::Decl(Decl::Var(VarDecl {
span: DUMMY_SP,
kind: VarDeclKind::Const,
Expand Down
Loading

0 comments on commit 03f9b93

Please sign in to comment.