Unverified Commit 08aad0c4 authored by Niklas Mischkulnig's avatar Niklas Mischkulnig Committed by GitHub

Turbopack: fix codegen of directives (#80895)

Closes PACK-4894

Previously, directives in the input weren't handled correctly, this was the output:
The `use strict` doesn't do anything here because directives only have an effect at the top of modules and at the top of functions.
```js
"[project]/turbopack/crates/turbopack-tests/tests/snapshot/basic/use-strict/input/index.js [test] (ecmascript)": ((__turbopack_context__) => {

var { m: module, e: exports } = __turbopack_context__;
{
'use strict';
console.log('this is CJS');
module.exports = 1234;
}}),
}]);
```

Instead:
1. detect if `use strict` is in the input file
2. strip all directives
3. prepend `"use strict"`  at the top of the chunk item, if it was strict strict
parent 5d94c806
......@@ -7,7 +7,7 @@ use swc_core::{
ecma::{
ast::*,
atoms::{Atom, atom},
utils::find_pat_ids,
utils::{IsDirective, find_pat_ids},
visit::{Visit, VisitWith},
},
};
......@@ -169,6 +169,9 @@ pub(crate) struct ImportMap {
/// True if the module is an ESM module due to top-level await.
has_top_level_await: bool,
/// True if the module has "use strict"
pub(crate) strict: bool,
/// Locations of [webpack-style "magic comments"][magic] that override import behaviors.
///
/// Most commonly, these are `/* webpackIgnore: true */` comments. See [ImportAttributes] for
......@@ -705,6 +708,18 @@ impl Visit for Analyzer<'_> {
fn visit_program(&mut self, m: &Program) {
self.data.has_top_level_await = has_top_level_await(m).is_some();
self.data.strict = match m {
Program::Module(module) => module
.body
.iter()
.take_while(|s| s.directive_continue())
.any(IsDirective::is_use_strict),
Program::Script(script) => script
.body
.iter()
.take_while(|s| s.directive_continue())
.any(IsDirective::is_use_strict),
};
m.visit_children_with(self);
}
......
......@@ -51,6 +51,7 @@ impl EcmascriptChunkItemContent {
let content = content.await?;
let async_module = async_module_options.owned().await?;
let strict = content.strict;
Ok(EcmascriptChunkItemContent {
rewrite_source_path: if *chunking_context.should_use_file_source_map_uris().await? {
......@@ -76,6 +77,7 @@ impl EcmascriptChunkItemContent {
}
EcmascriptChunkItemOptions {
strict,
refresh,
externals,
// These things are not available in ESM
......
......@@ -127,7 +127,7 @@ use crate::{
side_effect_optimization::reference::EcmascriptModulePartReference,
simple_tree_shake::{ModuleExportUsageInfo, get_module_export_usages},
swc_comments::{CowComments, ImmutableComments},
transform::remove_shebang,
transform::{remove_directives, remove_shebang},
};
#[derive(
......@@ -888,6 +888,7 @@ pub struct EcmascriptModuleContent {
pub inner_code: Rope,
pub source_map: Option<Rope>,
pub is_esm: bool,
pub strict: bool,
pub additional_ids: SmallVec<[ResolvedVc<ModuleId>; 1]>,
}
......@@ -1144,6 +1145,7 @@ impl EcmascriptModuleContent {
},
export_contexts: None,
is_esm: true,
strict: true,
generate_source_map: options.generate_source_map,
original_source_map: CodeGenResultOriginalSourceMap::ScopeHoisting(
original_source_maps,
......@@ -1546,6 +1548,7 @@ struct CodeGenResult {
/// `eval_context.imports.exports`
export_contexts: Option<FxHashMap<RcStr, Id>>,
is_esm: bool,
strict: bool,
generate_source_map: bool,
original_source_map: CodeGenResultOriginalSourceMap,
minify: MinifyType,
......@@ -1571,12 +1574,13 @@ async fn process_parse_result(
with_consumed_parse_result(
parsed,
async |mut program, source_map, globals, eval_context, comments| -> Result<CodeGenResult> {
let (top_level_mark, is_esm, export_contexts) = eval_context
let (top_level_mark, is_esm, strict, export_contexts) = eval_context
.map_either(
|e| {
(
e.top_level_mark,
e.is_esm(specified_module_type),
e.imports.strict,
Cow::Owned(e.imports.exports),
)
},
......@@ -1584,6 +1588,7 @@ async fn process_parse_result(
(
e.top_level_mark,
e.is_esm(specified_module_type),
e.imports.strict,
Cow::Borrowed(&e.imports.exports),
)
},
......@@ -1711,6 +1716,7 @@ async fn process_parse_result(
// we need to remove any shebang before bundling as it's only valid as the first
// line in a js file (not in a chunk item wrapped in the runtime)
remove_shebang(&mut program);
remove_directives(&mut program);
});
Ok(CodeGenResult {
......@@ -1725,6 +1731,7 @@ async fn process_parse_result(
// TODO ideally don't clone here at all
export_contexts: Some(export_contexts.into_owned()),
is_esm,
strict,
generate_source_map,
original_source_map: CodeGenResultOriginalSourceMap::Single(original_source_map),
minify,
......@@ -1760,6 +1767,7 @@ async fn process_parse_result(
comments: CodeGenResultComments::Empty,
export_contexts: None,
is_esm: false,
strict: false,
generate_source_map: false,
original_source_map: CodeGenResultOriginalSourceMap::Single(None),
minify: MinifyType::NoMinify,
......@@ -1787,6 +1795,7 @@ async fn process_parse_result(
comments: CodeGenResultComments::Empty,
export_contexts: None,
is_esm: false,
strict: false,
generate_source_map: false,
original_source_map: CodeGenResultOriginalSourceMap::Single(None),
minify: MinifyType::NoMinify,
......@@ -1877,6 +1886,7 @@ async fn emit_content(
source_map,
comments,
is_esm,
strict,
generate_source_map,
original_source_map,
minify,
......@@ -1941,6 +1951,7 @@ async fn emit_content(
inner_code: bytes.into(),
source_map,
is_esm,
strict,
additional_ids,
}
.cell())
......
......@@ -125,6 +125,7 @@ impl CachedExternalModule {
inner_code: code.build(),
source_map: None,
is_esm: self.external_type != CachedExternalType::CommonJs,
strict: false,
additional_ids: Default::default(),
}
.cell())
......
......@@ -8,7 +8,7 @@ use swc_core::{
base::SwcComments,
common::{Mark, SourceMap, comments::Comments},
ecma::{
ast::{ModuleItem, Pass, Program},
ast::{ExprStmt, ModuleItem, Pass, Program, Stmt},
preset_env::{self, Targets},
transforms::{
base::{
......@@ -18,6 +18,7 @@ use swc_core::{
optimization::inline_globals,
react::react,
},
utils::IsDirective,
},
quote,
};
......@@ -307,6 +308,51 @@ pub fn remove_shebang(program: &mut Program) {
}
}
pub fn remove_directives(program: &mut Program) {
match program {
Program::Module(module) => {
let directive_count = module
.body
.iter()
.take_while(|i| match i {
ModuleItem::Stmt(stmt) => stmt.directive_continue(),
ModuleItem::ModuleDecl(_) => false,
})
.take_while(|i| match i {
ModuleItem::Stmt(stmt) => match stmt {
Stmt::Expr(ExprStmt { expr, .. }) => expr
.as_lit()
.and_then(|lit| lit.as_str())
.and_then(|str| str.raw.as_ref())
.is_some_and(|raw| {
raw.starts_with("\"use ") || raw.starts_with("'use ")
}),
_ => false,
},
ModuleItem::ModuleDecl(_) => false,
})
.count();
module.body.drain(0..directive_count);
}
Program::Script(script) => {
let directive_count = script
.body
.iter()
.take_while(|stmt| stmt.directive_continue())
.take_while(|stmt| match stmt {
Stmt::Expr(ExprStmt { expr, .. }) => expr
.as_lit()
.and_then(|lit| lit.as_str())
.and_then(|str| str.raw.as_ref())
.is_some_and(|raw| raw.starts_with("\"use ") || raw.starts_with("'use ")),
_ => false,
})
.count();
script.body.drain(0..directive_count);
}
}
}
#[turbo_tasks::value(shared)]
pub struct UnsupportedServerActionIssue {
pub file_path: ResolvedVc<FileSystemPath>,
......
'use strict'
console.log('this is CJS')
module.exports = 1234
(globalThis.TURBOPACK = globalThis.TURBOPACK || []).push([
"output/4e721_crates_turbopack-tests_tests_snapshot_basic_use-strict_input_index_b7c39fa7.js",
{},
{"otherChunks":["output/4e721_crates_turbopack-tests_tests_snapshot_basic_use-strict_input_index_c0953835.js"],"runtimeModuleIds":["[project]/turbopack/crates/turbopack-tests/tests/snapshot/basic/use-strict/input/index.js [test] (ecmascript)"]}
]);
// Dummy runtime
\ No newline at end of file
(globalThis.TURBOPACK = globalThis.TURBOPACK || []).push(["output/4e721_crates_turbopack-tests_tests_snapshot_basic_use-strict_input_index_c0953835.js", {
"[project]/turbopack/crates/turbopack-tests/tests/snapshot/basic/use-strict/input/index.js [test] (ecmascript)": ((__turbopack_context__) => {
"use strict";
var { m: module, e: exports } = __turbopack_context__;
{
console.log('this is CJS');
module.exports = 1234;
}}),
}]);
//# sourceMappingURL=4e721_crates_turbopack-tests_tests_snapshot_basic_use-strict_input_index_c0953835.js.map
\ No newline at end of file
{
"version": 3,
"sources": [],
"sections": [
{"offset": {"line": 7, "column": 0}, "map": {"version":3,"sources":["turbopack:///[project]/turbopack/crates/turbopack-tests/tests/snapshot/basic/use-strict/input/index.js"],"sourcesContent":["'use strict'\n\nconsole.log('this is CJS')\nmodule.exports = 1234\n"],"names":[],"mappings":"AAEA,QAAQ,GAAG,CAAC;AACZ,OAAO,OAAO,GAAG"}}]
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment