Unverified Commit 9dfdf713 authored by Niklas Mischkulnig's avatar Niklas Mischkulnig Committed by GitHub

Turbopack: RawEcmascriptModule (#81806)

Add a JS module type that does no analysis, and just concatenates the source file into the output file.
As a consequence however, the input module cannot have
- `import`s/`require`s/`new Worker`/...
- ~~`process.env.*` that should be replaced~~
- JSX/any new syntax, since the module won't be transpiled using the user's browserslist

```
before:

 ✓ Starting...
 ✓ Ready in 335ms
 ○ Compiling / ...
 ✓ Compiled / in 980ms
 GET / 200 in 1258ms

after:

 ✓ Starting...
 ✓ Ready in 406ms
 ✓ Compiled / in 406ms
 GET / 200 in 692ms
```

<img width="1358" height="959" alt="Bildschirmfoto 2025-09-16 um 12 09 47" src="https://github.com/user-attachments/assets/87bb3161-8749-475b-9a84-bf62324735e4" />

<img width="1352" height="939" alt="Bildschirmfoto 2025-09-16 um 12 12 48" src="https://github.com/user-attachments/assets/4a42e724-7090-4ca7-bad9-41e5cf1af80b" />
parent 498c7fce
......@@ -4291,6 +4291,7 @@ dependencies = [
"serde_json",
"serde_path_to_error",
"swc_core",
"swc_sourcemap",
"thiserror 1.0.69",
"tracing",
"turbo-esregex",
......
......@@ -35,6 +35,7 @@ remove_console = { workspace = true }
itertools = { workspace = true }
percent-encoding = "2.3.1"
serde_path_to_error = { workspace = true }
swc_sourcemap = { workspace = true }
swc_core = { workspace = true, features = [
"base",
......
......@@ -35,6 +35,7 @@ mod next_shared;
pub mod next_telemetry;
mod page_loader;
pub mod pages_structure;
pub mod raw_ecmascript_module;
pub mod segment_config;
pub mod tracing_presets;
mod transform_options;
......
......@@ -2,7 +2,7 @@ use anyhow::Result;
use next_custom_transforms::transforms::strip_page_exports::ExportFilter;
use turbo_rcstr::RcStr;
use turbo_tasks::{ResolvedVc, Vc};
use turbopack::module_options::ModuleRule;
use turbopack::module_options::{ModuleRule, ModuleRuleEffect, ModuleType, RuleCondition};
use crate::{
mode::NextMode,
......@@ -17,6 +17,7 @@ use crate::{
next_disallow_re_export_all_in_page::get_next_disallow_export_all_in_page_rule,
next_pure::get_next_pure_rule, server_actions::ActionsTransform,
},
raw_ecmascript_module::RawEcmascriptModuleType,
};
/// Returns a list of module rules which apply client-side, Next.js-specific
......@@ -44,6 +45,20 @@ pub async fn get_next_client_transforms_rules(
));
}
// This is purely a performance optimization:
// - The next-devtools file is very large and rather slow to analyze (unforatunately, at least
// with our current implementation)
// - It's used by every single application in dev, even tiny (CNA) apps
// - It's prebundled already and doesn't contain any imports/requires
rules.push(ModuleRule::new(
RuleCondition::ResourcePathEndsWith(
"next/dist/compiled/next-devtools/index.js".to_string(),
),
vec![ModuleRuleEffect::ModuleType(ModuleType::Custom(
ResolvedVc::upcast(RawEcmascriptModuleType {}.resolved_cell()),
))],
));
rules.push(get_next_font_transform_rule(enable_mdx_rs));
let is_development = mode.await?.is_development();
......
use anyhow::Result;
use anyhow::{Result, bail};
use serde::{Deserialize, Serialize};
use turbo_rcstr::rcstr;
use turbo_tasks::{NonLocalValue, ResolvedVc, TaskInput, Vc, fxindexmap, trace::TraceRawVcs};
......@@ -7,6 +7,7 @@ use turbopack_core::{
context::AssetContext, module::Module, reference_type::ReferenceType, resolve::ModulePart,
source::Source,
};
use turbopack_ecmascript::EcmascriptInputTransforms;
use turbopack_static::ecma::StaticUrlJsModule;
use super::source_asset::StructuredImageFileSource;
......@@ -95,4 +96,14 @@ impl CustomModuleType for StructuredImageModuleType {
module_asset_context,
)
}
#[turbo_tasks::function]
fn extend_ecmascript_transforms(
self: Vc<Self>,
_preprocess: Vc<EcmascriptInputTransforms>,
_main: Vc<EcmascriptInputTransforms>,
_postprocess: Vc<EcmascriptInputTransforms>,
) -> Result<Vc<Box<dyn CustomModuleType>>> {
bail!("StructuredImageModuleType does not support adding Ecmascript transforms");
}
}
This diff is collapsed.
......@@ -622,6 +622,38 @@ async fn process_default_internal(
analyze_types,
options,
}),
Some(ModuleType::Custom(custom_module_type)) => {
match custom_module_type
.extend_ecmascript_transforms(
**extend_preprocess,
**extend_main,
**extend_postprocess,
)
.to_resolved()
.await
{
Ok(custom_module_type) => {
Some(ModuleType::Custom(custom_module_type))
}
// TODO ideally this would print the actual error message
// returned by the CustomModuleType
Err(_) => {
ModuleIssue::new(
*ident,
rcstr!("Invalid module type"),
rcstr!(
"The custom module type didn't accept the \
additional Ecmascript transforms"
),
Some(IssueSource::from_source_only(current_source)),
)
.to_resolved()
.await?
.emit();
Some(ModuleType::Custom(custom_module_type))
}
}
}
Some(module_type) => {
ModuleIssue::new(
*ident,
......
use turbo_tasks::Vc;
use turbopack_core::{module::Module, resolve::ModulePart, source::Source};
use turbopack_ecmascript::EcmascriptInputTransforms;
use crate::ModuleAssetContext;
......@@ -12,4 +13,12 @@ pub trait CustomModuleType {
module_asset_context: Vc<ModuleAssetContext>,
part: Option<ModulePart>,
) -> Vc<Box<dyn Module>>;
#[turbo_tasks::function]
fn extend_ecmascript_transforms(
self: Vc<Self>,
preprocess: Vc<EcmascriptInputTransforms>,
main: Vc<EcmascriptInputTransforms>,
postprocess: Vc<EcmascriptInputTransforms>,
) -> Vc<Box<dyn CustomModuleType>>;
}
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