Skip to content

Commit 036b957

Browse files
authored
Turbopack: Don't convert script to module when using preset-env (#80724)
Both swc's `preset_env` and the `crates/next-core/src/next_shared/transforms/next_track_dynamic_imports.rs` were using the `Program::{Module, Script}` type to determine whether to insert CJS requires or ESM imports. Remove the normaliziation of modules to scripts to allow these transforms to insert the right type of import.
1 parent 6e5d86a commit 036b957

File tree

3 files changed

+15
-33
lines changed

3 files changed

+15
-33
lines changed

turbopack/crates/turbopack-ecmascript/src/transform/mod.rs

Lines changed: 8 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ use rustc_hash::FxHashMap;
66
use swc_core::{
77
atoms::{Atom, atom},
88
base::SwcComments,
9-
common::{Mark, SourceMap, comments::Comments, util::take::Take},
9+
common::{Mark, SourceMap, comments::Comments},
1010
ecma::{
11-
ast::{Module, ModuleItem, Program, Script},
11+
ast::{ModuleItem, Program},
1212
preset_env::{self, Targets},
1313
transforms::{base::assumptions::Assumptions, optimization::inline_globals, react::react},
1414
},
@@ -215,32 +215,14 @@ impl EcmascriptInputTransform {
215215
},
216216
);
217217

218-
let module_program = std::mem::replace(program, Program::Module(Module::dummy()));
219-
220-
let module_program = if let Program::Script(Script {
221-
span,
222-
mut body,
223-
shebang,
224-
}) = module_program
225-
{
226-
Program::Module(Module {
227-
span,
228-
body: body.drain(..).map(ModuleItem::Stmt).collect(),
229-
shebang,
230-
})
231-
} else {
232-
module_program
233-
};
234-
235218
// Explicit type annotation to ensure that we don't duplicate transforms in the
236219
// final binary
237-
*program =
238-
module_program.apply(preset_env::transform_from_env::<&'_ dyn Comments>(
239-
top_level_mark,
240-
Some(&comments),
241-
config,
242-
Assumptions::default(),
243-
));
220+
program.mutate(preset_env::transform_from_env::<&'_ dyn Comments>(
221+
top_level_mark,
222+
Some(&comments),
223+
config,
224+
Assumptions::default(),
225+
));
244226
}
245227
EcmascriptInputTransform::TypeScript {
246228
// TODO(WEB-1213)

turbopack/crates/turbopack-tests/tests/snapshot/swc_transforms/preset_env/output/turbopack_crates_turbopack-tests_tests_snapshot_bff03a32._.js

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

turbopack/crates/turbopack-tests/tests/snapshot/swc_transforms/preset_env/output/turbopack_crates_turbopack-tests_tests_snapshot_bff03a32._.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)