fix(swc): Fix bugs (#1932)

swc_common:
 - Fix handling of input source map. (#1930)

swc:
 - Respect `paths`. (#1858)

node:
 - Fix typings of `paths`.
This commit is contained in:
강동윤 2021-07-17 20:16:00 +09:00 committed by GitHub
parent 4a9b31df3e
commit ff47e2539e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 71 additions and 12 deletions

View File

@ -11,7 +11,7 @@ edition = "2018"
license = "Apache-2.0/MIT" license = "Apache-2.0/MIT"
name = "swc" name = "swc"
repository = "https://github.com/swc-project/swc.git" repository = "https://github.com/swc-project/swc.git"
version = "0.29.0" version = "0.29.1"
[lib] [lib]
name = "swc" name = "swc"

View File

@ -6,7 +6,7 @@ edition = "2018"
license = "Apache-2.0/MIT" license = "Apache-2.0/MIT"
name = "swc_common" name = "swc_common"
repository = "https://github.com/swc-project/swc.git" repository = "https://github.com/swc-project/swc.git"
version = "0.11.0" version = "0.11.1"
[features] [features]
concurrent = ["parking_lot"] concurrent = ["parking_lot"]

View File

@ -1074,8 +1074,8 @@ impl SourceMap {
let mut src_id = 0u32; let mut src_id = 0u32;
if let Some(orig) = orig { if let Some(orig) = orig {
for (idx, src) in orig.sources().enumerate() { for src in orig.sources() {
builder.set_source(idx as _, src); let idx = builder.add_source(src);
src_id = idx as u32 + 1; src_id = idx as u32 + 1;
} }
for (idx, contents) in orig.source_contents().enumerate() { for (idx, contents) in orig.source_contents().enumerate() {

View File

@ -6,7 +6,7 @@ edition = "2018"
license = "Apache-2.0/MIT" license = "Apache-2.0/MIT"
name = "swc_ecma_loader" name = "swc_ecma_loader"
repository = "https://github.com/swc-project/swc.git" repository = "https://github.com/swc-project/swc.git"
version = "0.11.0" version = "0.11.1"
[package.metadata.docs.rs] [package.metadata.docs.rs]
all-features = true all-features = true

View File

@ -126,7 +126,11 @@ where
Some(v) => v, Some(v) => v,
None => continue, None => continue,
}; };
let capture = captures.iter().next().flatten().expect(
let mut iter = captures.iter();
let _ = iter.next();
let capture = iter.next().flatten().expect(
"capture group should be created by initializer of TsConfigResolver", "capture group should be created by initializer of TsConfigResolver",
); );
let mut errors = vec![]; let mut errors = vec![];
@ -140,10 +144,15 @@ where
replaced, src replaced, src
) )
}); });
errors.push(match res { errors.push(match res {
Ok(v) => return Ok(v), Ok(v) => return Ok(v),
Err(err) => err, Err(err) => err,
}) });
if to.len() == 1 {
return Ok(FileName::Custom(replaced));
}
} }
bail!( bail!(

View File

@ -6,7 +6,7 @@ edition = "2018"
license = "Apache-2.0/MIT" license = "Apache-2.0/MIT"
name = "swc_ecma_transforms_module" name = "swc_ecma_transforms_module"
repository = "https://github.com/swc-project/swc.git" repository = "https://github.com/swc-project/swc.git"
version = "0.26.0" version = "0.26.1"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]

View File

@ -1,6 +1,6 @@
use anyhow::Error; use anyhow::Error;
use pathdiff::diff_paths; use pathdiff::diff_paths;
use std::{borrow::Cow, path::Component, sync::Arc}; use std::{borrow::Cow, env::current_dir, path::Component, sync::Arc};
use swc_atoms::JsWord; use swc_atoms::JsWord;
use swc_common::FileName; use swc_common::FileName;
use swc_ecma_loader::resolve::Resolve; use swc_ecma_loader::resolve::Resolve;
@ -22,8 +22,8 @@ impl ImportResolver for NoopImportResolver {
/// [ImportResolver] implementation for node.js /// [ImportResolver] implementation for node.js
/// ///
/// Supports [FileName::Real] for `base`, [FileName::Real] and /// Supports [FileName::Real] and [FileName::Anon] for `base`, [FileName::Real]
/// [FileName::Custom] for `target`. ([FileName::Custom] is used for core /// and [FileName::Custom] for `target`. ([FileName::Custom] is used for core
/// modules) /// modules)
#[derive(Debug, Clone, Default)] #[derive(Debug, Clone, Default)]
pub struct NodeImportResolver<R> pub struct NodeImportResolver<R>
@ -64,7 +64,8 @@ where
} }
}; };
let base = match base { let base = match base {
FileName::Real(v) => v, FileName::Real(v) => Cow::Borrowed(v),
FileName::Anon => Cow::Owned(current_dir().expect("failed to get current directory")),
_ => { _ => {
unreachable!( unreachable!(
"Node path provider does not support using `{:?}` as a base file name", "Node path provider does not support using `{:?}` as a base file name",

View File

@ -0,0 +1,26 @@
import swc from "../..";
it("should respect paths", async () => {
const { code } = await swc.transform(`
import foo from '@src/app';
console.log(foo)
`, {
jsc: {
parser: {
syntax: 'typescript',
},
target: 'es2021',
transform: {
},
paths: {
'@src/*': ['bar/*']
}
},
module: {
type: 'commonjs'
},
});
expect(code).toContain(`bar/app`);
})

View File

@ -281,6 +281,10 @@ export interface JscConfig {
* Keep class names. * Keep class names.
*/ */
keepClassNames?: boolean keepClassNames?: boolean
paths?: {
[from: string]: [string]
}
} }
export type JscTarget = export type JscTarget =

View File

@ -5,6 +5,7 @@ use either::Either;
use once_cell::sync::Lazy; use once_cell::sync::Lazy;
use regex::Regex; use regex::Regex;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::hash::BuildHasher;
use std::rc::Rc as RustRc; use std::rc::Rc as RustRc;
use std::{ use std::{
cell::RefCell, cell::RefCell,
@ -908,6 +909,24 @@ impl Merge for JscConfig {
self.target.merge(&from.target); self.target.merge(&from.target);
self.external_helpers.merge(&from.external_helpers); self.external_helpers.merge(&from.external_helpers);
self.keep_class_names.merge(&from.keep_class_names); self.keep_class_names.merge(&from.keep_class_names);
self.paths.merge(&from.paths);
}
}
impl<K, V, S> Merge for HashMap<K, V, S>
where
K: Clone + Eq + std::hash::Hash,
V: Clone,
S: Clone + BuildHasher,
{
fn merge(&mut self, from: &Self) {
if self.is_empty() {
*self = (*from).clone();
} else {
for (k, v) in from {
self.entry(k.clone()).or_insert(v.clone());
}
}
} }
} }