Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add nft chunking context to turbopack core #72313

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion crates/next-core/src/next_build.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use anyhow::Result;
use turbo_tasks::{RcStr, Vc};
use turbo_tasks_fs::FileSystemPath;
use turbopack_core::resolve::{options::ImportMapping, ExternalType};
use turbopack_core::resolve::{options::ImportMapping, ExternalTraced, ExternalType};

use crate::next_import_map::get_next_package;

Expand All @@ -18,11 +18,14 @@ pub async fn get_postcss_package_mapping(project_path: Vc<FileSystemPath>) -> Vc

#[turbo_tasks::function]
pub async fn get_external_next_compiled_package_mapping(
project_path: Vc<FileSystemPath>,
package_name: Vc<RcStr>,
) -> Result<Vc<ImportMapping>> {
Ok(ImportMapping::Alternatives(vec![ImportMapping::External(
Some(format!("next/dist/compiled/{}", &*package_name.await?).into()),
ExternalType::CommonJs,
ExternalTraced::Traced(project_path),
None,
)
.into()])
.cell())
Expand Down
5 changes: 3 additions & 2 deletions crates/next-core/src/next_dynamic/dynamic_transition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,15 @@ impl Transition for NextDynamicTransition {
module_asset_context,
Value::new(ReferenceType::Undefined),
)
.try_into_module()
.await?
{
ProcessResult::Module(client_module) => ProcessResult::Module(ResolvedVc::upcast(
Some(client_module) => ProcessResult::Module(ResolvedVc::upcast(
NextDynamicEntryModule::new(*client_module)
.to_resolved()
.await?,
)),
ProcessResult::Ignore => ProcessResult::Ignore,
None => ProcessResult::Ignore,
}
.cell())
}
Expand Down
68 changes: 55 additions & 13 deletions crates/next-core/src/next_import_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use turbopack_core::{
options::{ConditionValue, ImportMap, ImportMapping, ResolvedMap},
parse::Request,
pattern::Pattern,
resolve, AliasPattern, ExternalType, ResolveAliasMap, SubpathValue,
resolve, AliasPattern, ExternalTraced, ExternalType, ResolveAliasMap, SubpathValue,
},
source::Source,
};
Expand Down Expand Up @@ -239,7 +239,7 @@ pub async fn get_next_client_import_map(

/// Computes the Next-specific client import map.
#[turbo_tasks::function]
pub fn get_next_build_import_map() -> Vc<ImportMap> {
pub fn get_next_build_import_map(project_path: Vc<FileSystemPath>) -> Vc<ImportMap> {
let mut import_map = ImportMap::empty();

insert_package_alias(
Expand All @@ -248,14 +248,26 @@ pub fn get_next_build_import_map() -> Vc<ImportMap> {
next_js_fs().root(),
);

let external = ImportMapping::External(None, ExternalType::CommonJs).cell();
let external = ImportMapping::External(
None,
ExternalType::CommonJs,
ExternalTraced::Traced(project_path),
None,
)
.cell();

import_map.insert_exact_alias("next", external);
import_map.insert_wildcard_alias("next/", external);
import_map.insert_exact_alias("styled-jsx", external);
import_map.insert_exact_alias(
"styled-jsx/style",
ImportMapping::External(Some("styled-jsx/style.js".into()), ExternalType::CommonJs).cell(),
ImportMapping::External(
Some("styled-jsx/style.js".into()),
ExternalType::CommonJs,
ExternalTraced::Traced(project_path),
None,
)
.cell(),
);
import_map.insert_wildcard_alias("styled-jsx/", external);

Expand Down Expand Up @@ -318,7 +330,14 @@ pub async fn get_next_server_import_map(

let ty = ty.into_value();

let external: Vc<ImportMapping> = ImportMapping::External(None, ExternalType::CommonJs).cell();
let external: Vc<ImportMapping> = ImportMapping::External(
None,
ExternalType::CommonJs,
// TODO(arlyon): wiring up in a follow up PR
ExternalTraced::Untraced,
None,
)
.cell();

import_map.insert_exact_alias("next/dist/server/require-hook", external);
match ty {
Expand All @@ -333,8 +352,13 @@ pub async fn get_next_server_import_map(
import_map.insert_exact_alias("styled-jsx", external);
import_map.insert_exact_alias(
"styled-jsx/style",
ImportMapping::External(Some("styled-jsx/style.js".into()), ExternalType::CommonJs)
.cell(),
ImportMapping::External(
Some("styled-jsx/style.js".into()),
ExternalType::CommonJs,
ExternalTraced::Traced(project_path),
None,
)
.cell(),
);
import_map.insert_wildcard_alias("styled-jsx/", external);
// TODO: we should not bundle next/dist/build/utils in the pages renderer at all
Expand Down Expand Up @@ -554,11 +578,11 @@ async fn insert_next_server_special_aliases(
) -> Result<()> {
let external_cjs_if_node = move |context_dir: Vc<FileSystemPath>, request: &str| match runtime {
NextRuntime::Edge => request_to_import_mapping(context_dir, request),
NextRuntime::NodeJs => external_request_to_cjs_import_mapping(request),
NextRuntime::NodeJs => external_request_to_cjs_import_mapping(context_dir, request),
};
let external_esm_if_node = move |context_dir: Vc<FileSystemPath>, request: &str| match runtime {
NextRuntime::Edge => request_to_import_mapping(context_dir, request),
NextRuntime::NodeJs => external_request_to_esm_import_mapping(request),
NextRuntime::NodeJs => external_request_to_esm_import_mapping(context_dir, request),
};

import_map.insert_exact_alias(
Expand Down Expand Up @@ -1086,12 +1110,30 @@ fn request_to_import_mapping(context_path: Vc<FileSystemPath>, request: &str) ->

/// Creates a direct import mapping to the result of resolving an external
/// request.
fn external_request_to_cjs_import_mapping(request: &str) -> Vc<ImportMapping> {
ImportMapping::External(Some(request.into()), ExternalType::CommonJs).into()
fn external_request_to_cjs_import_mapping(
context_dir: Vc<FileSystemPath>,
request: &str,
) -> Vc<ImportMapping> {
ImportMapping::External(
Some(request.into()),
ExternalType::CommonJs,
ExternalTraced::Traced(context_dir),
Some(context_dir),
)
.into()
}

/// Creates a direct import mapping to the result of resolving an external
/// request.
fn external_request_to_esm_import_mapping(request: &str) -> Vc<ImportMapping> {
ImportMapping::External(Some(request.into()), ExternalType::EcmaScriptModule).into()
fn external_request_to_esm_import_mapping(
context_dir: Vc<FileSystemPath>,
request: &str,
) -> Vc<ImportMapping> {
ImportMapping::External(
Some(request.into()),
ExternalType::EcmaScriptModule,
ExternalTraced::Traced(context_dir),
Some(context_dir),
)
.into()
}
34 changes: 20 additions & 14 deletions crates/next-core/src/next_server/resolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ use turbopack_core::{
parse::Request,
pattern::Pattern,
plugin::{AfterResolvePlugin, AfterResolvePluginCondition},
resolve, ExternalType, FindContextFileResult, ResolveResult, ResolveResultItem,
ResolveResultOption,
resolve, ExternalTraced, ExternalType, FindContextFileResult, ResolveResult,
ResolveResultItem, ResolveResultOption,
},
source::Source,
};
Expand Down Expand Up @@ -375,10 +375,12 @@ impl AfterResolvePlugin for ExternalCjsModulesResolvePlugin {
(FileType::CommonJs, false) => {
// mark as external
Ok(ResolveResultOption::some(
ResolveResult::primary(ResolveResultItem::External(
request_str.into(),
ExternalType::CommonJs,
))
ResolveResult::primary(ResolveResultItem::External {
name: request_str.into(),
ty: ExternalType::CommonJs,
// TODO(arlyon): wiring up in a follow up PR
traced: ExternalTraced::Untraced,
})
.cell(),
))
}
Expand Down Expand Up @@ -412,25 +414,29 @@ impl AfterResolvePlugin for ExternalCjsModulesResolvePlugin {
} else {
// mark as external
Ok(ResolveResultOption::some(
ResolveResult::primary(ResolveResultItem::External(
request_str.into(),
if resolves_equal {
ResolveResult::primary(ResolveResultItem::External {
name: request_str.into(),
ty: if resolves_equal {
ExternalType::CommonJs
} else {
ExternalType::EcmaScriptModule
},
))
// TODO(arlyon): wiring up in a follow up PR
traced: ExternalTraced::Untraced,
})
.cell(),
))
}
}
(FileType::EcmaScriptModule, true) => {
// mark as external
Ok(ResolveResultOption::some(
ResolveResult::primary(ResolveResultItem::External(
request_str.into(),
ExternalType::EcmaScriptModule,
))
ResolveResult::primary(ResolveResultItem::External {
name: request_str.into(),
ty: ExternalType::EcmaScriptModule,
// TODO(arlyon): wiring up in a follow up PR
traced: ExternalTraced::Untraced,
})
.cell(),
))
}
Expand Down
26 changes: 14 additions & 12 deletions crates/next-core/src/next_shared/resolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use turbopack_core::{
AfterResolvePlugin, AfterResolvePluginCondition, BeforeResolvePlugin,
BeforeResolvePluginCondition,
},
ExternalType, ResolveResult, ResolveResultItem, ResolveResultOption,
ExternalTraced, ExternalType, ResolveResult, ResolveResultItem, ResolveResultOption,
},
};

Expand Down Expand Up @@ -204,14 +204,14 @@ pub(crate) fn get_invalid_styled_jsx_resolve_plugin(

#[turbo_tasks::value]
pub(crate) struct NextExternalResolvePlugin {
root: Vc<FileSystemPath>,
project_path: Vc<FileSystemPath>,
}

#[turbo_tasks::value_impl]
impl NextExternalResolvePlugin {
#[turbo_tasks::function]
pub fn new(root: Vc<FileSystemPath>) -> Vc<Self> {
NextExternalResolvePlugin { root }.cell()
pub fn new(project_path: Vc<FileSystemPath>) -> Vc<Self> {
NextExternalResolvePlugin { project_path }.cell()
}
}

Expand All @@ -220,7 +220,7 @@ impl AfterResolvePlugin for NextExternalResolvePlugin {
#[turbo_tasks::function]
fn after_resolve_condition(&self) -> Vc<AfterResolvePluginCondition> {
AfterResolvePluginCondition::new(
self.root.root(),
self.project_path.root(),
Glob::new("**/next/dist/**/*.{external,runtime.dev,runtime.prod}.js".into()),
)
}
Expand All @@ -233,18 +233,20 @@ impl AfterResolvePlugin for NextExternalResolvePlugin {
_reference_type: Value<ReferenceType>,
_request: Vc<Request>,
) -> Result<Vc<ResolveResultOption>> {
let raw_fs_path = &*fs_path.await?;
let path = raw_fs_path.path.to_string();
let path = fs_path.await?.path.to_string();
// Find the starting index of 'next/dist' and slice from that point. It should
// always be found since the glob pattern above is specific enough.
let starting_index = path.find("next/dist").unwrap();
let specifier = &path[starting_index..];
// Replace '/esm/' with '/' to match the CJS version of the file.
let modified_path = path[starting_index..].replace("/esm/", "/");
let specifier: RcStr = specifier.replace("/esm/", "/").into();

Ok(Vc::cell(Some(
ResolveResult::primary(ResolveResultItem::External(
modified_path.into(),
ExternalType::CommonJs,
))
ResolveResult::primary(ResolveResultItem::External {
name: specifier.clone(),
ty: ExternalType::CommonJs,
traced: ExternalTraced::Traced(self.project_path),
})
.into(),
)))
}
Expand Down
15 changes: 11 additions & 4 deletions crates/next-core/src/next_shared/webpack_rules/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ use anyhow::Result;
use turbo_tasks::{RcStr, Vc};
use turbo_tasks_fs::FileSystemPath;
use turbopack::module_options::WebpackLoadersOptions;
use turbopack_core::resolve::options::ImportMapping;
use turbopack_core::resolve::{options::ImportMapping, ExternalTraced, ExternalType};

use self::{babel::maybe_add_babel_loader, sass::maybe_add_sass_loader};
use crate::{next_build::get_external_next_compiled_package_mapping, next_config::NextConfig};
use crate::next_config::NextConfig;

pub(crate) mod babel;
pub(crate) mod sass;
Expand Down Expand Up @@ -33,6 +33,13 @@ pub async fn webpack_loader_options(
}

#[turbo_tasks::function]
fn loader_runner_package_mapping() -> Vc<ImportMapping> {
get_external_next_compiled_package_mapping(Vc::cell("loader-runner".into()))
async fn loader_runner_package_mapping() -> Result<Vc<ImportMapping>> {
Ok(ImportMapping::Alternatives(vec![ImportMapping::External(
Some("next/dist/compiled/loader-runner".into()),
ExternalType::CommonJs,
ExternalTraced::Untraced,
None,
)
.into()])
.cell())
}
27 changes: 25 additions & 2 deletions turbopack/crates/turbopack-core/src/chunk/chunk_group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,20 @@ use std::collections::HashSet;

use anyhow::Result;
use auto_hash_map::AutoSet;
use futures::future::try_join_all;
use turbo_tasks::{
FxIndexMap, FxIndexSet, ResolvedVc, TryFlatJoinIterExt, TryJoinIterExt, Value, Vc,
};
use turbo_tasks_fs::{FileSystem, VirtualFileSystem};

use super::{
availability_info::AvailabilityInfo, available_chunk_items::AvailableChunkItemInfo,
chunk_content, chunking::make_chunks, AsyncModuleInfo, Chunk, ChunkContentResult, ChunkItem,
ChunkingContext,
};
use crate::{module::Module, output::OutputAssets, reference::ModuleReference};
use crate::{
module::Module, output::OutputAssets, rebase::RebasedAsset, reference::ModuleReference,
};

pub struct MakeChunkGroupResult {
pub chunks: Vec<ResolvedVc<Box<dyn Chunk>>>,
Expand All @@ -27,6 +31,7 @@ pub async fn make_chunk_group(
let ChunkContentResult {
chunk_items,
async_modules,
traced_modules,
external_module_references,
forward_edges_inherit_async,
local_back_edges_inherit_async,
Expand Down Expand Up @@ -143,12 +148,24 @@ pub async fn make_chunk_group(
.flat_map(|references| references.iter().copied())
.collect();

let mut referenced_output_assets = references_to_output_assets(external_module_references)
.await?
.await?
.clone_value();

let rebased_modules = try_join_all(traced_modules.into_iter().map(|module| {
RebasedAsset::new(*module, module.ident().path().root(), traced_fs().root()).to_resolved()
}))
.await?;

referenced_output_assets.extend(rebased_modules.into_iter().map(ResolvedVc::upcast));

// Pass chunk items to chunking algorithm
let mut chunks = make_chunks(
chunking_context,
Vc::cell(chunk_items.into_iter().collect()),
"".into(),
references_to_output_assets(external_module_references).await?,
Vc::cell(referenced_output_assets),
)
.await?
.clone_value();
Expand Down Expand Up @@ -181,6 +198,12 @@ pub async fn make_chunk_group(
})
}

// Without this wrapper, VirtualFileSystem::new_with_name always returns a new filesystem
#[turbo_tasks::function]
fn traced_fs() -> Vc<VirtualFileSystem> {
VirtualFileSystem::new_with_name("traced".into())
}

async fn references_to_output_assets(
references: FxIndexSet<Vc<Box<dyn ModuleReference>>>,
) -> Result<Vc<OutputAssets>> {
Expand Down
Loading
Loading