Unverified Commit 9941eddc authored by vercel-fleet-prod[bot]'s avatar vercel-fleet-prod[bot] Committed by GitHub

Add Turbopack constant export inlining

Co-authored-by: 's avatarTobias Koppers <1365881+sokra@users.noreply.github.com>
parent 69d11874
...@@ -379,6 +379,7 @@ pub async fn get_client_module_options_context( ...@@ -379,6 +379,7 @@ pub async fn get_client_module_options_context(
mangle_export_names: *next_config.turbopack_mangle_export_names(mode).await?, mangle_export_names: *next_config.turbopack_mangle_export_names(mode).await?,
cjs_scope_hoisting: *next_config.turbopack_cjs_scope_hoisting().await?, cjs_scope_hoisting: *next_config.turbopack_cjs_scope_hoisting().await?,
cross_module_constants: *next_config.turbopack_cross_module_constants().await?, cross_module_constants: *next_config.turbopack_cross_module_constants().await?,
inline_constant_exports: *next_config.turbopack_inline_constant_exports().await?,
lazy_compilation: *next_config lazy_compilation: *next_config
.turbopack_lazy_dynamic_imports(*next_mode) .turbopack_lazy_dynamic_imports(*next_mode)
.await?, .await?,
......
...@@ -1453,8 +1453,10 @@ pub struct ExperimentalConfig { ...@@ -1453,8 +1453,10 @@ pub struct ExperimentalConfig {
turbopack_mangle_export_names: Option<bool>, turbopack_mangle_export_names: Option<bool>,
/// Enable scope hoisting of static CommonJS modules. Defaults to false. /// Enable scope hoisting of static CommonJS modules. Defaults to false.
turbopack_cjs_scope_hoisting: Option<bool>, turbopack_cjs_scope_hoisting: Option<bool>,
/// Enable cross-module constant inlining. Defaults to false. /// Enable analyzer-aware cross-module constant inlining. Defaults to false.
turbopack_cross_module_constants: Option<bool>, turbopack_cross_module_constants: Option<bool>,
/// Inline short constant exports during code generation. Defaults to false.
turbopack_inline_constant_exports: Option<bool>,
/// Devtool option for the segment explorer. /// Devtool option for the segment explorer.
devtool_segment_explorer: Option<bool>, devtool_segment_explorer: Option<bool>,
/// Whether to report inlined system environment variables as warnings or errors. /// Whether to report inlined system environment variables as warnings or errors.
...@@ -2621,6 +2623,15 @@ impl NextConfig { ...@@ -2621,6 +2623,15 @@ impl NextConfig {
) )
} }
#[turbo_tasks::function]
pub fn turbopack_inline_constant_exports(&self) -> Vc<bool> {
Vc::cell(
self.experimental
.turbopack_inline_constant_exports
.unwrap_or(false),
)
}
#[turbo_tasks::function] #[turbo_tasks::function]
pub fn turbopack_plugin_runtime_strategy(&self) -> Vc<TurbopackPluginRuntimeStrategy> { pub fn turbopack_plugin_runtime_strategy(&self) -> Vc<TurbopackPluginRuntimeStrategy> {
// Child processes cannot be spawned from inside wasm, so worker threads are the only // Child processes cannot be spawned from inside wasm, so worker threads are the only
......
...@@ -554,6 +554,7 @@ pub async fn get_server_module_options_context( ...@@ -554,6 +554,7 @@ pub async fn get_server_module_options_context(
mangle_export_names: *next_config.turbopack_mangle_export_names(mode).await?, mangle_export_names: *next_config.turbopack_mangle_export_names(mode).await?,
cjs_scope_hoisting: *next_config.turbopack_cjs_scope_hoisting().await?, cjs_scope_hoisting: *next_config.turbopack_cjs_scope_hoisting().await?,
cross_module_constants: *next_config.turbopack_cross_module_constants().await?, cross_module_constants: *next_config.turbopack_cross_module_constants().await?,
inline_constant_exports: *next_config.turbopack_inline_constant_exports().await?,
..Default::default() ..Default::default()
}, },
execution_context: Some(execution_context), execution_context: Some(execution_context),
......
...@@ -429,6 +429,7 @@ export const experimentalSchema = { ...@@ -429,6 +429,7 @@ export const experimentalSchema = {
turbopackMangleExportNames: z.boolean().optional(), turbopackMangleExportNames: z.boolean().optional(),
turbopackCjsScopeHoisting: z.boolean().optional(), turbopackCjsScopeHoisting: z.boolean().optional(),
turbopackCrossModuleConstants: z.boolean().optional(), turbopackCrossModuleConstants: z.boolean().optional(),
turbopackInlineConstantExports: z.boolean().optional(),
turbopackServerFastRefresh: z.boolean().optional(), turbopackServerFastRefresh: z.boolean().optional(),
optimizePackageImports: z.array(z.string()).optional(), optimizePackageImports: z.array(z.string()).optional(),
optimizeServerReact: z.boolean().optional(), optimizeServerReact: z.boolean().optional(),
......
...@@ -1052,6 +1052,14 @@ export interface ExperimentalConfig { ...@@ -1052,6 +1052,14 @@ export interface ExperimentalConfig {
*/ */
turbopackCrossModuleConstants?: boolean turbopackCrossModuleConstants?: boolean
/**
* Inline short, identity-free constant exports during Turbopack code generation without using
* their values for compile-time analysis.
*
* Defaults to `false`
*/
turbopackInlineConstantExports?: boolean
/** /**
* Set this to `false` to disable the automatic configuration of the babel loader when a Babel * Set this to `false` to disable the automatic configuration of the babel loader when a Babel
* configuration file is present. This option is enabled by default. * configuration file is present. This option is enabled by default.
......
...@@ -249,8 +249,12 @@ pub struct EcmascriptOptions { ...@@ -249,8 +249,12 @@ pub struct EcmascriptOptions {
pub mangle_export_names: bool, pub mangle_export_names: bool,
/// Whether to scope hoist static CommonJS modules. Defaults to false. /// Whether to scope hoist static CommonJS modules. Defaults to false.
pub cjs_scope_hoisting: bool, pub cjs_scope_hoisting: bool,
/// Whether to enable cross-module constant inlining. Defaults to false. /// Whether to use cross-module constants during analysis and code generation. Defaults to
/// false.
pub cross_module_constants: bool, pub cross_module_constants: bool,
/// Whether to inline short, identity-free constant exports only during code generation.
/// Defaults to false.
pub inline_constant_exports: bool,
/// Whether dynamic import targets are compiled after their runtime proxy is activated. /// Whether dynamic import targets are compiled after their runtime proxy is activated.
pub lazy_compilation: bool, pub lazy_compilation: bool,
} }
......
...@@ -6,14 +6,17 @@ use bincode::{Decode, Encode}; ...@@ -6,14 +6,17 @@ use bincode::{Decode, Encode};
use num_bigint::BigInt; use num_bigint::BigInt;
use parking_lot::Mutex; use parking_lot::Mutex;
use rustc_hash::FxHashMap; use rustc_hash::FxHashMap;
use swc_core::common::{GLOBALS, source_map::SmallPos}; use swc_core::{
common::{GLOBALS, source_map::SmallPos},
ecma::ast::{ModuleDecl, ModuleItem, Program},
};
use thread_local::ThreadLocal; use thread_local::ThreadLocal;
use tracing::instrument; use tracing::instrument;
use turbo_rcstr::{RcStr, rcstr}; use turbo_rcstr::{RcStr, rcstr};
use turbo_tasks::{NonLocalValue, ResolvedVc, TryJoinIterExt, Vc, trace::TraceRawVcs}; use turbo_tasks::{NonLocalValue, ResolvedVc, TryJoinIterExt, Vc, trace::TraceRawVcs};
use turbo_tasks_fs::FileSystemPath; use turbo_tasks_fs::FileSystemPath;
use turbopack_core::{ use turbopack_core::{
compile_time_info::CompileTimeInfo, compile_time_info::{CompileTimeDefineValue, CompileTimeInfo},
issue::{Issue, IssueExt, IssueSeverity, IssueSource, IssueStage, StyledString}, issue::{Issue, IssueExt, IssueSeverity, IssueSource, IssueStage, StyledString},
module::Module, module::Module,
reference::ModuleReference, reference::ModuleReference,
...@@ -35,6 +38,38 @@ const STRING_INLINE_THRESHOLD: usize = 6; ...@@ -35,6 +38,38 @@ const STRING_INLINE_THRESHOLD: usize = 6;
const NUMBER_INLINE_THRESHOLD: f64 = 1_000_000.0; const NUMBER_INLINE_THRESHOLD: f64 = 1_000_000.0;
const BIGINT_INLINE_THRESHOLD: i64 = 1_000_000; const BIGINT_INLINE_THRESHOLD: i64 = 1_000_000;
#[derive(Clone, Copy)]
enum AutomaticInlineStatus {
Inline,
TooLong,
HasIdentity,
}
fn automatic_inline_status(value: &ConstantValue) -> AutomaticInlineStatus {
match value {
ConstantValue::Str(s) if s.as_str().len() > STRING_INLINE_THRESHOLD => {
AutomaticInlineStatus::TooLong
}
ConstantValue::Num(n) if n.0.abs() > NUMBER_INLINE_THRESHOLD => {
AutomaticInlineStatus::TooLong
}
ConstantValue::BigInt(n)
if **n > BigInt::from(BIGINT_INLINE_THRESHOLD)
|| **n < BigInt::from(-BIGINT_INLINE_THRESHOLD) =>
{
AutomaticInlineStatus::TooLong
}
ConstantValue::Regex(_) => AutomaticInlineStatus::HasIdentity,
ConstantValue::Undefined
| ConstantValue::Null
| ConstantValue::True
| ConstantValue::False
| ConstantValue::Num(_)
| ConstantValue::BigInt(_)
| ConstantValue::Str(_) => AutomaticInlineStatus::Inline,
}
}
/// Import names that are all-uppercase and contain at least one letter are eligible for automatic /// Import names that are all-uppercase and contain at least one letter are eligible for automatic
/// constant inlining, even without an import attribute. /// constant inlining, even without an import attribute.
pub fn is_import_name_eligible_for_exports(name: &str) -> bool { pub fn is_import_name_eligible_for_exports(name: &str) -> bool {
...@@ -126,42 +161,21 @@ impl ConstantsModule { ...@@ -126,42 +161,21 @@ impl ConstantsModule {
match value { match value {
ConstantsModuleExport::Constant(value) => { ConstantsModuleExport::Constant(value) => {
if !has_opt_in { if !has_opt_in {
// when not having opt in, only inline short literals // when not having opt in, only inline short primitive values
match &value.0 { match automatic_inline_status(&value.0) {
ConstantValue::Str(s) AutomaticInlineStatus::Inline => {
if s.as_str().len() > STRING_INLINE_THRESHOLD => JsValue::Constant(value.0.clone())
{
JsValue::unknown_empty(
false,
rcstr!("constant too long"),
)
} }
ConstantValue::Num(n) AutomaticInlineStatus::TooLong => JsValue::unknown_empty(
if n.0.abs() > NUMBER_INLINE_THRESHOLD => false,
{ rcstr!("constant too long"),
JsValue::unknown_empty( ),
false, AutomaticInlineStatus::HasIdentity => {
rcstr!("constant too long"),
)
}
ConstantValue::BigInt(n)
if **n > BigInt::from(BIGINT_INLINE_THRESHOLD)
|| **n < BigInt::from(-BIGINT_INLINE_THRESHOLD) =>
{
JsValue::unknown_empty(
false,
rcstr!("constant too long"),
)
}
ConstantValue::Regex(_) => {
// Regexes are literals, but they are also objects, so
// have identity and aren't inlined without opt in.
JsValue::unknown_empty( JsValue::unknown_empty(
false, false,
rcstr!("regex not inlined"), rcstr!("regex not inlined"),
) )
} }
v => JsValue::Constant(v.clone()),
} }
} else { } else {
JsValue::Constant(value.0.clone()) JsValue::Constant(value.0.clone())
...@@ -189,10 +203,53 @@ impl ConstantsModule { ...@@ -189,10 +203,53 @@ impl ConstantsModule {
} }
} }
pub async fn get_inline_export(
import_reference: ResolvedVc<EsmAssetReference>,
export: &str,
compile_time_info: Vc<CompileTimeInfo>,
) -> Result<Option<CompileTimeDefineValue>> {
let resolved = import_reference.resolve_reference().await?;
let Some(module) = resolved.first_module().await? else {
return Ok(None);
};
let constants = get_inline_constants(*module, compile_time_info).await?;
let Some(constants) = constants.as_ref() else {
return Ok(None);
};
let Some((_, ConstantsModuleExport::Constant(value))) =
constants.exports.iter().find(|(name, _)| name == export)
else {
return Ok(None);
};
if !matches!(
automatic_inline_status(&value.0),
AutomaticInlineStatus::Inline
) {
return Ok(None);
}
Ok(CompileTimeDefineValue::try_from(&value.0).ok())
}
#[turbo_tasks::function] #[turbo_tasks::function]
pub async fn get_constants( pub async fn get_constants(
module: ResolvedVc<Box<dyn Module>>, module: ResolvedVc<Box<dyn Module>>,
compile_time_info: Vc<CompileTimeInfo>, compile_time_info: Vc<CompileTimeInfo>,
) -> Result<Vc<OptionConstantsModule>> {
get_constants_internal(module, compile_time_info, false).await
}
#[turbo_tasks::function]
async fn get_inline_constants(
module: ResolvedVc<Box<dyn Module>>,
compile_time_info: Vc<CompileTimeInfo>,
) -> Result<Vc<OptionConstantsModule>> {
get_constants_internal(module, compile_time_info, true).await
}
async fn get_constants_internal(
module: ResolvedVc<Box<dyn Module>>,
compile_time_info: Vc<CompileTimeInfo>,
include_default_expression: bool,
) -> Result<Vc<OptionConstantsModule>> { ) -> Result<Vc<OptionConstantsModule>> {
let Some(parseable) = ResolvedVc::try_sidecast::<Box<dyn EcmascriptParsable>>(module) else { let Some(parseable) = ResolvedVc::try_sidecast::<Box<dyn EcmascriptParsable>>(module) else {
// should never actually happen, there should be a "imported module is not chunkable" error // should never actually happen, there should be a "imported module is not chunkable" error
...@@ -216,6 +273,20 @@ pub async fn get_constants( ...@@ -216,6 +273,20 @@ pub async fn get_constants(
let arena = ThreadLocal::new(); let arena = ThreadLocal::new();
let default_export_value =
if include_default_expression && let Program::Module(module) = program {
module.body.iter().find_map(|item| match item {
ModuleItem::ModuleDecl(ModuleDecl::ExportDefaultExpr(export)) => {
Some(GLOBALS.set(globals, || {
eval_context.eval(arena.get_or_default(), &export.expr)
}))
}
_ => None,
})
} else {
None
};
let var_graph = { let var_graph = {
let supports_block_scoping = *compile_time_info let supports_block_scoping = *compile_time_info
.environment() .environment()
...@@ -251,7 +322,13 @@ pub async fn get_constants( ...@@ -251,7 +322,13 @@ pub async fn get_constants(
.iter() .iter()
.map(async |(export_name, (binding, span))| { .map(async |(export_name, (binding, span))| {
let value = GLOBALS.set(globals, || { let value = GLOBALS.set(globals, || {
eval_context.eval_id(arena.get_or_default(), binding.clone()) if export_name.as_str() == "default"
&& let Some(value) = &default_export_value
{
value.clone_in(arena.get_or_default())
} else {
eval_context.eval_id(arena.get_or_default(), binding.clone())
}
}); });
let linked_value = link( let linked_value = link(
......
...@@ -129,7 +129,8 @@ use crate::{ ...@@ -129,7 +129,8 @@ use crate::{
CjsRequireResolveAssetReference, CjsRequireResolveAssetReference,
}, },
cross_module_constants::{ cross_module_constants::{
is_import_name_eligible_for_exports, module_value_to_constants_module, get_inline_export, is_import_name_eligible_for_exports,
module_value_to_constants_module,
}, },
dynamic_expression::DynamicExpression, dynamic_expression::DynamicExpression,
emit_collect::{CollectReference, EmitReference}, emit_collect::{CollectReference, EmitReference},
...@@ -513,6 +514,8 @@ struct AnalysisState<'a> { ...@@ -513,6 +514,8 @@ struct AnalysisState<'a> {
var_cache: Mutex<FxHashMap<Id, JsValue<'a>>>, var_cache: Mutex<FxHashMap<Id, JsValue<'a>>>,
/// A cache for the linked value of imported constants. /// A cache for the linked value of imported constants.
constants_cache: Mutex<FxHashMap<ModuleValue, Option<JsValue<'a>>>>, constants_cache: Mutex<FxHashMap<ModuleValue, Option<JsValue<'a>>>>,
/// A cache for constants used only during export inlining code generation.
inline_exports_cache: Mutex<FxHashMap<(usize, RcStr), Option<CompileTimeDefineValue>>>,
// There can be many references to import.meta, but only the first should hoist // There can be many references to import.meta, but only the first should hoist
// the object allocation. // the object allocation.
first_import_meta: bool, first_import_meta: bool,
...@@ -543,6 +546,24 @@ struct AnalysisState<'a> { ...@@ -543,6 +546,24 @@ struct AnalysisState<'a> {
} }
impl<'a> AnalysisState<'a> { impl<'a> AnalysisState<'a> {
/// Gets a short constant export for code-generation-only inlining.
async fn get_inline_export(
&self,
reference_idx: usize,
export: RcStr,
) -> Result<Option<CompileTimeDefineValue>> {
let key = (reference_idx, export.clone());
if let Some(value) = self.inline_exports_cache.lock().get(&key) {
return Ok(value.clone());
}
let Some(reference) = self.import_references.get(reference_idx) else {
bail!("couldn't find import reference at index {reference_idx}");
};
let value = get_inline_export(*reference, &export, *self.compile_time_info).await?;
self.inline_exports_cache.lock().insert(key, value.clone());
Ok(value)
}
/// Links a value to the graph, returning the linked value. /// Links a value to the graph, returning the linked value.
async fn link_value( async fn link_value(
&self, &self,
...@@ -886,6 +907,7 @@ async fn analyze_ecmascript_module_internal( ...@@ -886,6 +907,7 @@ async fn analyze_ecmascript_module_internal(
fun_args_values: Default::default(), fun_args_values: Default::default(),
var_cache: Default::default(), var_cache: Default::default(),
constants_cache: Default::default(), constants_cache: Default::default(),
inline_exports_cache: Default::default(),
first_import_meta: true, first_import_meta: true,
first_webpack_exports_info: true, first_webpack_exports_info: true,
module_fragments_enabled: options.module_fragments_enabled, module_fragments_enabled: options.module_fragments_enabled,
...@@ -1411,6 +1433,16 @@ async fn analyze_ecmascript_module_internal( ...@@ -1411,6 +1433,16 @@ async fn analyze_ecmascript_module_internal(
// a reference // a reference
analysis analysis
.add_code_gen(ConstantValueCodeGen::new(c, ast_path.to_vec().into())); .add_code_gen(ConstantValueCodeGen::new(c, ast_path.to_vec().into()));
} else if options.inline_constant_exports
&& let Some(export) = export.as_ref()
&& let Some(c) = analysis_state
.get_inline_export(esm_reference_index, export.clone())
.await?
{
analysis.add_code_gen(ConstantValueCodeGen::new_inline_export(
c,
ast_path.to_vec().into(),
));
} else if let Some("__turbopack_module_id__") = export.as_deref() { } else if let Some("__turbopack_module_id__") = export.as_deref() {
let chunking_type = r.await?.chunking_type(); let chunking_type = r.await?.chunking_type();
analysis.add_reference_code_gen( analysis.add_reference_code_gen(
......
...@@ -285,6 +285,8 @@ struct TestOptions { ...@@ -285,6 +285,8 @@ struct TestOptions {
#[serde(default = "default_true")] #[serde(default = "default_true")]
cross_module_constants: bool, cross_module_constants: bool,
#[serde(default)] #[serde(default)]
inline_constant_exports: bool,
#[serde(default)]
cjs_scope_hoisting: bool, cjs_scope_hoisting: bool,
#[serde(default)] #[serde(default)]
minify: bool, minify: bool,
...@@ -317,6 +319,7 @@ impl Default for TestOptions { ...@@ -317,6 +319,7 @@ impl Default for TestOptions {
mangle_export_names: default_true(), mangle_export_names: default_true(),
cjs_scope_hoisting: false, cjs_scope_hoisting: false,
cross_module_constants: true, cross_module_constants: true,
inline_constant_exports: false,
infer_module_side_effects: default_true(), infer_module_side_effects: default_true(),
minify: false, minify: false,
production_chunking: false, production_chunking: false,
...@@ -485,6 +488,7 @@ async fn run_test_operation(prepared_test: ResolvedVc<PreparedTest>) -> Result<V ...@@ -485,6 +488,7 @@ async fn run_test_operation(prepared_test: ResolvedVc<PreparedTest>) -> Result<V
mangle_export_names: options.mangle_export_names, mangle_export_names: options.mangle_export_names,
cjs_scope_hoisting: options.cjs_scope_hoisting, cjs_scope_hoisting: options.cjs_scope_hoisting,
cross_module_constants: options.cross_module_constants, cross_module_constants: options.cross_module_constants,
inline_constant_exports: options.inline_constant_exports,
infer_module_side_effects: options.infer_module_side_effects, infer_module_side_effects: options.infer_module_side_effects,
..Default::default() ..Default::default()
}, },
......
import { readA } from './b'
export const value = 'a'
export function completeCycle() {
return readA()
}
import { readA } from './b'
it('inlines across an import cycle without retaining the constants module', () => {
expect(readA()).toBe('a')
const source = readA.toString()
expect(source).toContain('"a"')
expect(source).not.toContain('TURBOPACK compile-time value')
expect(source).not.toContain('value')
const modules = Array.from(__turbopack_modules__.keys())
expect(modules).toContainEqual(expect.stringMatching(/input\/b\.js/))
expect(modules).not.toContainEqual(expect.stringMatching(/input\/a\.js/))
})
{
"scopeHoisting": false,
"mangleExportNames": false,
"crossModuleConstants": false,
"inlineConstantExports": true
}
import { value } from 'disabled-constants'
function readValue() {
return value
}
it('keeps runtime bindings when both constant features are disabled', () => {
expect(readValue()).toBe('dev')
expect(readValue.toString()).toContain('["value"]')
const modules = Array.from(__turbopack_modules__.keys())
expect(modules).toContainEqual(
expect.stringMatching(/node_modules\/disabled-constants\/index\.js/)
)
})
{
"scopeHoisting": false,
"mangleExportNames": false,
"crossModuleConstants": false,
"inlineConstantExports": false
}
import { ANALYZED, late } from 'inline-values'
import { forced } from 'inline-values' with { turbopackConstants: 'true' }
import { annotationOff } from 'inline-values' with { turbopackConstants: 'false' }
if (ANALYZED) require('./analysis-marker')
if (forced) require('./forced-marker')
if (late !== 'dev') require('./late-marker')
if (annotationOff !== 'off') require('./annotation-off-marker')
function readAnalyzed() {
return ANALYZED
}
function readLate() {
return late
}
function readAnnotationOff() {
return annotationOff
}
it('preserves analyzer-aware precedence and falls back to codegen-only inlining', () => {
expect(readAnalyzed()).toBe(false)
expect(readLate()).toBe('dev')
expect(readAnnotationOff()).toBe('off')
expect(readAnalyzed.toString()).toContain('TURBOPACK compile-time value')
expect(readLate.toString()).not.toContain('TURBOPACK compile-time value')
expect(readLate.toString()).not.toContain('late')
expect(readAnnotationOff.toString()).not.toContain('annotationOff')
const modules = Array.from(__turbopack_modules__.keys())
expect(modules).not.toContainEqual(
expect.stringMatching(/input\/analysis-marker\.js/)
)
expect(modules).not.toContainEqual(
expect.stringMatching(/input\/forced-marker\.js/)
)
expect(modules).toContainEqual(expect.stringMatching(/input\/late-marker\.js/))
expect(modules).toContainEqual(
expect.stringMatching(/input\/annotation-off-marker\.js/)
)
})
export const ANALYZED = false
export const forced = false
export const late = 'dev'
export const annotationOff = 'off'
{
"scopeHoisting": false,
"mangleExportNames": false,
"crossModuleConstants": true,
"inlineConstantExports": true
}
import { bValue } from './b'
globalThis.mutualAEvaluations = (globalThis.mutualAEvaluations ?? 0) + 1
export const aValue = 'a'
export function readB() {
return bValue
}
import { aValue } from './a'
globalThis.mutualBEvaluations = (globalThis.mutualBEvaluations ?? 0) + 1
export const bValue = 'b'
export function readA() {
return aValue
}
import { readB } from './a'
import { readA } from './b'
it('supports two retained modules importing constants from each other', () => {
expect(readB()).toBe('b')
expect(readA()).toBe('a')
expect(globalThis.mutualAEvaluations).toBe(1)
expect(globalThis.mutualBEvaluations).toBe(1)
expect(readB.toString()).toContain('"b"')
expect(readB.toString()).not.toContain('bValue')
expect(readA.toString()).toContain('"a"')
expect(readA.toString()).not.toContain('aValue')
const modules = Array.from(__turbopack_modules__.keys())
expect(modules).toContainEqual(expect.stringMatching(/input\/a\.js/))
expect(modules).toContainEqual(expect.stringMatching(/input\/b\.js/))
})
{
"scopeHoisting": false,
"mangleExportNames": false,
"crossModuleConstants": false,
"inlineConstantExports": true
}
import {
shortString,
smallNumber,
smallBigInt,
booleanFalse,
nil,
undef,
stableLet,
stableVar,
} from 'short-constants'
import { regex } from 'identity-values'
import {
longString,
nan,
positiveInfinity,
negativeInfinity,
} from 'long-values'
import shortDefault from 'short-default'
import longDefault from 'long-default'
import { mode } from 'analysis-constant'
import { effectValue } from 'effect-constant'
import { reassigned } from 'reassigned-binding'
import * as staticNamespace from 'namespace-static'
import * as escapingNamespace from 'namespace-escape'
const escapedNamespace = escapingNamespace
if (mode !== 'dev') {
require('./marker')
}
function readShortValues() {
return [
shortString,
smallNumber,
smallBigInt,
booleanFalse,
nil,
undef,
stableLet,
stableVar,
]
}
function readMode() {
return mode
}
function readStaticNamespace() {
return staticNamespace.short
}
function readShortDefault() {
return shortDefault
}
it('inlines short primitive exports during code generation', () => {
expect(readShortValues()).toEqual([
'dev',
1,
1n,
false,
null,
undefined,
'let',
'var',
])
expect(readMode()).toBe('dev')
expect(readStaticNamespace()).toBe('ns')
expect(readShortDefault()).toBe('def')
const source = readShortValues.toString()
expect(source).toContain('"dev"')
expect(source).toContain('1n')
expect(source).toContain('false')
expect(source).toContain('null')
expect(source).toContain('void 0')
expect(source).not.toContain('TURBOPACK compile-time value')
expect(source).not.toContain('shortString')
expect(source).not.toContain('smallNumber')
expect(readStaticNamespace.toString()).not.toContain('staticNamespace')
expect(readShortDefault.toString()).toContain('"def"')
expect(readShortDefault.toString()).not.toContain('shortDefault')
})
it('omits inlined export modules without using values for analysis', () => {
const modules = Array.from(__turbopack_modules__.keys())
expect(modules).not.toContainEqual(
expect.stringMatching(/node_modules\/short-constants\/index\.js/)
)
expect(modules).not.toContainEqual(
expect.stringMatching(/node_modules\/analysis-constant\/index\.js/)
)
expect(modules).not.toContainEqual(
expect.stringMatching(/node_modules\/namespace-static\/index\.js/)
)
expect(modules).not.toContainEqual(
expect.stringMatching(/node_modules\/short-default\/index\.js/)
)
expect(modules).toContainEqual(expect.stringMatching(/input\/marker\.js/))
})
it('preserves evaluation references for side-effectful modules', () => {
expect(effectValue).toBe('ok')
expect(globalThis.inlineExportSideEffect).toBe(1)
const modules = Array.from(__turbopack_modules__.keys())
expect(modules).toContainEqual(
expect.stringMatching(/node_modules\/effect-constant\/index\.js/)
)
})
it('does not inline reassigned bindings, identity values, long values, or escaping namespaces', () => {
expect(reassigned).toBe('b')
expect(escapedNamespace.short).toBe('ns')
expect(regex).toBe(regex)
expect(longString).toBe('this value is deliberately too long')
expect(nan).toBeNaN()
expect(positiveInfinity).toBe(Infinity)
expect(negativeInfinity).toBe(-Infinity)
expect(longDefault).toBe('this default value is too long')
const modules = Array.from(__turbopack_modules__.keys())
expect(modules).toContainEqual(
expect.stringMatching(/node_modules\/reassigned-binding\/index\.js/)
)
expect(modules).toContainEqual(
expect.stringMatching(/node_modules\/namespace-escape\/index\.js/)
)
expect(modules).toContainEqual(
expect.stringMatching(/node_modules\/identity-values\/index\.js/)
)
expect(modules).toContainEqual(
expect.stringMatching(/node_modules\/long-values\/index\.js/)
)
expect(modules).toContainEqual(
expect.stringMatching(/node_modules\/long-default\/index\.js/)
)
})
globalThis.inlineExportSideEffect = (globalThis.inlineExportSideEffect ?? 0) + 1
export const effectValue = 'ok'
export const longString = 'this value is deliberately too long'
export const nan = NaN
export const positiveInfinity = Infinity
export const negativeInfinity = -Infinity
export const shortString = 'dev'
export const smallNumber = 1
export const smallBigInt = 1n
export const booleanFalse = false
export const nil = null
export const undef = undefined
export let stableLet = 'let'
export var stableVar = 'var'
{
"scopeHoisting": false,
"mangleExportNames": false,
"crossModuleConstants": false,
"inlineConstantExports": true
}
...@@ -250,6 +250,7 @@ impl ModuleOptions { ...@@ -250,6 +250,7 @@ impl ModuleOptions {
mangle_export_names, mangle_export_names,
cjs_scope_hoisting, cjs_scope_hoisting,
cross_module_constants, cross_module_constants,
inline_constant_exports,
lazy_compilation, lazy_compilation,
ref preset_env_config, ref preset_env_config,
.. ..
...@@ -348,6 +349,7 @@ impl ModuleOptions { ...@@ -348,6 +349,7 @@ impl ModuleOptions {
mangle_export_names, mangle_export_names,
cjs_scope_hoisting, cjs_scope_hoisting,
cross_module_constants, cross_module_constants,
inline_constant_exports,
lazy_compilation, lazy_compilation,
..Default::default() ..Default::default()
}; };
......
...@@ -295,11 +295,16 @@ pub struct EcmascriptOptionsContext { ...@@ -295,11 +295,16 @@ pub struct EcmascriptOptionsContext {
/// Whether to scope-hoist static CommonJS modules. Defaults to false. /// Whether to scope-hoist static CommonJS modules. Defaults to false.
pub cjs_scope_hoisting: bool, pub cjs_scope_hoisting: bool,
/// Whether to enable cross-module constant inlining. Defaults to false. /// Whether to use cross-module constants during analysis and code generation. Defaults to
/// false.
pub cross_module_constants: bool, pub cross_module_constants: bool,
/// Whether dynamic import targets are compiled after their runtime proxy is activated. /// Whether dynamic import targets are compiled after their runtime proxy is activated.
pub lazy_compilation: bool, pub lazy_compilation: bool,
/// Whether to inline short, identity-free constant exports only during code generation.
/// Defaults to false.
pub inline_constant_exports: bool,
/// Additional SWC preset-env options (mode, coreJs, include, exclude, etc.). /// Additional SWC preset-env options (mode, coreJs, include, exclude, etc.).
pub preset_env_config: Option<ResolvedVc<PresetEnvConfig>>, pub preset_env_config: Option<ResolvedVc<PresetEnvConfig>>,
......
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