Skip to content

Commit

Permalink
fix(swc): Fix bugs (#1932)
Browse files Browse the repository at this point in the history
swc_common:
 - Fix handling of input source map. (#1930)

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

node:
 - Fix typings of `paths`.
  • Loading branch information
kdy1 authored Jul 17, 2021
1 parent 4a9b31d commit ff47e25
Show file tree
Hide file tree
Showing 10 changed files with 71 additions and 12 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down
4 changes: 2 additions & 2 deletions common/src/source_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
2 changes: 1 addition & 1 deletion ecmascript/loader/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 11 additions & 2 deletions ecmascript/loader/src/resolvers/tsc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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![];
Expand All @@ -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!(
Expand Down
2 changes: 1 addition & 1 deletion ecmascript/transforms/module/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
9 changes: 5 additions & 4 deletions ecmascript/transforms/module/src/path.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<R>
Expand Down Expand Up @@ -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",
Expand Down
26 changes: 26 additions & 0 deletions node-swc/__tests__/paths_test.mjs
Original file line number Diff line number Diff line change
@@ -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`);
})
4 changes: 4 additions & 0 deletions node-swc/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,10 @@ export interface JscConfig {
* Keep class names.
*/
keepClassNames?: boolean

paths?: {
[from: string]: [string]
}
}

export type JscTarget =
Expand Down
19 changes: 19 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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<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());
}
}
}
}

Expand Down

0 comments on commit ff47e25

Please sign in to comment.