Commit 038dabd3 authored by Tobias Koppers's avatar Tobias Koppers

Turbopack: use exports.evaluation to check for side effects, remove…

Turbopack: use exports.evaluation to check for side effects, remove is_marked_as_side_effect_free trait method
parent 0944df74
...@@ -3,7 +3,7 @@ use std::io::Write; ...@@ -3,7 +3,7 @@ use std::io::Write;
use anyhow::Result; use anyhow::Result;
use turbo_rcstr::{RcStr, rcstr}; use turbo_rcstr::{RcStr, rcstr};
use turbo_tasks::{ResolvedVc, ValueToString, Vc}; use turbo_tasks::{ResolvedVc, ValueToString, Vc};
use turbo_tasks_fs::{FileSystem, VirtualFileSystem, glob::Glob, rope::RopeBuilder}; use turbo_tasks_fs::{FileSystem, VirtualFileSystem, rope::RopeBuilder};
use turbopack_core::{ use turbopack_core::{
asset::{Asset, AssetContent}, asset::{Asset, AssetContent},
chunk::{ chunk::{
...@@ -109,11 +109,6 @@ impl EcmascriptChunkPlaceable for HmrEntryModule { ...@@ -109,11 +109,6 @@ impl EcmascriptChunkPlaceable for HmrEntryModule {
} }
.cell() .cell()
} }
#[turbo_tasks::function]
fn is_marked_as_side_effect_free(self: Vc<Self>, _: Vc<Glob>) -> Vc<bool> {
Vc::cell(false)
}
} }
#[turbo_tasks::value_impl] #[turbo_tasks::value_impl]
......
use anyhow::{Result, bail}; use anyhow::{Result, bail};
use turbo_rcstr::RcStr; use turbo_rcstr::RcStr;
use turbo_tasks::{ResolvedVc, Vc}; use turbo_tasks::{ResolvedVc, Vc};
use turbo_tasks_fs::{FileSystemPath, glob::Glob}; use turbo_tasks_fs::FileSystemPath;
use crate::{ use crate::{
compile_time_info::CompileTimeInfo, compile_time_info::CompileTimeInfo,
...@@ -104,7 +104,4 @@ pub trait AssetContext { ...@@ -104,7 +104,4 @@ pub trait AssetContext {
/// Gets a new AssetContext with the transition applied. /// Gets a new AssetContext with the transition applied.
#[turbo_tasks::function] #[turbo_tasks::function]
fn with_transition(self: Vc<Self>, transition: RcStr) -> Vc<Box<dyn AssetContext>>; fn with_transition(self: Vc<Self>, transition: RcStr) -> Vc<Box<dyn AssetContext>>;
#[turbo_tasks::function]
fn side_effect_free_packages(self: Vc<Self>) -> Vc<Glob>;
} }
...@@ -36,7 +36,10 @@ pub use self::{ ...@@ -36,7 +36,10 @@ pub use self::{
EcmascriptChunkItem, EcmascriptChunkItemContent, EcmascriptChunkItemExt, EcmascriptChunkItem, EcmascriptChunkItemContent, EcmascriptChunkItemExt,
EcmascriptChunkItemOptions, EcmascriptChunkItemWithAsyncInfo, EcmascriptChunkItemOptions, EcmascriptChunkItemWithAsyncInfo,
}, },
placeable::{EcmascriptChunkPlaceable, EcmascriptExports, EcmascriptExportsType}, placeable::{
EcmascriptChunkPlaceable, EcmascriptChunkPlaceableExt, EcmascriptExports,
EcmascriptExportsType,
},
}; };
#[turbo_tasks::value] #[turbo_tasks::value]
......
use anyhow::Result; use anyhow::Result;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use turbo_rcstr::rcstr; use turbo_rcstr::rcstr;
use turbo_tasks::{NonLocalValue, ResolvedVc, TryFlatJoinIterExt, Vc, trace::TraceRawVcs}; use turbo_tasks::{NonLocalValue, ResolvedVc, TryFlatJoinIterExt, Upcast, Vc, trace::TraceRawVcs};
use turbo_tasks_fs::{ use turbo_tasks_fs::{
FileJsonContent, FileSystemPath, FileJsonContent, FileSystemPath,
glob::{Glob, GlobOptions}, glob::{Glob, GlobOptions},
...@@ -36,15 +36,22 @@ pub trait EcmascriptChunkPlaceable: ChunkableModule + Module + Asset { ...@@ -36,15 +36,22 @@ pub trait EcmascriptChunkPlaceable: ChunkableModule + Module + Asset {
async fn get_evaluation(self: Vc<Self>) -> Result<Vc<EcmascriptEvaluation>> { async fn get_evaluation(self: Vc<Self>) -> Result<Vc<EcmascriptEvaluation>> {
Ok(self.get_exports().await?.evaluation.cell()) Ok(self.get_exports().await?.evaluation.cell())
} }
#[turbo_tasks::function] }
async fn is_marked_as_side_effect_free(
self: Vc<Self>, pub trait EcmascriptChunkPlaceableExt {
side_effect_free_packages: Vc<Glob>, fn is_marked_as_side_effect_free(self: Vc<Self>) -> impl Future<Output = Result<bool>> + Send;
) -> Result<Vc<bool>> { }
Ok(is_marked_as_side_effect_free(
self.ident().path().owned().await?, impl<T: EcmascriptChunkPlaceable + Send + Upcast<Box<dyn EcmascriptChunkPlaceable>>>
Some(side_effect_free_packages), EcmascriptChunkPlaceableExt for T
)) {
async fn is_marked_as_side_effect_free(self: Vc<Self>) -> Result<bool> {
Ok(match *self.get_evaluation().await? {
EcmascriptEvaluation::SideEffectFree => true,
EcmascriptEvaluation::SideEffects => false,
EcmascriptEvaluation::LocalSideEffectFree => false,
EcmascriptEvaluation::DelegatedSideEffects(_) => false,
})
} }
} }
......
...@@ -3,7 +3,7 @@ use std::io::{Read, Write}; ...@@ -3,7 +3,7 @@ use std::io::{Read, Write};
use anyhow::{Result, bail}; use anyhow::{Result, bail};
use turbo_rcstr::rcstr; use turbo_rcstr::rcstr;
use turbo_tasks::{ResolvedVc, ValueToString, Vc}; use turbo_tasks::{ResolvedVc, ValueToString, Vc};
use turbo_tasks_fs::{FileContent, glob::Glob, rope::RopeBuilder}; use turbo_tasks_fs::{FileContent, rope::RopeBuilder};
use turbopack_core::{ use turbopack_core::{
asset::{Asset, AssetContent}, asset::{Asset, AssetContent},
chunk::{ChunkItem, ChunkType, ChunkableModule, ChunkingContext}, chunk::{ChunkItem, ChunkType, ChunkableModule, ChunkingContext},
...@@ -75,15 +75,10 @@ impl EcmascriptChunkPlaceable for InlinedBytesJsModule { ...@@ -75,15 +75,10 @@ impl EcmascriptChunkPlaceable for InlinedBytesJsModule {
fn get_exports(&self) -> Vc<EcmascriptExports> { fn get_exports(&self) -> Vc<EcmascriptExports> {
EcmascriptExports { EcmascriptExports {
ty: EcmascriptExportsType::Value, ty: EcmascriptExportsType::Value,
evaluation: EcmascriptEvaluation::SideEffects, evaluation: EcmascriptEvaluation::SideEffectFree,
} }
.cell() .cell()
} }
#[turbo_tasks::function]
fn is_marked_as_side_effect_free(&self, _side_effect_free_packages: Vc<Glob>) -> Vc<bool> {
Vc::cell(true)
}
} }
#[turbo_tasks::value] #[turbo_tasks::value]
......
...@@ -750,20 +750,6 @@ impl EcmascriptChunkPlaceable for EcmascriptModuleAsset { ...@@ -750,20 +750,6 @@ impl EcmascriptChunkPlaceable for EcmascriptModuleAsset {
async fn get_async_module(self: Vc<Self>) -> Result<Vc<OptionAsyncModule>> { async fn get_async_module(self: Vc<Self>) -> Result<Vc<OptionAsyncModule>> {
Ok(*self.analyze().await?.async_module) Ok(*self.analyze().await?.async_module)
} }
#[turbo_tasks::function]
async fn is_marked_as_side_effect_free(
self: Vc<Self>,
_side_effect_free_packages: Vc<Glob>,
) -> Result<Vc<bool>> {
// Check package.json first, so that we can skip parsing the module if it's marked that way.
let pkg_side_effect_free = self.check_package_json_side_effect_free();
Ok(if *pkg_side_effect_free.await? {
pkg_side_effect_free
} else {
Vc::cell(self.analyze().await?.has_side_effect_free_directive)
})
}
} }
#[turbo_tasks::value_impl] #[turbo_tasks::value_impl]
......
...@@ -17,7 +17,6 @@ use turbopack_core::{ ...@@ -17,7 +17,6 @@ use turbopack_core::{
ChunkableModuleReference, ChunkingContext, ChunkingType, ChunkingTypeOption, ChunkableModuleReference, ChunkingContext, ChunkingType, ChunkingTypeOption,
ModuleChunkItemIdExt, ModuleChunkItemIdExt,
}, },
context::AssetContext,
issue::{ issue::{
Issue, IssueExt, IssueSeverity, IssueSource, IssueStage, OptionIssueSource, Issue, IssueExt, IssueSeverity, IssueSource, IssueStage, OptionIssueSource,
OptionStyledString, StyledString, OptionStyledString, StyledString,
...@@ -39,7 +38,9 @@ use super::export::{all_known_export_names, is_export_missing}; ...@@ -39,7 +38,9 @@ use super::export::{all_known_export_names, is_export_missing};
use crate::{ use crate::{
ScopeHoistingContext, TreeShakingMode, ScopeHoistingContext, TreeShakingMode,
analyzer::imports::ImportAnnotations, analyzer::imports::ImportAnnotations,
chunk::{EcmascriptChunkPlaceable, EcmascriptExportsType}, chunk::{
EcmascriptChunkPlaceable, EcmascriptExportsType, placeable::EcmascriptChunkPlaceableExt,
},
code_gen::{CodeGeneration, CodeGenerationHoistedStmt}, code_gen::{CodeGeneration, CodeGenerationHoistedStmt},
export::Liveness, export::Liveness,
magic_identifier, magic_identifier,
...@@ -408,12 +409,7 @@ impl ModuleReference for EsmAssetReference { ...@@ -408,12 +409,7 @@ impl ModuleReference for EsmAssetReference {
let tree_shaking_mode = module.options().await?.tree_shaking_mode; let tree_shaking_mode = module.options().await?.tree_shaking_mode;
if let Some(TreeShakingMode::ModuleFragments) = tree_shaking_mode { if let Some(TreeShakingMode::ModuleFragments) = tree_shaking_mode {
let side_effect_free_packages = module.asset_context().side_effect_free_packages(); if module.is_marked_as_side_effect_free().await? {
if *module
.is_marked_as_side_effect_free(side_effect_free_packages)
.await?
{
return Ok(ModuleResolveResult { return Ok(ModuleResolveResult {
primary: Box::new([( primary: Box::new([(
RequestKey::default(), RequestKey::default(),
......
...@@ -16,7 +16,6 @@ use turbo_tasks::{ ...@@ -16,7 +16,6 @@ use turbo_tasks::{
FxIndexMap, NonLocalValue, ResolvedVc, TryFlatJoinIterExt, ValueToString, Vc, FxIndexMap, NonLocalValue, ResolvedVc, TryFlatJoinIterExt, ValueToString, Vc,
trace::TraceRawVcs, trace::TraceRawVcs,
}; };
use turbo_tasks_fs::glob::Glob;
use turbopack_core::{ use turbopack_core::{
chunk::{ChunkingContext, ModuleChunkItemIdExt}, chunk::{ChunkingContext, ModuleChunkItemIdExt},
ident::AssetIdent, ident::AssetIdent,
...@@ -155,7 +154,6 @@ pub struct FollowExportsResult { ...@@ -155,7 +154,6 @@ pub struct FollowExportsResult {
pub async fn follow_reexports( pub async fn follow_reexports(
module: ResolvedVc<Box<dyn EcmascriptChunkPlaceable>>, module: ResolvedVc<Box<dyn EcmascriptChunkPlaceable>>,
export_name: RcStr, export_name: RcStr,
side_effect_free_packages: Vc<Glob>,
ignore_side_effect_of_entry: bool, ignore_side_effect_of_entry: bool,
) -> Result<Vc<FollowExportsResult>> { ) -> Result<Vc<FollowExportsResult>> {
let mut ignore_side_effects = ignore_side_effect_of_entry; let mut ignore_side_effects = ignore_side_effect_of_entry;
...@@ -163,20 +161,15 @@ pub async fn follow_reexports( ...@@ -163,20 +161,15 @@ pub async fn follow_reexports(
let mut module = module; let mut module = module;
let mut export_name = export_name; let mut export_name = export_name;
loop { loop {
let exports = module.get_exports().await?; if !ignore_side_effects {
let EcmascriptExportsType::EsmExports(exports) = &exports.ty else { let evaluation = module.get_evaluation().await?;
return Ok(FollowExportsResult::cell(FollowExportsResult { let has_side_effects = match *evaluation {
module, EcmascriptEvaluation::SideEffects => true,
export_name: Some(export_name), EcmascriptEvaluation::LocalSideEffectFree => false,
ty: FoundExportType::Dynamic, EcmascriptEvaluation::SideEffectFree => false,
})); EcmascriptEvaluation::DelegatedSideEffects(_) => false,
}; };
if has_side_effects {
if !ignore_side_effects
&& !*module
.is_marked_as_side_effect_free(side_effect_free_packages)
.await?
{
// TODO It's unfortunate that we have to use the whole module here. // TODO It's unfortunate that we have to use the whole module here.
// This is often the Facade module, which includes all reexports. // This is often the Facade module, which includes all reexports.
// Often we could use Locals + the followed reexports instead. // Often we could use Locals + the followed reexports instead.
...@@ -186,10 +179,20 @@ pub async fn follow_reexports( ...@@ -186,10 +179,20 @@ pub async fn follow_reexports(
ty: FoundExportType::SideEffects, ty: FoundExportType::SideEffects,
})); }));
} }
}
ignore_side_effects = false; ignore_side_effects = false;
// Try to find the export in the local exports let exports = module.get_exports().await?;
let EcmascriptExportsType::EsmExports(exports) = &exports.ty else {
return Ok(FollowExportsResult::cell(FollowExportsResult {
module,
export_name: Some(export_name),
ty: FoundExportType::Dynamic,
}));
};
let exports_ref = exports.await?; let exports_ref = exports.await?;
// Try to find the export in the local exports
if let Some(export) = exports_ref.exports.get(&export_name) { if let Some(export) = exports_ref.exports.get(&export_name) {
match handle_declared_export(module, export_name, export).await? { match handle_declared_export(module, export_name, export).await? {
ControlFlow::Continue((m, n)) => { ControlFlow::Continue((m, n)) => {
......
...@@ -5,7 +5,7 @@ use serde::{Deserialize, Serialize}; ...@@ -5,7 +5,7 @@ use serde::{Deserialize, Serialize};
use turbo_rcstr::{RcStr, rcstr}; use turbo_rcstr::{RcStr, rcstr};
use turbo_tasks::{NonLocalValue, ResolvedVc, TaskInput, TryJoinIterExt, Vc, trace::TraceRawVcs}; use turbo_tasks::{NonLocalValue, ResolvedVc, TaskInput, TryJoinIterExt, Vc, trace::TraceRawVcs};
use turbo_tasks_fs::{ use turbo_tasks_fs::{
FileContent, FileSystem, FileSystemPath, VirtualFileSystem, glob::Glob, rope::RopeBuilder, FileContent, FileSystem, FileSystemPath, VirtualFileSystem, rope::RopeBuilder,
}; };
use turbopack_core::{ use turbopack_core::{
asset::{Asset, AssetContent}, asset::{Asset, AssetContent},
...@@ -355,14 +355,6 @@ impl EcmascriptChunkPlaceable for CachedExternalModule { ...@@ -355,14 +355,6 @@ impl EcmascriptChunkPlaceable for CachedExternalModule {
}, },
) )
} }
#[turbo_tasks::function]
fn is_marked_as_side_effect_free(
self: Vc<Self>,
_side_effect_free_packages: Vc<Glob>,
) -> Vc<bool> {
Vc::cell(false)
}
} }
#[turbo_tasks::value] #[turbo_tasks::value]
......
...@@ -176,7 +176,6 @@ pub struct AnalyzeEcmascriptModuleResult { ...@@ -176,7 +176,6 @@ pub struct AnalyzeEcmascriptModuleResult {
pub code_generation: ResolvedVc<CodeGens>, pub code_generation: ResolvedVc<CodeGens>,
pub exports: ResolvedVc<EcmascriptExports>, pub exports: ResolvedVc<EcmascriptExports>,
pub async_module: ResolvedVc<OptionAsyncModule>, pub async_module: ResolvedVc<OptionAsyncModule>,
pub has_side_effect_free_directive: bool,
/// `true` when the analysis was successful. /// `true` when the analysis was successful.
pub successful: bool, pub successful: bool,
pub source_map: Option<ResolvedVc<Box<dyn GenerateSourceMap>>>, pub source_map: Option<ResolvedVc<Box<dyn GenerateSourceMap>>>,
...@@ -231,7 +230,6 @@ pub struct AnalyzeEcmascriptModuleResultBuilder { ...@@ -231,7 +230,6 @@ pub struct AnalyzeEcmascriptModuleResultBuilder {
async_module: ResolvedVc<OptionAsyncModule>, async_module: ResolvedVc<OptionAsyncModule>,
successful: bool, successful: bool,
source_map: Option<ResolvedVc<Box<dyn GenerateSourceMap>>>, source_map: Option<ResolvedVc<Box<dyn GenerateSourceMap>>>,
has_side_effect_free_directive: bool,
} }
impl AnalyzeEcmascriptModuleResultBuilder { impl AnalyzeEcmascriptModuleResultBuilder {
...@@ -250,7 +248,6 @@ impl AnalyzeEcmascriptModuleResultBuilder { ...@@ -250,7 +248,6 @@ impl AnalyzeEcmascriptModuleResultBuilder {
async_module: ResolvedVc::cell(None), async_module: ResolvedVc::cell(None),
successful: false, successful: false,
source_map: None, source_map: None,
has_side_effect_free_directive: false,
} }
} }
...@@ -317,11 +314,6 @@ impl AnalyzeEcmascriptModuleResultBuilder { ...@@ -317,11 +314,6 @@ impl AnalyzeEcmascriptModuleResultBuilder {
self.async_module = ResolvedVc::cell(Some(async_module)); self.async_module = ResolvedVc::cell(Some(async_module));
} }
/// Set whether this module is side-efffect free according to a user-provided directive.
pub fn set_has_side_effect_free_directive(&mut self, value: bool) {
self.has_side_effect_free_directive = value;
}
/// Sets whether the analysis was successful. /// Sets whether the analysis was successful.
pub fn set_successful(&mut self, successful: bool) { pub fn set_successful(&mut self, successful: bool) {
self.successful = successful; self.successful = successful;
...@@ -424,7 +416,6 @@ impl AnalyzeEcmascriptModuleResultBuilder { ...@@ -424,7 +416,6 @@ impl AnalyzeEcmascriptModuleResultBuilder {
} }
.resolved_cell(), .resolved_cell(),
async_module: self.async_module, async_module: self.async_module,
has_side_effect_free_directive: self.has_side_effect_free_directive,
successful: self.successful, successful: self.successful,
source_map: self.source_map, source_map: self.source_map,
}, },
...@@ -633,7 +624,6 @@ pub async fn analyse_ecmascript_module_internal( ...@@ -633,7 +624,6 @@ pub async fn analyse_ecmascript_module_internal(
}, },
_ => false, _ => false,
}); });
analysis.set_has_side_effect_free_directive(has_side_effect_free_directive);
let is_esm = eval_context.is_esm(specified_type); let is_esm = eval_context.is_esm(specified_type);
let compile_time_info = compile_time_info_for_module_options( let compile_time_info = compile_time_info_for_module_options(
......
...@@ -2,7 +2,7 @@ use std::collections::BTreeMap; ...@@ -2,7 +2,7 @@ use std::collections::BTreeMap;
use anyhow::{Result, bail}; use anyhow::{Result, bail};
use turbo_tasks::{ResolvedVc, Vc}; use turbo_tasks::{ResolvedVc, Vc};
use turbo_tasks_fs::{File, FileContent, glob::Glob}; use turbo_tasks_fs::{File, FileContent};
use turbopack_core::{ use turbopack_core::{
asset::{Asset, AssetContent}, asset::{Asset, AssetContent},
chunk::{ chunk::{
...@@ -352,22 +352,6 @@ impl EcmascriptChunkPlaceable for EcmascriptModuleFacadeModule { ...@@ -352,22 +352,6 @@ impl EcmascriptChunkPlaceable for EcmascriptModuleFacadeModule {
.cell()) .cell())
} }
#[turbo_tasks::function]
fn is_marked_as_side_effect_free(
&self,
side_effect_free_packages: Vc<Glob>,
) -> Result<Vc<bool>> {
Ok(match self.part {
ModulePart::Facade => self
.module
.is_marked_as_side_effect_free(side_effect_free_packages),
ModulePart::RenamedExport { .. } | ModulePart::RenamedNamespace { .. } => {
Vc::cell(true)
}
_ => bail!("Unexpected ModulePart for EcmascriptModuleFacadeModule"),
})
}
#[turbo_tasks::function] #[turbo_tasks::function]
async fn get_async_module(self: Vc<Self>) -> Result<Vc<OptionAsyncModule>> { async fn get_async_module(self: Vc<Self>) -> Result<Vc<OptionAsyncModule>> {
Ok(Vc::cell(Some(self.async_module().to_resolved().await?))) Ok(Vc::cell(Some(self.async_module().to_resolved().await?)))
......
...@@ -2,7 +2,6 @@ use std::collections::BTreeMap; ...@@ -2,7 +2,6 @@ use std::collections::BTreeMap;
use anyhow::{Result, bail}; use anyhow::{Result, bail};
use turbo_tasks::{ResolvedVc, Vc}; use turbo_tasks::{ResolvedVc, Vc};
use turbo_tasks_fs::glob::Glob;
use turbopack_core::{ use turbopack_core::{
asset::{Asset, AssetContent}, asset::{Asset, AssetContent},
chunk::{ chunk::{
...@@ -169,12 +168,6 @@ impl EcmascriptChunkPlaceable for EcmascriptModuleLocalsModule { ...@@ -169,12 +168,6 @@ impl EcmascriptChunkPlaceable for EcmascriptModuleLocalsModule {
.cell()) .cell())
} }
#[turbo_tasks::function]
fn is_marked_as_side_effect_free(&self, side_effect_free_packages: Vc<Glob>) -> Vc<bool> {
self.module
.is_marked_as_side_effect_free(side_effect_free_packages)
}
#[turbo_tasks::function] #[turbo_tasks::function]
fn get_async_module(&self) -> Vc<OptionAsyncModule> { fn get_async_module(&self) -> Vc<OptionAsyncModule> {
self.module.get_async_module() self.module.get_async_module()
......
use anyhow::Result; use anyhow::Result;
use turbo_rcstr::{RcStr, rcstr}; use turbo_rcstr::{RcStr, rcstr};
use turbo_tasks::{ResolvedVc, Vc}; use turbo_tasks::{ResolvedVc, Vc};
use turbo_tasks_fs::glob::Glob;
use turbopack_core::{ use turbopack_core::{
asset::{Asset, AssetContent}, asset::{Asset, AssetContent},
chunk::{AsyncModuleInfo, ChunkableModule, ChunkingContext, EvaluatableAsset}, chunk::{AsyncModuleInfo, ChunkableModule, ChunkingContext, EvaluatableAsset},
context::AssetContext,
ident::AssetIdent, ident::AssetIdent,
module::Module, module::Module,
module_graph::ModuleGraph, module_graph::ModuleGraph,
reference::{ModuleReference, ModuleReferences, SingleChunkableModuleReference}, reference::{ModuleReference, ModuleReferences, SingleChunkableModuleReference},
resolve::{ExportUsage, ModulePart, origin::ResolveOrigin}, resolve::{ExportUsage, ModulePart},
}; };
use super::{ use super::{
...@@ -21,7 +19,7 @@ use crate::{ ...@@ -21,7 +19,7 @@ use crate::{
AnalyzeEcmascriptModuleResult, EcmascriptAnalyzable, EcmascriptModuleAsset, AnalyzeEcmascriptModuleResult, EcmascriptAnalyzable, EcmascriptModuleAsset,
EcmascriptModuleAssetType, EcmascriptModuleContent, EcmascriptModuleContentOptions, EcmascriptModuleAssetType, EcmascriptModuleContent, EcmascriptModuleContentOptions,
EcmascriptParsable, EcmascriptParsable,
chunk::{EcmascriptChunkPlaceable, EcmascriptExports}, chunk::{EcmascriptChunkPlaceable, EcmascriptExports, placeable::EcmascriptChunkPlaceableExt},
parse::ParseResult, parse::ParseResult,
references::{ references::{
FollowExportsResult, analyse_ecmascript_module, esm::FoundExportType, follow_reexports, FollowExportsResult, analyse_ecmascript_module, esm::FoundExportType, follow_reexports,
...@@ -175,17 +173,11 @@ impl EcmascriptModulePartAsset { ...@@ -175,17 +173,11 @@ impl EcmascriptModulePartAsset {
), ),
)); ));
} }
let side_effect_free_packages = module.asset_context().side_effect_free_packages();
let source_module = Vc::upcast(module); let source_module = Vc::upcast(module);
let FollowExportsWithSideEffectsResult { let FollowExportsWithSideEffectsResult {
side_effects, side_effects,
result, result,
} = &*follow_reexports_with_side_effects( } = &*follow_reexports_with_side_effects(source_module, export.clone()).await?;
source_module,
export.clone(),
side_effect_free_packages,
)
.await?;
let FollowExportsResult { let FollowExportsResult {
module: final_module, module: final_module,
export_name: new_export, export_name: new_export,
...@@ -256,28 +248,20 @@ struct FollowExportsWithSideEffectsResult { ...@@ -256,28 +248,20 @@ struct FollowExportsWithSideEffectsResult {
async fn follow_reexports_with_side_effects( async fn follow_reexports_with_side_effects(
module: ResolvedVc<Box<dyn EcmascriptChunkPlaceable>>, module: ResolvedVc<Box<dyn EcmascriptChunkPlaceable>>,
export_name: RcStr, export_name: RcStr,
side_effect_free_packages: Vc<Glob>,
) -> Result<Vc<FollowExportsWithSideEffectsResult>> { ) -> Result<Vc<FollowExportsWithSideEffectsResult>> {
let mut side_effects = vec![]; let mut side_effects = vec![];
let mut current_module = module; let mut current_module = module;
let mut current_export_name = export_name; let mut current_export_name = export_name;
let result = loop { let result = loop {
let is_side_effect_free = *current_module let is_side_effect_free = current_module.is_marked_as_side_effect_free().await?;
.is_marked_as_side_effect_free(side_effect_free_packages)
.await?;
if !is_side_effect_free { if !is_side_effect_free {
side_effects.push(only_effects(*current_module).to_resolved().await?); side_effects.push(only_effects(*current_module).to_resolved().await?);
} }
// We ignore the side effect of the entry module here, because we need to proceed. // We ignore the side effect of the entry module here, because we need to proceed.
let result = follow_reexports( let result = follow_reexports(*current_module, current_export_name.clone(), true)
*current_module,
current_export_name.clone(),
side_effect_free_packages,
true,
)
.to_resolved() .to_resolved()
.await?; .await?;
...@@ -362,21 +346,6 @@ impl EcmascriptChunkPlaceable for EcmascriptModulePartAsset { ...@@ -362,21 +346,6 @@ impl EcmascriptChunkPlaceable for EcmascriptModulePartAsset {
async fn get_exports(self: Vc<Self>) -> Result<Vc<EcmascriptExports>> { async fn get_exports(self: Vc<Self>) -> Result<Vc<EcmascriptExports>> {
Ok(*self.analyze().await?.exports) Ok(*self.analyze().await?.exports)
} }
#[turbo_tasks::function]
async fn is_marked_as_side_effect_free(
self: Vc<Self>,
side_effect_free_packages: Vc<Glob>,
) -> Result<Vc<bool>> {
let this = self.await?;
match this.part {
ModulePart::Exports | ModulePart::Export(..) => Ok(Vc::cell(true)),
_ => Ok(this
.full_module
.is_marked_as_side_effect_free(side_effect_free_packages)),
}
}
} }
#[turbo_tasks::value_impl] #[turbo_tasks::value_impl]
......
use anyhow::Result; use anyhow::Result;
use turbo_rcstr::{RcStr, rcstr}; use turbo_rcstr::{RcStr, rcstr};
use turbo_tasks::{ResolvedVc, TryJoinIterExt, Vc}; use turbo_tasks::{ResolvedVc, TryJoinIterExt, Vc};
use turbo_tasks_fs::glob::Glob;
use turbopack_core::{ use turbopack_core::{
asset::{Asset, AssetContent}, asset::{Asset, AssetContent},
chunk::{ChunkableModule, ChunkingContext, EvaluatableAsset}, chunk::{ChunkableModule, ChunkingContext, EvaluatableAsset},
...@@ -123,11 +122,6 @@ impl EcmascriptChunkPlaceable for SideEffectsModule { ...@@ -123,11 +122,6 @@ impl EcmascriptChunkPlaceable for SideEffectsModule {
fn get_exports(&self) -> Vc<EcmascriptExports> { fn get_exports(&self) -> Vc<EcmascriptExports> {
self.resolved_as.get_exports() self.resolved_as.get_exports()
} }
#[turbo_tasks::function]
fn is_marked_as_side_effect_free(self: Vc<Self>, _: Vc<Glob>) -> Vc<bool> {
Vc::cell(true)
}
} }
#[turbo_tasks::value_impl] #[turbo_tasks::value_impl]
......
...@@ -14,7 +14,7 @@ use std::fmt::Write; ...@@ -14,7 +14,7 @@ use std::fmt::Write;
use anyhow::{Error, Result, bail}; use anyhow::{Error, Result, bail};
use turbo_rcstr::rcstr; use turbo_rcstr::rcstr;
use turbo_tasks::{ResolvedVc, ValueToString, Vc}; use turbo_tasks::{ResolvedVc, ValueToString, Vc};
use turbo_tasks_fs::{FileContent, FileJsonContent, glob::Glob}; use turbo_tasks_fs::{FileContent, FileJsonContent};
use turbopack_core::{ use turbopack_core::{
asset::{Asset, AssetContent}, asset::{Asset, AssetContent},
chunk::{ChunkItem, ChunkType, ChunkableModule, ChunkingContext}, chunk::{ChunkItem, ChunkType, ChunkableModule, ChunkingContext},
...@@ -86,11 +86,6 @@ impl EcmascriptChunkPlaceable for JsonModuleAsset { ...@@ -86,11 +86,6 @@ impl EcmascriptChunkPlaceable for JsonModuleAsset {
} }
.cell() .cell()
} }
#[turbo_tasks::function]
fn is_marked_as_side_effect_free(&self, _side_effect_free_packages: Vc<Glob>) -> Vc<bool> {
Vc::cell(true)
}
} }
#[turbo_tasks::value] #[turbo_tasks::value]
......
use anyhow::Result; use anyhow::Result;
use turbo_rcstr::{RcStr, rcstr}; use turbo_rcstr::RcStr;
use turbo_tasks::{ResolvedVc, Vc}; use turbo_tasks::{ResolvedVc, Vc};
use turbo_tasks_fs::{FileSystemPath, glob::Glob}; use turbo_tasks_fs::FileSystemPath;
use turbopack_core::{ use turbopack_core::{
compile_time_info::CompileTimeInfo, compile_time_info::CompileTimeInfo,
context::{AssetContext, ProcessResult}, context::{AssetContext, ProcessResult},
...@@ -73,9 +73,4 @@ impl AssetContext for NoopAssetContext { ...@@ -73,9 +73,4 @@ impl AssetContext for NoopAssetContext {
) -> Result<Vc<Box<dyn AssetContext>>> { ) -> Result<Vc<Box<dyn AssetContext>>> {
Ok(Vc::upcast(self)) Ok(Vc::upcast(self))
} }
#[turbo_tasks::function]
async fn side_effect_free_packages(&self) -> Result<Vc<Glob>> {
Ok(Glob::new(rcstr!(""), Default::default()))
}
} }
...@@ -26,10 +26,7 @@ use module_options::{ModuleOptions, ModuleOptionsContext, ModuleRuleEffect, Modu ...@@ -26,10 +26,7 @@ use module_options::{ModuleOptions, ModuleOptionsContext, ModuleRuleEffect, Modu
use tracing::{Instrument, field::Empty}; use tracing::{Instrument, field::Empty};
use turbo_rcstr::{RcStr, rcstr}; use turbo_rcstr::{RcStr, rcstr};
use turbo_tasks::{ResolvedVc, ValueToString, Vc}; use turbo_tasks::{ResolvedVc, ValueToString, Vc};
use turbo_tasks_fs::{ use turbo_tasks_fs::FileSystemPath;
FileSystemPath,
glob::{Glob, GlobOptions},
};
pub use turbopack_core::condition; pub use turbopack_core::condition;
use turbopack_core::{ use turbopack_core::{
asset::Asset, asset::Asset,
...@@ -56,6 +53,7 @@ use turbopack_core::{ ...@@ -56,6 +53,7 @@ use turbopack_core::{
pub use turbopack_css as css; pub use turbopack_css as css;
pub use turbopack_ecmascript as ecmascript; pub use turbopack_ecmascript as ecmascript;
use turbopack_ecmascript::{ use turbopack_ecmascript::{
chunk::EcmascriptChunkPlaceableExt,
inlined_bytes_module::InlinedBytesJsModule, inlined_bytes_module::InlinedBytesJsModule,
references::external_module::{ references::external_module::{
CachedExternalModule, CachedExternalTracingMode, CachedExternalType, CachedExternalModule, CachedExternalTracingMode, CachedExternalType,
...@@ -156,15 +154,7 @@ async fn apply_module_type( ...@@ -156,15 +154,7 @@ async fn apply_module_type(
} else { } else {
if matches!(&part, Some(ModulePart::Evaluation)) { if matches!(&part, Some(ModulePart::Evaluation)) {
// Skip the evaluation part if the module is marked as side effect free. // Skip the evaluation part if the module is marked as side effect free.
let side_effect_free_packages = module_asset_context if module.is_marked_as_side_effect_free().await? {
.side_effect_free_packages()
.resolve()
.await?;
if *module
.is_marked_as_side_effect_free(side_effect_free_packages)
.await?
{
return Ok(ProcessResult::Ignore.cell()); return Ok(ProcessResult::Ignore.cell());
} }
} }
...@@ -185,11 +175,6 @@ async fn apply_module_type( ...@@ -185,11 +175,6 @@ async fn apply_module_type(
Vc::upcast(EcmascriptModuleLocalsModule::new(*module)) Vc::upcast(EcmascriptModuleLocalsModule::new(*module))
} }
ModulePart::Export(_) => { ModulePart::Export(_) => {
let side_effect_free_packages = module_asset_context
.side_effect_free_packages()
.resolve()
.await?;
apply_reexport_tree_shaking( apply_reexport_tree_shaking(
Vc::upcast( Vc::upcast(
EcmascriptModuleFacadeModule::new( EcmascriptModuleFacadeModule::new(
...@@ -200,7 +185,6 @@ async fn apply_module_type( ...@@ -200,7 +185,6 @@ async fn apply_module_type(
.await?, .await?,
), ),
part, part,
side_effect_free_packages,
) )
.await? .await?
} }
...@@ -278,14 +262,13 @@ async fn apply_module_type( ...@@ -278,14 +262,13 @@ async fn apply_module_type(
async fn apply_reexport_tree_shaking( async fn apply_reexport_tree_shaking(
module: Vc<Box<dyn EcmascriptChunkPlaceable>>, module: Vc<Box<dyn EcmascriptChunkPlaceable>>,
part: ModulePart, part: ModulePart,
side_effect_free_packages: Vc<Glob>,
) -> Result<Vc<Box<dyn Module>>> { ) -> Result<Vc<Box<dyn Module>>> {
if let ModulePart::Export(export) = &part { if let ModulePart::Export(export) = &part {
let FollowExportsResult { let FollowExportsResult {
module: final_module, module: final_module,
export_name: new_export, export_name: new_export,
.. ..
} = &*follow_reexports(module, export.clone(), side_effect_free_packages, true).await?; } = &*follow_reexports(module, export.clone(), true).await?;
let module = if let Some(new_export) = new_export { let module = if let Some(new_export) = new_export {
if *new_export == *export { if *new_export == *export {
Vc::upcast(**final_module) Vc::upcast(**final_module)
...@@ -896,21 +879,6 @@ impl AssetContext for ModuleAssetContext { ...@@ -896,21 +879,6 @@ impl AssetContext for ModuleAssetContext {
}, },
) )
} }
#[turbo_tasks::function]
async fn side_effect_free_packages(&self) -> Result<Vc<Glob>> {
Ok(self
.module_options_context
.await?
.side_effect_free_packages
.map_or_else(
|| {
// If no side effect free packages are configured, return an empty glob.
Glob::new(rcstr!(""), GlobOptions::default())
},
|glob| *glob,
))
}
} }
#[turbo_tasks::function] #[turbo_tasks::function]
......
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