use anyhow::{Context, Result, bail}; use turbo_rcstr::rcstr; use turbo_tasks::{IntoTraitRef, ResolvedVc, Vc, fxindexmap}; use turbo_tasks_fs::FileSystemPath; use turbopack_core::{ asset::{Asset, AssetContent}, chunk::{ AsyncModuleInfo, ChunkItem, ChunkType, ChunkableModule, ChunkingContext, chunk_group::references_to_output_assets, }, context::AssetContext, ident::AssetIdent, module::{Module, OptionModule}, module_graph::ModuleGraph, output::OutputAssets, reference::{ModuleReferences, SingleChunkableModuleReference}, reference_type::ReferenceType, resolve::{ExportUsage, origin::ResolveOrigin, parse::Request}, source::Source, }; use turbopack_ecmascript::{ chunk::{ EcmascriptChunkItem, EcmascriptChunkItemContent, EcmascriptChunkPlaceable, EcmascriptChunkType, EcmascriptExports, }, references::async_module::OptionAsyncModule, }; use crate::{ loader::{compiling_loader_source, instantiating_loader_source}, output_asset::WebAssemblyAsset, raw::RawWebAssemblyModuleAsset, source::WebAssemblySource, }; /// Creates a javascript loader which instantiates the WebAssembly source and /// re-exports its exports. #[turbo_tasks::value] #[derive(Clone)] pub struct WebAssemblyModuleAsset { source: ResolvedVc, asset_context: ResolvedVc>, } #[turbo_tasks::value_impl] impl WebAssemblyModuleAsset { #[turbo_tasks::function] pub fn new( source: ResolvedVc, asset_context: ResolvedVc>, ) -> Vc { Self::cell(WebAssemblyModuleAsset { source, asset_context, }) } #[turbo_tasks::function] fn wasm_asset(&self, chunking_context: Vc>) -> Vc { WebAssemblyAsset::new(*self.source, chunking_context) } #[turbo_tasks::function] async fn loader_as_module(self: Vc) -> Result>> { let this = self.await?; let query = &this.source.ident().await?.query; let loader_source = if query == "?module" { compiling_loader_source(*this.source) } else { instantiating_loader_source(*this.source) }; let module = this.asset_context.process( loader_source, ReferenceType::Internal(ResolvedVc::cell(fxindexmap! { rcstr!("WASM_PATH") => ResolvedVc::upcast(RawWebAssemblyModuleAsset::new(*this.source, *this.asset_context).to_resolved().await?), })), ).module(); Ok(module) } #[turbo_tasks::function] async fn loader_as_resolve_origin(self: Vc) -> Result>> { let module = self.loader_as_module(); let Some(esm_asset) = Vc::try_resolve_sidecast::>(module).await? else { bail!("WASM loader was not processed into an EcmascriptModuleAsset"); }; Ok(esm_asset) } #[turbo_tasks::function] async fn loader(self: Vc) -> Result>> { let module = self.loader_as_module(); let Some(esm_asset) = Vc::try_resolve_sidecast::>(module).await? else { bail!("WASM loader was not processed into an EcmascriptModuleAsset"); }; Ok(esm_asset) } #[turbo_tasks::function] async fn references(self: Vc) -> Result> { Ok(Vc::cell(vec![ResolvedVc::upcast( SingleChunkableModuleReference::new( Vc::upcast(self.loader()), rcstr!("wasm loader"), ExportUsage::all(), ) .to_resolved() .await?, )])) } } #[turbo_tasks::value_impl] impl Module for WebAssemblyModuleAsset { #[turbo_tasks::function] async fn ident(&self) -> Result> { Ok(self .source .ident() .with_modifier(rcstr!("wasm module")) .with_layer(self.asset_context.into_trait_ref().await?.layer())) } #[turbo_tasks::function] fn references(self: Vc) -> Vc { self.loader().references() } #[turbo_tasks::function] fn is_self_async(self: Vc) -> Vc { Vc::cell(true) } } #[turbo_tasks::value_impl] impl Asset for WebAssemblyModuleAsset { #[turbo_tasks::function] fn content(&self) -> Vc { self.source.content() } } #[turbo_tasks::value_impl] impl ChunkableModule for WebAssemblyModuleAsset { #[turbo_tasks::function] fn as_chunk_item( self: ResolvedVc, module_graph: ResolvedVc, chunking_context: ResolvedVc>, ) -> Vc> { Vc::upcast( ModuleChunkItem { module: self, module_graph, chunking_context, } .cell(), ) } } #[turbo_tasks::value_impl] impl EcmascriptChunkPlaceable for WebAssemblyModuleAsset { #[turbo_tasks::function] fn get_exports(self: Vc) -> Vc { self.loader().get_exports() } #[turbo_tasks::function] fn get_async_module(self: Vc) -> Vc { self.loader().get_async_module() } } #[turbo_tasks::value_impl] impl ResolveOrigin for WebAssemblyModuleAsset { #[turbo_tasks::function] fn origin_path(&self) -> Vc { self.source.ident().path() } #[turbo_tasks::function] fn asset_context(&self) -> Vc> { *self.asset_context } #[turbo_tasks::function] fn get_inner_asset(self: Vc, request: Vc) -> Vc { self.loader_as_resolve_origin().get_inner_asset(request) } } #[turbo_tasks::value] struct ModuleChunkItem { module: ResolvedVc, chunking_context: ResolvedVc>, module_graph: ResolvedVc, } #[turbo_tasks::value_impl] impl ChunkItem for ModuleChunkItem { #[turbo_tasks::function] fn asset_ident(&self) -> Vc { self.module.ident() } #[turbo_tasks::function] async fn references(&self) -> Result> { let loader_references = self.module.loader().references().await?; references_to_output_assets(&*loader_references).await } #[turbo_tasks::function] fn chunking_context(&self) -> Vc> { Vc::upcast(*self.chunking_context) } #[turbo_tasks::function] async fn ty(&self) -> Result>> { Ok(Vc::upcast( Vc::::default().resolve().await?, )) } #[turbo_tasks::function] fn module(&self) -> Vc> { Vc::upcast(*self.module) } } #[turbo_tasks::value_impl] impl EcmascriptChunkItem for ModuleChunkItem { #[turbo_tasks::function] fn content(self: Vc) -> Vc { panic!("content() should not be called"); } #[turbo_tasks::function] async fn content_with_async_module_info( &self, async_module_info: Option>, ) -> Result> { let loader_asset = self.module.loader(); let item = loader_asset.as_chunk_item(*self.module_graph, Vc::upcast(*self.chunking_context)); let ecmascript_item = Vc::try_resolve_downcast::>(item) .await? .context("EcmascriptModuleAsset must implement EcmascriptChunkItem")?; let chunk_item_content = ecmascript_item .content_with_async_module_info(async_module_info) .owned() .await?; Ok(chunk_item_content.cell()) } }