2021-10-30 15:31:58 +02:00
|
|
|
use next_swc::{custom_before_pass, TransformOptions};
|
2021-10-10 07:25:15 +02:00
|
|
|
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");
|
2021-10-30 15:31:58 +02:00
|
|
|
|
|
|
|
let options = TransformOptions {
|
|
|
|
swc: swc::config::Options {
|
2021-10-10 07:25:15 +02:00
|
|
|
swcrc: true,
|
2021-11-25 00:33:34 +01:00
|
|
|
is_module: swc::config::IsModule::Bool(true),
|
2021-10-10 07:25:15 +02:00
|
|
|
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,
|
|
|
|
dynamic_import: true,
|
|
|
|
..Default::default()
|
|
|
|
})),
|
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
..Default::default()
|
|
|
|
},
|
2021-10-30 15:31:58 +02:00
|
|
|
disable_next_ssg: false,
|
|
|
|
disable_page_config: false,
|
|
|
|
pages_dir: None,
|
|
|
|
is_page_file: false,
|
|
|
|
is_development: true,
|
2021-11-09 11:05:31 +01:00
|
|
|
styled_components: Some(assert_json("{}")),
|
2021-11-16 10:18:05 +01:00
|
|
|
remove_console: None,
|
2021-10-30 15:31:58 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
let options = options.patch(&fm);
|
|
|
|
|
|
|
|
match c.process_js_with_custom_pass(
|
|
|
|
fm.clone(),
|
2021-11-09 11:05:31 +01:00
|
|
|
None,
|
2021-10-30 15:31:58 +02:00
|
|
|
&handler,
|
|
|
|
&options.swc,
|
2021-11-09 11:05:31 +01:00
|
|
|
|_| custom_before_pass(fm.clone(), &options),
|
|
|
|
|_| noop(),
|
2021-10-10 07:25:15 +02:00
|
|
|
) {
|
|
|
|
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")
|
|
|
|
}
|