Files
magic-akariandGitHub 3a340f871f refactor(es/module): introduce source module lowering pipeline (#11999)
**Description:**

Refactor module transforms around a shared, target-neutral
`SourceModule` collection stage.

`SourceModule` only collects source-level module facts. CommonJS, AMD,
and UMD share syntax stripping and CommonJS-like module-entry reduction,
while SystemJS intentionally bypasses those stages and lowers the
collected source model into its own IR for dependency setters, live
bindings, and execution timing.

Key decisions:

- Import attributes are retained for SystemJS. CommonJS, AMD, and UMD
intentionally do not process them because those formats cannot express
import attributes; these lowering paths assume attributed module
requests are not provided.
- Module transforms now own TypeScript `import = require(...)` and
`export =` lowering.
- A non-exported `import x = require(...)` is modeled as the CTS form of
ES `import.sync`, outside the ES module linking stage. It therefore does
not mark the file as ESM, emit an `__esModule` marker, or introduce an
`exports` wrapper dependency. The exported form additionally registers
`x` as a local export in the linking model.
- AMD and UMD emit `import = require(...)` as wrapper dependencies. UMD
routes `export =` through the factory result, and SystemJS now supports
`export =`.
- Anonymous SystemJS default functions are initialized in the
`System.register` declaration callback to preserve their hoisting
semantics.
2026-07-13 11:52:16 +09:00

100 lines
2.6 KiB
Rust

use std::{fs::File, path::PathBuf};
use swc_common::Mark;
use swc_ecma_ast::Pass;
use swc_ecma_parser::{Syntax, TsSyntax};
use swc_ecma_transforms_base::resolver;
use swc_ecma_transforms_compat::es2015::for_of;
use swc_ecma_transforms_module::common_js::{self, common_js, FeatureFlag};
use swc_ecma_transforms_testing::{test, test_fixture, FixtureTestConfig};
use swc_ecma_transforms_typescript::typescript;
fn syntax() -> Syntax {
Default::default()
}
fn ts_syntax() -> Syntax {
Syntax::Typescript(TsSyntax::default())
}
fn tr(config: common_js::Config, is_ts: bool) -> impl Pass {
let unresolved_mark = Mark::new();
let top_level_mark = Mark::new();
(
resolver(unresolved_mark, top_level_mark, is_ts),
typescript::typescript(
typescript::Config {
import_export_assign_config: typescript::TsImportExportAssignConfig::Preserve,
..Default::default()
},
unresolved_mark,
top_level_mark,
),
common_js(
Default::default(),
unresolved_mark,
config,
FeatureFlag {
support_block_scoping: true,
support_arrow: true,
},
),
)
}
#[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();
let dir = input.parent().unwrap().to_path_buf();
let output = dir
.join("output.js")
.with_extension(if is_ts { "cts" } else { "cjs" });
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(),
};
test_fixture(
if is_ts { ts_syntax() } else { syntax() },
&|_| tr(config.clone(), is_ts),
&input,
&output,
FixtureTestConfig {
sourcemap: false,
module: Some(true),
..Default::default()
},
);
}
test!(
module,
syntax(),
|_| (
for_of(for_of::Config {
assume_array: true,
..Default::default()
}),
tr(Default::default(), false)
),
for_of_as_array_for_of_import_commonjs,
r#"
import { array } from "foo";
for (const elm of array) {
console.log(elm);
}
"#
);