Skip to content

Commit

Permalink
Refactor ES chunk evaluate logic into a Runtime trait (#4141)
Browse files Browse the repository at this point in the history
### Description

This refactors the EcmaScript chunk evaluate segment logic into a
separate `EcmascriptChunkRuntime` trait.

`ChunkingContext` implementors can now optionally implement the
`EcmascriptChunkContext` trait (which already existed before as a
struct). This trait has two additional methods:
* `EcmascriptChunkContext::ecmascript_runtime()` -> Creates a ES runtime
that only registers chunks' contents
* `EcmascriptChunkContext::evaluated_ecmascript_runtime()` -> Creates an
ES runtime that registers chunks' contents and evaluates its entries

This is important because we are going to start having more than one
runtime evaluation logic with `next build` (and `ncc` and any future
bundler-specific work). In the future, this could even be used to have
non-Ecmascript runtimes.

Also fixes WEB-688

PR for the Next.js side: vercel/next.js#47116

### TODO

- [x] ~~I need to ensure that `waitForChunksToLoad` works as expected on
first load and does not re-create requests to JS and CSS chunks that are
already being loaded.~~

- [x] ~~Looks like there's a hashing problem (module ident perhaps?)
that creates additional snapshots.~~

### Testing Instructions

Snapshots + Integrations tests
  • Loading branch information
alexkirsz committed Mar 21, 2023
1 parent 5a19933 commit 549d314
Show file tree
Hide file tree
Showing 245 changed files with 17,169 additions and 12,281 deletions.
19 changes: 19 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ members = [
"crates/turbopack-core",
"crates/turbopack-create-test-app",
"crates/turbopack-css",
"crates/turbopack-dev",
"crates/turbopack-dev-server",
"crates/turbopack-ecmascript",
"crates/turbopack-env",
Expand Down Expand Up @@ -60,6 +61,7 @@ default-members = [
"crates/turbopack-core",
"crates/turbopack-create-test-app",
"crates/turbopack-css",
"crates/turbopack-dev",
"crates/turbopack-dev-server",
"crates/turbopack-ecmascript",
"crates/turbopack-env",
Expand Down Expand Up @@ -113,6 +115,7 @@ turbopack-cli-utils = { path = "crates/turbopack-cli-utils" }
turbopack-core = { path = "crates/turbopack-core" }
turbopack-create-test-app = { path = "crates/turbopack-create-test-app" }
turbopack-css = { path = "crates/turbopack-css" }
turbopack-dev = { path = "crates/turbopack-dev" }
turbopack-dev-server = { path = "crates/turbopack-dev-server" }
turbopack-ecmascript = { path = "crates/turbopack-ecmascript" }
turbopack-env = { path = "crates/turbopack-env" }
Expand Down
5 changes: 3 additions & 2 deletions crates/turbopack-core/src/chunk/list/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,9 @@ pub(super) async fn update_chunk_list(
let chunk_update = content.update(TraitRef::cell(from_version.clone())).await?;

match &*chunk_update {
// Getting a total or not found update from a merger is unexpected. If it happens,
// we have no better option than to short-circuit the update.
// Getting a total or not found update from a merger is unexpected. If it
// happens, we have no better option than to short-circuit
// the update.
Update::Total(_) => {
return Ok(Update::Total(TotalUpdate {
to: to_version.into(),
Expand Down
1 change: 0 additions & 1 deletion crates/turbopack-core/src/chunk/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
pub mod availability_info;
pub mod available_assets;
pub mod chunk_in_group;
pub mod dev;
pub(crate) mod list;
pub mod optimize;

Expand Down
13 changes: 13 additions & 0 deletions crates/turbopack-core/src/code_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ use std::{

use anyhow::Result;
use sourcemap::SourceMapBuilder;
use turbo_tasks::primitives::U64Vc;
use turbo_tasks_fs::rope::{Rope, RopeBuilder};
use turbo_tasks_hash::hash_xxh3_hash64;

use crate::{
source_map::{GenerateSourceMap, GenerateSourceMapVc, SourceMapSection, SourceMapVc},
Expand Down Expand Up @@ -176,6 +178,17 @@ impl GenerateSourceMap for Code {
}
}

#[turbo_tasks::value_impl]
impl CodeVc {
/// Returns the hash of the source code of this Code.
#[turbo_tasks::function]
pub async fn source_code_hash(self) -> Result<U64Vc> {
let code = self.await?;
let hash = hash_xxh3_hash64(code.source_code());
Ok(U64Vc::cell(hash))
}
}

/// A source map that contains no actual source location information (no
/// `sources`, no mappings that point into a source). This is used to tell
/// Chrome that the generated code starting at a particular offset is no longer
Expand Down
20 changes: 10 additions & 10 deletions crates/turbopack-css/src/module_asset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ use turbopack_ecmascript::{
chunk::{
EcmascriptChunkItem, EcmascriptChunkItemContent, EcmascriptChunkItemContentVc,
EcmascriptChunkItemVc, EcmascriptChunkPlaceable, EcmascriptChunkPlaceableVc,
EcmascriptChunkVc, EcmascriptExports, EcmascriptExportsVc,
EcmascriptChunkVc, EcmascriptChunkingContextVc, EcmascriptExports, EcmascriptExportsVc,
},
utils::stringify_js,
utils::StringifyJs,
ParseResultSourceMap, ParseResultSourceMapVc,
};

Expand Down Expand Up @@ -213,7 +213,7 @@ impl EcmascriptChunkPlaceable for ModuleCssModuleAsset {
#[turbo_tasks::function]
fn as_chunk_item(
self_vc: ModuleCssModuleAssetVc,
context: ChunkingContextVc,
context: EcmascriptChunkingContextVc,
) -> EcmascriptChunkItemVc {
ModuleChunkItem {
context,
Expand Down Expand Up @@ -245,7 +245,7 @@ impl ResolveOrigin for ModuleCssModuleAsset {
#[turbo_tasks::value]
struct ModuleChunkItem {
module: ModuleCssModuleAssetVc,
context: ChunkingContextVc,
context: EcmascriptChunkingContextVc,
}

#[turbo_tasks::value_impl]
Expand Down Expand Up @@ -277,7 +277,7 @@ impl ChunkItem for ModuleChunkItem {
#[turbo_tasks::value_impl]
impl EcmascriptChunkItem for ModuleChunkItem {
#[turbo_tasks::function]
fn chunking_context(&self) -> ChunkingContextVc {
fn chunking_context(&self) -> EcmascriptChunkingContextVc {
self.context
}

Expand Down Expand Up @@ -332,24 +332,24 @@ impl EcmascriptChunkItem for ModuleChunkItem {
unreachable!("ModuleCssModuleAsset implements EcmascriptChunkPlaceableVc");
};

let module_id =
stringify_js(&*placeable.as_chunk_item(self.context).id().await?);
let original_name = stringify_js(original_name);
let module_id = placeable.as_chunk_item(self.context).id().await?;
let module_id = StringifyJs(&*module_id);
let original_name = StringifyJs(original_name);
exported_class_names.push(format! {
"__turbopack_import__({module_id})[{original_name}]"
});
}
ModuleCssClass::Local { name: class_name }
| ModuleCssClass::Global { name: class_name } => {
exported_class_names.push(stringify_js(class_name));
exported_class_names.push(StringifyJs(class_name).to_string());
}
}
}

writeln!(
code,
" {}: {},",
stringify_js(export_name),
StringifyJs(export_name),
exported_class_names.join(" + \" \" + ")
)?;
}
Expand Down
27 changes: 27 additions & 0 deletions crates/turbopack-dev/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
[package]
name = "turbopack-dev"
version = "0.1.0"
description = "TBD"
license = "MPL-2.0"
edition = "2021"
autobenches = false

[lib]
bench = false

[dependencies]
anyhow = { workspace = true }
indexmap = { workspace = true }
indoc = { workspace = true }
serde = { workspace = true }
serde_json = { workspace = true }
serde_qs = { workspace = true }

turbo-tasks = { workspace = true }
turbo-tasks-fs = { workspace = true }
turbo-tasks-hash = { workspace = true }
turbopack-core = { workspace = true }
turbopack-ecmascript = { workspace = true }

[build-dependencies]
turbo-tasks-build = { workspace = true }
5 changes: 5 additions & 0 deletions crates/turbopack-dev/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
use turbo_tasks_build::generate_register;

fn main() {
generate_register();
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@vercel/turbopack-runtime",
"name": "@vercel/turbopack-dev-runtime",
"version": "0.0.0",
"description": "turbopack runtime",
"description": "Turbopack development runtime",
"license": "UNLICENSED",
"private": true,
"scripts": {
Expand Down

0 comments on commit 549d314

Please sign in to comment.