diff --git a/Cargo.toml b/Cargo.toml index 4b5f4bdbf76..e55b98a5cb0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,7 +11,7 @@ edition = "2018" license = "Apache-2.0/MIT" name = "swc" repository = "https://github.com/swc-project/swc.git" -version = "0.29.0" +version = "0.29.1" [lib] name = "swc" diff --git a/common/Cargo.toml b/common/Cargo.toml index 745531170ff..9c07ce10345 100644 --- a/common/Cargo.toml +++ b/common/Cargo.toml @@ -6,7 +6,7 @@ edition = "2018" license = "Apache-2.0/MIT" name = "swc_common" repository = "https://github.com/swc-project/swc.git" -version = "0.11.0" +version = "0.11.1" [features] concurrent = ["parking_lot"] diff --git a/common/src/source_map.rs b/common/src/source_map.rs index c6f7df67a11..f6e504f6b6e 100644 --- a/common/src/source_map.rs +++ b/common/src/source_map.rs @@ -1074,8 +1074,8 @@ impl SourceMap { let mut src_id = 0u32; if let Some(orig) = orig { - for (idx, src) in orig.sources().enumerate() { - builder.set_source(idx as _, src); + for src in orig.sources() { + let idx = builder.add_source(src); src_id = idx as u32 + 1; } for (idx, contents) in orig.source_contents().enumerate() { diff --git a/ecmascript/loader/Cargo.toml b/ecmascript/loader/Cargo.toml index 7880af41897..2e0aa27516a 100644 --- a/ecmascript/loader/Cargo.toml +++ b/ecmascript/loader/Cargo.toml @@ -6,7 +6,7 @@ edition = "2018" license = "Apache-2.0/MIT" name = "swc_ecma_loader" repository = "https://github.com/swc-project/swc.git" -version = "0.11.0" +version = "0.11.1" [package.metadata.docs.rs] all-features = true diff --git a/ecmascript/loader/src/resolvers/tsc.rs b/ecmascript/loader/src/resolvers/tsc.rs index 18cc7b88f69..b3c1ddc94a1 100644 --- a/ecmascript/loader/src/resolvers/tsc.rs +++ b/ecmascript/loader/src/resolvers/tsc.rs @@ -126,7 +126,11 @@ where Some(v) => v, 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", ); let mut errors = vec![]; @@ -140,10 +144,15 @@ where replaced, src ) }); + errors.push(match res { Ok(v) => return Ok(v), Err(err) => err, - }) + }); + + if to.len() == 1 { + return Ok(FileName::Custom(replaced)); + } } bail!( diff --git a/ecmascript/transforms/module/Cargo.toml b/ecmascript/transforms/module/Cargo.toml index 186068994b5..cf43ec8350b 100644 --- a/ecmascript/transforms/module/Cargo.toml +++ b/ecmascript/transforms/module/Cargo.toml @@ -6,7 +6,7 @@ edition = "2018" license = "Apache-2.0/MIT" name = "swc_ecma_transforms_module" 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 [dependencies] diff --git a/ecmascript/transforms/module/src/path.rs b/ecmascript/transforms/module/src/path.rs index 7711e132c6f..12113f0b4de 100644 --- a/ecmascript/transforms/module/src/path.rs +++ b/ecmascript/transforms/module/src/path.rs @@ -1,6 +1,6 @@ use anyhow::Error; 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_common::FileName; use swc_ecma_loader::resolve::Resolve; @@ -22,8 +22,8 @@ impl ImportResolver for NoopImportResolver { /// [ImportResolver] implementation for node.js /// -/// Supports [FileName::Real] for `base`, [FileName::Real] and -/// [FileName::Custom] for `target`. ([FileName::Custom] is used for core +/// Supports [FileName::Real] and [FileName::Anon] for `base`, [FileName::Real] +/// and [FileName::Custom] for `target`. ([FileName::Custom] is used for core /// modules) #[derive(Debug, Clone, Default)] pub struct NodeImportResolver @@ -64,7 +64,8 @@ where } }; 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!( "Node path provider does not support using `{:?}` as a base file name", diff --git a/node-swc/__tests__/paths_test.mjs b/node-swc/__tests__/paths_test.mjs new file mode 100644 index 00000000000..5dc3a105c1f --- /dev/null +++ b/node-swc/__tests__/paths_test.mjs @@ -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`); +}) diff --git a/node-swc/src/types.ts b/node-swc/src/types.ts index a06887dc6c7..21688d88b91 100644 --- a/node-swc/src/types.ts +++ b/node-swc/src/types.ts @@ -281,6 +281,10 @@ export interface JscConfig { * Keep class names. */ keepClassNames?: boolean + + paths?: { + [from: string]: [string] + } } export type JscTarget = diff --git a/src/config.rs b/src/config.rs index 39aa52cbc7d..c2dbe859428 100644 --- a/src/config.rs +++ b/src/config.rs @@ -5,6 +5,7 @@ use either::Either; use once_cell::sync::Lazy; use regex::Regex; use serde::{Deserialize, Serialize}; +use std::hash::BuildHasher; use std::rc::Rc as RustRc; use std::{ cell::RefCell, @@ -908,6 +909,24 @@ impl Merge for JscConfig { self.target.merge(&from.target); self.external_helpers.merge(&from.external_helpers); self.keep_class_names.merge(&from.keep_class_names); + self.paths.merge(&from.paths); + } +} + +impl Merge for HashMap +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()); + } + } } }