Skip to content

Commit 82bc061

Browse files
authoredJan 31, 2024
fix(cli): Make Rust CLI use sourcemap code from the swc crate (#8576)
**Related issue:** - Closes #8495
1 parent 4fd7923 commit 82bc061

File tree

8 files changed

+120
-60
lines changed

8 files changed

+120
-60
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"$schema": "http://json.schemastore.org/swcrc",
3+
"jsc": {
4+
"target": "es2022",
5+
"parser": {
6+
"syntax": "typescript"
7+
}
8+
},
9+
"module": {
10+
"type": "commonjs"
11+
}
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import type {
2+
AnimationConfigWithData,
3+
AnimationConfigWithPath,
4+
} from "lottie-web";
5+
import type { CSSProperties } from "react";
6+
7+
type Without<T, U> = {
8+
// For each `K` in the keys of `T` excluding keys of `U`
9+
// map `K` to an optional `never` type.
10+
//
11+
// See: https://www.typescriptlang.org/docs/handbook/2/mapped-types.html
12+
[K in Exclude<keyof T, keyof U>]?: never;
13+
};
14+
15+
// A type that either has one set of properties or another, but not both.
16+
export type XOR<T, U> = T | U extends object
17+
? (Without<T, U> & U) | (Without<U, T> & T)
18+
: T | U;
19+
20+
export type LottieWebParams = XOR<
21+
Omit<AnimationConfigWithPath<"svg">, "renderer" | "container">,
22+
Omit<AnimationConfigWithData<"svg">, "renderer" | "container">
23+
>;
24+
25+
export type LottieAnimationProps = {
26+
className?: string;
27+
style?: CSSProperties;
28+
withShadowRoot?: boolean;
29+
onAnimationEnd?: () => void;
30+
} & LottieWebParams;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
"use strict";
2+
Object.defineProperty(exports, "__esModule", {
3+
value: true
4+
});

‎crates/swc_cli_impl/src/commands/compile.rs

+8-60
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use std::{
2-
borrow::Cow,
32
fs::{self, File},
43
io::{self, Read, Write},
54
path::{Component, Path, PathBuf},
@@ -213,22 +212,10 @@ fn resolve_output_file_path(
213212

214213
fn emit_output(
215214
mut output: TransformOutput,
216-
source_file_name: &Option<String>,
217-
source_root: &Option<String>,
218215
out_dir: &Option<PathBuf>,
219216
file_path: &Path,
220217
file_extension: PathBuf,
221218
) -> anyhow::Result<()> {
222-
let source_map = if let Some(ref source_map) = &output.map {
223-
Some(extend_source_map(
224-
source_map.to_owned(),
225-
source_file_name,
226-
source_root,
227-
)?)
228-
} else {
229-
None
230-
};
231-
232219
if let Some(out_dir) = out_dir {
233220
let output_file_path = resolve_output_file_path(out_dir, file_path, file_extension)?;
234221
let output_dir = output_file_path
@@ -239,7 +226,7 @@ fn emit_output(
239226
fs::create_dir_all(output_dir)?;
240227
}
241228

242-
if let Some(ref source_map) = source_map {
229+
if let Some(ref source_map) = output.map {
243230
let source_map_path = output_file_path.with_extension("js.map");
244231

245232
output.code.push_str("\n//# sourceMappingURL=");
@@ -252,10 +239,10 @@ fn emit_output(
252239

253240
fs::write(output_file_path, &output.code)?;
254241
} else {
255-
let source_map = if let Some(ref source_map) = source_map {
256-
String::from_utf8_lossy(source_map)
242+
let source_map = if let Some(ref source_map) = output.map {
243+
&**source_map
257244
} else {
258-
Cow::Borrowed("")
245+
""
259246
};
260247

261248
println!("{}\n{}\n{}", file_path.display(), output.code, source_map,);
@@ -484,17 +471,10 @@ impl CompileOptions {
484471
buf_srcmap = Some(File::create(map_out_file)?);
485472
}
486473

487-
let source_map = extend_source_map(
488-
src_map.to_owned(),
489-
&self.source_file_name,
490-
&self.source_root,
491-
)
492-
.unwrap();
493-
494474
buf_srcmap
495475
.as_ref()
496476
.expect("Srcmap buffer should be available")
497-
.write(&source_map)
477+
.write(src_map.as_bytes())
498478
.and(Ok(()))?;
499479
}
500480

@@ -520,14 +500,9 @@ impl CompileOptions {
520500
let result = execute(compiler, fm, options);
521501

522502
match result {
523-
Ok(output) => emit_output(
524-
output,
525-
&self.source_file_name,
526-
&self.source_root,
527-
&self.out_dir,
528-
&file_path,
529-
file_extension,
530-
),
503+
Ok(output) => {
504+
emit_output(output, &self.out_dir, &file_path, file_extension)
505+
}
531506
Err(e) => Err(e),
532507
}
533508
},
@@ -536,33 +511,6 @@ impl CompileOptions {
536511
}
537512
}
538513

539-
// TODO: remove once fixed in core https://github.com/swc-project/swc/issues/1388
540-
fn extend_source_map(
541-
source_map: String,
542-
source_file_name: &Option<String>,
543-
source_root: &Option<String>,
544-
) -> anyhow::Result<Vec<u8>> {
545-
let mut source_map = sourcemap::SourceMap::from_reader(source_map.as_bytes())
546-
.context("failed to encode source map")?;
547-
548-
if !source_map.get_token_count() != 0 {
549-
if let Some(ref source_file_name) = source_file_name {
550-
source_map.set_source(0u32, source_file_name);
551-
}
552-
}
553-
554-
if source_root.is_some() {
555-
source_map.set_source_root(source_root.clone());
556-
}
557-
558-
let mut buf = vec![];
559-
source_map
560-
.to_writer(&mut buf)
561-
.context("failed to decode source map")?;
562-
563-
Ok(buf)
564-
}
565-
566514
#[swc_trace]
567515
impl super::CommandRunner for CompileOptions {
568516
fn execute(&self) -> anyhow::Result<()> {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
dist/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"$schema": "http://json.schemastore.org/swcrc",
3+
"jsc": {
4+
"target": "es2022",
5+
"parser": {
6+
"syntax": "typescript"
7+
}
8+
},
9+
"module": {
10+
"type": "commonjs"
11+
}
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import type {
2+
AnimationConfigWithData,
3+
AnimationConfigWithPath,
4+
} from "lottie-web";
5+
import type { CSSProperties } from "react";
6+
7+
type Without<T, U> = {
8+
// For each `K` in the keys of `T` excluding keys of `U`
9+
// map `K` to an optional `never` type.
10+
//
11+
// See: https://www.typescriptlang.org/docs/handbook/2/mapped-types.html
12+
[K in Exclude<keyof T, keyof U>]?: never;
13+
};
14+
15+
// A type that either has one set of properties or another, but not both.
16+
export type XOR<T, U> = T | U extends object
17+
? (Without<T, U> & U) | (Without<U, T> & T)
18+
: T | U;
19+
20+
export type LottieWebParams = XOR<
21+
Omit<AnimationConfigWithPath<"svg">, "renderer" | "container">,
22+
Omit<AnimationConfigWithData<"svg">, "renderer" | "container">
23+
>;
24+
25+
export type LottieAnimationProps = {
26+
className?: string;
27+
style?: CSSProperties;
28+
withShadowRoot?: boolean;
29+
onAnimationEnd?: () => void;
30+
} & LottieWebParams;

‎crates/swc_cli_impl/tests/issues.rs

+23
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,29 @@ fn issue_8265_1() -> Result<()> {
5858
Ok(())
5959
}
6060

61+
#[test]
62+
fn issue_8495_1() -> Result<()> {
63+
let pwd = Path::new("tests/fixture-manual/8495").canonicalize()?;
64+
65+
let mut cmd = cli()?;
66+
cmd.current_dir(&pwd)
67+
.arg("compile")
68+
.arg("--source-maps")
69+
.arg("true")
70+
.arg("--source-file-name")
71+
.arg("input.ts")
72+
.arg("--config-file")
73+
.arg(".swcrc")
74+
.arg("--out-file")
75+
.arg("dist/input.js")
76+
.arg("src/input.ts");
77+
78+
cmd.assert().success();
79+
80+
fs::read_to_string(pwd.join("dist/input.js"))?;
81+
Ok(())
82+
}
83+
6184
/// ln -s $a $b
6285
fn symlink(a: &Path, b: &Path) {
6386
#[cfg(unix)]

0 commit comments

Comments
 (0)
Please sign in to comment.