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