Unverified Commit 37554159 authored by Niklas Mischkulnig's avatar Niklas Mischkulnig Committed by GitHub

Turbopack: side effect directive (#76876)

Any suggestions regarding the exact naming?

```
"use turbopack no side effects"

export const bar = 123;
```
parent ad0d49fe
...@@ -67,7 +67,7 @@ use turbo_tasks::{ ...@@ -67,7 +67,7 @@ use turbo_tasks::{
trace::TraceRawVcs, FxIndexMap, NonLocalValue, ReadRef, ResolvedVc, TaskInput, TryJoinIterExt, trace::TraceRawVcs, FxIndexMap, NonLocalValue, ReadRef, ResolvedVc, TaskInput, TryJoinIterExt,
Value, ValueToString, Vc, Value, ValueToString, Vc,
}; };
use turbo_tasks_fs::{rope::Rope, FileJsonContent, FileSystemPath}; use turbo_tasks_fs::{glob::Glob, rope::Rope, FileJsonContent, FileSystemPath};
use turbopack_core::{ use turbopack_core::{
asset::{Asset, AssetContent}, asset::{Asset, AssetContent},
chunk::{ chunk::{
...@@ -92,7 +92,7 @@ pub use turbopack_resolve::ecmascript as resolve; ...@@ -92,7 +92,7 @@ pub use turbopack_resolve::ecmascript as resolve;
use self::chunk::{EcmascriptChunkItemContent, EcmascriptChunkType, EcmascriptExports}; use self::chunk::{EcmascriptChunkItemContent, EcmascriptChunkType, EcmascriptExports};
use crate::{ use crate::{
chunk::EcmascriptChunkPlaceable, chunk::{placeable::is_marked_as_side_effect_free, EcmascriptChunkPlaceable},
code_gen::CodeGens, code_gen::CodeGens,
parse::generate_js_source_map, parse::generate_js_source_map,
references::{ references::{
...@@ -646,6 +646,21 @@ impl EcmascriptChunkPlaceable for EcmascriptModuleAsset { ...@@ -646,6 +646,21 @@ 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(), 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]
......
...@@ -23,6 +23,7 @@ use std::{borrow::Cow, collections::BTreeMap, future::Future, mem::take, ops::De ...@@ -23,6 +23,7 @@ use std::{borrow::Cow, collections::BTreeMap, future::Future, mem::take, ops::De
use anyhow::{bail, Result}; use anyhow::{bail, Result};
use constant_condition::{ConstantConditionCodeGen, ConstantConditionValue}; use constant_condition::{ConstantConditionCodeGen, ConstantConditionValue};
use constant_value::ConstantValueCodeGen; use constant_value::ConstantValueCodeGen;
use either::Either;
use indexmap::map::Entry; use indexmap::map::Entry;
use lazy_static::lazy_static; use lazy_static::lazy_static;
use num_traits::Zero; use num_traits::Zero;
...@@ -42,6 +43,7 @@ use swc_core::{ ...@@ -42,6 +43,7 @@ use swc_core::{
}, },
ecma::{ ecma::{
ast::*, ast::*,
utils::IsDirective,
visit::{ visit::{
fields::{AssignExprField, AssignTargetField, SimpleAssignTargetField}, fields::{AssignExprField, AssignTargetField, SimpleAssignTargetField},
AstParentKind, AstParentNodeRef, VisitAstPath, VisitWithAstPath, AstParentKind, AstParentNodeRef, VisitAstPath, VisitWithAstPath,
...@@ -169,6 +171,7 @@ pub struct AnalyzeEcmascriptModuleResult { ...@@ -169,6 +171,7 @@ 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: ResolvedVc<OptionStringifiedSourceMap>, pub source_map: ResolvedVc<OptionStringifiedSourceMap>,
...@@ -221,6 +224,7 @@ pub struct AnalyzeEcmascriptModuleResultBuilder { ...@@ -221,6 +224,7 @@ pub struct AnalyzeEcmascriptModuleResultBuilder {
async_module: ResolvedVc<OptionAsyncModule>, async_module: ResolvedVc<OptionAsyncModule>,
successful: bool, successful: bool,
source_map: Option<ResolvedVc<OptionStringifiedSourceMap>>, source_map: Option<ResolvedVc<OptionStringifiedSourceMap>>,
has_side_effect_free_directive: bool,
} }
impl AnalyzeEcmascriptModuleResultBuilder { impl AnalyzeEcmascriptModuleResultBuilder {
...@@ -238,6 +242,7 @@ impl AnalyzeEcmascriptModuleResultBuilder { ...@@ -238,6 +242,7 @@ 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,
} }
} }
...@@ -297,6 +302,11 @@ impl AnalyzeEcmascriptModuleResultBuilder { ...@@ -297,6 +302,11 @@ 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;
...@@ -411,6 +421,7 @@ impl AnalyzeEcmascriptModuleResultBuilder { ...@@ -411,6 +421,7 @@ impl AnalyzeEcmascriptModuleResultBuilder {
code_generation: ResolvedVc::cell(self.code_gens), code_generation: ResolvedVc::cell(self.code_gens),
exports: self.exports.resolved_cell(), exports: self.exports.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, source_map,
}, },
...@@ -586,6 +597,33 @@ pub(crate) async fn analyse_ecmascript_module_internal( ...@@ -586,6 +597,33 @@ pub(crate) async fn analyse_ecmascript_module_internal(
return analysis.build(Default::default(), false).await; return analysis.build(Default::default(), false).await;
}; };
let has_side_effect_free_directive = match program {
Program::Module(module) => Either::Left(
module
.body
.iter()
.take_while(|i| match i {
ModuleItem::Stmt(stmt) => stmt.directive_continue(),
ModuleItem::ModuleDecl(_) => false,
})
.filter_map(|i| i.as_stmt()),
),
Program::Script(script) => Either::Right(
script
.body
.iter()
.take_while(|stmt| stmt.directive_continue()),
),
}
.any(|f| match f {
Stmt::Expr(ExprStmt { expr, .. }) => match &**expr {
Expr::Lit(Lit::Str(Str { value, .. })) => value == "use turbopack no side effects",
_ => false,
},
_ => false,
});
analysis.set_has_side_effect_free_directive(has_side_effect_free_directive);
let compile_time_info = compile_time_info_for_module_type( let compile_time_info = compile_time_info_for_module_type(
*raw_module.compile_time_info, *raw_module.compile_time_info,
eval_context.is_esm(specified_type), eval_context.is_esm(specified_type),
......
import { foo } from './lib/index.js'
it('should respect side effects directive', () => {
expect(foo).toBe(789)
const modules = Object.keys(__turbopack_modules__)
expect(modules).toContainEqual(expect.stringContaining('input/lib/foo'))
expect(modules).not.toContainEqual(expect.stringContaining('input/lib/index'))
expect(modules).not.toContainEqual(expect.stringContaining('input/lib/bar'))
})
"use turbopack no side effects"
export {foo} from "./foo";
export {bar} from "./bar";
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