Commit 7af2a39e authored by Tobias Koppers's avatar Tobias Koppers

move evaluation to EcmascriptExports

parent 8c732389
...@@ -21,6 +21,7 @@ use turbopack_ecmascript::{ ...@@ -21,6 +21,7 @@ use turbopack_ecmascript::{
EcmascriptChunkItem, EcmascriptChunkItemContent, EcmascriptChunkItemOptions, EcmascriptChunkItem, EcmascriptChunkItemContent, EcmascriptChunkItemOptions,
EcmascriptChunkPlaceable, EcmascriptChunkType, EcmascriptExports, EcmascriptExportsType, EcmascriptChunkPlaceable, EcmascriptChunkType, EcmascriptExports, EcmascriptExportsType,
}, },
references::esm::EsmEvaluation,
runtime_functions::TURBOPACK_REQUIRE, runtime_functions::TURBOPACK_REQUIRE,
utils::StringifyJs, utils::StringifyJs,
}; };
...@@ -104,6 +105,7 @@ impl EcmascriptChunkPlaceable for HmrEntryModule { ...@@ -104,6 +105,7 @@ impl EcmascriptChunkPlaceable for HmrEntryModule {
fn get_exports(self: Vc<Self>) -> Vc<EcmascriptExports> { fn get_exports(self: Vc<Self>) -> Vc<EcmascriptExports> {
EcmascriptExports { EcmascriptExports {
ty: EcmascriptExportsType::None, ty: EcmascriptExportsType::None,
evaluation: EsmEvaluation::SideEffects,
} }
.cell() .cell()
} }
......
...@@ -118,10 +118,10 @@ impl EcmascriptChunkPlaceable for NextDynamicEntryModule { ...@@ -118,10 +118,10 @@ impl EcmascriptChunkPlaceable for NextDynamicEntryModule {
EsmExports { EsmExports {
exports, exports,
star_exports: vec![module_reference], star_exports: vec![module_reference],
evaluation: EsmEvaluation::DelegatedSideEffects(module_reference),
} }
.resolved_cell(), .resolved_cell(),
), ),
evaluation: EsmEvaluation::DelegatedSideEffects(module_reference),
} }
.cell()) .cell())
} }
......
...@@ -111,10 +111,10 @@ impl EcmascriptChunkPlaceable for NextServerComponentModule { ...@@ -111,10 +111,10 @@ impl EcmascriptChunkPlaceable for NextServerComponentModule {
EsmExports { EsmExports {
exports, exports,
star_exports: vec![module_reference], star_exports: vec![module_reference],
evaluation: EsmEvaluation::DelegatedSideEffects(module_reference),
} }
.resolved_cell(), .resolved_cell(),
), ),
evaluation: EsmEvaluation::DelegatedSideEffects(module_reference),
} }
.cell()) .cell())
} }
......
...@@ -111,10 +111,10 @@ impl EcmascriptChunkPlaceable for NextServerUtilityModule { ...@@ -111,10 +111,10 @@ impl EcmascriptChunkPlaceable for NextServerUtilityModule {
EsmExports { EsmExports {
exports, exports,
star_exports: vec![module_reference], star_exports: vec![module_reference],
evaluation: EsmEvaluation::DelegatedSideEffects(module_reference),
} }
.resolved_cell(), .resolved_cell(),
), ),
evaluation: EsmEvaluation::DelegatedSideEffects(module_reference),
} }
.cell()) .cell())
} }
......
...@@ -29,6 +29,7 @@ use turbopack_ecmascript::{ ...@@ -29,6 +29,7 @@ use turbopack_ecmascript::{
EcmascriptChunkType, EcmascriptExports, EcmascriptExportsType, EcmascriptChunkType, EcmascriptExports, EcmascriptExportsType,
}, },
parse::generate_js_source_map, parse::generate_js_source_map,
references::esm::EsmEvaluation,
runtime_functions::{TURBOPACK_EXPORT_VALUE, TURBOPACK_IMPORT}, runtime_functions::{TURBOPACK_EXPORT_VALUE, TURBOPACK_IMPORT},
utils::StringifyJs, utils::StringifyJs,
}; };
...@@ -265,6 +266,7 @@ impl EcmascriptChunkPlaceable for ModuleCssAsset { ...@@ -265,6 +266,7 @@ impl EcmascriptChunkPlaceable for ModuleCssAsset {
fn get_exports(&self) -> Vc<EcmascriptExports> { fn get_exports(&self) -> Vc<EcmascriptExports> {
EcmascriptExports { EcmascriptExports {
ty: EcmascriptExportsType::Value, ty: EcmascriptExportsType::Value,
evaluation: EsmEvaluation::SideEffects,
} }
.cell() .cell()
} }
......
...@@ -21,7 +21,7 @@ use turbopack_core::{ ...@@ -21,7 +21,7 @@ use turbopack_core::{
use crate::references::{ use crate::references::{
async_module::OptionAsyncModule, async_module::OptionAsyncModule,
esm::{EsmExport, EsmExports}, esm::{EsmEvaluation, EsmExport, EsmExports},
}; };
#[turbo_tasks::value_trait] #[turbo_tasks::value_trait]
...@@ -222,6 +222,7 @@ pub async fn is_marked_as_side_effect_free( ...@@ -222,6 +222,7 @@ pub async fn is_marked_as_side_effect_free(
#[turbo_tasks::value(shared)] #[turbo_tasks::value(shared)]
pub struct EcmascriptExports { pub struct EcmascriptExports {
pub ty: EcmascriptExportsType, pub ty: EcmascriptExportsType,
pub evaluation: EsmEvaluation,
} }
#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize, TraceRawVcs, NonLocalValue)] #[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize, TraceRawVcs, NonLocalValue)]
......
...@@ -16,7 +16,10 @@ use turbopack_core::{ ...@@ -16,7 +16,10 @@ use turbopack_core::{
}; };
use super::chunk_item::ManifestChunkItem; use super::chunk_item::ManifestChunkItem;
use crate::chunk::{EcmascriptChunkPlaceable, EcmascriptExports, EcmascriptExportsType}; use crate::{
chunk::{EcmascriptChunkPlaceable, EcmascriptExports, EcmascriptExportsType},
references::esm::EsmEvaluation,
};
/// The manifest module is deferred until requested by the manifest loader /// The manifest module is deferred until requested by the manifest loader
/// item when the dynamic `import()` expression is reached. /// item when the dynamic `import()` expression is reached.
...@@ -174,6 +177,7 @@ impl EcmascriptChunkPlaceable for ManifestAsyncModule { ...@@ -174,6 +177,7 @@ impl EcmascriptChunkPlaceable for ManifestAsyncModule {
fn get_exports(&self) -> Vc<EcmascriptExports> { fn get_exports(&self) -> Vc<EcmascriptExports> {
EcmascriptExports { EcmascriptExports {
ty: EcmascriptExportsType::Value, ty: EcmascriptExportsType::Value,
evaluation: EsmEvaluation::SideEffects,
} }
.cell() .cell()
} }
......
...@@ -561,7 +561,6 @@ pub enum EsmEvaluation { ...@@ -561,7 +561,6 @@ pub enum EsmEvaluation {
pub struct EsmExports { pub struct EsmExports {
pub exports: BTreeMap<RcStr, EsmExport>, pub exports: BTreeMap<RcStr, EsmExport>,
pub star_exports: Vec<ResolvedVc<Box<dyn ModuleReference>>>, pub star_exports: Vec<ResolvedVc<Box<dyn ModuleReference>>>,
pub evaluation: EsmEvaluation,
} }
/// The expanded version of [`EsmExports`], the `exports` field here includes all exports that could /// The expanded version of [`EsmExports`], the `exports` field here includes all exports that could
......
...@@ -26,7 +26,10 @@ use crate::{ ...@@ -26,7 +26,10 @@ use crate::{
EcmascriptChunkItem, EcmascriptChunkItemContent, EcmascriptChunkPlaceable, EcmascriptChunkItem, EcmascriptChunkItemContent, EcmascriptChunkPlaceable,
EcmascriptChunkType, EcmascriptExports, EcmascriptExportsType, EcmascriptChunkType, EcmascriptExports, EcmascriptExportsType,
}, },
references::async_module::{AsyncModule, OptionAsyncModule}, references::{
async_module::{AsyncModule, OptionAsyncModule},
esm::EsmEvaluation,
},
runtime_functions::{ runtime_functions::{
TURBOPACK_EXPORT_NAMESPACE, TURBOPACK_EXPORT_VALUE, TURBOPACK_EXTERNAL_IMPORT, TURBOPACK_EXPORT_NAMESPACE, TURBOPACK_EXPORT_VALUE, TURBOPACK_EXTERNAL_IMPORT,
TURBOPACK_EXTERNAL_REQUIRE, TURBOPACK_LOAD_BY_URL, TURBOPACK_EXTERNAL_REQUIRE, TURBOPACK_LOAD_BY_URL,
...@@ -321,11 +324,13 @@ impl EcmascriptChunkPlaceable for CachedExternalModule { ...@@ -321,11 +324,13 @@ impl EcmascriptChunkPlaceable for CachedExternalModule {
if self.external_type == CachedExternalType::CommonJs { if self.external_type == CachedExternalType::CommonJs {
EcmascriptExports { EcmascriptExports {
ty: EcmascriptExportsType::CommonJs, ty: EcmascriptExportsType::CommonJs,
evaluation: EsmEvaluation::SideEffects,
} }
.cell() .cell()
} else { } else {
EcmascriptExports { EcmascriptExports {
ty: EcmascriptExportsType::DynamicNamespace, ty: EcmascriptExportsType::DynamicNamespace,
evaluation: EsmEvaluation::SideEffects,
} }
.cell() .cell()
} }
......
...@@ -227,6 +227,7 @@ pub struct AnalyzeEcmascriptModuleResultBuilder { ...@@ -227,6 +227,7 @@ pub struct AnalyzeEcmascriptModuleResultBuilder {
code_gens: Vec<CodeGen>, code_gens: Vec<CodeGen>,
exports: EcmascriptExportsType, exports: EcmascriptExportsType,
evaluation: EsmEvaluation,
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>>>,
...@@ -245,6 +246,7 @@ impl AnalyzeEcmascriptModuleResultBuilder { ...@@ -245,6 +246,7 @@ impl AnalyzeEcmascriptModuleResultBuilder {
esm_references_free_var: Default::default(), esm_references_free_var: Default::default(),
code_gens: Default::default(), code_gens: Default::default(),
exports: EcmascriptExportsType::Unknown, exports: EcmascriptExportsType::Unknown,
evaluation: EsmEvaluation::SideEffects,
async_module: ResolvedVc::cell(None), async_module: ResolvedVc::cell(None),
successful: false, successful: false,
source_map: None, source_map: None,
...@@ -305,6 +307,11 @@ impl AnalyzeEcmascriptModuleResultBuilder { ...@@ -305,6 +307,11 @@ impl AnalyzeEcmascriptModuleResultBuilder {
self.exports = exports; self.exports = exports;
} }
/// Sets the analysis result evaluation.
pub fn set_evaluation(&mut self, evaluation: EsmEvaluation) {
self.evaluation = evaluation;
}
/// Sets the analysis result ES export. /// Sets the analysis result ES export.
pub fn set_async_module(&mut self, async_module: ResolvedVc<AsyncModule>) { pub fn set_async_module(&mut self, async_module: ResolvedVc<AsyncModule>) {
self.async_module = ResolvedVc::cell(Some(async_module)); self.async_module = ResolvedVc::cell(Some(async_module));
...@@ -411,7 +418,11 @@ impl AnalyzeEcmascriptModuleResultBuilder { ...@@ -411,7 +418,11 @@ impl AnalyzeEcmascriptModuleResultBuilder {
esm_reexport_references.unwrap_or_default(), esm_reexport_references.unwrap_or_default(),
), ),
code_generation: ResolvedVc::cell(self.code_gens), code_generation: ResolvedVc::cell(self.code_gens),
exports: EcmascriptExports { ty: self.exports }.resolved_cell(), exports: EcmascriptExports {
ty: self.exports,
evaluation: self.evaluation,
}
.resolved_cell(),
async_module: self.async_module, async_module: self.async_module,
has_side_effect_free_directive: self.has_side_effect_free_directive, has_side_effect_free_directive: self.has_side_effect_free_directive,
successful: self.successful, successful: self.successful,
...@@ -580,6 +591,18 @@ pub async fn analyse_ecmascript_module_internal( ...@@ -580,6 +591,18 @@ pub async fn analyse_ecmascript_module_internal(
.. ..
} = &*parsed } = &*parsed
else { else {
// Even a file that failed parsing can be side effect free.
// That's important as
let marked_as_side_effect_free =
*is_marked_as_side_effect_free(path, options.side_effect_free_packages.map(|l| *l))
.await?;
let evaluation = if marked_as_side_effect_free {
EsmEvaluation::SideEffectFree
} else {
EsmEvaluation::SideEffects
};
analysis.set_evaluation(evaluation);
return analysis.build(Default::default(), false).await; return analysis.build(Default::default(), false).await;
}; };
...@@ -806,6 +829,8 @@ pub async fn analyse_ecmascript_module_internal( ...@@ -806,6 +829,8 @@ pub async fn analyse_ecmascript_module_internal(
} else { } else {
EsmEvaluation::SideEffects EsmEvaluation::SideEffects
}; };
analysis.set_evaluation(evaluation);
let exports = if !esm_exports.is_empty() || !esm_star_exports.is_empty() { let exports = if !esm_exports.is_empty() || !esm_star_exports.is_empty() {
if specified_type == SpecifiedModuleType::CommonJs { if specified_type == SpecifiedModuleType::CommonJs {
SpecifiedModuleTypeIssue { SpecifiedModuleTypeIssue {
...@@ -820,7 +845,6 @@ pub async fn analyse_ecmascript_module_internal( ...@@ -820,7 +845,6 @@ pub async fn analyse_ecmascript_module_internal(
let esm_exports = EsmExports { let esm_exports = EsmExports {
exports: esm_exports, exports: esm_exports,
star_exports: esm_star_exports, star_exports: esm_star_exports,
evaluation,
} }
.cell(); .cell();
...@@ -841,7 +865,6 @@ pub async fn analyse_ecmascript_module_internal( ...@@ -841,7 +865,6 @@ pub async fn analyse_ecmascript_module_internal(
EsmExports { EsmExports {
exports: Default::default(), exports: Default::default(),
star_exports: Default::default(), star_exports: Default::default(),
evaluation,
} }
.resolved_cell(), .resolved_cell(),
) )
...@@ -853,7 +876,6 @@ pub async fn analyse_ecmascript_module_internal( ...@@ -853,7 +876,6 @@ pub async fn analyse_ecmascript_module_internal(
EsmExports { EsmExports {
exports: Default::default(), exports: Default::default(),
star_exports: Default::default(), star_exports: Default::default(),
evaluation,
} }
.resolved_cell(), .resolved_cell(),
), ),
...@@ -868,7 +890,6 @@ pub async fn analyse_ecmascript_module_internal( ...@@ -868,7 +890,6 @@ pub async fn analyse_ecmascript_module_internal(
EsmExports { EsmExports {
exports: Default::default(), exports: Default::default(),
star_exports: Default::default(), star_exports: Default::default(),
evaluation,
} }
.resolved_cell(), .resolved_cell(),
) )
......
...@@ -47,6 +47,7 @@ use crate::{ ...@@ -47,6 +47,7 @@ use crate::{
create_visitor, create_visitor,
references::{ references::{
AstPath, AstPath,
esm::EsmEvaluation,
pattern_mapping::{PatternMapping, ResolveType}, pattern_mapping::{PatternMapping, ResolveType},
}, },
runtime_functions::{TURBOPACK_EXPORT_VALUE, TURBOPACK_MODULE_CONTEXT, TURBOPACK_REQUIRE}, runtime_functions::{TURBOPACK_EXPORT_VALUE, TURBOPACK_MODULE_CONTEXT, TURBOPACK_REQUIRE},
...@@ -458,6 +459,7 @@ impl EcmascriptChunkPlaceable for RequireContextAsset { ...@@ -458,6 +459,7 @@ impl EcmascriptChunkPlaceable for RequireContextAsset {
fn get_exports(&self) -> Vc<EcmascriptExports> { fn get_exports(&self) -> Vc<EcmascriptExports> {
EcmascriptExports { EcmascriptExports {
ty: EcmascriptExportsType::Value, ty: EcmascriptExportsType::Value,
evaluation: EsmEvaluation::SideEffects,
} }
.cell() .cell()
} }
......
...@@ -343,11 +343,11 @@ impl EcmascriptChunkPlaceable for EcmascriptModuleFacadeModule { ...@@ -343,11 +343,11 @@ impl EcmascriptChunkPlaceable for EcmascriptModuleFacadeModule {
let exports = EsmExports { let exports = EsmExports {
exports, exports,
star_exports, star_exports,
evaluation,
} }
.resolved_cell(); .resolved_cell();
Ok(EcmascriptExports { Ok(EcmascriptExports {
ty: EcmascriptExportsType::EsmExports(exports), ty: EcmascriptExportsType::EsmExports(exports),
evaluation,
} }
.cell()) .cell())
} }
......
...@@ -133,7 +133,8 @@ impl EcmascriptAnalyzable for EcmascriptModuleLocalsModule { ...@@ -133,7 +133,8 @@ impl EcmascriptAnalyzable for EcmascriptModuleLocalsModule {
impl EcmascriptChunkPlaceable for EcmascriptModuleLocalsModule { impl EcmascriptChunkPlaceable for EcmascriptModuleLocalsModule {
#[turbo_tasks::function] #[turbo_tasks::function]
async fn get_exports(&self) -> Result<Vc<EcmascriptExports>> { async fn get_exports(&self) -> Result<Vc<EcmascriptExports>> {
let EcmascriptExportsType::EsmExports(exports) = self.module.get_exports().await?.ty else { let module_exports = self.module.get_exports().await?;
let EcmascriptExportsType::EsmExports(exports) = &module_exports.ty else {
bail!("EcmascriptModuleLocalsModule must only be used on modules with EsmExports"); bail!("EcmascriptModuleLocalsModule must only be used on modules with EsmExports");
}; };
let esm_exports = exports.await?; let esm_exports = exports.await?;
...@@ -159,11 +160,11 @@ impl EcmascriptChunkPlaceable for EcmascriptModuleLocalsModule { ...@@ -159,11 +160,11 @@ impl EcmascriptChunkPlaceable for EcmascriptModuleLocalsModule {
let exports = EsmExports { let exports = EsmExports {
exports, exports,
star_exports: vec![], star_exports: vec![],
evaluation: esm_exports.evaluation,
} }
.resolved_cell(); .resolved_cell();
Ok(EcmascriptExports { Ok(EcmascriptExports {
ty: EcmascriptExportsType::EsmExports(exports), ty: EcmascriptExportsType::EsmExports(exports),
evaluation: module_exports.evaluation,
} }
.cell()) .cell())
} }
......
...@@ -28,6 +28,7 @@ use turbopack_ecmascript::{ ...@@ -28,6 +28,7 @@ use turbopack_ecmascript::{
EcmascriptChunkItem, EcmascriptChunkItemContent, EcmascriptChunkPlaceable, EcmascriptChunkItem, EcmascriptChunkItemContent, EcmascriptChunkPlaceable,
EcmascriptChunkType, EcmascriptExports, EcmascriptExportsType, EcmascriptChunkType, EcmascriptExports, EcmascriptExportsType,
}, },
references::esm::EsmEvaluation,
runtime_functions::TURBOPACK_EXPORT_VALUE, runtime_functions::TURBOPACK_EXPORT_VALUE,
}; };
...@@ -81,6 +82,7 @@ impl EcmascriptChunkPlaceable for JsonModuleAsset { ...@@ -81,6 +82,7 @@ impl EcmascriptChunkPlaceable for JsonModuleAsset {
fn get_exports(&self) -> Vc<EcmascriptExports> { fn get_exports(&self) -> Vc<EcmascriptExports> {
EcmascriptExports { EcmascriptExports {
ty: EcmascriptExportsType::Value, ty: EcmascriptExportsType::Value,
evaluation: EsmEvaluation::SideEffects,
} }
.cell() .cell()
} }
......
...@@ -15,6 +15,7 @@ use turbopack_ecmascript::{ ...@@ -15,6 +15,7 @@ use turbopack_ecmascript::{
EcmascriptChunkItem, EcmascriptChunkItemContent, EcmascriptChunkPlaceable, EcmascriptChunkItem, EcmascriptChunkItemContent, EcmascriptChunkPlaceable,
EcmascriptChunkType, EcmascriptExports, EcmascriptExportsType, EcmascriptChunkType, EcmascriptExports, EcmascriptExportsType,
}, },
references::esm::EsmEvaluation,
runtime_functions::TURBOPACK_EXPORT_VALUE, runtime_functions::TURBOPACK_EXPORT_VALUE,
utils::StringifyJs, utils::StringifyJs,
}; };
...@@ -88,6 +89,7 @@ impl EcmascriptChunkPlaceable for StaticUrlJsModule { ...@@ -88,6 +89,7 @@ impl EcmascriptChunkPlaceable for StaticUrlJsModule {
fn get_exports(&self) -> Vc<EcmascriptExports> { fn get_exports(&self) -> Vc<EcmascriptExports> {
EcmascriptExports { EcmascriptExports {
ty: EcmascriptExportsType::Value, ty: EcmascriptExportsType::Value,
evaluation: EsmEvaluation::SideEffects,
} }
.cell() .cell()
} }
......
...@@ -16,6 +16,7 @@ use turbopack_ecmascript::{ ...@@ -16,6 +16,7 @@ use turbopack_ecmascript::{
EcmascriptChunkItem, EcmascriptChunkItemContent, EcmascriptChunkPlaceable, EcmascriptChunkItem, EcmascriptChunkItemContent, EcmascriptChunkPlaceable,
EcmascriptChunkType, EcmascriptExports, EcmascriptExportsType, EcmascriptChunkType, EcmascriptExports, EcmascriptExportsType,
}, },
references::esm::EsmEvaluation,
runtime_functions::TURBOPACK_EXPORT_VALUE, runtime_functions::TURBOPACK_EXPORT_VALUE,
utils::StringifyJs, utils::StringifyJs,
}; };
...@@ -94,6 +95,7 @@ impl EcmascriptChunkPlaceable for RawWebAssemblyModuleAsset { ...@@ -94,6 +95,7 @@ impl EcmascriptChunkPlaceable for RawWebAssemblyModuleAsset {
fn get_exports(self: Vc<Self>) -> Vc<EcmascriptExports> { fn get_exports(self: Vc<Self>) -> Vc<EcmascriptExports> {
EcmascriptExports { EcmascriptExports {
ty: EcmascriptExportsType::Value, ty: EcmascriptExportsType::Value,
evaluation: EsmEvaluation::SideEffects,
} }
.cell() .cell()
} }
......
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