mirror of
https://github.com/swc-project/swc.git
synced 2024-12-24 06:05:02 +03:00
Fix bundler (#1242)
swc_ecma_codegen: - Fix codegen of unicode escapes. (denoland/deno#8541) swc_bundler: - Allow importing a module multiple time. (denoland/deno#8530)
This commit is contained in:
parent
46b553ecc3
commit
ea6beaa06d
@ -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.2"
|
||||
version = "0.17.3"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
[features]
|
||||
|
@ -18,7 +18,7 @@ use std::{borrow::Cow, collections::HashMap, mem::take};
|
||||
use swc_atoms::js_word;
|
||||
use swc_common::{sync::Lock, FileName, SyntaxContext, DUMMY_SP};
|
||||
use swc_ecma_ast::*;
|
||||
use swc_ecma_utils::{find_ids, prepend, private_ident};
|
||||
use swc_ecma_utils::{find_ids, prepend, private_ident, ExprFactory};
|
||||
use swc_ecma_visit::{noop_fold_type, noop_visit_mut_type, Fold, FoldWith, VisitMut, VisitMutWith};
|
||||
use util::CHashSet;
|
||||
|
||||
@ -102,6 +102,7 @@ where
|
||||
&self,
|
||||
ctx: &Ctx,
|
||||
dep_id: ModuleId,
|
||||
src: &Source,
|
||||
specifiers: &[Specifier],
|
||||
) -> Result<Module, Error> {
|
||||
log::debug!("Merging {:?} directly", dep_id);
|
||||
@ -114,27 +115,25 @@ where
|
||||
let mut module = self.get_module_for_merging(ctx, dep_id, false)?;
|
||||
module = self.wrap_esm(ctx, dep_id, module)?;
|
||||
|
||||
{
|
||||
// Inject local_name = wrapped_esm_module_name
|
||||
// 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,
|
||||
})
|
||||
.unwrap();
|
||||
let esm_id = self.scope.wrapped_esm_id(dep_id).unwrap();
|
||||
|
||||
module.body.push(
|
||||
self.scope
|
||||
.wrapped_esm_id(dep_id)
|
||||
.unwrap()
|
||||
.assign_to(module_ident)
|
||||
.into_module_item("merge_direct_import"),
|
||||
);
|
||||
}
|
||||
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;
|
||||
@ -158,6 +157,23 @@ where
|
||||
.merge_deps(ctx, false, module, plan, &dep_info, false)
|
||||
.context("failed to merge dependencies")?;
|
||||
|
||||
// Required to handle edge cases liek https://github.com/denoland/deno/issues/8530
|
||||
for specifier in specifiers {
|
||||
match specifier {
|
||||
Specifier::Specific { local, alias } => {
|
||||
let i = alias.clone().unwrap_or_else(|| local.clone());
|
||||
|
||||
let from = esm_id.clone().into_ident().make_member(i.clone());
|
||||
|
||||
module.body.push(
|
||||
from.assign_to(i.with_ctxt(src.export_ctxt))
|
||||
.into_module_item("namespace_with_normal"),
|
||||
);
|
||||
}
|
||||
Specifier::Namespace { .. } => continue,
|
||||
}
|
||||
}
|
||||
|
||||
self.handle_import_deps(ctx, &dep_info, &mut module, false);
|
||||
|
||||
module
|
||||
@ -336,8 +352,8 @@ where
|
||||
.iter()
|
||||
.find(|(src, _)| src.module_id == dep.id);
|
||||
|
||||
if let Some((_, specifiers)) = res {
|
||||
self.merge_direct_import(ctx, dep.id, &specifiers)?
|
||||
if let Some((src, specifiers)) = res {
|
||||
self.merge_direct_import(ctx, dep.id, &src, &specifiers)?
|
||||
} else {
|
||||
// Common js requires different planning strategy.
|
||||
self.merge_transitive_import(ctx, dep.id)?
|
||||
|
@ -122,6 +122,19 @@ where
|
||||
idents_to_deglob: HashSet<Id>,
|
||||
}
|
||||
|
||||
impl RawImports {
|
||||
fn insert(&mut self, import: &ImportDecl) {
|
||||
for prev in self.imports.iter_mut() {
|
||||
if prev.src.value == import.src.value {
|
||||
prev.specifiers.extend(import.specifiers.clone());
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
self.imports.push(import.clone());
|
||||
}
|
||||
}
|
||||
|
||||
impl<L, R> ImportHandler<'_, '_, L, R>
|
||||
where
|
||||
L: Load,
|
||||
@ -217,7 +230,7 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
self.info.imports.push(import.clone());
|
||||
self.info.insert(&import);
|
||||
return import;
|
||||
}
|
||||
|
||||
@ -306,17 +319,6 @@ where
|
||||
|
||||
if use_ns {
|
||||
wrapping_required.push(import.src.value.clone());
|
||||
|
||||
import.specifiers.retain(|s| match s {
|
||||
ImportSpecifier::Namespace(_) => true,
|
||||
_ => false,
|
||||
});
|
||||
|
||||
debug_assert_ne!(
|
||||
import.specifiers,
|
||||
vec![],
|
||||
"forced_ns should be modified only if a namespace import specifier exist"
|
||||
);
|
||||
} else {
|
||||
// De-glob namespace imports
|
||||
import.specifiers.retain(|s| match s {
|
||||
@ -506,7 +508,7 @@ where
|
||||
};
|
||||
|
||||
if self.top_level {
|
||||
self.info.imports.push(decl);
|
||||
self.info.insert(&decl);
|
||||
return Expr::Call(e);
|
||||
}
|
||||
|
||||
|
@ -368,6 +368,17 @@ fn deno_8481_1() {
|
||||
)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn deno_8530() {
|
||||
run("tests/deno/deno-8530/input/entry.ts", &[])
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore]
|
||||
fn deno_8545() {
|
||||
run("tests/deno/deno-8545/input/entry.ts", &[])
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn merging_order_01() {
|
||||
run(
|
||||
|
9
bundler/tests/deno/deno-8530/input/entry.ts
Normal file
9
bundler/tests/deno/deno-8530/input/entry.ts
Normal file
@ -0,0 +1,9 @@
|
||||
import Head from "https://deno.land/x/aleph/head.ts"
|
||||
import * as Head2 from "https://deno.land/x/aleph/head.ts"
|
||||
console.log(Head, Head2);
|
||||
if (typeof Head !== 'function') {
|
||||
throw new Error()
|
||||
}
|
||||
if (typeof Head2 !== 'object') {
|
||||
throw new Error()
|
||||
}
|
18
bundler/tests/deno/deno-8545/input/entry.ts
Normal file
18
bundler/tests/deno/deno-8545/input/entry.ts
Normal file
@ -0,0 +1,18 @@
|
||||
import { Application, Router } from "https://raw.githubusercontent.com/kdy1/oak-bundle-issue/master/deps.ts";
|
||||
|
||||
const app = new Application();
|
||||
const router = new Router();
|
||||
|
||||
router.get("/", (ctx) => {
|
||||
ctx.response.body = "Index Page";
|
||||
});
|
||||
|
||||
router.get("/users", (ctx) => {
|
||||
ctx.response.body = "Users Page";
|
||||
});
|
||||
|
||||
app.use(router.routes());
|
||||
app.use(router.allowedMethods());
|
||||
|
||||
console.log(`Now listening on http://0.0.0.0:3000`);
|
||||
await app.listen("0.0.0.0:3000");
|
@ -7,7 +7,7 @@ include = ["Cargo.toml", "src/**/*.rs"]
|
||||
license = "Apache-2.0/MIT"
|
||||
name = "swc_ecma_codegen"
|
||||
repository = "https://github.com/swc-project/swc.git"
|
||||
version = "0.41.1"
|
||||
version = "0.41.2"
|
||||
|
||||
[dependencies]
|
||||
bitflags = "1"
|
||||
|
@ -2463,8 +2463,11 @@ fn escape<'s>(cm: &SourceMap, span: Span, s: &'s str, single_quote: Option<bool>
|
||||
}
|
||||
'u' => match orig_iter.next() {
|
||||
Some('{') => {
|
||||
buf.push('{');
|
||||
loop {
|
||||
if orig_iter.next() == Some('}') {
|
||||
let ch = orig_iter.next();
|
||||
buf.extend(ch);
|
||||
if ch == Some('}') {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
@ -470,6 +470,22 @@ fn integration_01_reduced_01() {
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn dneo_8541_1() {
|
||||
test_from_to(
|
||||
"React.createElement('span', null, '\\u{b7}');",
|
||||
"React.createElement('span', null, '\\u{b7}');",
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn dneo_8541_2() {
|
||||
test_from_to(
|
||||
"React.createElement('span', null, '\\u00b7');",
|
||||
"React.createElement('span', null, '\\u00b7');",
|
||||
);
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
struct Buf(Arc<RwLock<Vec<u8>>>);
|
||||
impl Write for Buf {
|
||||
|
@ -1 +1 @@
|
||||
('\u');
|
||||
('\u{10FFFF}');
|
||||
|
@ -1 +1 @@
|
||||
"\u";
|
||||
"\u{00000000034}";
|
||||
|
@ -1 +1 @@
|
||||
('\u');
|
||||
('\u{0000000000F8}');
|
||||
|
@ -1 +1 @@
|
||||
"\u\u";
|
||||
"\u{714E}\u{8336}";
|
||||
|
@ -1 +1 @@
|
||||
('\u');
|
||||
('\u{00F8}');
|
||||
|
@ -1 +1 @@
|
||||
('\u');
|
||||
('\u{0}');
|
||||
|
@ -1 +1 @@
|
||||
"\u\u\u";
|
||||
"\u{20BB7}\u{91CE}\u{5BB6}";
|
||||
|
Loading…
Reference in New Issue
Block a user