2022-06-27 15:32:06 +03:00
|
|
|
use std::{fs::File, path::PathBuf};
|
|
|
|
|
2021-08-13 13:03:04 +03:00
|
|
|
use swc_common::{chain, Mark};
|
2022-06-27 15:32:06 +03:00
|
|
|
use swc_ecma_parser::{Syntax, TsConfig};
|
|
|
|
use swc_ecma_transforms_base::{feature::FeatureFlag, resolver};
|
|
|
|
use swc_ecma_transforms_module::umd::{umd, Config};
|
|
|
|
use swc_ecma_transforms_testing::{test_fixture, Tester};
|
2020-07-23 20:18:22 +03:00
|
|
|
use swc_ecma_visit::Fold;
|
2019-12-24 16:53:48 +03:00
|
|
|
|
2020-09-04 10:18:57 +03:00
|
|
|
fn syntax() -> Syntax {
|
2022-06-27 15:32:06 +03:00
|
|
|
Default::default()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn ts_syntax() -> Syntax {
|
|
|
|
Syntax::Typescript(TsConfig::default())
|
2019-02-05 06:50:19 +03:00
|
|
|
}
|
|
|
|
|
2022-06-27 15:32:06 +03:00
|
|
|
fn tr(tester: &mut Tester<'_>, config: Config, typescript: bool) -> impl Fold {
|
2022-04-26 10:38:50 +03:00
|
|
|
let unresolved_mark = Mark::new();
|
|
|
|
let top_level_mark = Mark::new();
|
|
|
|
|
2022-06-27 15:32:06 +03:00
|
|
|
let avalible_set = FeatureFlag::all();
|
|
|
|
|
2020-04-30 17:48:52 +03:00
|
|
|
chain!(
|
2022-06-27 15:32:06 +03:00
|
|
|
resolver(unresolved_mark, top_level_mark, typescript),
|
2022-07-03 08:54:07 +03:00
|
|
|
umd(
|
|
|
|
tester.cm.clone(),
|
|
|
|
unresolved_mark,
|
|
|
|
config,
|
|
|
|
avalible_set,
|
|
|
|
Some(tester.comments.clone())
|
|
|
|
),
|
2020-04-30 17:48:52 +03:00
|
|
|
)
|
2019-02-05 06:50:19 +03:00
|
|
|
}
|
|
|
|
|
2022-06-27 15:32:06 +03:00
|
|
|
#[testing::fixture("tests/fixture/common/**/input.js")]
|
|
|
|
#[testing::fixture("tests/fixture/common/**/input.ts")]
|
|
|
|
#[testing::fixture("tests/fixture/common/**/input.cts")]
|
|
|
|
fn esm_to_umd(input: PathBuf) {
|
|
|
|
let is_ts = input
|
|
|
|
.file_name()
|
|
|
|
.map(|x| x.to_string_lossy())
|
|
|
|
.map(|x| x.ends_with(".ts") || x.ends_with(".mts") || x.ends_with(".cts"))
|
|
|
|
.unwrap_or_default();
|
|
|
|
|
|
|
|
let dir = input.parent().unwrap().to_path_buf();
|
|
|
|
|
|
|
|
let output = dir
|
|
|
|
.join("output.umd.js")
|
|
|
|
.with_extension(if is_ts { "ts" } else { "js" });
|
|
|
|
|
|
|
|
let umd_config_path = dir.join("module.umd.json");
|
|
|
|
let config_path = dir.join("module.json");
|
|
|
|
let config: Config = match File::open(umd_config_path).or_else(|_| File::open(config_path)) {
|
|
|
|
Ok(file) => serde_json::from_reader(file).unwrap(),
|
|
|
|
Err(..) => Default::default(),
|
|
|
|
};
|
|
|
|
|
|
|
|
test_fixture(
|
|
|
|
if is_ts { ts_syntax() } else { syntax() },
|
|
|
|
&|tester| tr(tester, config.clone(), is_ts),
|
|
|
|
&input,
|
|
|
|
&output,
|
|
|
|
);
|
2019-02-05 06:50:19 +03:00
|
|
|
}
|