2021-10-10 07:25:15 +02:00
|
|
|
use next_swc::{
|
2021-12-08 20:04:13 +01:00
|
|
|
amp_attributes::amp_attributes,
|
|
|
|
next_dynamic::next_dynamic,
|
|
|
|
next_ssg::next_ssg,
|
|
|
|
page_config::page_config_test,
|
|
|
|
react_remove_properties::remove_properties,
|
|
|
|
remove_console::remove_console,
|
|
|
|
shake_exports::{shake_exports, Config as ShakeExportsConfig},
|
|
|
|
styled_jsx::styled_jsx,
|
2021-10-10 07:25:15 +02:00
|
|
|
};
|
2021-08-03 14:02:23 +02:00
|
|
|
use std::path::PathBuf;
|
2021-10-17 09:50:01 +02:00
|
|
|
use swc_common::{chain, comments::SingleThreadedComments, FileName, Mark, Span, DUMMY_SP};
|
2021-08-03 14:02:23 +02:00
|
|
|
use swc_ecma_transforms_testing::{test, test_fixture};
|
|
|
|
use swc_ecmascript::{
|
2021-09-13 13:37:07 +02:00
|
|
|
parser::{EsConfig, Syntax},
|
2021-10-17 12:17:13 +02:00
|
|
|
transforms::{react::jsx, resolver},
|
2021-08-03 14:02:23 +02:00
|
|
|
};
|
|
|
|
use testing::fixture;
|
|
|
|
|
|
|
|
fn syntax() -> Syntax {
|
2021-09-13 13:37:07 +02:00
|
|
|
Syntax::Es(EsConfig {
|
|
|
|
jsx: true,
|
|
|
|
..Default::default()
|
|
|
|
})
|
2021-08-03 18:54:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[fixture("tests/fixture/amp/**/input.js")]
|
|
|
|
fn amp_attributes_fixture(input: PathBuf) {
|
2021-09-13 13:37:07 +02:00
|
|
|
let output = input.parent().unwrap().join("output.js");
|
|
|
|
test_fixture(syntax(), &|_tr| amp_attributes(), &input, &output);
|
2021-08-03 14:02:23 +02:00
|
|
|
}
|
|
|
|
|
2021-08-06 18:53:32 +02:00
|
|
|
#[fixture("tests/fixture/next-dynamic/**/input.js")]
|
|
|
|
fn next_dynamic_fixture(input: PathBuf) {
|
2021-12-14 11:33:04 +01:00
|
|
|
let output_dev = input.parent().unwrap().join("output-dev.js");
|
|
|
|
let output_prod = input.parent().unwrap().join("output-prod.js");
|
|
|
|
test_fixture(
|
|
|
|
syntax(),
|
|
|
|
&|_tr| {
|
|
|
|
next_dynamic(
|
|
|
|
true,
|
|
|
|
false,
|
|
|
|
FileName::Real(PathBuf::from("/some-project/src/some-file.js")),
|
|
|
|
Some("/some-project/src".into()),
|
|
|
|
)
|
|
|
|
},
|
|
|
|
&input,
|
|
|
|
&output_dev,
|
|
|
|
);
|
2021-09-13 13:37:07 +02:00
|
|
|
test_fixture(
|
|
|
|
syntax(),
|
|
|
|
&|_tr| {
|
|
|
|
next_dynamic(
|
2021-12-14 11:33:04 +01:00
|
|
|
false,
|
|
|
|
false,
|
2021-09-13 13:37:07 +02:00
|
|
|
FileName::Real(PathBuf::from("/some-project/src/some-file.js")),
|
|
|
|
Some("/some-project/src".into()),
|
|
|
|
)
|
|
|
|
},
|
|
|
|
&input,
|
2021-12-14 11:33:04 +01:00
|
|
|
&output_prod,
|
2021-09-13 13:37:07 +02:00
|
|
|
);
|
2021-08-06 18:53:32 +02:00
|
|
|
}
|
|
|
|
|
2021-08-03 14:02:23 +02:00
|
|
|
#[fixture("tests/fixture/ssg/**/input.js")]
|
|
|
|
fn next_ssg_fixture(input: PathBuf) {
|
2021-09-13 13:37:07 +02:00
|
|
|
let output = input.parent().unwrap().join("output.js");
|
|
|
|
test_fixture(
|
|
|
|
syntax(),
|
|
|
|
&|tr| {
|
2021-10-07 12:11:53 +02:00
|
|
|
let top_level_mark = Mark::fresh(Mark::root());
|
2021-09-13 13:37:07 +02:00
|
|
|
let jsx = jsx::<SingleThreadedComments>(
|
|
|
|
tr.cm.clone(),
|
|
|
|
None,
|
|
|
|
swc_ecmascript::transforms::react::Options {
|
|
|
|
next: false,
|
|
|
|
runtime: None,
|
|
|
|
import_source: "".into(),
|
|
|
|
pragma: "__jsx".into(),
|
|
|
|
pragma_frag: "__jsxFrag".into(),
|
|
|
|
throw_if_namespace: false,
|
|
|
|
development: false,
|
|
|
|
use_builtins: true,
|
|
|
|
use_spread: true,
|
|
|
|
refresh: Default::default(),
|
|
|
|
},
|
2021-10-07 12:11:53 +02:00
|
|
|
top_level_mark,
|
2021-09-13 13:37:07 +02:00
|
|
|
);
|
|
|
|
chain!(next_ssg(), jsx)
|
2021-08-03 14:02:23 +02:00
|
|
|
},
|
2021-09-13 13:37:07 +02:00
|
|
|
&input,
|
|
|
|
&output,
|
|
|
|
);
|
2021-08-03 14:02:23 +02:00
|
|
|
}
|
2021-09-15 09:24:31 +02:00
|
|
|
|
|
|
|
#[fixture("tests/fixture/styled-jsx/**/input.js")]
|
|
|
|
fn styled_jsx_fixture(input: PathBuf) {
|
2021-09-16 15:08:42 +02:00
|
|
|
let output = input.parent().unwrap().join("output.js");
|
2021-10-17 12:17:13 +02:00
|
|
|
test_fixture(
|
|
|
|
syntax(),
|
2021-12-16 12:23:31 +01:00
|
|
|
&|t| chain!(resolver(), styled_jsx(t.cm.clone())),
|
2021-10-17 12:17:13 +02:00
|
|
|
&input,
|
|
|
|
&output,
|
|
|
|
);
|
2021-10-17 09:50:01 +02:00
|
|
|
|
|
|
|
test_fixture(
|
|
|
|
syntax(),
|
2021-12-16 12:23:31 +01:00
|
|
|
&|t| {
|
2021-10-17 12:17:13 +02:00
|
|
|
// `resolver` uses `Mark` which is stored in a thread-local storage (namely
|
|
|
|
// swc_common::GLOBALS), and this loop will make `Mark` to be different from the
|
2021-10-19 09:16:59 +02:00
|
|
|
// invocation above.
|
2021-10-17 12:17:13 +02:00
|
|
|
//
|
|
|
|
// 1000 is used because in future I (kdy1) may optimize logic of resolver.
|
|
|
|
for _ in 0..1000 {
|
|
|
|
let _mark = Mark::fresh(Mark::root());
|
|
|
|
}
|
|
|
|
|
2021-12-16 12:23:31 +01:00
|
|
|
chain!(resolver(), styled_jsx(t.cm.clone()))
|
2021-10-17 12:17:13 +02:00
|
|
|
},
|
2021-10-17 09:50:01 +02:00
|
|
|
&input,
|
|
|
|
&output,
|
|
|
|
);
|
|
|
|
}
|
2021-10-23 01:08:09 +02:00
|
|
|
|
2021-12-16 12:23:31 +01:00
|
|
|
pub struct DropSpan;
|
|
|
|
impl swc_ecmascript::visit::VisitMut for DropSpan {
|
|
|
|
fn visit_mut_span(&mut self, span: &mut Span) {
|
|
|
|
*span = DUMMY_SP
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-23 01:08:09 +02:00
|
|
|
#[fixture("tests/fixture/page-config/**/input.js")]
|
|
|
|
fn page_config_fixture(input: PathBuf) {
|
|
|
|
let output = input.parent().unwrap().join("output.js");
|
|
|
|
test_fixture(syntax(), &|_tr| page_config_test(), &input, &output);
|
|
|
|
}
|
2021-11-16 10:18:05 +01:00
|
|
|
|
|
|
|
#[fixture("tests/fixture/remove-console/**/input.js")]
|
|
|
|
fn remove_console_fixture(input: PathBuf) {
|
|
|
|
let output = input.parent().unwrap().join("output.js");
|
|
|
|
test_fixture(
|
|
|
|
syntax(),
|
|
|
|
&|_tr| remove_console(next_swc::remove_console::Config::All(true)),
|
|
|
|
&input,
|
|
|
|
&output,
|
|
|
|
);
|
|
|
|
}
|
2021-11-26 14:45:03 +01:00
|
|
|
|
|
|
|
#[fixture("tests/fixture/react-remove-properties/default/**/input.js")]
|
|
|
|
fn react_remove_properties_default_fixture(input: PathBuf) {
|
|
|
|
let output = input.parent().unwrap().join("output.js");
|
|
|
|
test_fixture(
|
|
|
|
syntax(),
|
|
|
|
&|_tr| remove_properties(next_swc::react_remove_properties::Config::All(true)),
|
|
|
|
&input,
|
|
|
|
&output,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[fixture("tests/fixture/react-remove-properties/custom/**/input.js")]
|
|
|
|
fn react_remove_properties_custom_fixture(input: PathBuf) {
|
|
|
|
let output = input.parent().unwrap().join("output.js");
|
|
|
|
test_fixture(
|
|
|
|
syntax(),
|
|
|
|
&|_tr| {
|
|
|
|
remove_properties(next_swc::react_remove_properties::Config::WithOptions(
|
|
|
|
next_swc::react_remove_properties::Options {
|
|
|
|
properties: vec!["^data-custom$".into()],
|
|
|
|
},
|
|
|
|
))
|
|
|
|
},
|
|
|
|
&input,
|
|
|
|
&output,
|
|
|
|
);
|
|
|
|
}
|
2021-12-08 20:04:13 +01:00
|
|
|
|
|
|
|
#[fixture("tests/fixture/shake-exports/most-usecases/input.js")]
|
|
|
|
fn shake_exports_fixture(input: PathBuf) {
|
|
|
|
let output = input.parent().unwrap().join("output.js");
|
|
|
|
test_fixture(
|
|
|
|
syntax(),
|
|
|
|
&|_tr| {
|
|
|
|
shake_exports(ShakeExportsConfig {
|
|
|
|
ignore: vec![
|
|
|
|
String::from("keep").into(),
|
|
|
|
String::from("keep1").into(),
|
|
|
|
String::from("keep2").into(),
|
|
|
|
String::from("keep3").into(),
|
|
|
|
String::from("keep4").into(),
|
|
|
|
],
|
|
|
|
})
|
|
|
|
},
|
|
|
|
&input,
|
|
|
|
&output,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[fixture("tests/fixture/shake-exports/keep-default/input.js")]
|
|
|
|
fn shake_exports_fixture_default(input: PathBuf) {
|
|
|
|
let output = input.parent().unwrap().join("output.js");
|
|
|
|
test_fixture(
|
|
|
|
syntax(),
|
|
|
|
&|_tr| {
|
|
|
|
shake_exports(ShakeExportsConfig {
|
|
|
|
ignore: vec![String::from("default").into()],
|
|
|
|
})
|
|
|
|
},
|
|
|
|
&input,
|
|
|
|
&output,
|
|
|
|
);
|
|
|
|
}
|