Skip to content

Commit

Permalink
refactor internal modules (vercel/turborepo#4018)
Browse files Browse the repository at this point in the history
### Description

* get rid of attached filesystem for our embedded modules
* get rid of import "." in favor of inner assets

depends on vercel/turborepo#3999

### Testing Instructions

existing tests

<!--
  When the below is checked (default) our PR bot will automatically
  assign labels to your PR based on the content to help the team
  organize and review it faster.
-->

[X] Auto label
  • Loading branch information
sokra authored Mar 2, 2023
1 parent ec99792 commit 9ed4166
Show file tree
Hide file tree
Showing 66 changed files with 609 additions and 545 deletions.
3 changes: 2 additions & 1 deletion crates/next-core/js/src/entry/app/layout-entry.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ import * as serverHooks from "next/dist/client/components/hooks-server-context.j
export { serverHooks };
export { renderToReadableStream } from "next/dist/compiled/react-server-dom-webpack/server.edge";

import * as module from ".";
// @ts-expect-error PAGE is provided by rust
import * as module from "PAGE";
export { module };
9 changes: 5 additions & 4 deletions crates/next-core/js/src/entry/app/server-to-client-ssr.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { createProxy } from "next/dist/build/webpack/loaders/next-flight-loader/module-proxy";

("TURBOPACK { transition: next-ssr-client-module; chunking-type: parallel }");
import { __turbopack_module_id__ as id } from ".";
("TURBOPACK { chunking-type: parallel }");
// @ts-expect-error CLIENT_MODULE is provided by rust
import { __turbopack_module_id__ as id } from "CLIENT_MODULE";

("TURBOPACK { transition: next-client-chunks }");
import client_id, { chunks } from ".";
// @ts-expect-error CLIENT_CHUNKS is provided by rust
import client_id, { chunks } from "CLIENT_CHUNKS";

export default createProxy(JSON.stringify([client_id, chunks, id]));
4 changes: 2 additions & 2 deletions crates/next-core/js/src/entry/app/server-to-client.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { createProxy } from "next/dist/build/webpack/loaders/next-flight-loader/module-proxy";

("TURBOPACK { transition: next-client-chunks }");
import client_id, { chunks } from ".";
// @ts-expect-error CLIENT_CHUNKS is provided by rust
import client_id, { chunks } from "CLIENT_CHUNKS";

export default createProxy(JSON.stringify([client_id, chunks]));
2 changes: 1 addition & 1 deletion crates/next-core/js/src/entry/config/next.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import loadConfig from "next/dist/server/config";
import loadCustomRoutes from "next/dist/lib/load-custom-routes";
import { PHASE_DEVELOPMENT_SERVER } from "next/dist/shared/lib/constants";
import assert from "assert";
import assert from "node:assert";

const loadNextConfig = async (silent) => {
const nextConfig = await loadConfig(
Expand Down
3 changes: 2 additions & 1 deletion crates/next-core/js/src/entry/next-hydrate.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ import {
} from "@vercel/turbopack-next/dev/hmr-client";

import * as _app from "@vercel/turbopack-next/pages/_app";
import * as page from ".";
// @ts-expect-error PAGE is provided by rust
import * as page from "PAGE";

async function loadPageChunk(assetPrefix: string, chunkPath: string) {
const fullPath = assetPrefix + chunkPath;
Expand Down
2 changes: 1 addition & 1 deletion crates/next-core/js/src/entry/page-loader.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as page from ".";
import * as page from "PAGE";

// inserted by rust code
declare const PAGE_PATH: string;
Expand Down
4 changes: 2 additions & 2 deletions crates/next-core/js/src/entry/server-api.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import startHandler from "@vercel/turbopack-next/internal/api-server-handler";

import "next/dist/server/node-polyfill-fetch.js";

import * as allExports from ".";
// @ts-expect-error INNER is provided by rust
import * as allExports from "INNER";
import { apiResolver } from "next/dist/server/api-utils/node";
import { IncomingMessage, ServerResponse } from "node:http";
import {
NodeNextRequest,
NodeNextResponse,
Expand Down
3 changes: 2 additions & 1 deletion crates/next-core/js/src/entry/server-data.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import startHandler from "@vercel/turbopack-next/internal/page-server-handler";
// eslint-disable-next-line
import Document from "next/document";
import App from "next/app";
import * as otherExports from ".";
// @ts-expect-error INNER is provided by rust
import * as otherExports from "INNER";

startHandler({
isDataReq: true,
Expand Down
4 changes: 2 additions & 2 deletions crates/next-core/js/src/entry/server-edge-api.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import { join } from "path";

import "next/dist/server/node-polyfill-fetch.js";

("TURBOPACK { transition: next-edge }");
import chunkGroup from ".";
// @ts-expect-error INNER_EDGE_CHUNK_GROUP is provided by rust
import chunkGroup from "INNER_EDGE_CHUNK_GROUP";

import {
NodeNextRequest,
Expand Down
7 changes: 4 additions & 3 deletions crates/next-core/js/src/entry/server-renderer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ import startHandler from "@vercel/turbopack-next/internal/page-server-handler";
import App from "@vercel/turbopack-next/pages/_app";
import Document from "@vercel/turbopack-next/pages/_document";

import Component, * as otherExports from ".";
// @ts-expect-error INNER is provided by rust
import Component, * as otherExports from "INNER";

("TURBOPACK { transition: next-client }");
import chunkGroup from ".";
// @ts-expect-error INNER_CLIENT_CHUNK_GROUP is provided by rust
import chunkGroup from "INNER_CLIENT_CHUNK_GROUP";

startHandler({
isDataReq: false,
Expand Down
60 changes: 31 additions & 29 deletions crates/next-core/src/app_render/next_layout_entry_transition.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use anyhow::Result;
use indexmap::indexmap;
use turbo_tasks::Value;
use turbo_tasks_fs::FileSystemPathVc;
use turbopack::{
self,
Expand All @@ -7,15 +9,14 @@ use turbopack::{
transition::{Transition, TransitionVc},
ModuleAssetContextVc,
};
use turbopack_core::{
asset::{Asset, AssetVc},
compile_time_info::CompileTimeInfoVc,
virtual_asset::VirtualAssetVc,
use turbopack_core::{asset::AssetVc, compile_time_info::CompileTimeInfoVc, context::AssetContext};
use turbopack_ecmascript::{
EcmascriptInputTransform, EcmascriptInputTransformsVc, EcmascriptModuleAssetType,
EcmascriptModuleAssetVc, InnerAssetsVc,
};
use turbopack_ecmascript::chunk::EcmascriptChunkPlaceableVc;

use crate::{
embed_js::next_js_file, next_client_component::with_client_chunks::WithClientChunksAsset,
embed_js::next_asset, next_client_component::with_client_chunks::WithClientChunksAsset,
};

#[turbo_tasks::value(shared)]
Expand All @@ -28,15 +29,6 @@ pub struct NextLayoutEntryTransition {

#[turbo_tasks::value_impl]
impl Transition for NextLayoutEntryTransition {
#[turbo_tasks::function]
fn process_source(&self, asset: AssetVc) -> AssetVc {
VirtualAssetVc::new(
asset.ident().path().join("layout-entry.tsx"),
next_js_file("entry/app/layout-entry.tsx").into(),
)
.into()
}

#[turbo_tasks::function]
fn process_compile_time_info(
&self,
Expand Down Expand Up @@ -65,20 +57,30 @@ impl Transition for NextLayoutEntryTransition {
async fn process_module(
&self,
asset: AssetVc,
_context: ModuleAssetContextVc,
context: ModuleAssetContextVc,
) -> Result<AssetVc> {
Ok(
if let Some(placeable) = EcmascriptChunkPlaceableVc::resolve_from(asset).await? {
WithClientChunksAsset {
asset: placeable,
// next.js code already adds _next prefix
server_root: self.server_root.join("_next"),
}
.cell()
.into()
} else {
asset
},
)
let internal_asset = next_asset("entry/app/layout-entry.tsx");

let asset = EcmascriptModuleAssetVc::new_with_inner_assets(
internal_asset,
context.into(),
Value::new(EcmascriptModuleAssetType::Typescript),
EcmascriptInputTransformsVc::cell(vec![
EcmascriptInputTransform::TypeScript,
EcmascriptInputTransform::React { refresh: false },
]),
context.compile_time_info(),
InnerAssetsVc::cell(indexmap! {
"PAGE".to_string() => asset
}),
);

Ok(WithClientChunksAsset {
asset: asset.into(),
// next.js code already adds _next prefix
server_root: self.server_root.join("_next"),
}
.cell()
.into())
}
}
15 changes: 8 additions & 7 deletions crates/next-core/src/app_source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::{
};

use anyhow::{anyhow, Result};
use indexmap::IndexMap;
use indexmap::indexmap;
use turbo_tasks::{TryJoinIterExt, Value, ValueToString};
use turbo_tasks_env::ProcessEnvVc;
use turbo_tasks_fs::{rebase, rope::RopeBuilder, File, FileContent, FileSystemPathVc};
Expand Down Expand Up @@ -46,7 +46,7 @@ use crate::{
next_layout_entry_transition::NextLayoutEntryTransition, LayoutSegment, LayoutSegmentsVc,
},
app_structure::{AppStructure, AppStructureItem, AppStructureVc, OptionAppStructureVc},
embed_js::{next_js_file, wrap_with_next_js_fs},
embed_js::next_js_file,
env::env_for_js,
fallback::get_fallback_page,
next_client::{
Expand Down Expand Up @@ -314,8 +314,6 @@ pub async fn create_app_source(
next_config: NextConfigVc,
server_addr: ServerAddrVc,
) -> Result<ContentSourceVc> {
let project_path = wrap_with_next_js_fs(project_path);

let Some(app_structure) = *app_structure.await? else {
return Ok(NoContentSourceVc::new().into());
};
Expand Down Expand Up @@ -363,7 +361,7 @@ pub async fn create_app_source(
next_config,
);

Ok(create_app_source_for_directory(
let source = create_app_source_for_directory(
app_structure,
context_ssr,
context,
Expand All @@ -372,7 +370,8 @@ pub async fn create_app_source(
EcmascriptChunkPlaceablesVc::cell(server_runtime_entries),
fallback_page,
output_path,
))
);
Ok(source)
}

#[allow(clippy::too_many_arguments)]
Expand Down Expand Up @@ -717,7 +716,9 @@ impl AppRouteVc {
Value::new(EcmascriptModuleAssetType::Typescript),
EcmascriptInputTransformsVc::cell(vec![EcmascriptInputTransform::TypeScript]),
this.context.compile_time_info(),
InnerAssetsVc::cell(IndexMap::from([("ROUTE_CHUNK_GROUP".to_string(), entry)])),
InnerAssetsVc::cell(indexmap! {
"ROUTE_CHUNK_GROUP".to_string() => entry
}),
),
chunking_context,
intermediate_output_path: this.intermediate_output_path,
Expand Down
3 changes: 0 additions & 3 deletions crates/next-core/src/app_structure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ use turbopack_dev_server::source::specificity::SpecificityVc;

use crate::{
app_render::{LayoutSegment, LayoutSegmentVc, LayoutSegmentsVc},
embed_js::wrap_with_next_js_fs,
next_config::NextConfigVc,
};

Expand Down Expand Up @@ -101,8 +100,6 @@ pub async fn find_app_structure(
server_root: FileSystemPathVc,
next_config: NextConfigVc,
) -> Result<OptionAppStructureVc> {
let project_path = wrap_with_next_js_fs(project_path);

if !*next_config.app_dir().await? {
return Ok(OptionAppStructureVc::cell(None));
}
Expand Down
8 changes: 4 additions & 4 deletions crates/next-core/src/babel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,16 +117,16 @@ pub async fn maybe_add_babel_loader(
}

#[turbo_tasks::function]
pub async fn is_babel_loader_available(project_root: FileSystemPathVc) -> Result<BoolVc> {
pub async fn is_babel_loader_available(project_path: FileSystemPathVc) -> Result<BoolVc> {
let result = resolve(
project_root,
project_path,
RequestVc::parse(Value::new(Pattern::Constant(
"babel-loader/package.json".to_string(),
))),
resolve_options(
project_root,
project_path,
ResolveOptionsContext {
enable_node_modules: true,
enable_node_modules: Some(project_path.root().resolve().await?),
enable_node_native_modules: true,
custom_conditions: vec!["development".to_string()],
..Default::default()
Expand Down
28 changes: 4 additions & 24 deletions crates/next-core/src/embed_js.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
use anyhow::Result;
use turbo_tasks_fs::{
attach::AttachedFileSystemVc, embed_directory, FileContentVc, FileSystem, FileSystemPathVc,
FileSystemVc,
};
use turbopack_core::{asset::AssetVc, virtual_asset::VirtualAssetVc};
use turbo_tasks_fs::{embed_directory, FileContentVc, FileSystem, FileSystemPathVc, FileSystemVc};
use turbopack_core::{asset::AssetVc, source_asset::SourceAssetVc};

pub const VIRTUAL_PACKAGE_NAME: &str = "@vercel/turbopack-next";

Expand All @@ -23,22 +19,6 @@ pub(crate) fn next_js_file_path(path: &str) -> FileSystemPathVc {
}

#[turbo_tasks::function]
pub(crate) async fn attached_next_js_package_path(
project_path: FileSystemPathVc,
) -> FileSystemPathVc {
project_path.join(&format!("[embedded_modules]/{}", VIRTUAL_PACKAGE_NAME))
}

#[turbo_tasks::function]
pub(crate) async fn wrap_with_next_js_fs(
project_path: FileSystemPathVc,
) -> Result<FileSystemPathVc> {
let attached_path = attached_next_js_package_path(project_path);
let fs = AttachedFileSystemVc::new(attached_path, next_js_fs());
Ok(fs.convert_path(project_path))
}

#[turbo_tasks::function]
pub(crate) fn next_asset(asset_path: FileSystemPathVc, path: &str) -> AssetVc {
VirtualAssetVc::new(asset_path, next_js_file(path).into()).into()
pub(crate) fn next_asset(path: &str) -> AssetVc {
SourceAssetVc::new(next_js_file_path(path)).into()
}
8 changes: 4 additions & 4 deletions crates/next-core/src/next_client/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::collections::HashMap;
use anyhow::Result;
use turbo_tasks::{primitives::StringVc, Value};
use turbo_tasks_env::ProcessEnvVc;
use turbo_tasks_fs::FileSystemPathVc;
use turbo_tasks_fs::{FileSystem, FileSystemPathVc};
use turbopack::{
module_options::{
module_options_context::{ModuleOptionsContext, ModuleOptionsContextVc},
Expand All @@ -28,7 +28,7 @@ use turbopack_node::execution_context::ExecutionContextVc;
use super::transforms::get_next_client_transforms_rules;
use crate::{
babel::maybe_add_babel_loader,
embed_js::attached_next_js_package_path,
embed_js::next_js_fs,
env::env_for_js,
next_build::{get_external_next_compiled_package_mapping, get_postcss_package_mapping},
next_client::runtime_entry::{RuntimeEntriesVc, RuntimeEntry},
Expand Down Expand Up @@ -89,7 +89,7 @@ pub async fn get_client_resolve_options_context(
let next_client_fallback_import_map = get_next_client_fallback_import_map(ty);
let next_client_resolved_map = get_next_client_resolved_map(project_path, project_path);
let module_options_context = ResolveOptionsContext {
enable_node_modules: true,
enable_node_modules: Some(project_path.root().resolve().await?),
custom_conditions: vec!["development".to_string()],
import_map: Some(next_client_import_map),
fallback_import_map: Some(next_client_fallback_import_map),
Expand Down Expand Up @@ -268,7 +268,7 @@ pub async fn get_client_runtime_entries(
RequestVc::parse(Value::new(Pattern::Constant(
"./dev/bootstrap.ts".to_string(),
))),
attached_next_js_package_path(project_root).join("_"),
next_js_fs().root().join("_"),
)
.cell(),
);
Expand Down
Loading

0 comments on commit 9ed4166

Please sign in to comment.