Unverified Commit 6254a987 authored by Niklas Mischkulnig's avatar Niklas Mischkulnig Committed by GitHub

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)
parent c6cd1f45
...@@ -387,6 +387,7 @@ pub async fn parse_segment_config_from_source( ...@@ -387,6 +387,7 @@ pub async fn parse_segment_config_from_source(
}, },
EcmascriptInputTransforms::empty(), EcmascriptInputTransforms::empty(),
false, false,
false,
) )
.await?; .await?;
......
...@@ -20,12 +20,10 @@ pub async fn get_runtime_asset_context( ...@@ -20,12 +20,10 @@ pub async fn get_runtime_asset_context(
enable_typescript_transform: Some( enable_typescript_transform: Some(
TypescriptTransformOptions::default().resolved_cell(), TypescriptTransformOptions::default().resolved_cell(),
), ),
inline_helpers: true,
..Default::default() ..Default::default()
}, },
// TODO: This fails when enabled, we cannot insert helpers for the runtime code as this environment: Some(environment),
// happens after bundling.
// environment: Some(environment),
environment: None,
tree_shaking_mode: Some(TreeShakingMode::ReexportsOnly), tree_shaking_mode: Some(TreeShakingMode::ReexportsOnly),
..Default::default() ..Default::default()
} }
......
...@@ -63,6 +63,7 @@ swc_core = { workspace = true, features = [ ...@@ -63,6 +63,7 @@ swc_core = { workspace = true, features = [
"common_sourcemap", "common_sourcemap",
"ecma_ast_shrink", "ecma_ast_shrink",
"ecma_codegen", "ecma_codegen",
"ecma_helpers_inline",
"ecma_lints", "ecma_lints",
"ecma_minifier", "ecma_minifier",
"ecma_parser", "ecma_parser",
......
...@@ -270,6 +270,8 @@ pub struct EcmascriptOptions { ...@@ -270,6 +270,8 @@ pub struct EcmascriptOptions {
// node_modules. // node_modules.
/// Whether to replace `typeof window` with some constant value. /// Whether to replace `typeof window` with some constant value.
pub enable_typeof_window_inlining: Option<TypeofWindow>, pub enable_typeof_window_inlining: Option<TypeofWindow>,
pub inline_helpers: bool,
} }
#[turbo_tasks::value] #[turbo_tasks::value]
...@@ -687,11 +689,13 @@ impl EcmascriptModuleAsset { ...@@ -687,11 +689,13 @@ impl EcmascriptModuleAsset {
impl EcmascriptModuleAsset { impl EcmascriptModuleAsset {
pub async fn parse(&self) -> Result<Vc<ParseResult>> { pub async fn parse(&self) -> Result<Vc<ParseResult>> {
let options = self.options.await?;
Ok(parse( Ok(parse(
*self.source, *self.source,
self.ty, self.ty,
*self.transforms, *self.transforms,
self.options.await?.analyze_mode == AnalyzeMode::Tracing, options.analyze_mode == AnalyzeMode::Tracing,
options.inline_helpers,
)) ))
} }
......
...@@ -13,11 +13,14 @@ use swc_core::{ ...@@ -13,11 +13,14 @@ use swc_core::{
}, },
ecma::{ ecma::{
ast::{EsVersion, Id, ObjectPatProp, Pat, Program, VarDecl}, ast::{EsVersion, Id, ObjectPatProp, Pat, Program, VarDecl},
lints::{config::LintConfig, rules::LintParams}, lints::{self, config::LintConfig, rules::LintParams},
parser::{EsSyntax, Parser, Syntax, TsSyntax, lexer::Lexer}, parser::{EsSyntax, Parser, Syntax, TsSyntax, lexer::Lexer},
transforms::base::{ transforms::{
helpers::{HELPERS, Helpers}, base::{
resolver, helpers::{HELPERS, Helpers},
resolver,
},
proposal::explicit_resource_management::explicit_resource_management,
}, },
visit::{Visit, VisitMutWith, VisitWith, noop_visit_type}, visit::{Visit, VisitMutWith, VisitWith, noop_visit_type},
}, },
...@@ -174,6 +177,7 @@ pub async fn parse( ...@@ -174,6 +177,7 @@ pub async fn parse(
ty: EcmascriptModuleAssetType, ty: EcmascriptModuleAssetType,
transforms: ResolvedVc<EcmascriptInputTransforms>, transforms: ResolvedVc<EcmascriptInputTransforms>,
is_external_tracing: bool, is_external_tracing: bool,
inline_helpers: bool,
) -> Result<Vc<ParseResult>> { ) -> Result<Vc<ParseResult>> {
let span = tracing::info_span!( let span = tracing::info_span!(
"parse ecmascript", "parse ecmascript",
...@@ -186,6 +190,7 @@ pub async fn parse( ...@@ -186,6 +190,7 @@ pub async fn parse(
ty, ty,
transforms, transforms,
is_external_tracing && matches!(ty, EcmascriptModuleAssetType::EcmascriptExtensionless), is_external_tracing && matches!(ty, EcmascriptModuleAssetType::EcmascriptExtensionless),
inline_helpers,
) )
.instrument(span) .instrument(span)
.await .await
...@@ -203,6 +208,7 @@ async fn parse_internal( ...@@ -203,6 +208,7 @@ async fn parse_internal(
ty: EcmascriptModuleAssetType, ty: EcmascriptModuleAssetType,
transforms: ResolvedVc<EcmascriptInputTransforms>, transforms: ResolvedVc<EcmascriptInputTransforms>,
loose_errors: bool, loose_errors: bool,
inline_helpers: bool,
) -> Result<Vc<ParseResult>> { ) -> Result<Vc<ParseResult>> {
let content = source.content(); let content = source.content();
let fs_path = source.ident().path().owned().await?; let fs_path = source.ident().path().owned().await?;
...@@ -247,6 +253,7 @@ async fn parse_internal( ...@@ -247,6 +253,7 @@ async fn parse_internal(
ty, ty,
transforms, transforms,
loose_errors, loose_errors,
inline_helpers,
) )
.await .await
{ {
...@@ -302,6 +309,7 @@ async fn parse_file_content( ...@@ -302,6 +309,7 @@ async fn parse_file_content(
ty: EcmascriptModuleAssetType, ty: EcmascriptModuleAssetType,
transforms: &[EcmascriptInputTransform], transforms: &[EcmascriptInputTransform],
loose_errors: bool, loose_errors: bool,
inline_helpers: bool,
) -> Result<Vc<ParseResult>> { ) -> Result<Vc<ParseResult>> {
let source_map: Arc<swc_core::common::SourceMap> = Default::default(); let source_map: Arc<swc_core::common::SourceMap> = Default::default();
let (emitter, collector) = IssueEmitter::new( let (emitter, collector) = IssueEmitter::new(
...@@ -331,19 +339,20 @@ async fn parse_file_content( ...@@ -331,19 +339,20 @@ async fn parse_file_content(
let lexer = Lexer::new( let lexer = Lexer::new(
match ty { match ty {
EcmascriptModuleAssetType::Ecmascript EcmascriptModuleAssetType::Ecmascript
| EcmascriptModuleAssetType::EcmascriptExtensionless | EcmascriptModuleAssetType::EcmascriptExtensionless => {
=> Syntax::Es(EsSyntax { Syntax::Es(EsSyntax {
jsx: true, jsx: true,
fn_bind: true, fn_bind: true,
decorators: true, decorators: true,
decorators_before_export: true, decorators_before_export: true,
export_default_from: true, export_default_from: true,
import_attributes: true, import_attributes: true,
allow_super_outside_method: true, allow_super_outside_method: true,
allow_return_outside_function: true, allow_return_outside_function: true,
auto_accessors: true, auto_accessors: true,
explicit_resource_management: true, explicit_resource_management: true,
}), })
}
EcmascriptModuleAssetType::Typescript { tsx, .. } => { EcmascriptModuleAssetType::Typescript { tsx, .. } => {
Syntax::Typescript(TsSyntax { Syntax::Typescript(TsSyntax {
decorators: true, decorators: true,
...@@ -408,7 +417,7 @@ async fn parse_file_content( ...@@ -408,7 +417,7 @@ async fn parse_file_content(
| EcmascriptModuleAssetType::TypescriptDeclaration | EcmascriptModuleAssetType::TypescriptDeclaration
); );
let helpers=Helpers::new(true); let helpers = Helpers::new(!inline_helpers);
let span = tracing::trace_span!("swc_resolver").entered(); let span = tracing::trace_span!("swc_resolver").entered();
parsed_program.visit_mut_with(&mut resolver( parsed_program.visit_mut_with(&mut resolver(
...@@ -421,7 +430,7 @@ async fn parse_file_content( ...@@ -421,7 +430,7 @@ async fn parse_file_content(
let span = tracing::trace_span!("swc_lint").entered(); let span = tracing::trace_span!("swc_lint").entered();
let lint_config = LintConfig::default(); let lint_config = LintConfig::default();
let rules = swc_core::ecma::lints::rules::all(LintParams { let rules = lints::rules::all(LintParams {
program: &parsed_program, program: &parsed_program,
lint_config: &lint_config, lint_config: &lint_config,
unresolved_ctxt: SyntaxContext::empty().apply_mark(unresolved_mark), unresolved_ctxt: SyntaxContext::empty().apply_mark(unresolved_mark),
...@@ -430,13 +439,11 @@ async fn parse_file_content( ...@@ -430,13 +439,11 @@ async fn parse_file_content(
source_map: source_map.clone(), source_map: source_map.clone(),
}); });
parsed_program.mutate(swc_core::ecma::lints::rules::lint_pass(rules)); parsed_program.mutate(lints::rules::lint_pass(rules));
drop(span); drop(span);
HELPERS.set(&helpers, || { HELPERS.set(&helpers, || {
parsed_program.mutate( parsed_program.mutate(explicit_resource_management());
swc_core::ecma::transforms::proposal::explicit_resource_management::explicit_resource_management(),
);
}); });
let var_with_ts_declare = if is_typescript { let var_with_ts_declare = if is_typescript {
...@@ -456,7 +463,7 @@ async fn parse_file_content( ...@@ -456,7 +463,7 @@ async fn parse_file_content(
file_name_hash: file_path_hash, file_name_hash: file_path_hash,
query_str: query, query_str: query,
file_path: fs_path.clone(), file_path: fs_path.clone(),
source source,
}; };
let span = tracing::trace_span!("transforms"); let span = tracing::trace_span!("transforms");
async { async {
...@@ -467,8 +474,8 @@ async fn parse_file_content( ...@@ -467,8 +474,8 @@ async fn parse_file_content(
} }
anyhow::Ok(()) anyhow::Ok(())
} }
.instrument(span) .instrument(span)
.await?; .await?;
if parser_handler.has_errors() { if parser_handler.has_errors() {
let messages = if let Some(error) = collector_parse.last_emitted_issue() { let messages = if let Some(error) = collector_parse.last_emitted_issue() {
...@@ -481,16 +488,15 @@ async fn parse_file_content( ...@@ -481,16 +488,15 @@ async fn parse_file_content(
} else { } else {
None None
}; };
let messages = let messages = Some(messages.unwrap_or_else(|| vec![fm.src.clone().into()]));
Some(messages.unwrap_or_else(|| vec![fm.src.clone().into()]));
return Ok(ParseResult::Unparsable { messages }); return Ok(ParseResult::Unparsable { messages });
} }
let helpers = Helpers::from_data(helpers); let helpers = Helpers::from_data(helpers);
HELPERS.set(&helpers, || { HELPERS.set(&helpers, || {
parsed_program.mutate( parsed_program.mutate(swc_core::ecma::transforms::base::helpers::inject_helpers(
swc_core::ecma::transforms::base::helpers::inject_helpers(unresolved_mark), unresolved_mark,
); ));
}); });
let eval_context = EvalContext::new( let eval_context = EvalContext::new(
...@@ -512,13 +518,9 @@ async fn parse_file_content( ...@@ -512,13 +518,9 @@ async fn parse_file_content(
source_map, source_map,
}) })
}, },
|f, cx| { |f, cx| GLOBALS.set(globals_ref, || HANDLER.set(&handler, || f.poll(cx))),
GLOBALS.set(globals_ref, || {
HANDLER.set(&handler, || f.poll(cx))
})
},
) )
.await?; .await?;
if let ParseResult::Ok { if let ParseResult::Ok {
globals: ref mut g, .. globals: ref mut g, ..
} = result } = result
......
...@@ -195,6 +195,7 @@ pub async fn webpack_runtime( ...@@ -195,6 +195,7 @@ pub async fn webpack_runtime(
EcmascriptModuleAssetType::Ecmascript, EcmascriptModuleAssetType::Ecmascript,
transforms, transforms,
false, false,
false,
) )
.await?; .await?;
match &*parsed { match &*parsed {
......
...@@ -31,6 +31,7 @@ pub async fn module_references( ...@@ -31,6 +31,7 @@ pub async fn module_references(
EcmascriptModuleAssetType::Ecmascript, EcmascriptModuleAssetType::Ecmascript,
*transforms, *transforms,
false, false,
false,
) )
.await?; .await?;
match &*parsed { match &*parsed {
......
{ {
"browserslist": "chrome 41" "browserslist": "chrome 41",
"runtimeType": "Production"
} }
...@@ -209,6 +209,7 @@ impl ModuleOptions { ...@@ -209,6 +209,7 @@ impl ModuleOptions {
esm_url_rewrite_behavior, esm_url_rewrite_behavior,
enable_typeof_window_inlining, enable_typeof_window_inlining,
source_maps: ecmascript_source_maps, source_maps: ecmascript_source_maps,
inline_helpers,
.. ..
}, },
enable_mdx, enable_mdx,
...@@ -282,6 +283,7 @@ impl ModuleOptions { ...@@ -282,6 +283,7 @@ impl ModuleOptions {
keep_last_successful_parse, keep_last_successful_parse,
analyze_mode, analyze_mode,
enable_typeof_window_inlining, enable_typeof_window_inlining,
inline_helpers,
..Default::default() ..Default::default()
}; };
let ecmascript_options_vc = ecmascript_options.resolved_cell(); let ecmascript_options_vc = ecmascript_options.resolved_cell();
......
...@@ -239,6 +239,9 @@ pub struct EcmascriptOptionsContext { ...@@ -239,6 +239,9 @@ pub struct EcmascriptOptionsContext {
/// Specifies how Source Maps are handled. /// Specifies how Source Maps are handled.
pub source_maps: SourceMapsType, pub source_maps: SourceMapsType,
// TODO should this be a part of Environment instead?
pub inline_helpers: bool,
pub placeholder_for_future_extensions: (), pub placeholder_for_future_extensions: (),
} }
......
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