Skip to content

Commit

Permalink
Source trait (#5483)
Browse files Browse the repository at this point in the history
### Description

* rename SourceAsset to FileSource
* rename VirtualAsset to VirtualSource
* add Source trait
* use SourceVc in some places

next.js PR: vercel/next.js#52511
  • Loading branch information
sokra authored Jul 11, 2023
1 parent b4fe7e9 commit 0bb91d3
Show file tree
Hide file tree
Showing 66 changed files with 360 additions and 354 deletions.
1 change: 1 addition & 0 deletions .cargo/config.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
[env]
CARGO_WORKSPACE_DIR = { value = "", relative = true }
TURBO_PNPM_WORKSPACE_DIR = { value = "", relative = true }

[target.x86_64-pc-windows-msvc]
linker = "rust-lld"
Expand Down
8 changes: 4 additions & 4 deletions Cargo.lock

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

6 changes: 3 additions & 3 deletions crates/node-file-trace/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@ use turbopack_core::{
compile_time_info::CompileTimeInfo,
context::{AssetContext, AssetContextVc},
environment::{EnvironmentVc, ExecutionEnvironment, NodeJsEnvironment},
file_source::FileSourceVc,
issue::{IssueContextExt, IssueReporter, IssueSeverity, IssueVc},
reference::all_assets,
resolve::options::{ImportMapping, ResolvedMap},
source_asset::SourceAssetVc,
};

use crate::nft_json::NftJsonAssetVc;
Expand Down Expand Up @@ -213,7 +213,7 @@ async fn add_glob_results(
let result = result.await?;
for entry in result.results.values() {
if let DirectoryEntry::File(path) = entry {
let source = SourceAssetVc::new(*path).into();
let source = FileSourceVc::new(*path).into();
list.push(
context
.process(
Expand Down Expand Up @@ -259,7 +259,7 @@ async fn input_to_modules<'a>(
let mut list = Vec::new();
for input in input.iter() {
if exact {
let source = SourceAssetVc::new(root.join(input)).into();
let source = FileSourceVc::new(root.join(input)).into();
list.push(
context
.process(
Expand Down
2 changes: 1 addition & 1 deletion crates/turbopack-build/src/chunking_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ where
.copied()
.map(|chunk| chunk.as_chunk())
.chain(css_chunks.iter().copied().map(|chunk| chunk.as_chunk()))
.chain(other_chunks.into_iter())
.chain(other_chunks)
.collect();

Ok(ChunksVc::cell(chunks))
Expand Down
7 changes: 4 additions & 3 deletions crates/turbopack-cli-utils/src/runtime_entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,20 @@ use anyhow::{bail, Result};
use turbo_tasks::ValueToString;
use turbo_tasks_fs::FileSystemPathVc;
use turbopack_core::{
asset::{Asset, AssetVc},
asset::Asset,
chunk::{EvaluatableAssetVc, EvaluatableAssetsVc},
context::AssetContextVc,
issue::{IssueSeverity, OptionIssueSourceVc},
resolve::{origin::PlainResolveOriginVc, parse::RequestVc},
source::SourceVc,
};
use turbopack_ecmascript::resolve::cjs_resolve;

#[turbo_tasks::value(shared)]
pub enum RuntimeEntry {
Request(RequestVc, FileSystemPathVc),
Evaluatable(EvaluatableAssetVc),
Source(AssetVc),
Source(SourceVc),
}

#[turbo_tasks::value_impl]
Expand All @@ -24,7 +25,7 @@ impl RuntimeEntryVc {
let (request, path) = match *self.await? {
RuntimeEntry::Evaluatable(e) => return Ok(EvaluatableAssetsVc::one(e)),
RuntimeEntry::Source(source) => {
return Ok(EvaluatableAssetsVc::one(EvaluatableAssetVc::from_asset(
return Ok(EvaluatableAssetsVc::one(EvaluatableAssetVc::from_source(
source, context,
)));
}
Expand Down
4 changes: 2 additions & 2 deletions crates/turbopack-cli/src/dev/web_entry_source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ use turbopack_core::{
compile_time_info::{CompileTimeDefinesVc, CompileTimeInfo, CompileTimeInfoVc},
context::AssetContextVc,
environment::{BrowserEnvironment, EnvironmentVc, ExecutionEnvironment},
file_source::FileSourceVc,
reference_type::{EntryReferenceSubType, ReferenceType},
resolve::{
options::{ImportMap, ImportMapVc, ImportMapping},
origin::PlainResolveOriginVc,
parse::RequestVc,
},
source_asset::SourceAssetVc,
};
use turbopack_dev::{react_refresh::assert_can_resolve_react_refresh, DevChunkingContextVc};
use turbopack_dev_server::{
Expand Down Expand Up @@ -239,7 +239,7 @@ pub async fn get_client_runtime_entries(
};

runtime_entries.push(
RuntimeEntry::Source(SourceAssetVc::new(embed_file_path("entry/bootstrap.ts")).into())
RuntimeEntry::Source(FileSourceVc::new(embed_file_path("entry/bootstrap.ts")).into())
.cell(),
);

Expand Down
8 changes: 6 additions & 2 deletions crates/turbopack-core/src/chunk/evaluate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use crate::{
context::{AssetContext, AssetContextVc},
module::{Module, ModuleVc},
reference_type::{EntryReferenceSubType, ReferenceType},
source::SourceVc,
};

/// Marker trait for the chunking context to accept evaluated entries.
Expand All @@ -19,9 +20,12 @@ pub trait EvaluatableAsset: Asset + Module + ChunkableModule {}
#[turbo_tasks::value_impl]
impl EvaluatableAssetVc {
#[turbo_tasks::function]
pub async fn from_asset(asset: AssetVc, context: AssetContextVc) -> Result<EvaluatableAssetVc> {
pub async fn from_source(
source: SourceVc,
context: AssetContextVc,
) -> Result<EvaluatableAssetVc> {
let asset = context.process(
asset,
source,
Value::new(ReferenceType::Entry(EntryReferenceSubType::Runtime)),
);
let Some(entry) = EvaluatableAssetVc::resolve_from(asset).await? else {
Expand Down
6 changes: 3 additions & 3 deletions crates/turbopack-core/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@ use turbo_tasks::Value;
use turbo_tasks_fs::FileSystemPathVc;

use crate::{
asset::AssetVc,
compile_time_info::CompileTimeInfoVc,
module::ModuleVc,
reference_type::ReferenceType,
resolve::{options::ResolveOptionsVc, parse::RequestVc, ResolveResultVc},
source::SourceVc,
};

/// A context for building an asset graph. It's passed through the assets while
/// creating them. It's needed to resolve assets and upgrade assets to a higher
/// type (e. g. from SourceAsset to ModuleAsset).
/// type (e. g. from FileSource to ModuleAsset).
#[turbo_tasks::value_trait]
pub trait AssetContext {
fn compile_time_info(&self) -> CompileTimeInfoVc;
Expand All @@ -28,7 +28,7 @@ pub trait AssetContext {
resolve_options: ResolveOptionsVc,
reference_type: Value<ReferenceType>,
) -> ResolveResultVc;
fn process(&self, asset: AssetVc, reference_type: Value<ReferenceType>) -> ModuleVc;
fn process(&self, asset: SourceVc, reference_type: Value<ReferenceType>) -> ModuleVc;
fn process_resolve_result(
&self,
result: ResolveResultVc,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,29 @@ use crate::{
asset::{Asset, AssetContent, AssetContentVc, AssetVc},
ident::AssetIdentVc,
reference::AssetReferencesVc,
source::{Source, SourceVc},
};

/// The raw [Asset]. It represents raw content from a path without any
/// references to other [Asset]s.
#[turbo_tasks::value]
pub struct SourceAsset {
pub struct FileSource {
pub path: FileSystemPathVc,
}

#[turbo_tasks::value_impl]
impl SourceAssetVc {
impl FileSourceVc {
#[turbo_tasks::function]
pub fn new(path: FileSystemPathVc) -> Self {
Self::cell(SourceAsset { path })
Self::cell(FileSource { path })
}
}

#[turbo_tasks::value_impl]
impl Asset for SourceAsset {
impl Source for FileSource {}

#[turbo_tasks::value_impl]
impl Asset for FileSource {
#[turbo_tasks::function]
fn ident(&self) -> AssetIdentVc {
AssetIdentVc::from_path(self.path)
Expand Down
12 changes: 6 additions & 6 deletions crates/turbopack-core/src/issue/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -426,15 +426,15 @@ impl CapturedIssues {
#[turbo_tasks::value]
#[derive(Clone)]
pub struct IssueSource {
pub asset: AssetVc,
pub source: AssetVc,
pub start: SourcePos,
pub end: SourcePos,
}

#[turbo_tasks::value_impl]
impl IssueSourceVc {
#[turbo_tasks::function]
pub async fn from_byte_offset(asset: AssetVc, start: usize, end: usize) -> Result<Self> {
pub async fn from_byte_offset(source: AssetVc, start: usize, end: usize) -> Result<Self> {
fn find_line_and_column(lines: &[FileLine], offset: usize) -> SourcePos {
match lines.binary_search_by(|line| line.bytes_offset.cmp(&offset)) {
Ok(i) => SourcePos { line: i, column: 0 },
Expand All @@ -454,13 +454,13 @@ impl IssueSourceVc {
}
}
Ok(Self::cell(
if let FileLinesContent::Lines(lines) = &*asset.content().lines().await? {
if let FileLinesContent::Lines(lines) = &*source.content().lines().await? {
let start = find_line_and_column(lines.as_ref(), start);
let end = find_line_and_column(lines.as_ref(), end);
IssueSource { asset, start, end }
IssueSource { source, start, end }
} else {
IssueSource {
asset,
source,
start: SourcePos::default(),
end: SourcePos::max(),
}
Expand Down Expand Up @@ -620,7 +620,7 @@ impl IssueSourceVc {
pub async fn into_plain(self) -> Result<PlainIssueSourceVc> {
let this = self.await?;
Ok(PlainIssueSource {
asset: PlainAssetVc::from_asset(this.asset).await?,
asset: PlainAssetVc::from_asset(this.source).await?,
start: this.start,
end: this.end,
}
Expand Down
5 changes: 3 additions & 2 deletions crates/turbopack-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ pub mod compile_time_info;
pub mod context;
pub mod environment;
pub mod error;
pub mod file_source;
pub mod ident;
pub mod introspect;
pub mod issue;
Expand All @@ -24,14 +25,14 @@ pub mod reference;
pub mod reference_type;
pub mod resolve;
pub mod server_fs;
pub mod source_asset;
pub mod source;
pub mod source_map;
pub mod source_pos;
pub mod source_transform;
pub mod target;
mod utils;
pub mod version;
pub mod virtual_asset;
pub mod virtual_source;

pub mod virtual_fs {
pub use turbo_tasks_fs::VirtualFileSystemVc;
Expand Down
5 changes: 3 additions & 2 deletions crates/turbopack-core/src/raw_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ use crate::{
asset::{Asset, AssetContentVc, AssetVc},
ident::AssetIdentVc,
module::{Module, ModuleVc},
source::SourceVc,
};

/// A module where source code doesn't need to be parsed but can be usd as is.
/// This module has no references to other modules.
#[turbo_tasks::value]
pub struct RawModule {
source: AssetVc,
source: SourceVc,
}

#[turbo_tasks::value_impl]
Expand All @@ -30,7 +31,7 @@ impl Asset for RawModule {
#[turbo_tasks::value_impl]
impl RawModuleVc {
#[turbo_tasks::function]
pub fn new(source: AssetVc) -> RawModuleVc {
pub fn new(source: SourceVc) -> RawModuleVc {
RawModule { source }.cell()
}
}
4 changes: 2 additions & 2 deletions crates/turbopack-core/src/reference/source_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use turbo_tasks_fs::{FileSystemEntryType, FileSystemPathVc};

use super::{AssetReference, AssetReferenceVc};
use crate::{
file_source::FileSourceVc,
resolve::{ResolveResult, ResolveResultVc},
source_asset::SourceAssetVc,
};

#[turbo_tasks::value]
Expand All @@ -29,7 +29,7 @@ impl AssetReference for SourceMapReference {
let file_type = self.file.get_type().await;
if let Ok(file_type_result) = file_type.as_ref() {
if let FileSystemEntryType::File = &**file_type_result {
return ResolveResult::asset(SourceAssetVc::new(self.file).into()).into();
return ResolveResult::asset(FileSourceVc::new(self.file).into()).into();
}
}
ResolveResult::unresolveable().into()
Expand Down
8 changes: 4 additions & 4 deletions crates/turbopack-core/src/resolve/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ use self::{
};
use crate::{
asset::{Asset, AssetOptionVc, AssetVc, AssetsVc},
file_source::FileSourceVc,
issue::resolve::{ResolvingIssue, ResolvingIssueVc},
package_json::{read_package_json, PackageJsonIssue, PackageJsonIssueVc},
reference::{AssetReference, AssetReferenceVc},
Expand All @@ -36,7 +37,6 @@ use crate::{
pattern::{read_matches, Pattern, PatternMatch, PatternVc},
plugin::ResolvePlugin,
},
source_asset::SourceAssetVc,
};

mod alias_map;
Expand Down Expand Up @@ -591,7 +591,7 @@ pub async fn resolve_raw(
async fn to_result(path: FileSystemPathVc) -> Result<ResolveResultVc> {
let RealPathResult { path, symlinks } = &*path.realpath_with_links().await?;
Ok(ResolveResult::asset_with_references(
SourceAssetVc::new(*path).into(),
FileSourceVc::new(*path).into(),
symlinks
.iter()
.map(|p| AffectingResolvingAssetReferenceVc::new(*p).into())
Expand Down Expand Up @@ -1253,7 +1253,7 @@ async fn resolved(
}

Ok(ResolveResult::asset_with_references(
SourceAssetVc::new(*path).into(),
FileSourceVc::new(*path).into(),
symlinks
.iter()
.map(|p| AffectingResolvingAssetReferenceVc::new(*p).into())
Expand Down Expand Up @@ -1370,7 +1370,7 @@ impl AffectingResolvingAssetReferenceVc {
impl AssetReference for AffectingResolvingAssetReference {
#[turbo_tasks::function]
fn resolve_reference(&self) -> ResolveResultVc {
ResolveResult::asset(SourceAssetVc::new(self.path).into()).into()
ResolveResult::asset(FileSourceVc::new(self.path).into()).into()
}
}

Expand Down
Loading

0 comments on commit 0bb91d3

Please sign in to comment.