Skip to content

Commit

Permalink
Misc. changes to support App Router build
Browse files Browse the repository at this point in the history
  • Loading branch information
alexkirsz committed Jul 4, 2023
1 parent 5050577 commit 7e1fa83
Show file tree
Hide file tree
Showing 12 changed files with 128 additions and 35 deletions.
3 changes: 1 addition & 2 deletions crates/turbopack-build/src/chunking_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ impl BuildChunkingContextVc {
/// * exports the result of evaluating the given module as a CommonJS
/// default export.
#[turbo_tasks::function]
pub async fn generate_entry_chunk(
pub async fn entry_chunk(
self_vc: BuildChunkingContextVc,
path: FileSystemPathVc,
module: EcmascriptChunkPlaceableVc,
Expand All @@ -129,7 +129,6 @@ impl BuildChunkingContextVc {
let asset = EcmascriptBuildNodeEntryChunkVc::new(
path,
self_vc,
entry_chunk,
AssetsVc::cell(other_chunks),
evaluatable_assets,
module,
Expand Down
5 changes: 1 addition & 4 deletions crates/turbopack-build/src/ecmascript/node/entry/chunk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use turbo_tasks::{primitives::StringVc, ValueToString, ValueToStringVc};
use turbo_tasks_fs::{File, FileSystemPathVc};
use turbopack_core::{
asset::{Asset, AssetContentVc, AssetVc, AssetsVc},
chunk::{ChunkVc, ChunkingContext, EvaluatableAssetsVc},
chunk::{ChunkingContext, EvaluatableAssetsVc},
code_builder::{CodeBuilder, CodeVc},
ident::AssetIdentVc,
reference::{AssetReferencesVc, SingleAssetReferenceVc},
Expand All @@ -28,7 +28,6 @@ use crate::BuildChunkingContextVc;
pub(crate) struct EcmascriptBuildNodeEntryChunk {
path: FileSystemPathVc,
chunking_context: BuildChunkingContextVc,
entry_chunk: ChunkVc,
other_chunks: AssetsVc,
evaluatable_assets: EvaluatableAssetsVc,
exported_module: EcmascriptChunkPlaceableVc,
Expand All @@ -41,15 +40,13 @@ impl EcmascriptBuildNodeEntryChunkVc {
pub fn new(
path: FileSystemPathVc,
chunking_context: BuildChunkingContextVc,
entry_chunk: ChunkVc,
other_chunks: AssetsVc,
evaluatable_assets: EvaluatableAssetsVc,
exported_module: EcmascriptChunkPlaceableVc,
) -> Self {
EcmascriptBuildNodeEntryChunk {
path,
chunking_context,
entry_chunk,
other_chunks,
evaluatable_assets,
exported_module,
Expand Down
4 changes: 2 additions & 2 deletions crates/turbopack-css/src/chunk/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use turbopack_core::{
asset::{Asset, AssetContentVc, AssetVc, AssetsVc},
chunk::{
availability_info::AvailabilityInfo, chunk_content, chunk_content_split, Chunk,
ChunkContentResult, ChunkGroupReferenceVc, ChunkItem, ChunkItemVc, ChunkVc,
ChunkContentResult, ChunkGroupReferenceVc, ChunkItem, ChunkItemVc, ChunkVc, ChunkableAsset,
ChunkableAssetVc, ChunkingContext, ChunkingContextVc, ChunksVc, FromChunkableAsset,
ModuleId, ModuleIdVc, ModuleIdsVc, OutputChunk, OutputChunkRuntimeInfo,
OutputChunkRuntimeInfoVc, OutputChunkVc,
Expand Down Expand Up @@ -467,7 +467,7 @@ impl CssChunkContextVc {
}

#[turbo_tasks::value_trait]
pub trait CssChunkPlaceable: Asset {
pub trait CssChunkPlaceable: ChunkableAsset + Asset {
fn as_chunk_item(&self, context: ChunkingContextVc) -> CssChunkItemVc;
}

Expand Down
42 changes: 29 additions & 13 deletions crates/turbopack-dev/src/chunking_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ impl DevChunkingContextBuilder {
self
}

pub fn chunk_base_path(mut self, chunk_base_path: &str) -> Self {
self.context.chunk_base_path =
(!chunk_base_path.is_empty()).then(|| chunk_base_path.to_string());
self
}

pub fn layer(mut self, layer: &str) -> Self {
self.context.layer = (!layer.is_empty()).then(|| layer.to_string());
self
Expand All @@ -61,8 +67,8 @@ impl DevChunkingContextBuilder {
self
}

pub fn build(self) -> ChunkingContextVc {
DevChunkingContextVc::new(Value::new(self.context)).into()
pub fn build(self) -> DevChunkingContextVc {
DevChunkingContextVc::new(Value::new(self.context))
}
}

Expand All @@ -87,6 +93,9 @@ pub struct DevChunkingContext {
reference_css_chunk_source_maps: bool,
/// Static assets are placed at this path
asset_root_path: FileSystemPathVc,
/// Base path that will be prepended to all chunk URLs when loading them.
/// This path will not appear in chunk paths or chunk data.
chunk_base_path: Option<String>,
/// Layer name within this context
layer: Option<String>,
/// Enable HMR for this chunking
Expand All @@ -113,6 +122,7 @@ impl DevChunkingContextVc {
reference_chunk_source_maps: true,
reference_css_chunk_source_maps: true,
asset_root_path,
chunk_base_path: None,
layer: None,
enable_hot_module_replacement: false,
environment,
Expand All @@ -130,6 +140,11 @@ impl DevChunkingContext {
pub fn runtime_type(&self) -> RuntimeType {
self.runtime_type
}

/// Returns the chunk public path.
pub fn chunk_base_path(&self) -> Option<&String> {
self.chunk_base_path.as_ref()
}
}

#[turbo_tasks::value_impl]
Expand Down Expand Up @@ -203,17 +218,18 @@ impl ChunkingContext for DevChunkingContext {

#[turbo_tasks::function]
async fn reference_chunk_source_maps(&self, chunk: AssetVc) -> Result<BoolVc> {
let mut source_maps = self.reference_chunk_source_maps;
let path = chunk.ident().path().await?;
let extension = path.extension().unwrap_or_default();
#[allow(clippy::single_match, reason = "future extensions")]
match extension {
".css" => {
source_maps = self.reference_css_chunk_source_maps;
}
_ => {}
}
Ok(BoolVc::cell(source_maps))
return Ok(BoolVc::cell(false));
// let mut source_maps = self.reference_chunk_source_maps;
// let path = chunk.ident().path().await?;
// let extension = path.extension().unwrap_or_default();
// #[allow(clippy::single_match, reason = "future extensions")]
// match extension {
// ".css" => {
// source_maps = self.reference_css_chunk_source_maps;
// }
// _ => {}
// }
// Ok(BoolVc::cell(source_maps))
}

#[turbo_tasks::function]
Expand Down
13 changes: 10 additions & 3 deletions crates/turbopack-dev/src/ecmascript/evaluate/chunk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ use std::io::Write;
use anyhow::{bail, Result};
use indoc::writedoc;
use serde::Serialize;
use turbo_tasks::{primitives::StringVc, TryJoinIterExt, Value, ValueToString, ValueToStringVc};
use turbo_tasks::{
primitives::{OptionStringVc, StringVc},
TryJoinIterExt, Value, ValueToString, ValueToStringVc,
};
use turbo_tasks_fs::File;
use turbopack_core::{
asset::{Asset, AssetContentVc, AssetVc, AssetsVc},
Expand Down Expand Up @@ -67,6 +70,7 @@ impl EcmascriptDevEvaluateChunkVc {
#[turbo_tasks::function]
async fn code(self) -> Result<CodeVc> {
let this = self.await?;
let chunking_context = this.chunking_context.await?;
let environment = this.chunking_context.environment();

let output_root = this.chunking_context.output_root().await?;
Expand Down Expand Up @@ -137,9 +141,12 @@ impl EcmascriptDevEvaluateChunkVc {
StringifyJs(&params),
)?;

match this.chunking_context.await?.runtime_type() {
match chunking_context.runtime_type() {
RuntimeType::Default => {
let runtime_code = turbopack_ecmascript_runtime::get_dev_runtime_code(environment);
let runtime_code = turbopack_ecmascript_runtime::get_dev_runtime_code(
environment,
OptionStringVc::cell(chunking_context.chunk_base_path().cloned()),
);
code.push_code(&*runtime_code.await?);
}
#[cfg(feature = "test")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,8 @@ pub struct ClientDirectiveTransformer {
}

impl ClientDirectiveTransformer {
pub fn new(transition_name: &StringVc) -> Self {
Self {
transition_name: *transition_name,
}
pub fn new(transition_name: StringVc) -> Self {
Self { transition_name }
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use swc_core::{
common::DUMMY_SP,
ecma::{
ast::{
Expr, ExprStmt, Ident, ImportDecl, ImportDefaultSpecifier, ImportSpecifier,
Expr, ExprStmt, Ident, ImportDecl, ImportSpecifier, ImportStarAsSpecifier,
KeyValueProp, Lit, Module, ModuleDecl, ModuleItem, ObjectLit, Program, Prop, PropName,
PropOrSpread, Stmt, Str,
},
Expand All @@ -25,7 +25,8 @@ pub fn create_proxy_module(transition_name: &str, target_import: &str) -> Progra
span: DUMMY_SP,
})),
ModuleItem::ModuleDecl(ModuleDecl::Import(ImportDecl {
specifiers: vec![ImportSpecifier::Default(ImportDefaultSpecifier {
// TODO(alexkirsz) Is this correct?
specifiers: vec![ImportSpecifier::Namespace(ImportStarAsSpecifier {
local: ident.clone(),
span: DUMMY_SP,
})],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type ChunkUpdateProvider = {
push: (registration: [ChunkPath, UpdateCallback]) => void;
};

declare var CHUNK_BASE_PATH: string;
declare var TURBOPACK: ChunkRegistry | ChunkRegistration[] | undefined;
declare var TURBOPACK_CHUNK_LISTS: ChunkListProvider | ChunkList[] | undefined;
declare var TURBOPACK_CHUNK_UPDATE_LISTENERS:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ function commonJsRequireContext(
if (chunkPath.endsWith(".css")) {
const link = document.createElement("link");
link.rel = "stylesheet";
link.href = `/${chunkPath}`;
link.href = `/${CHUNK_BASE_PATH}${chunkPath}`;
link.onerror = () => {
resolver.reject();
};
Expand All @@ -209,7 +209,7 @@ function commonJsRequireContext(
document.body.appendChild(link);
} else if (chunkPath.endsWith(".js")) {
const script = document.createElement("script");
script.src = `/${chunkPath}`;
script.src = `/${CHUNK_BASE_PATH}${chunkPath}`;
// We'll only mark the chunk as loaded once the script has been executed,
// which happens in `registerChunk`. Hence the absence of `resolve()` in
// this branch.
Expand Down
17 changes: 14 additions & 3 deletions crates/turbopack-ecmascript-runtime/src/dev_runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,22 @@ use std::io::Write;

use anyhow::Result;
use indoc::writedoc;
use turbo_tasks::primitives::OptionStringVc;
use turbopack_core::{
code_builder::{CodeBuilder, CodeVc},
context::AssetContext,
environment::{ChunkLoading, EnvironmentVc},
};
use turbopack_ecmascript::StaticEcmascriptCodeVc;
use turbopack_ecmascript::{utils::StringifyJs, StaticEcmascriptCodeVc};

use crate::{asset_context::get_runtime_asset_context, embed_file_path};

/// Returns the code for the development ECMAScript runtime.
#[turbo_tasks::function]
pub async fn get_dev_runtime_code(environment: EnvironmentVc) -> Result<CodeVc> {
pub async fn get_dev_runtime_code(
environment: EnvironmentVc,
chunk_base_path: OptionStringVc,
) -> Result<CodeVc> {
let asset_context = get_runtime_asset_context(environment);

let shared_runtime_utils_code =
Expand Down Expand Up @@ -50,7 +54,14 @@ pub async fn get_dev_runtime_code(environment: EnvironmentVc) -> Result<CodeVc>
if (!Array.isArray(globalThis.TURBOPACK)) {{
return;
}}
"#
const CHUNK_BASE_PATH = {};
"#,
StringifyJs(if let Some(chunk_base_path) = &*chunk_base_path.await? {
chunk_base_path.as_str()
} else {
""
})
)?;

code.push_code(&*shared_runtime_utils_code.await?);
Expand Down
60 changes: 60 additions & 0 deletions crates/turbopack/src/transition/context_transition.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
use anyhow::Result;
use turbopack_core::compile_time_info::CompileTimeInfoVc;

use crate::{
module_options::ModuleOptionsContextVc,
resolve_options_context::ResolveOptionsContextVc,
transition::{Transition, TransitionVc},
};

/// A transition that only affects the asset context.
#[turbo_tasks::value(shared)]
pub struct ContextTransition {
compile_time_info: CompileTimeInfoVc,
module_options_context: ModuleOptionsContextVc,
resolve_options_context: ResolveOptionsContextVc,
}

#[turbo_tasks::value_impl]
impl ContextTransitionVc {
#[turbo_tasks::function]
pub async fn new(
compile_time_info: CompileTimeInfoVc,
module_options_context: ModuleOptionsContextVc,
resolve_options_context: ResolveOptionsContextVc,
) -> Result<ContextTransitionVc> {
Ok(ContextTransition {
module_options_context,
resolve_options_context,
compile_time_info,
}
.cell())
}
}

#[turbo_tasks::value_impl]
impl Transition for ContextTransition {
#[turbo_tasks::function]
fn process_compile_time_info(
&self,
_compile_time_info: CompileTimeInfoVc,
) -> CompileTimeInfoVc {
self.compile_time_info
}

#[turbo_tasks::function]
fn process_module_options_context(
&self,
_context: ModuleOptionsContextVc,
) -> ModuleOptionsContextVc {
self.module_options_context
}

#[turbo_tasks::function]
fn process_resolve_options_context(
&self,
_context: ResolveOptionsContextVc,
) -> ResolveOptionsContextVc {
self.resolve_options_context
}
}
3 changes: 3 additions & 0 deletions crates/turbopack/src/transition/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
pub(crate) mod context_transition;

use std::collections::HashMap;

use anyhow::Result;
pub use context_transition::ContextTransitionVc;
use turbo_tasks::Value;
use turbopack_core::{
asset::AssetVc, compile_time_info::CompileTimeInfoVc, reference_type::ReferenceType,
Expand Down

0 comments on commit 7e1fa83

Please sign in to comment.