Unverified Commit 8c8030ed authored by Tobias Koppers's avatar Tobias Koppers Committed by GitHub

Turbopack: apply side effects false removal on all module types (#86133)

### What?

fix the side effects false handling for other module types. Before it did only work for ecmascript modules.
parent 283c6f57
...@@ -70,6 +70,11 @@ impl Module for HmrEntryModule { ...@@ -70,6 +70,11 @@ impl Module for HmrEntryModule {
.await?, .await?,
)])) )]))
} }
#[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]
...@@ -105,11 +110,6 @@ impl EcmascriptChunkPlaceable for HmrEntryModule { ...@@ -105,11 +110,6 @@ impl EcmascriptChunkPlaceable for HmrEntryModule {
fn get_exports(self: Vc<Self>) -> Vc<EcmascriptExports> { fn get_exports(self: Vc<Self>) -> Vc<EcmascriptExports> {
EcmascriptExports::None.cell() EcmascriptExports::None.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]
......
...@@ -92,6 +92,14 @@ impl Module for RawEcmascriptModule { ...@@ -92,6 +92,14 @@ impl Module for RawEcmascriptModule {
fn ident(&self) -> Vc<AssetIdent> { fn ident(&self) -> Vc<AssetIdent> {
self.source.ident().with_modifier(rcstr!("raw")) self.source.ident().with_modifier(rcstr!("raw"))
} }
#[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_impl] #[turbo_tasks::value_impl]
...@@ -126,11 +134,6 @@ impl EcmascriptChunkPlaceable for RawEcmascriptModule { ...@@ -126,11 +134,6 @@ impl EcmascriptChunkPlaceable for RawEcmascriptModule {
fn get_exports(&self) -> Vc<EcmascriptExports> { fn get_exports(&self) -> Vc<EcmascriptExports> {
EcmascriptExports::CommonJs.cell() EcmascriptExports::CommonJs.cell()
} }
#[turbo_tasks::function]
fn is_marked_as_side_effect_free(&self, _side_effect_free_packages: Vc<Glob>) -> Vc<bool> {
Vc::cell(false)
}
} }
#[turbo_tasks::value] #[turbo_tasks::value]
......
use turbo_rcstr::RcStr; use turbo_rcstr::RcStr;
use turbo_tasks::{ResolvedVc, TaskInput, ValueToString, Vc}; use turbo_tasks::{ResolvedVc, TaskInput, ValueToString, Vc};
use turbo_tasks_fs::glob::Glob;
use crate::{asset::Asset, ident::AssetIdent, reference::ModuleReferences}; use crate::{asset::Asset, ident::AssetIdent, reference::ModuleReferences};
...@@ -38,6 +39,15 @@ pub trait Module: Asset { ...@@ -38,6 +39,15 @@ pub trait Module: Asset {
fn is_self_async(self: Vc<Self>) -> Vc<bool> { fn is_self_async(self: Vc<Self>) -> Vc<bool> {
Vc::cell(false) Vc::cell(false)
} }
/// Returns true if the module is marked as side effect free in package.json or by other means.
#[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_trait] #[turbo_tasks::value_trait]
......
...@@ -31,16 +31,6 @@ pub trait EcmascriptChunkPlaceable: ChunkableModule + Module + Asset { ...@@ -31,16 +31,6 @@ pub trait EcmascriptChunkPlaceable: ChunkableModule + Module + Asset {
fn get_async_module(self: Vc<Self>) -> Vc<OptionAsyncModule> { fn get_async_module(self: Vc<Self>) -> Vc<OptionAsyncModule> {
Vc::cell(None) Vc::cell(None)
} }
#[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?,
side_effect_free_packages,
))
}
} }
#[turbo_tasks::value] #[turbo_tasks::value]
......
...@@ -44,6 +44,14 @@ impl Module for InlinedBytesJsModule { ...@@ -44,6 +44,14 @@ impl Module for InlinedBytesJsModule {
.ident() .ident()
.with_modifier(rcstr!("static bytes in ecmascript")) .with_modifier(rcstr!("static bytes in ecmascript"))
} }
#[turbo_tasks::function]
fn is_marked_as_side_effect_free(
self: Vc<Self>,
_side_effect_free_packages: Vc<Glob>,
) -> Vc<bool> {
Vc::cell(true)
}
} }
#[turbo_tasks::value_impl] #[turbo_tasks::value_impl]
...@@ -75,11 +83,6 @@ impl EcmascriptChunkPlaceable for InlinedBytesJsModule { ...@@ -75,11 +83,6 @@ impl EcmascriptChunkPlaceable for InlinedBytesJsModule {
fn get_exports(&self) -> Vc<EcmascriptExports> { fn get_exports(&self) -> Vc<EcmascriptExports> {
EcmascriptExports::Value.cell() EcmascriptExports::Value.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]
......
...@@ -749,6 +749,23 @@ impl Module for EcmascriptModuleAsset { ...@@ -749,6 +749,23 @@ impl Module for EcmascriptModuleAsset {
Ok(Vc::cell(false)) Ok(Vc::cell(false))
} }
} }
#[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 = is_marked_as_side_effect_free(
self.ident().path().owned().await?,
side_effect_free_packages,
);
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]
...@@ -785,23 +802,6 @@ impl EcmascriptChunkPlaceable for EcmascriptModuleAsset { ...@@ -785,23 +802,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 = is_marked_as_side_effect_free(
self.ident().path().owned().await?,
side_effect_free_packages,
);
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]
......
...@@ -298,6 +298,14 @@ impl Module for CachedExternalModule { ...@@ -298,6 +298,14 @@ impl Module for CachedExternalModule {
|| self.external_type == CachedExternalType::Script, || self.external_type == CachedExternalType::Script,
)) ))
} }
#[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_impl] #[turbo_tasks::value_impl]
...@@ -357,14 +365,6 @@ impl EcmascriptChunkPlaceable for CachedExternalModule { ...@@ -357,14 +365,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]
......
...@@ -175,6 +175,22 @@ impl Module for EcmascriptModuleFacadeModule { ...@@ -175,6 +175,22 @@ impl Module for EcmascriptModuleFacadeModule {
.await?; .await?;
Ok(is_self_async) Ok(is_self_async)
} }
#[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::value_impl] #[turbo_tasks::value_impl]
...@@ -335,22 +351,6 @@ impl EcmascriptChunkPlaceable for EcmascriptModuleFacadeModule { ...@@ -335,22 +351,6 @@ impl EcmascriptChunkPlaceable for EcmascriptModuleFacadeModule {
Ok(EcmascriptExports::EsmExports(exports).cell()) Ok(EcmascriptExports::EsmExports(exports).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?)))
......
...@@ -66,6 +66,12 @@ impl Module for EcmascriptModuleLocalsModule { ...@@ -66,6 +66,12 @@ impl Module for EcmascriptModuleLocalsModule {
Ok(Vc::cell(false)) Ok(Vc::cell(false))
} }
} }
#[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::value_impl] #[turbo_tasks::value_impl]
...@@ -164,12 +170,6 @@ impl EcmascriptChunkPlaceable for EcmascriptModuleLocalsModule { ...@@ -164,12 +170,6 @@ impl EcmascriptChunkPlaceable for EcmascriptModuleLocalsModule {
Ok(EcmascriptExports::EsmExports(exports).cell()) Ok(EcmascriptExports::EsmExports(exports).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()
......
...@@ -346,6 +346,19 @@ impl Module for EcmascriptModulePartAsset { ...@@ -346,6 +346,19 @@ impl Module for EcmascriptModulePartAsset {
Ok(analyze.references()) Ok(analyze.references())
} }
#[turbo_tasks::function]
async fn is_marked_as_side_effect_free(
&self,
side_effect_free_packages: Vc<Glob>,
) -> Result<Vc<bool>> {
match self.part {
ModulePart::Exports | ModulePart::Export(..) => Ok(Vc::cell(true)),
_ => Ok(self
.full_module
.is_marked_as_side_effect_free(side_effect_free_packages)),
}
}
} }
#[turbo_tasks::value_impl] #[turbo_tasks::value_impl]
...@@ -362,21 +375,6 @@ impl EcmascriptChunkPlaceable for EcmascriptModulePartAsset { ...@@ -362,21 +375,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]
......
...@@ -107,6 +107,11 @@ impl Module for SideEffectsModule { ...@@ -107,6 +107,11 @@ impl Module for SideEffectsModule {
Ok(Vc::cell(references)) Ok(Vc::cell(references))
} }
#[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]
...@@ -123,11 +128,6 @@ impl EcmascriptChunkPlaceable for SideEffectsModule { ...@@ -123,11 +128,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]
......
...@@ -52,6 +52,14 @@ impl Module for JsonModuleAsset { ...@@ -52,6 +52,14 @@ impl Module for JsonModuleAsset {
fn ident(&self) -> Vc<AssetIdent> { fn ident(&self) -> Vc<AssetIdent> {
self.source.ident().with_modifier(rcstr!("json")) self.source.ident().with_modifier(rcstr!("json"))
} }
#[turbo_tasks::function]
fn is_marked_as_side_effect_free(
self: Vc<Self>,
_side_effect_free_packages: Vc<Glob>,
) -> Vc<bool> {
Vc::cell(true)
}
} }
#[turbo_tasks::value_impl] #[turbo_tasks::value_impl]
...@@ -83,11 +91,6 @@ impl EcmascriptChunkPlaceable for JsonModuleAsset { ...@@ -83,11 +91,6 @@ impl EcmascriptChunkPlaceable for JsonModuleAsset {
fn get_exports(&self) -> Vc<EcmascriptExports> { fn get_exports(&self) -> Vc<EcmascriptExports> {
EcmascriptExports::Value.cell() EcmascriptExports::Value.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]
......
import used from './used.cjs'
import unused from './unused.cjs'
export { used }
import { used } from './json.js'
import { used as used2 } from './js.js'
import { used as used3 } from './cjs.js'
it('should tree shake all unused modules', () => {
expect(used).toEqual({ used: true })
expect(used2).toBe(true)
expect(used3).toBe(true)
const modules = Array.from(__turbopack_modules__.keys())
expect(modules).toContainEqual(expect.stringMatching(/input\/json\.js/))
expect(modules).toContainEqual(expect.stringMatching(/input\/used\.json/))
expect(modules).not.toContainEqual(
expect.stringMatching(/input\/unused\.json/)
)
expect(modules).toContainEqual(expect.stringMatching(/input\/js\.js/))
expect(modules).toContainEqual(expect.stringMatching(/input\/used\.js/))
expect(modules).not.toContainEqual(expect.stringMatching(/input\/unused\.js/))
expect(modules).toContainEqual(expect.stringMatching(/input\/cjs\.js/))
expect(modules).toContainEqual(expect.stringMatching(/input\/used\.cjs/))
expect(modules).not.toContainEqual(
expect.stringMatching(/input\/unused\.cjs/)
)
})
import used from './used.json'
import unused from './unused.json'
export { used }
...@@ -82,8 +82,14 @@ async fn apply_module_type( ...@@ -82,8 +82,14 @@ async fn apply_module_type(
css_import_context: Option<ResolvedVc<ImportContext>>, css_import_context: Option<ResolvedVc<ImportContext>>,
runtime_code: bool, runtime_code: bool,
) -> Result<Vc<ProcessResult>> { ) -> Result<Vc<ProcessResult>> {
let tree_shaking_mode = module_asset_context
.module_options_context()
.await?
.tree_shaking_mode;
let is_evaluation = matches!(&part, Some(ModulePart::Evaluation));
let module_type = &*module_type.await?; let module_type = &*module_type.await?;
Ok(ProcessResult::Module(match module_type { let module = match module_type {
ModuleType::Ecmascript { ModuleType::Ecmascript {
preprocess, preprocess,
main, main,
...@@ -164,10 +170,11 @@ async fn apply_module_type( ...@@ -164,10 +170,11 @@ async fn apply_module_type(
if runtime_code { if runtime_code {
ResolvedVc::upcast(module) ResolvedVc::upcast(module)
} else { } else {
let options_value = options.await?; // Check side effect free on the intermediate module before following reexports
if options_value.tree_shaking_mode.is_some() // This can skip the module earlier and could skip more modules than only doing it
&& matches!(&part, Some(ModulePart::Evaluation)) // at the end. Also we avoid parsing/analyzing the module in this
{ // case, because we would need to parse/analyze it for reexports.
if tree_shaking_mode.is_some() && is_evaluation {
// If we are tree shaking, skip the evaluation part if the module is marked as // If we are tree shaking, skip the evaluation part if the module is marked as
// side effect free. // side effect free.
let side_effect_free_packages = module_asset_context let side_effect_free_packages = module_asset_context
...@@ -183,7 +190,7 @@ async fn apply_module_type( ...@@ -183,7 +190,7 @@ async fn apply_module_type(
} }
} }
match options_value.tree_shaking_mode { match tree_shaking_mode {
Some(TreeShakingMode::ModuleFragments) => { Some(TreeShakingMode::ModuleFragments) => {
Vc::upcast(EcmascriptModulePartAsset::select_part( Vc::upcast(EcmascriptModulePartAsset::select_part(
*module, *module,
...@@ -299,8 +306,25 @@ async fn apply_module_type( ...@@ -299,8 +306,25 @@ async fn apply_module_type(
.to_resolved() .to_resolved()
.await? .await?
} }
}) };
.cell())
if tree_shaking_mode.is_some() && is_evaluation {
// If we are tree shaking, 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?
{
return Ok(ProcessResult::Ignore.cell());
}
}
Ok(ProcessResult::Module(module).cell())
} }
async fn apply_reexport_tree_shaking( async fn apply_reexport_tree_shaking(
...@@ -547,7 +571,8 @@ async fn process_default_internal( ...@@ -547,7 +571,8 @@ async fn process_default_internal(
_ => None, _ => None,
}; };
for (i, rule) in options.await?.rules.iter().enumerate() { let options_value = options.await?;
for (i, rule) in options_value.rules.iter().enumerate() {
if has_type_attribute && current_module_type.is_some() { if has_type_attribute && current_module_type.is_some() {
continue; continue;
} }
...@@ -710,7 +735,7 @@ async fn process_default_internal( ...@@ -710,7 +735,7 @@ async fn process_default_internal(
return Ok(ProcessResult::Unknown(current_source).cell()); return Ok(ProcessResult::Unknown(current_source).cell());
}; };
apply_module_type( let module = apply_module_type(
current_source, current_source,
module_asset_context, module_asset_context,
module_type.cell(), module_type.cell(),
...@@ -723,7 +748,9 @@ async fn process_default_internal( ...@@ -723,7 +748,9 @@ async fn process_default_internal(
}, },
matches!(reference_type, ReferenceType::Runtime), matches!(reference_type, ReferenceType::Runtime),
) )
.await .await?;
Ok(module)
} }
#[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