Skip to content

Commit

Permalink
Refactor ES chunk evaluate logic into a Runtime trait (vercel#47116)
Browse files Browse the repository at this point in the history
This is the Next.js side of vercel/turborepo#4141
  • Loading branch information
alexkirsz authored Mar 22, 2023
1 parent f029023 commit 8195e19
Show file tree
Hide file tree
Showing 31 changed files with 385 additions and 340 deletions.
420 changes: 215 additions & 205 deletions packages/next-swc/Cargo.lock

Large diffs are not rendered by default.

61 changes: 31 additions & 30 deletions packages/next-swc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,36 +46,37 @@ swc_emotion = { version = "0.29.10" }
testing = { version = "0.31.31" }

# Turbo crates
auto-hash-map = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-230321.1" }
node-file-trace = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-230321.1" }
swc-ast-explorer = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-230321.1" }
turbo-malloc = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-230321.1", default-features = false }
turbo-tasks = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-230321.1" }
turbo-tasks-build = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-230321.1" }
turbo-tasks-env = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-230321.1" }
turbo-tasks-fetch = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-230321.1", default-features = false }
turbo-tasks-fs = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-230321.1" }
turbo-tasks-hash = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-230321.1" }
turbo-tasks-macros = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-230321.1" }
turbo-tasks-macros-shared = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-230321.1" }
turbo-tasks-memory = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-230321.1" }
turbo-tasks-testing = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-230321.1" }
turbo-updater = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-230321.1" }
turbopack = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-230321.1" }
turbopack-cli-utils = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-230321.1" }
turbopack-core = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-230321.1" }
turbopack-create-test-app = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-230321.1" }
turbopack-css = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-230321.1" }
turbopack-dev-server = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-230321.1" }
turbopack-ecmascript = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-230321.1" }
turbopack-env = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-230321.1" }
turbopack-json = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-230321.1" }
turbopack-mdx = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-230321.1" }
turbopack-node = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-230321.1" }
turbopack-static = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-230321.1" }
turbopack-swc-utils = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-230321.1" }
turbopack-test-utils = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-230321.1" }
turbopack-tests = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-230321.1" }
auto-hash-map = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-230322.2" }
node-file-trace = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-230322.2" }
swc-ast-explorer = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-230322.2" }
turbo-malloc = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-230322.2", default-features = false }
turbo-tasks = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-230322.2" }
turbo-tasks-build = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-230322.2" }
turbo-tasks-env = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-230322.2" }
turbo-tasks-fetch = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-230322.2", default-features = false }
turbo-tasks-fs = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-230322.2" }
turbo-tasks-hash = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-230322.2" }
turbo-tasks-macros = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-230322.2" }
turbo-tasks-macros-shared = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-230322.2" }
turbo-tasks-memory = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-230322.2" }
turbo-tasks-testing = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-230322.2" }
turbo-updater = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-230322.2" }
turbopack = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-230322.2" }
turbopack-cli-utils = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-230322.2" }
turbopack-core = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-230322.2" }
turbopack-create-test-app = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-230322.2" }
turbopack-css = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-230322.2" }
turbopack-dev = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-230322.2" }
turbopack-dev-server = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-230322.2" }
turbopack-ecmascript = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-230322.2" }
turbopack-env = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-230322.2" }
turbopack-json = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-230322.2" }
turbopack-mdx = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-230322.2" }
turbopack-node = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-230322.2" }
turbopack-static = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-230322.2" }
turbopack-swc-utils = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-230322.2" }
turbopack-test-utils = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-230322.2" }
turbopack-tests = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-230322.2" }

# General Deps

Expand Down
1 change: 1 addition & 0 deletions packages/next-swc/crates/next-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ turbo-tasks-fs = { workspace = true }
turbo-tasks-hash = { workspace = true }
turbopack = { workspace = true }
turbopack-core = { workspace = true }
turbopack-dev = { workspace = true }
turbopack-dev-server = { workspace = true }
turbopack-ecmascript = { workspace = true }
turbopack-env = { workspace = true }
Expand Down
2 changes: 1 addition & 1 deletion packages/next-swc/crates/next-core/js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"build:compiled": "node build.mjs"
},
"dependencies": {
"@vercel/turbopack-runtime": "https://gitpkg.now.sh/vercel/turbo/crates/turbopack-ecmascript/js?8a8038f94",
"@vercel/turbopack-dev-runtime": "https://gitpkg.now.sh/vercel/turbo/crates/turbopack-dev/js?turbopack-230321.3",
"anser": "^2.1.1",
"css.escape": "^1.5.1",
"next": "*",
Expand Down
4 changes: 2 additions & 2 deletions packages/next-swc/crates/next-core/js/src/dev/hmr-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ import type {
Issue,
ResourceIdentifier,
ServerMessage,
} from "@vercel/turbopack-runtime/types/protocol";
} from "@vercel/turbopack-dev-runtime/types/protocol";
import type {
ChunkPath,
ModuleId,
UpdateCallback,
TurbopackGlobals,
} from "@vercel/turbopack-runtime/types";
} from "@vercel/turbopack-dev-runtime/types";

import stripAnsi from "@vercel/turbopack-next/compiled/strip-ansi";

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Issue } from "@vercel/turbopack-runtime/types/protocol";
import type { Issue } from "@vercel/turbopack-dev-runtime/types/protocol";

import * as Bus from "./internal/bus";
import { parseStack } from "./internal/helpers/parseStack";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as React from "react";

import type { Issue } from "@vercel/turbopack-runtime/types/protocol";
import type { Issue } from "@vercel/turbopack-dev-runtime/types/protocol";

import * as Bus from "./bus";
import { ShadowPortal } from "./components/ShadowPortal";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { StackFrame } from "stacktrace-parser";

import type { Issue } from "@vercel/turbopack-runtime/types/protocol";
import type { Issue } from "@vercel/turbopack-dev-runtime/types/protocol";

export const TYPE_BUILD_OK = "build-ok";
export const TYPE_TURBOPACK_ISSUES = "turbopack-error";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as React from 'react'

import { Issue } from '@vercel/turbopack-runtime/types/protocol'
import { Issue } from '@vercel/turbopack-dev-runtime/types/protocol'

import {
TYPE_UNHANDLED_ERROR,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Issue } from '@vercel/turbopack-runtime/types/protocol'
import { Issue } from '@vercel/turbopack-dev-runtime/types/protocol'

import { LeftRightDialogHeader } from '../components/LeftRightDialogHeader'
import { DialogBody, DialogBodyProps } from '../components/Dialog'
Expand Down
2 changes: 1 addition & 1 deletion packages/next-swc/crates/next-core/js/types/next.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import { ChunkPath } from "@vercel/turbopack-runtime/types";
import { ChunkPath } from "@vercel/turbopack-dev-runtime/types";

export type ChunkGroup = ChunkPath[];
13 changes: 6 additions & 7 deletions packages/next-swc/crates/next-core/src/app_source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,22 @@ use turbopack::{
ModuleAssetContextVc,
};
use turbopack_core::{
chunk::dev::DevChunkingContextVc,
compile_time_info::CompileTimeInfoVc,
context::{AssetContext, AssetContextVc},
environment::{EnvironmentIntention, ServerAddrVc},
reference_type::{EntryReferenceSubType, ReferenceType},
source_asset::SourceAssetVc,
virtual_asset::VirtualAssetVc,
};
use turbopack_dev::DevChunkingContextVc;
use turbopack_dev_server::{
html::DevHtmlAssetVc,
source::{
combined::CombinedContentSource, ContentSourceData, ContentSourceVc, NoContentSourceVc,
},
};
use turbopack_ecmascript::{
chunk::EcmascriptChunkPlaceablesVc, magic_identifier, utils::stringify_js,
chunk::EcmascriptChunkPlaceablesVc, magic_identifier, utils::StringifyJs,
EcmascriptInputTransformsVc, EcmascriptModuleAssetType, EcmascriptModuleAssetVc, InnerAssetsVc,
};
use turbopack_env::ProcessEnvAssetVc;
Expand Down Expand Up @@ -107,7 +107,6 @@ async fn next_client_transition(

Ok(NextClientTransition {
is_app: true,
server_root,
client_chunking_context,
client_module_options_context,
client_resolve_options_context,
Expand Down Expand Up @@ -575,7 +574,7 @@ impl AppRendererVc {
));
}
}
Ok((stringify_js(segment_path), imports))
Ok((StringifyJs(segment_path).to_string(), imports))
});
futures
})
Expand All @@ -600,7 +599,7 @@ impl AppRendererVc {
"import {}, {{ chunks as {} }} from {};\n",
identifier,
chunks_identifier,
stringify_js(p)
StringifyJs(p)
)?
}
}
Expand All @@ -610,7 +609,7 @@ impl AppRendererVc {
r#"("TURBOPACK {{ transition: next-client }}");
import BOOTSTRAP from {};
"#,
stringify_js(&page)
StringifyJs(&page)
)?;
}

Expand All @@ -621,7 +620,7 @@ import BOOTSTRAP from {};
writeln!(
result,
" {key}: {{ module: {identifier}, chunks: {chunks_identifier} }},",
key = stringify_js(key),
key = StringifyJs(key),
)?;
}
result += " },";
Expand Down
1 change: 1 addition & 0 deletions packages/next-swc/crates/next-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ pub fn register() {
turbo_tasks::register();
turbo_tasks_fs::register();
turbo_tasks_fetch::register();
turbopack_dev::register();
turbopack_dev_server::register();
turbopack_node::register();
turbopack::register();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,14 @@ use turbopack::{
ModuleAssetContextVc,
};
use turbopack_core::{
chunk::{dev::DevChunkingContextVc, ChunkingContextVc},
chunk::ChunkingContextVc,
compile_time_defines,
compile_time_info::{CompileTimeDefinesVc, CompileTimeInfo, CompileTimeInfoVc},
context::AssetContextVc,
environment::{BrowserEnvironment, EnvironmentIntention, EnvironmentVc, ExecutionEnvironment},
resolve::{parse::RequestVc, pattern::Pattern},
};
use turbopack_dev::DevChunkingContextVc;
use turbopack_env::ProcessEnvAssetVc;
use turbopack_node::execution_context::ExecutionContextVc;

Expand Down
11 changes: 6 additions & 5 deletions packages/next-swc/crates/next-core/src/next_client/transition.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use anyhow::Result;
use indexmap::indexmap;
use turbo_tasks::{primitives::OptionStringVc, Value};
use turbo_tasks_fs::FileSystemPathVc;
use turbopack::{
ecmascript::chunk_group_files_asset::ChunkGroupFilesAsset,
module_options::ModuleOptionsContextVc,
Expand All @@ -10,7 +9,9 @@ use turbopack::{
ModuleAssetContextVc,
};
use turbopack_core::{
asset::AssetVc, chunk::ChunkingContextVc, compile_time_info::CompileTimeInfoVc,
asset::AssetVc,
chunk::{ChunkingContext, ChunkingContextVc},
compile_time_info::CompileTimeInfoVc,
context::AssetContext,
};
use turbopack_ecmascript::{
Expand All @@ -33,7 +34,6 @@ pub struct NextClientTransition {
pub client_module_options_context: ModuleOptionsContextVc,
pub client_resolve_options_context: ResolveOptionsContextVc,
pub client_chunking_context: ChunkingContextVc,
pub server_root: FileSystemPathVc,
pub runtime_entries: RuntimeEntriesVc,
}

Expand Down Expand Up @@ -99,9 +99,10 @@ impl Transition for NextClientTransition {

let asset = ChunkGroupFilesAsset {
asset: asset.into(),
// This ensures that the chunk group files asset will strip out the _next prefix from
// all chunk paths, which is what the Next.js renderer code expects.
client_root: self.client_chunking_context.output_root().join("_next"),
chunking_context: self.client_chunking_context,
base_path: self.server_root.join("_next"),
server_root: self.server_root,
runtime_entries: Some(runtime_entries),
};

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use anyhow::Result;
use anyhow::{bail, Result};
use turbo_tasks::{primitives::StringVc, Value};
use turbopack::ecmascript::chunk::{
EcmascriptChunkItemVc, EcmascriptChunkPlaceable, EcmascriptChunkPlaceableVc, EcmascriptChunkVc,
Expand All @@ -13,6 +13,7 @@ use turbopack_core::{
ident::AssetIdentVc,
reference::AssetReferencesVc,
};
use turbopack_ecmascript::chunk::EcmascriptChunkingContextVc;

#[turbo_tasks::function]
fn modifier() -> StringVc {
Expand Down Expand Up @@ -58,8 +59,14 @@ impl ChunkableAsset for InChunkingContextAsset {
#[turbo_tasks::value_impl]
impl EcmascriptChunkPlaceable for InChunkingContextAsset {
#[turbo_tasks::function]
fn as_chunk_item(&self, _context: ChunkingContextVc) -> EcmascriptChunkItemVc {
self.asset.as_chunk_item(self.chunking_context)
async fn as_chunk_item(
&self,
_context: EcmascriptChunkingContextVc,
) -> Result<EcmascriptChunkItemVc> {
let Some(chunking_context) = EcmascriptChunkingContextVc::resolve_from(&self.chunking_context).await? else {
bail!("chunking context is not an EcmascriptChunkingContext")
};
Ok(self.asset.as_chunk_item(chunking_context))
}

#[turbo_tasks::function]
Expand Down
Loading

0 comments on commit 8195e19

Please sign in to comment.