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::{ ...@@ -7,7 +7,7 @@ use swc_core::{
ecma::{ ecma::{
ast::*, ast::*,
atoms::{Atom, atom}, atoms::{Atom, atom},
utils::find_pat_ids, utils::{IsDirective, find_pat_ids},
visit::{Visit, VisitWith}, visit::{Visit, VisitWith},
}, },
}; };
...@@ -169,6 +169,9 @@ pub(crate) struct ImportMap { ...@@ -169,6 +169,9 @@ pub(crate) struct ImportMap {
/// True if the module is an ESM module due to top-level await. /// True if the module is an ESM module due to top-level await.
has_top_level_await: bool, 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. /// Locations of [webpack-style "magic comments"][magic] that override import behaviors.
/// ///
/// Most commonly, these are `/* webpackIgnore: true */` comments. See [ImportAttributes] for /// Most commonly, these are `/* webpackIgnore: true */` comments. See [ImportAttributes] for
...@@ -705,6 +708,18 @@ impl Visit for Analyzer<'_> { ...@@ -705,6 +708,18 @@ impl Visit for Analyzer<'_> {
fn visit_program(&mut self, m: &Program) { fn visit_program(&mut self, m: &Program) {
self.data.has_top_level_await = has_top_level_await(m).is_some(); 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); m.visit_children_with(self);
} }
......
...@@ -51,6 +51,7 @@ impl EcmascriptChunkItemContent { ...@@ -51,6 +51,7 @@ impl EcmascriptChunkItemContent {
let content = content.await?; let content = content.await?;
let async_module = async_module_options.owned().await?; let async_module = async_module_options.owned().await?;
let strict = content.strict;
Ok(EcmascriptChunkItemContent { Ok(EcmascriptChunkItemContent {
rewrite_source_path: if *chunking_context.should_use_file_source_map_uris().await? { rewrite_source_path: if *chunking_context.should_use_file_source_map_uris().await? {
...@@ -76,6 +77,7 @@ impl EcmascriptChunkItemContent { ...@@ -76,6 +77,7 @@ impl EcmascriptChunkItemContent {
} }
EcmascriptChunkItemOptions { EcmascriptChunkItemOptions {
strict,
refresh, refresh,
externals, externals,
// These things are not available in ESM // These things are not available in ESM
......
...@@ -127,7 +127,7 @@ use crate::{ ...@@ -127,7 +127,7 @@ use crate::{
side_effect_optimization::reference::EcmascriptModulePartReference, side_effect_optimization::reference::EcmascriptModulePartReference,
simple_tree_shake::{ModuleExportUsageInfo, get_module_export_usages}, simple_tree_shake::{ModuleExportUsageInfo, get_module_export_usages},
swc_comments::{CowComments, ImmutableComments}, swc_comments::{CowComments, ImmutableComments},
transform::remove_shebang, transform::{remove_directives, remove_shebang},
}; };
#[derive( #[derive(
...@@ -888,6 +888,7 @@ pub struct EcmascriptModuleContent { ...@@ -888,6 +888,7 @@ pub struct EcmascriptModuleContent {
pub inner_code: Rope, pub inner_code: Rope,
pub source_map: Option<Rope>, pub source_map: Option<Rope>,
pub is_esm: bool, pub is_esm: bool,
pub strict: bool,
pub additional_ids: SmallVec<[ResolvedVc<ModuleId>; 1]>, pub additional_ids: SmallVec<[ResolvedVc<ModuleId>; 1]>,
} }
...@@ -1144,6 +1145,7 @@ impl EcmascriptModuleContent { ...@@ -1144,6 +1145,7 @@ impl EcmascriptModuleContent {
}, },
export_contexts: None, export_contexts: None,
is_esm: true, is_esm: true,
strict: true,
generate_source_map: options.generate_source_map, generate_source_map: options.generate_source_map,
original_source_map: CodeGenResultOriginalSourceMap::ScopeHoisting( original_source_map: CodeGenResultOriginalSourceMap::ScopeHoisting(
original_source_maps, original_source_maps,
...@@ -1546,6 +1548,7 @@ struct CodeGenResult { ...@@ -1546,6 +1548,7 @@ struct CodeGenResult {
/// `eval_context.imports.exports` /// `eval_context.imports.exports`
export_contexts: Option<FxHashMap<RcStr, Id>>, export_contexts: Option<FxHashMap<RcStr, Id>>,
is_esm: bool, is_esm: bool,
strict: bool,
generate_source_map: bool, generate_source_map: bool,
original_source_map: CodeGenResultOriginalSourceMap, original_source_map: CodeGenResultOriginalSourceMap,
minify: MinifyType, minify: MinifyType,
...@@ -1571,12 +1574,13 @@ async fn process_parse_result( ...@@ -1571,12 +1574,13 @@ async fn process_parse_result(
with_consumed_parse_result( with_consumed_parse_result(
parsed, parsed,
async |mut program, source_map, globals, eval_context, comments| -> Result<CodeGenResult> { 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( .map_either(
|e| { |e| {
( (
e.top_level_mark, e.top_level_mark,
e.is_esm(specified_module_type), e.is_esm(specified_module_type),
e.imports.strict,
Cow::Owned(e.imports.exports), Cow::Owned(e.imports.exports),
) )
}, },
...@@ -1584,6 +1588,7 @@ async fn process_parse_result( ...@@ -1584,6 +1588,7 @@ async fn process_parse_result(
( (
e.top_level_mark, e.top_level_mark,
e.is_esm(specified_module_type), e.is_esm(specified_module_type),
e.imports.strict,
Cow::Borrowed(&e.imports.exports), Cow::Borrowed(&e.imports.exports),
) )
}, },
...@@ -1711,6 +1716,7 @@ async fn process_parse_result( ...@@ -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 // 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) // line in a js file (not in a chunk item wrapped in the runtime)
remove_shebang(&mut program); remove_shebang(&mut program);
remove_directives(&mut program);
}); });
Ok(CodeGenResult { Ok(CodeGenResult {
...@@ -1725,6 +1731,7 @@ async fn process_parse_result( ...@@ -1725,6 +1731,7 @@ async fn process_parse_result(
// TODO ideally don't clone here at all // TODO ideally don't clone here at all
export_contexts: Some(export_contexts.into_owned()), export_contexts: Some(export_contexts.into_owned()),
is_esm, is_esm,
strict,
generate_source_map, generate_source_map,
original_source_map: CodeGenResultOriginalSourceMap::Single(original_source_map), original_source_map: CodeGenResultOriginalSourceMap::Single(original_source_map),
minify, minify,
...@@ -1760,6 +1767,7 @@ async fn process_parse_result( ...@@ -1760,6 +1767,7 @@ async fn process_parse_result(
comments: CodeGenResultComments::Empty, comments: CodeGenResultComments::Empty,
export_contexts: None, export_contexts: None,
is_esm: false, is_esm: false,
strict: false,
generate_source_map: false, generate_source_map: false,
original_source_map: CodeGenResultOriginalSourceMap::Single(None), original_source_map: CodeGenResultOriginalSourceMap::Single(None),
minify: MinifyType::NoMinify, minify: MinifyType::NoMinify,
...@@ -1787,6 +1795,7 @@ async fn process_parse_result( ...@@ -1787,6 +1795,7 @@ async fn process_parse_result(
comments: CodeGenResultComments::Empty, comments: CodeGenResultComments::Empty,
export_contexts: None, export_contexts: None,
is_esm: false, is_esm: false,
strict: false,
generate_source_map: false, generate_source_map: false,
original_source_map: CodeGenResultOriginalSourceMap::Single(None), original_source_map: CodeGenResultOriginalSourceMap::Single(None),
minify: MinifyType::NoMinify, minify: MinifyType::NoMinify,
...@@ -1877,6 +1886,7 @@ async fn emit_content( ...@@ -1877,6 +1886,7 @@ async fn emit_content(
source_map, source_map,
comments, comments,
is_esm, is_esm,
strict,
generate_source_map, generate_source_map,
original_source_map, original_source_map,
minify, minify,
...@@ -1941,6 +1951,7 @@ async fn emit_content( ...@@ -1941,6 +1951,7 @@ async fn emit_content(
inner_code: bytes.into(), inner_code: bytes.into(),
source_map, source_map,
is_esm, is_esm,
strict,
additional_ids, additional_ids,
} }
.cell()) .cell())
......
...@@ -125,6 +125,7 @@ impl CachedExternalModule { ...@@ -125,6 +125,7 @@ impl CachedExternalModule {
inner_code: code.build(), inner_code: code.build(),
source_map: None, source_map: None,
is_esm: self.external_type != CachedExternalType::CommonJs, is_esm: self.external_type != CachedExternalType::CommonJs,
strict: false,
additional_ids: Default::default(), additional_ids: Default::default(),
} }
.cell()) .cell())
......
...@@ -8,7 +8,7 @@ use swc_core::{ ...@@ -8,7 +8,7 @@ use swc_core::{
base::SwcComments, base::SwcComments,
common::{Mark, SourceMap, comments::Comments}, common::{Mark, SourceMap, comments::Comments},
ecma::{ ecma::{
ast::{ModuleItem, Pass, Program}, ast::{ExprStmt, ModuleItem, Pass, Program, Stmt},
preset_env::{self, Targets}, preset_env::{self, Targets},
transforms::{ transforms::{
base::{ base::{
...@@ -18,6 +18,7 @@ use swc_core::{ ...@@ -18,6 +18,7 @@ use swc_core::{
optimization::inline_globals, optimization::inline_globals,
react::react, react::react,
}, },
utils::IsDirective,
}, },
quote, quote,
}; };
...@@ -307,6 +308,51 @@ pub fn remove_shebang(program: &mut Program) { ...@@ -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)] #[turbo_tasks::value(shared)]
pub struct UnsupportedServerActionIssue { pub struct UnsupportedServerActionIssue {
pub file_path: ResolvedVc<FileSystemPath>, 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