rsnext/packages/next/build/swc/tests/full.rs
Donny/강동윤 f1c62d5073
feat(build/swc): Add debug utilities (#29730)
* Make debug build faster

* Organize for easier testing

* Add example tests

* Test system

* Update test refs

* Ensure that we are fully processing

* Update test refs

* Update test system

* Update swc

* Fix

* Update test refs

* Update test refs

* Ignore tests in nextbuild task

* Fix attrs

* Add a test

* Update test refs

* Add a test

* Fix hygiene bug

* Update test refs

* Update swc

* Build next-swc binaries

Co-authored-by: Tim Neutkens <timneutkens@me.com>
Co-authored-by: kdy1 <kdy1@users.noreply.github.com>
2021-10-10 07:25:15 +02:00

78 lines
2.4 KiB
Rust

use next_swc::custom_before_pass;
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");
match c.process_js_with_custom_pass(
fm.clone(),
&handler,
&swc::config::Options {
swcrc: true,
is_module: 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,
dynamic_import: true,
..Default::default()
})),
..Default::default()
},
..Default::default()
},
..Default::default()
},
custom_before_pass(&fm.name, &assert_json(&"{}")),
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")
}