Skip to content

Commit 6254a98

Browse files
authored
Turbopack: transpile runtime with swc (#84931)
Transpile the Turbopack runtime according to browserslist We need to inline swc helpers, as the runtime is what provides the module system at runtime (so we cannot require anything here) Part of #83799 Related to [PACK-5450](https://linear.app/vercel/issue/PACK-5450/turbopack-builds-do-not-correctly-process-all-files-ex-turbopack)
1 parent c6cd1f4 commit 6254a98

File tree

12 files changed

+1722
-45
lines changed

12 files changed

+1722
-45
lines changed

crates/next-core/src/segment_config.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,7 @@ pub async fn parse_segment_config_from_source(
387387
},
388388
EcmascriptInputTransforms::empty(),
389389
false,
390+
false,
390391
)
391392
.await?;
392393

turbopack/crates/turbopack-ecmascript-runtime/src/asset_context.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,10 @@ pub async fn get_runtime_asset_context(
2020
enable_typescript_transform: Some(
2121
TypescriptTransformOptions::default().resolved_cell(),
2222
),
23+
inline_helpers: true,
2324
..Default::default()
2425
},
25-
// TODO: This fails when enabled, we cannot insert helpers for the runtime code as this
26-
// happens after bundling.
27-
// environment: Some(environment),
28-
environment: None,
26+
environment: Some(environment),
2927
tree_shaking_mode: Some(TreeShakingMode::ReexportsOnly),
3028
..Default::default()
3129
}

turbopack/crates/turbopack-ecmascript/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ swc_core = { workspace = true, features = [
6363
"common_sourcemap",
6464
"ecma_ast_shrink",
6565
"ecma_codegen",
66+
"ecma_helpers_inline",
6667
"ecma_lints",
6768
"ecma_minifier",
6869
"ecma_parser",

turbopack/crates/turbopack-ecmascript/src/lib.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,8 @@ pub struct EcmascriptOptions {
270270
// node_modules.
271271
/// Whether to replace `typeof window` with some constant value.
272272
pub enable_typeof_window_inlining: Option<TypeofWindow>,
273+
274+
pub inline_helpers: bool,
273275
}
274276

275277
#[turbo_tasks::value]
@@ -687,11 +689,13 @@ impl EcmascriptModuleAsset {
687689

688690
impl EcmascriptModuleAsset {
689691
pub async fn parse(&self) -> Result<Vc<ParseResult>> {
692+
let options = self.options.await?;
690693
Ok(parse(
691694
*self.source,
692695
self.ty,
693696
*self.transforms,
694-
self.options.await?.analyze_mode == AnalyzeMode::Tracing,
697+
options.analyze_mode == AnalyzeMode::Tracing,
698+
options.inline_helpers,
695699
))
696700
}
697701

turbopack/crates/turbopack-ecmascript/src/parse.rs

Lines changed: 39 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,14 @@ use swc_core::{
1313
},
1414
ecma::{
1515
ast::{EsVersion, Id, ObjectPatProp, Pat, Program, VarDecl},
16-
lints::{config::LintConfig, rules::LintParams},
16+
lints::{self, config::LintConfig, rules::LintParams},
1717
parser::{EsSyntax, Parser, Syntax, TsSyntax, lexer::Lexer},
18-
transforms::base::{
19-
helpers::{HELPERS, Helpers},
20-
resolver,
18+
transforms::{
19+
base::{
20+
helpers::{HELPERS, Helpers},
21+
resolver,
22+
},
23+
proposal::explicit_resource_management::explicit_resource_management,
2124
},
2225
visit::{Visit, VisitMutWith, VisitWith, noop_visit_type},
2326
},
@@ -174,6 +177,7 @@ pub async fn parse(
174177
ty: EcmascriptModuleAssetType,
175178
transforms: ResolvedVc<EcmascriptInputTransforms>,
176179
is_external_tracing: bool,
180+
inline_helpers: bool,
177181
) -> Result<Vc<ParseResult>> {
178182
let span = tracing::info_span!(
179183
"parse ecmascript",
@@ -186,6 +190,7 @@ pub async fn parse(
186190
ty,
187191
transforms,
188192
is_external_tracing && matches!(ty, EcmascriptModuleAssetType::EcmascriptExtensionless),
193+
inline_helpers,
189194
)
190195
.instrument(span)
191196
.await
@@ -203,6 +208,7 @@ async fn parse_internal(
203208
ty: EcmascriptModuleAssetType,
204209
transforms: ResolvedVc<EcmascriptInputTransforms>,
205210
loose_errors: bool,
211+
inline_helpers: bool,
206212
) -> Result<Vc<ParseResult>> {
207213
let content = source.content();
208214
let fs_path = source.ident().path().owned().await?;
@@ -247,6 +253,7 @@ async fn parse_internal(
247253
ty,
248254
transforms,
249255
loose_errors,
256+
inline_helpers,
250257
)
251258
.await
252259
{
@@ -302,6 +309,7 @@ async fn parse_file_content(
302309
ty: EcmascriptModuleAssetType,
303310
transforms: &[EcmascriptInputTransform],
304311
loose_errors: bool,
312+
inline_helpers: bool,
305313
) -> Result<Vc<ParseResult>> {
306314
let source_map: Arc<swc_core::common::SourceMap> = Default::default();
307315
let (emitter, collector) = IssueEmitter::new(
@@ -331,19 +339,20 @@ async fn parse_file_content(
331339
let lexer = Lexer::new(
332340
match ty {
333341
EcmascriptModuleAssetType::Ecmascript
334-
| EcmascriptModuleAssetType::EcmascriptExtensionless
335-
=> Syntax::Es(EsSyntax {
336-
jsx: true,
337-
fn_bind: true,
338-
decorators: true,
339-
decorators_before_export: true,
340-
export_default_from: true,
341-
import_attributes: true,
342-
allow_super_outside_method: true,
343-
allow_return_outside_function: true,
344-
auto_accessors: true,
345-
explicit_resource_management: true,
346-
}),
342+
| EcmascriptModuleAssetType::EcmascriptExtensionless => {
343+
Syntax::Es(EsSyntax {
344+
jsx: true,
345+
fn_bind: true,
346+
decorators: true,
347+
decorators_before_export: true,
348+
export_default_from: true,
349+
import_attributes: true,
350+
allow_super_outside_method: true,
351+
allow_return_outside_function: true,
352+
auto_accessors: true,
353+
explicit_resource_management: true,
354+
})
355+
}
347356
EcmascriptModuleAssetType::Typescript { tsx, .. } => {
348357
Syntax::Typescript(TsSyntax {
349358
decorators: true,
@@ -408,7 +417,7 @@ async fn parse_file_content(
408417
| EcmascriptModuleAssetType::TypescriptDeclaration
409418
);
410419

411-
let helpers=Helpers::new(true);
420+
let helpers = Helpers::new(!inline_helpers);
412421
let span = tracing::trace_span!("swc_resolver").entered();
413422

414423
parsed_program.visit_mut_with(&mut resolver(
@@ -421,7 +430,7 @@ async fn parse_file_content(
421430
let span = tracing::trace_span!("swc_lint").entered();
422431

423432
let lint_config = LintConfig::default();
424-
let rules = swc_core::ecma::lints::rules::all(LintParams {
433+
let rules = lints::rules::all(LintParams {
425434
program: &parsed_program,
426435
lint_config: &lint_config,
427436
unresolved_ctxt: SyntaxContext::empty().apply_mark(unresolved_mark),
@@ -430,13 +439,11 @@ async fn parse_file_content(
430439
source_map: source_map.clone(),
431440
});
432441

433-
parsed_program.mutate(swc_core::ecma::lints::rules::lint_pass(rules));
442+
parsed_program.mutate(lints::rules::lint_pass(rules));
434443
drop(span);
435444

436445
HELPERS.set(&helpers, || {
437-
parsed_program.mutate(
438-
swc_core::ecma::transforms::proposal::explicit_resource_management::explicit_resource_management(),
439-
);
446+
parsed_program.mutate(explicit_resource_management());
440447
});
441448

442449
let var_with_ts_declare = if is_typescript {
@@ -456,7 +463,7 @@ async fn parse_file_content(
456463
file_name_hash: file_path_hash,
457464
query_str: query,
458465
file_path: fs_path.clone(),
459-
source
466+
source,
460467
};
461468
let span = tracing::trace_span!("transforms");
462469
async {
@@ -467,8 +474,8 @@ async fn parse_file_content(
467474
}
468475
anyhow::Ok(())
469476
}
470-
.instrument(span)
471-
.await?;
477+
.instrument(span)
478+
.await?;
472479

473480
if parser_handler.has_errors() {
474481
let messages = if let Some(error) = collector_parse.last_emitted_issue() {
@@ -481,16 +488,15 @@ async fn parse_file_content(
481488
} else {
482489
None
483490
};
484-
let messages =
485-
Some(messages.unwrap_or_else(|| vec![fm.src.clone().into()]));
491+
let messages = Some(messages.unwrap_or_else(|| vec![fm.src.clone().into()]));
486492
return Ok(ParseResult::Unparsable { messages });
487493
}
488494

489495
let helpers = Helpers::from_data(helpers);
490496
HELPERS.set(&helpers, || {
491-
parsed_program.mutate(
492-
swc_core::ecma::transforms::base::helpers::inject_helpers(unresolved_mark),
493-
);
497+
parsed_program.mutate(swc_core::ecma::transforms::base::helpers::inject_helpers(
498+
unresolved_mark,
499+
));
494500
});
495501

496502
let eval_context = EvalContext::new(
@@ -512,13 +518,9 @@ async fn parse_file_content(
512518
source_map,
513519
})
514520
},
515-
|f, cx| {
516-
GLOBALS.set(globals_ref, || {
517-
HANDLER.set(&handler, || f.poll(cx))
518-
})
519-
},
521+
|f, cx| GLOBALS.set(globals_ref, || HANDLER.set(&handler, || f.poll(cx))),
520522
)
521-
.await?;
523+
.await?;
522524
if let ParseResult::Ok {
523525
globals: ref mut g, ..
524526
} = result

turbopack/crates/turbopack-ecmascript/src/webpack/parse.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,7 @@ pub async fn webpack_runtime(
195195
EcmascriptModuleAssetType::Ecmascript,
196196
transforms,
197197
false,
198+
false,
198199
)
199200
.await?;
200201
match &*parsed {

turbopack/crates/turbopack-ecmascript/src/webpack/references.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ pub async fn module_references(
3131
EcmascriptModuleAssetType::Ecmascript,
3232
*transforms,
3333
false,
34+
false,
3435
)
3536
.await?;
3637
match &*parsed {
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
{
2-
"browserslist": "chrome 41"
2+
"browserslist": "chrome 41",
3+
"runtimeType": "Production"
34
}

0 commit comments

Comments
 (0)