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

turbopack: preserve reexport intermediate side effects

Co-authored-by: 's avatarLuke Sandberg <210140+lukesandberg@users.noreply.github.com>
parent e6b43fb1
......@@ -5,9 +5,15 @@ use turbopack_core::resolve::ModulePart;
use crate::{
EcmascriptModuleAsset,
chunk::EcmascriptChunkPlaceable,
module_fragments::part::module::EcmascriptModulePartAsset,
references::{FollowExportsResult, follow_reexports},
chunk::{EcmascriptChunkPlaceable, placeable::SideEffectsDeclaration},
module_fragments::{
part::module::{
EcmascriptModulePartAsset, FollowExportsWithSideEffectsResult,
follow_reexports_with_side_effects, side_effects_declaration_for_reexport,
},
side_effects::module::SideEffectsModule,
},
references::{FollowExportsResult, esm::FoundExportType, follow_reexports},
rename::module::EcmascriptModuleRenameModule,
side_effect_optimization::{
facade::module::EcmascriptModuleFacadeModule, locals::module::EcmascriptModuleLocalsModule,
......@@ -34,6 +40,8 @@ pub async fn canonicalize_ecmascript_module(
}
EcmascriptModuleCanonicalization::FollowReexports(part) => {
if *module.get_exports().split_locals_and_reexports().await? {
let facade = EcmascriptModuleFacadeModule::new(Vc::upcast(*module));
if let Some(part) = part {
match part {
ModulePart::Evaluation => {
......@@ -41,11 +49,8 @@ pub async fn canonicalize_ecmascript_module(
}
ModulePart::Export(_) => {
apply_reexport_tree_shaking(
Vc::upcast(
*EcmascriptModuleFacadeModule::new(Vc::upcast(*module))
.to_resolved()
.await?,
),
module,
Vc::upcast(*facade.to_resolved().await?),
part,
)
.await?
......@@ -56,7 +61,7 @@ pub async fn canonicalize_ecmascript_module(
),
}
} else {
Vc::upcast(EcmascriptModuleFacadeModule::new(Vc::upcast(*module)))
Vc::upcast(facade)
}
} else {
Vc::upcast(*module)
......@@ -66,30 +71,76 @@ pub async fn canonicalize_ecmascript_module(
}
async fn apply_reexport_tree_shaking(
source_module: ResolvedVc<EcmascriptModuleAsset>,
module: Vc<Box<dyn EcmascriptChunkPlaceable>>,
part: ModulePart,
) -> Result<Vc<Box<dyn EcmascriptChunkPlaceable>>> {
if let ModulePart::Export(export) = &part {
let FollowExportsResult {
module: final_module,
export_name: new_export,
..
} = &*follow_reexports(module, export.clone(), true).await?;
return Ok(if let Some(new_export) = new_export {
if *new_export == *export {
**final_module
} else {
Vc::upcast(EcmascriptModuleRenameModule::new(
**final_module,
ModulePart::renamed_export(new_export.clone(), export.clone()),
))
}
let ModulePart::Export(export) = &part else {
return Ok(module);
};
let declaration =
side_effects_declaration_for_reexport(ResolvedVc::upcast(source_module)).await?;
let (mut side_effects, result) = if matches!(declaration, SideEffectsDeclaration::None) {
(
Vec::new(),
follow_reexports(module, export.clone(), true, false)
.to_resolved()
.await?,
)
} else {
let FollowExportsWithSideEffectsResult {
side_effects,
result,
} = &*follow_reexports_with_side_effects(
module,
export.clone(),
true,
matches!(declaration, SideEffectsDeclaration::SideEffectful),
)
.await?;
(side_effects.clone(), *result)
};
let FollowExportsResult {
module: final_module,
export_name: new_export,
ty,
} = &*result.await?;
if matches!(
ty,
FoundExportType::Dynamic | FoundExportType::Unknown | FoundExportType::NotFound
) {
// Dynamic and unknown targets need the original reexport module's runtime machinery. The
// statically composed side-effect wrapper only applies when a concrete binding was found.
side_effects.clear();
}
let final_module = if let Some(new_export) = new_export {
if *new_export == *export {
**final_module
} else {
Vc::upcast(EcmascriptModuleRenameModule::new(
**final_module,
ModulePart::renamed_namespace(export.clone()),
ModulePart::renamed_export(new_export.clone(), export.clone()),
))
});
}
} else {
Vc::upcast(EcmascriptModuleRenameModule::new(
**final_module,
ModulePart::renamed_namespace(export.clone()),
))
};
if side_effects.is_empty() {
return Ok(final_module);
}
Ok(module)
Ok(Vc::upcast(SideEffectsModule::new(
*source_module,
part,
final_module,
side_effects
.iter()
.map(|side_effect| **side_effect)
.collect(),
)))
}
......@@ -37,7 +37,7 @@ use crate::{
/// [`EcmascriptModuleLocalsModule`]: crate::side_effect_optimization::locals::module::EcmascriptModuleLocalsModule
#[turbo_tasks::value]
pub struct EcmascriptModuleFacadeModule {
module: ResolvedVc<Box<dyn EcmascriptChunkPlaceable>>,
pub(crate) module: ResolvedVc<Box<dyn EcmascriptChunkPlaceable>>,
}
#[turbo_tasks::value_impl]
......
export * from './left.js'
export * from './right.js'
export * from './commonjs.js'
import { effect } from './check-side-effect.js'
effect('effect.js')
......@@ -23,18 +23,32 @@ import { a as a9 } from 'package-intermediate-side-effect'
import { effects as effects9 } from 'package-intermediate-side-effect/check-side-effect'
it('should run side effects of an intermediate module with side effects (star reexports)', () => {
expect(a9).toBe('a')
expect(effects9).toEqual([
// TODO This should execute the side effect of the intermediate module
// 'side-effect.js'
])
expect(effects9).toEqual(['side-effect.js'])
})
import { a as a10 } from 'package-intermediate-named-side-effect'
import { effects as effects10 } from 'package-intermediate-named-side-effect/check-side-effect'
it('should run side effects of an intermediate module with side effects (named reexports)', () => {
expect(a10).toBe('a')
expect(effects10).toEqual([
// TODO This should execute the side effect of the intermediate module
// 'side-effect.js'
])
expect(effects10).toEqual(['side-effect.js'])
})
import { a as a11 } from 'package-intermediate-mixed-star-side-effect'
import { effects as effects11 } from 'package-intermediate-mixed-star-side-effect/check-side-effect'
it('should preserve evaluation order across a mixed star reexport route', () => {
expect(a11).toBe('a')
expect(effects11).toEqual(['effect-2.js', 'effect-1.js'])
})
import {
selected as selected12,
dynamicValue as dynamicValue12,
} from 'package-intermediate-ambiguous-dynamic-side-effect'
import * as namespace12 from 'package-intermediate-ambiguous-dynamic-side-effect'
import { effects as effects12 } from 'package-intermediate-ambiguous-dynamic-side-effect/check-side-effect'
it('should preserve star ambiguity and dynamic fallback through an effectful route', () => {
expect(selected12).toBe('selected')
expect(dynamicValue12).toBe('dynamic')
expect(namespace12.ambiguous).toBe('right')
expect(effects12).toEqual(['effect.js'])
})
warning - [analysis] /turbopack/crates/turbopack-tests/tests/execution/turbopack/side-effects-optimization/node_modules/package-intermediate-ambiguous-dynamic-side-effect/commonjs.js unexpected export *
export * used with module [project]/turbopack/crates/turbopack-tests/tests/execution/turbopack/side-effects-optimization/node_modules/package-intermediate-ambiguous-dynamic-side-effect/commonjs.js [test] (ecmascript) which is a CommonJS module with exports only available at runtime
List all export names manually (`export { a, b, c } from "...") or rewrite the module to ESM, to avoid the additional runtime code.`
Import trace:
test:
./turbopack/crates/turbopack-tests/tests/execution/turbopack/side-effects-optimization/node_modules/package-intermediate-ambiguous-dynamic-side-effect/commonjs.js
./turbopack/crates/turbopack-tests/tests/execution/turbopack/side-effects-optimization/reexport-side-effects/input/index.js
\ No newline at end of file
warning - [analysis] /turbopack/crates/turbopack-tests/tests/execution/turbopack/side-effects-optimization/node_modules/package-intermediate-ambiguous-dynamic-side-effect/commonjs.js unexpected export *
export * used with module [project]/turbopack/crates/turbopack-tests/tests/execution/turbopack/side-effects-optimization/node_modules/package-intermediate-ambiguous-dynamic-side-effect/commonjs.js [test] (ecmascript) which is a CommonJS module with exports only available at runtime
List all export names manually (`export { a, b, c } from "...") or rewrite the module to ESM, to avoid the additional runtime code.`
Import trace:
test:
./turbopack/crates/turbopack-tests/tests/execution/turbopack/side-effects-optimization/node_modules/package-intermediate-ambiguous-dynamic-side-effect/commonjs.js
./turbopack/crates/turbopack-tests/tests/execution/turbopack/side-effects-optimization/reexport-side-effects/input/index.js
./turbopack/crates/turbopack-tests/tests/execution/turbopack/tree-shaking/reexport-side-effects/input/index.js
\ No newline at end of file
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