/
full.rs
106 lines (95 loc) · 3.31 KB
/
full.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
use next_swc::{custom_before_pass, TransformOptions};
use serde::de::DeserializeOwned;
use std::path::{Path, PathBuf};
use swc::Compiler;
use swc_ecmascript::{
parser::{Syntax, TsConfig},
transforms::pass::noop,
};
use testing::{NormalizedOutput, Tester};
#[testing::fixture("tests/full/**/input.js")]
fn full(input: PathBuf) {
test(&input, true);
}
#[testing::fixture("tests/loader/**/input.js")]
fn loader(input: PathBuf) {
test(&input, false);
}
fn test(input: &Path, minify: bool) {
let output = input.parent().unwrap().join("output.js");
Tester::new()
.print_errors(|cm, handler| {
let c = Compiler::new(cm.clone());
let fm = cm.load_file(input).expect("failed to load file");
let options = TransformOptions {
swc: swc::config::Options {
swcrc: true,
is_module: swc::config::IsModule::Bool(true),
output_path: Some(output.to_path_buf()),
config: swc::config::Config {
jsc: swc::config::JscConfig {
minify: if minify {
Some(assert_json("{ \"compress\": true, \"mangle\": true }"))
} else {
None
},
syntax: Some(Syntax::Typescript(TsConfig {
tsx: true,
..Default::default()
})),
..Default::default()
},
..Default::default()
},
..Default::default()
},
disable_next_ssg: false,
disable_page_config: false,
pages_dir: None,
is_page_file: false,
is_development: true,
is_server: false,
styled_components: Some(assert_json("{}")),
remove_console: None,
react_remove_properties: None,
relay: None,
shake_exports: None,
emotion: Some(assert_json("{}")),
modularize_imports: None,
};
let options = options.patch(&fm);
match c.process_js_with_custom_pass(
fm.clone(),
None,
&handler,
&options.swc,
|_, comments| {
custom_before_pass(
cm.clone(),
fm.clone(),
&options,
comments.clone(),
Default::default(),
)
},
|_, _| noop(),
) {
Ok(v) => {
NormalizedOutput::from(v.code)
.compare_to_file(output)
.unwrap();
}
Err(err) => panic!("Error: {:?}", err),
};
Ok(())
})
.map(|_| ())
.expect("failed");
}
/// Using this, we don't have to break code by adding field.s
fn assert_json<T>(json_str: &str) -> T
where
T: DeserializeOwned,
{
serde_json::from_str(json_str).expect("failed to deserialize")
}