Skip to content

Commit

Permalink
fix(bundler): Fix bugs (#1349)
Browse files Browse the repository at this point in the history
swc_bundler:
 - Fix deglobbing of imports. (denoland/deno#8985)
 - Use correct syntax context while deglobbing imports. (denoland/deno#9212)
 - Allow reexporting from wrapped esms. (denoland/deno#8959, denoland/deno#9200)
 - Fix statement orderings. (denoland/deno#9250)
 - Emit injected items as early as possible. (denoland/deno#9250)
 - Respect `external_modules`. (#1338)
 - Fix cjs suppport. (#1328)

swc_ecma_transforms_base:
 - hygiene: Fix for hoisting. (denoland/deno#9212)
  • Loading branch information
kdy1 authored Jan 25, 2021
1 parent 947f9c5 commit 947161b
Show file tree
Hide file tree
Showing 152 changed files with 23,534 additions and 859 deletions.
5 changes: 1 addition & 4 deletions .github/workflows/cargo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ on: [push, pull_request]

env:
CARGO_INCREMENTAL: 0
CI: "1"

jobs:
fmt:
Expand All @@ -40,7 +39,6 @@ jobs:
name: test
runs-on: ubuntu-latest
strategy:
fail-fast: true
matrix:
crate:
- ast_node
Expand Down Expand Up @@ -105,8 +103,7 @@ jobs:
with:
path: |
~/.cargo/
**/target/
key: ${{ runner.os }}-cargo-test
key: ${{ runner.os }}-cargo-test-${{ matrix.crate }}

- name: Run cargo test
run: |
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ swc_ecma_codegen = {version = "0.43.5", path = "./ecmascript/codegen"}
swc_ecma_ext_transforms = {version = "0.2.4", path = "./ecmascript/ext-transforms"}
swc_ecma_parser = {version = "0.45.4", path = "./ecmascript/parser"}
swc_ecma_preset_env = {version = "0.3.6", path = "./ecmascript/preset_env"}
swc_ecma_transforms = {version = "0.33.6", path = "./ecmascript/transforms", features = [
swc_ecma_transforms = {version = "0.33.7", path = "./ecmascript/transforms", features = [
"compat",
"module",
"optimization",
Expand Down
4 changes: 2 additions & 2 deletions bundler/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ swc_common = {version = "0.10.0", path = "../common"}
swc_ecma_ast = {version = "0.37.3", path = "../ecmascript/ast"}
swc_ecma_codegen = {version = "0.43.5", path = "../ecmascript/codegen"}
swc_ecma_parser = {version = "0.45.4", path = "../ecmascript/parser"}
swc_ecma_transforms = {version = "0.33.6", path = "../ecmascript/transforms", features = ["optimization"]}
swc_ecma_transforms = {version = "0.33.7", path = "../ecmascript/transforms", features = ["optimization"]}
swc_ecma_utils = {version = "0.27.3", path = "../ecmascript/utils"}
swc_ecma_visit = {version = "0.23.3", path = "../ecmascript/visit"}

Expand All @@ -43,7 +43,7 @@ hex = "0.4"
ntest = "0.7.2"
reqwest = {version = "0.10.8", features = ["blocking"]}
sha-1 = "0.9"
swc_ecma_transforms = {version = "0.33.6", path = "../ecmascript/transforms", features = ["react", "typescript"]}
swc_ecma_transforms = {version = "0.33.7", path = "../ecmascript/transforms", features = ["react", "typescript"]}
tempfile = "3.1.0"
testing = {version = "0.10.0", path = "../testing"}
url = "2.1.1"
Expand Down
78 changes: 76 additions & 2 deletions bundler/src/bundler/chunk/computed_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ where
mut module: Modules,
) -> Result<Modules, Error> {
let span = DUMMY_SP;
let var_name = match self.scope.wrapped_esm_id(id) {
let info = self.scope.get_module(id).unwrap();
let module_var_name = match self.scope.wrapped_esm_id(id) {
Some(v) => v,
None => bail!("{:?} should not be wrapped with a function", id),
};
Expand Down Expand Up @@ -94,6 +95,79 @@ where
take(&mut module.body)
.into_iter()
.filter_map(|v| match v {
ModuleItem::Stmt(Stmt::Decl(Decl::Var(var))) => {
// Handle `export *`-s from dependency modules.
//
// See: https://github.com/denoland/deno/issues/9200

if var.span.ctxt == injected_ctxt {
let decl = &var.decls[0];
match &decl.name {
Pat::Ident(i) => {
if i.sym == js_word!("default") {
return Some(Stmt::Decl(Decl::Var(var)));
}

if let Some(remapped) = ctx.transitive_remap.get(&i.span.ctxt) {
// Create
//
// const local = mod.local;
// expodt { local as exported }
//

let local_var = Ident::new(
i.sym.clone(),
i.span.with_ctxt(info.local_ctxt()),
);

let var_decl = VarDeclarator {
span: DUMMY_SP,
name: Pat::Ident(local_var.clone()),
init: Some(Box::new(Expr::Member(MemberExpr {
span: DUMMY_SP,
obj: module_var_name.clone().as_obj(),
prop: {
let mut prop = i.clone();
prop.span.ctxt = SyntaxContext::empty();

Box::new(Expr::Ident(prop))
},
computed: false,
}))),
definite: false,
};
module_items.push(var_decl.into_module_item(
injected_ctxt,
"reexport from wrapped module",
));

let specifier =
ExportSpecifier::Named(ExportNamedSpecifier {
span: DUMMY_SP,
orig: local_var.clone(),
exported: {
let mut exported = local_var.clone();
exported.span.ctxt = remapped;
Some(exported)
},
});
module_items.push(ModuleItem::ModuleDecl(
ModuleDecl::ExportNamed(NamedExport {
span: DUMMY_SP.with_ctxt(injected_ctxt),
specifiers: vec![specifier],
src: None,
type_only: false,
}),
));
}
}
_ => {}
}
}

Some(Stmt::Decl(Decl::Var(var)))
}

ModuleItem::Stmt(s) => Some(s),
ModuleItem::ModuleDecl(ModuleDecl::ExportAll(ref export)) => {
// We handle this later.
Expand Down Expand Up @@ -148,7 +222,7 @@ where
decls: vec![VarDeclarator {
span: DUMMY_SP,
definite: false,
name: Pat::Ident(var_name.into_ident()),
name: Pat::Ident(module_var_name.into_ident()),
init: Some(Box::new(module_expr)),
}],
};
Expand Down
10 changes: 4 additions & 6 deletions bundler/src/bundler/chunk/export.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,6 @@ pub(super) fn inject_export(
entry: &mut Modules,
ctx: &Ctx,
entry_export_ctxt: SyntaxContext,
wrapped: bool,
dep: Modules,
source: Source,
) {
Expand All @@ -121,10 +120,9 @@ pub(super) fn inject_export(
ModuleItem::ModuleDecl(ModuleDecl::ExportAll(ref export))
if export.src.value == source.src.value =>
{
if !wrapped {
let export_ctxt = export.span.ctxt;
ctx.transitive_remap.insert(export_ctxt, entry_export_ctxt);
}
let export_ctxt = export.span.ctxt;
ctx.transitive_remap.insert(export_ctxt, entry_export_ctxt);

*item = Stmt::Empty(EmptyStmt { span: DUMMY_SP }).into()
}
ModuleItem::ModuleDecl(ModuleDecl::Import(ref import))
Expand Down Expand Up @@ -171,7 +169,7 @@ pub(super) fn inject_export(
}
});

entry.prepend_all(dep);
entry.add_dep(dep);
}

/// Converts
Expand Down
60 changes: 52 additions & 8 deletions bundler/src/bundler/chunk/merge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,15 +257,20 @@ where
for source in &deps {
match decl {
ModuleDecl::ExportNamed(export @ NamedExport { src: Some(..), .. }) => {
if self
.config
.external_modules
.contains(&export.src.as_ref().unwrap().value)
{
continue;
}

if export.src.as_ref().unwrap().value == source.src.value {
export.src = None;
break;
}
}
ModuleDecl::ExportAll(export) => {
// TODO
if export.src.value == source.src.value {}
}
ModuleDecl::ExportAll(..) => {}
_ => continue,
}
}
Expand Down Expand Up @@ -377,13 +382,17 @@ where
}

if is_export {
if let Some(src) = &source {
if self.config.external_modules.contains(&src.src.value) {
continue;
}
}
assert!(dep_info.is_es6, "export statements are es6-only");

inject_export(
&mut module,
ctx,
info.export_ctxt(),
self.scope.should_be_wrapped_with_a_fn(info.id),
dep_module,
source.unwrap().clone(),
);
Expand All @@ -402,7 +411,7 @@ where

match dep.ty {
DepType::Transitive => {
module.prepend_all(dep_module);
module.add_dep(dep_module);

log::debug!(
"Merged {} into {} as a transitive es module",
Expand All @@ -423,7 +432,7 @@ where
// );

if info.is_es6 && dep_info.is_es6 {
module.push_all(dep_module);
module.add_dep(dep_module);
continue;
}

Expand Down Expand Up @@ -695,13 +704,29 @@ where

entry.retain_mut(|item| {
match item {
ModuleItem::ModuleDecl(ModuleDecl::ExportAll(..)) => return false,
ModuleItem::ModuleDecl(ModuleDecl::ExportAll(export)) => {
if self.config.external_modules.contains(&export.src.value) {
return true;
}

return false;
}

ModuleItem::ModuleDecl(ModuleDecl::ExportNamed(export)) => {
if let Some(src) = &export.src {
if self.config.external_modules.contains(&src.value) {
return true;
}
}

export.src = None;
}

ModuleItem::ModuleDecl(ModuleDecl::Import(import)) => {
if self.config.external_modules.contains(&import.src.value) {
return true;
}

for (id, p) in &ctx.plan.normal {
if import.span.ctxt == self.scope.get_module(*id).unwrap().export_ctxt() {
log::debug!("Dropping import");
Expand Down Expand Up @@ -772,6 +797,12 @@ where
for item in items {
match item {
ModuleItem::ModuleDecl(ModuleDecl::Import(mut import)) => {
// Preserve imports from node.js builtin modules.
if self.config.external_modules.contains(&import.src.value) {
new.push(ModuleItem::ModuleDecl(ModuleDecl::Import(import)));
continue;
}

if let Some((src, _)) = info
.imports
.specifiers
Expand Down Expand Up @@ -1067,6 +1098,11 @@ where
for stmt in stmts {
match &stmt {
ModuleItem::ModuleDecl(ModuleDecl::Import(import)) => {
if self.config.external_modules.contains(&import.src.value) {
new.push(stmt);
continue;
}

for specifier in &import.specifiers {
match specifier {
ImportSpecifier::Named(named) => match &named.imported {
Expand Down Expand Up @@ -1168,6 +1204,14 @@ where
ModuleItem::ModuleDecl(mut decl) => {
stmt = match decl {
ModuleDecl::ExportNamed(export) => {
if let Some(src) = &export.src {
if self.config.external_modules.contains(&src.value) {
*orig_stmt =
ModuleItem::ModuleDecl(ModuleDecl::ExportNamed(export));
continue;
}
}

for specifier in &export.specifiers {
match specifier {
ExportSpecifier::Namespace(ns) => {
Expand Down
2 changes: 1 addition & 1 deletion bundler/src/bundler/helpers/_require.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ function __spack_require__(mod) {
mod(module, module.exports);
cache = module.exports;
return cache;
}
}()
}
Loading

0 comments on commit 947161b

Please sign in to comment.