2022-07-03 08:54:07 +03:00
|
|
|
use std::{fs::File, path::PathBuf, rc::Rc};
|
2022-02-09 09:33:32 +03:00
|
|
|
|
2022-07-03 08:54:07 +03:00
|
|
|
use swc_common::{chain, comments::SingleThreadedComments, 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_compat::es2015::for_of;
|
|
|
|
use swc_ecma_transforms_module::common_js::{self, common_js};
|
|
|
|
use swc_ecma_transforms_testing::{test, test_fixture};
|
2020-07-23 20:18:22 +03:00
|
|
|
use swc_ecma_visit::Fold;
|
2019-12-24 16:53:48 +03:00
|
|
|
|
2020-07-27 16:58:42 +03:00
|
|
|
fn syntax() -> Syntax {
|
2022-06-27 15:32:06 +03:00
|
|
|
Default::default()
|
2019-02-05 06:50:19 +03:00
|
|
|
}
|
2021-01-05 08:29:52 +03:00
|
|
|
|
2022-06-27 15:32:06 +03:00
|
|
|
fn ts_syntax() -> Syntax {
|
|
|
|
Syntax::Typescript(TsConfig::default())
|
2021-01-05 08:29:52 +03:00
|
|
|
}
|
|
|
|
|
2022-07-03 08:54:07 +03:00
|
|
|
fn tr(
|
|
|
|
config: common_js::Config,
|
|
|
|
typescript: bool,
|
|
|
|
comments: Rc<SingleThreadedComments>,
|
|
|
|
) -> impl Fold {
|
2022-06-27 15:32:06 +03:00
|
|
|
let unresolved_mark = Mark::new();
|
|
|
|
let top_level_mark = Mark::new();
|
2021-01-05 08:29:52 +03:00
|
|
|
|
2022-06-27 15:32:06 +03:00
|
|
|
let avalible_set = FeatureFlag::all();
|
2021-01-05 08:29:52 +03:00
|
|
|
|
2022-06-27 15:32:06 +03:00
|
|
|
chain!(
|
|
|
|
resolver(unresolved_mark, top_level_mark, typescript),
|
2022-07-03 08:54:07 +03:00
|
|
|
common_js(unresolved_mark, config, avalible_set, Some(comments)),
|
2022-06-27 15:32:06 +03:00
|
|
|
)
|
2021-01-05 08:29:52 +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_cjs(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();
|
2021-01-05 08:29:52 +03:00
|
|
|
|
2022-06-27 15:32:06 +03:00
|
|
|
let dir = input.parent().unwrap().to_path_buf();
|
2021-01-05 08:29:52 +03:00
|
|
|
|
2022-06-27 15:32:06 +03:00
|
|
|
let output = dir
|
|
|
|
.join("output.js")
|
|
|
|
.with_extension(if is_ts { "cts" } else { "cjs" });
|
2021-01-05 08:29:52 +03:00
|
|
|
|
2022-06-27 15:32:06 +03:00
|
|
|
let config_path = dir.join("module.json");
|
|
|
|
let config: common_js::Config = match File::open(config_path) {
|
|
|
|
Ok(file) => serde_json::from_reader(file).unwrap(),
|
|
|
|
Err(..) => Default::default(),
|
|
|
|
};
|
2021-01-05 08:29:52 +03:00
|
|
|
|
2022-06-27 15:32:06 +03:00
|
|
|
test_fixture(
|
|
|
|
if is_ts { ts_syntax() } else { syntax() },
|
2022-07-03 08:54:07 +03:00
|
|
|
&|tester| tr(config.clone(), is_ts, tester.comments.clone()),
|
2022-06-27 15:32:06 +03:00
|
|
|
&input,
|
|
|
|
&output,
|
|
|
|
);
|
2021-01-05 08:29:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
test!(
|
|
|
|
syntax(),
|
2022-07-03 08:54:07 +03:00
|
|
|
|tester| chain!(
|
2022-06-27 15:32:06 +03:00
|
|
|
for_of(for_of::Config { assume_array: true }),
|
2022-07-03 08:54:07 +03:00
|
|
|
tr(Default::default(), false, tester.comments.clone())
|
2021-01-05 08:29:52 +03:00
|
|
|
),
|
2022-06-27 15:32:06 +03:00
|
|
|
for_of_as_array_for_of_import_commonjs,
|
2021-01-05 08:29:52 +03:00
|
|
|
r#"
|
2022-06-27 15:32:06 +03:00
|
|
|
import { array } from "foo";
|
2021-04-17 09:00:14 +03:00
|
|
|
|
2022-06-27 15:32:06 +03:00
|
|
|
for (const elm of array) {
|
|
|
|
console.log(elm);
|
2021-10-23 15:13:56 +03:00
|
|
|
}
|
2022-05-26 16:06:43 +03:00
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
"use strict";
|
|
|
|
Object.defineProperty(exports, "__esModule", {
|
|
|
|
value: true
|
|
|
|
});
|
2022-06-27 15:32:06 +03:00
|
|
|
const _foo = require("foo");
|
|
|
|
for(let _i = 0; _i < _foo.array.length; _i++){
|
|
|
|
const elm = _foo.array[_i];
|
|
|
|
console.log(elm);
|
|
|
|
}
|
2022-05-26 16:06:43 +03:00
|
|
|
"#
|
|
|
|
);
|