Skip to content

Commit 9dfdf71

Browse files
authored
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" />
1 parent 498c7fc commit 9dfdf71

File tree

8 files changed

+389
-2
lines changed

8 files changed

+389
-2
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/next-core/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ remove_console = { workspace = true }
3535
itertools = { workspace = true }
3636
percent-encoding = "2.3.1"
3737
serde_path_to_error = { workspace = true }
38+
swc_sourcemap = { workspace = true }
3839

3940
swc_core = { workspace = true, features = [
4041
"base",

crates/next-core/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ mod next_shared;
3535
pub mod next_telemetry;
3636
mod page_loader;
3737
pub mod pages_structure;
38+
pub mod raw_ecmascript_module;
3839
pub mod segment_config;
3940
pub mod tracing_presets;
4041
mod transform_options;

crates/next-core/src/next_client/transforms.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use anyhow::Result;
22
use next_custom_transforms::transforms::strip_page_exports::ExportFilter;
33
use turbo_rcstr::RcStr;
44
use turbo_tasks::{ResolvedVc, Vc};
5-
use turbopack::module_options::ModuleRule;
5+
use turbopack::module_options::{ModuleRule, ModuleRuleEffect, ModuleType, RuleCondition};
66

77
use crate::{
88
mode::NextMode,
@@ -17,6 +17,7 @@ use crate::{
1717
next_disallow_re_export_all_in_page::get_next_disallow_export_all_in_page_rule,
1818
next_pure::get_next_pure_rule, server_actions::ActionsTransform,
1919
},
20+
raw_ecmascript_module::RawEcmascriptModuleType,
2021
};
2122

2223
/// 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(
4445
));
4546
}
4647

48+
// This is purely a performance optimization:
49+
// - The next-devtools file is very large and rather slow to analyze (unforatunately, at least
50+
// with our current implementation)
51+
// - It's used by every single application in dev, even tiny (CNA) apps
52+
// - It's prebundled already and doesn't contain any imports/requires
53+
rules.push(ModuleRule::new(
54+
RuleCondition::ResourcePathEndsWith(
55+
"next/dist/compiled/next-devtools/index.js".to_string(),
56+
),
57+
vec![ModuleRuleEffect::ModuleType(ModuleType::Custom(
58+
ResolvedVc::upcast(RawEcmascriptModuleType {}.resolved_cell()),
59+
))],
60+
));
61+
4762
rules.push(get_next_font_transform_rule(enable_mdx_rs));
4863

4964
let is_development = mode.await?.is_development();

crates/next-core/src/next_image/module.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use anyhow::Result;
1+
use anyhow::{Result, bail};
22
use serde::{Deserialize, Serialize};
33
use turbo_rcstr::rcstr;
44
use turbo_tasks::{NonLocalValue, ResolvedVc, TaskInput, Vc, fxindexmap, trace::TraceRawVcs};
@@ -7,6 +7,7 @@ use turbopack_core::{
77
context::AssetContext, module::Module, reference_type::ReferenceType, resolve::ModulePart,
88
source::Source,
99
};
10+
use turbopack_ecmascript::EcmascriptInputTransforms;
1011
use turbopack_static::ecma::StaticUrlJsModule;
1112

1213
use super::source_asset::StructuredImageFileSource;
@@ -95,4 +96,14 @@ impl CustomModuleType for StructuredImageModuleType {
9596
module_asset_context,
9697
)
9798
}
99+
100+
#[turbo_tasks::function]
101+
fn extend_ecmascript_transforms(
102+
self: Vc<Self>,
103+
_preprocess: Vc<EcmascriptInputTransforms>,
104+
_main: Vc<EcmascriptInputTransforms>,
105+
_postprocess: Vc<EcmascriptInputTransforms>,
106+
) -> Result<Vc<Box<dyn CustomModuleType>>> {
107+
bail!("StructuredImageModuleType does not support adding Ecmascript transforms");
108+
}
98109
}

0 commit comments

Comments
 (0)