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