File size: 5,793 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 |
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 turbopack_core::{
asset::{Asset, AssetContent},
chunk::{
ChunkItem, ChunkType, ChunkableModule, ChunkableModuleReference, ChunkingContext,
EvaluatableAsset,
},
ident::AssetIdent,
module::Module,
module_graph::ModuleGraph,
reference::{ModuleReference, ModuleReferences},
resolve::ModuleResolveResult,
};
use turbopack_ecmascript::{
chunk::{
EcmascriptChunkItem, EcmascriptChunkItemContent, EcmascriptChunkItemOptions,
EcmascriptChunkPlaceable, EcmascriptChunkType, EcmascriptExports,
},
runtime_functions::TURBOPACK_REQUIRE,
utils::StringifyJs,
};
/// Each entry point in the HMR system has an ident with a different nested asset.
/// This produces the 'base' ident for the HMR entry point, which is then modified
#[turbo_tasks::function]
async fn hmr_entry_point_base_ident() -> Result<Vc<AssetIdent>> {
Ok(AssetIdent::from_path(
VirtualFileSystem::new_with_name(rcstr!("hmr-entry"))
.root()
.await?
.join("hmr-entry.js")?,
))
}
#[turbo_tasks::value(shared)]
pub struct HmrEntryModule {
pub ident: ResolvedVc<AssetIdent>,
pub module: ResolvedVc<Box<dyn ChunkableModule>>,
}
#[turbo_tasks::value_impl]
impl HmrEntryModule {
#[turbo_tasks::function]
pub fn new(
ident: ResolvedVc<AssetIdent>,
module: ResolvedVc<Box<dyn ChunkableModule>>,
) -> Vc<Self> {
Self { ident, module }.cell()
}
}
#[turbo_tasks::value_impl]
impl Module for HmrEntryModule {
#[turbo_tasks::function]
fn ident(&self) -> Vc<AssetIdent> {
hmr_entry_point_base_ident().with_asset(rcstr!("ENTRY"), *self.ident)
}
#[turbo_tasks::function]
async fn references(&self) -> Result<Vc<ModuleReferences>> {
Ok(Vc::cell(vec![ResolvedVc::upcast(
HmrEntryModuleReference::new(Vc::upcast(*self.module))
.to_resolved()
.await?,
)]))
}
}
#[turbo_tasks::value_impl]
impl ChunkableModule for HmrEntryModule {
#[turbo_tasks::function]
fn as_chunk_item(
self: ResolvedVc<Self>,
module_graph: ResolvedVc<ModuleGraph>,
chunking_context: ResolvedVc<Box<dyn ChunkingContext>>,
) -> Vc<Box<dyn ChunkItem>> {
Vc::upcast(
HmrEntryChunkItem {
module: self,
module_graph,
chunking_context,
}
.cell(),
)
}
}
#[turbo_tasks::value_impl]
impl Asset for HmrEntryModule {
#[turbo_tasks::function]
fn content(self: Vc<Self>) -> Vc<AssetContent> {
todo!("HmrEntryModule doesn't implement content()")
}
}
#[turbo_tasks::value_impl]
impl EcmascriptChunkPlaceable for HmrEntryModule {
#[turbo_tasks::function]
fn get_exports(self: Vc<Self>) -> Vc<EcmascriptExports> {
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]
impl EvaluatableAsset for HmrEntryModule {}
#[turbo_tasks::value]
pub struct HmrEntryModuleReference {
pub module: ResolvedVc<Box<dyn Module>>,
}
#[turbo_tasks::value_impl]
impl HmrEntryModuleReference {
#[turbo_tasks::function]
pub fn new(module: ResolvedVc<Box<dyn Module>>) -> Vc<Self> {
HmrEntryModuleReference { module }.cell()
}
}
#[turbo_tasks::value_impl]
impl ValueToString for HmrEntryModuleReference {
#[turbo_tasks::function]
fn to_string(&self) -> Vc<RcStr> {
Vc::cell(rcstr!("entry"))
}
}
#[turbo_tasks::value_impl]
impl ModuleReference for HmrEntryModuleReference {
#[turbo_tasks::function]
fn resolve_reference(&self) -> Vc<ModuleResolveResult> {
*ModuleResolveResult::module(self.module)
}
}
#[turbo_tasks::value_impl]
impl ChunkableModuleReference for HmrEntryModuleReference {}
/// The chunk item for [`HmrEntryModule`].
#[turbo_tasks::value]
struct HmrEntryChunkItem {
module: ResolvedVc<HmrEntryModule>,
module_graph: ResolvedVc<ModuleGraph>,
chunking_context: ResolvedVc<Box<dyn ChunkingContext>>,
}
#[turbo_tasks::value_impl]
impl ChunkItem for HmrEntryChunkItem {
#[turbo_tasks::function]
fn chunking_context(&self) -> Vc<Box<dyn ChunkingContext>> {
Vc::upcast(*self.chunking_context)
}
#[turbo_tasks::function]
fn asset_ident(&self) -> Vc<AssetIdent> {
self.module.ident()
}
#[turbo_tasks::function]
fn ty(&self) -> Vc<Box<dyn ChunkType>> {
Vc::upcast(Vc::<EcmascriptChunkType>::default())
}
#[turbo_tasks::function]
fn module(&self) -> Vc<Box<dyn Module>> {
Vc::upcast(*self.module)
}
}
#[turbo_tasks::value_impl]
impl EcmascriptChunkItem for HmrEntryChunkItem {
#[turbo_tasks::function]
async fn content(&self) -> Result<Vc<EcmascriptChunkItemContent>> {
let this = self.module.await?;
let module = this.module;
let chunk_item = module.as_chunk_item(*self.module_graph, *self.chunking_context);
let id = self.chunking_context.chunk_item_id(chunk_item).await?;
let mut code = RopeBuilder::default();
writeln!(code, "{TURBOPACK_REQUIRE}({});", StringifyJs(&id))?;
Ok(EcmascriptChunkItemContent {
inner_code: code.build(),
options: EcmascriptChunkItemOptions {
strict: true,
module: true,
..Default::default()
},
..Default::default()
}
.cell())
}
}
|