Skip to content

Commit

Permalink
change some return values to Modules
Browse files Browse the repository at this point in the history
  • Loading branch information
sokra committed Jul 7, 2023
1 parent bbf2d7e commit a4b8034
Show file tree
Hide file tree
Showing 20 changed files with 129 additions and 57 deletions.
24 changes: 16 additions & 8 deletions crates/node-file-trace/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,10 +214,14 @@ async fn add_glob_results(
for entry in result.results.values() {
if let DirectoryEntry::File(path) = entry {
let source = SourceAssetVc::new(*path).into();
list.push(context.process(
source,
Value::new(turbopack_core::reference_type::ReferenceType::Undefined),
));
list.push(
context
.process(
source,
Value::new(turbopack_core::reference_type::ReferenceType::Undefined),
)
.into(),
);
}
}
for result in result.inner.values() {
Expand Down Expand Up @@ -256,10 +260,14 @@ async fn input_to_modules<'a>(
for input in input.iter() {
if exact {
let source = SourceAssetVc::new(root.join(input)).into();
list.push(context.process(
source,
Value::new(turbopack_core::reference_type::ReferenceType::Undefined),
));
list.push(
context
.process(
source,
Value::new(turbopack_core::reference_type::ReferenceType::Undefined),
)
.into(),
);
} else {
let glob = GlobVc::new(input);
add_glob_results(context, root.read_glob(glob, false), &mut list).await?;
Expand Down
3 changes: 2 additions & 1 deletion crates/turbopack-core/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use turbo_tasks_fs::FileSystemPathVc;
use crate::{
asset::AssetVc,
compile_time_info::CompileTimeInfoVc,
module::ModuleVc,
reference_type::ReferenceType,
resolve::{options::ResolveOptionsVc, parse::RequestVc, ResolveResultVc},
};
Expand All @@ -27,7 +28,7 @@ pub trait AssetContext {
resolve_options: ResolveOptionsVc,
reference_type: Value<ReferenceType>,
) -> ResolveResultVc;
fn process(&self, asset: AssetVc, reference_type: Value<ReferenceType>) -> AssetVc;
fn process(&self, asset: AssetVc, reference_type: Value<ReferenceType>) -> ModuleVc;
fn process_resolve_result(
&self,
result: ResolveResultVc,
Expand Down
1 change: 1 addition & 0 deletions crates/turbopack-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ pub mod issue;
pub mod module;
pub mod package_json;
pub mod proxied_asset;
pub mod raw_module;
pub mod reference;
pub mod reference_type;
pub mod resolve;
Expand Down
3 changes: 3 additions & 0 deletions crates/turbopack-core/src/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,6 @@ use crate::asset::{Asset, AssetVc};

#[turbo_tasks::value_trait]
pub trait Module: Asset {}

#[turbo_tasks::value(transparent)]
pub struct OptionModule(Option<ModuleVc>);
34 changes: 34 additions & 0 deletions crates/turbopack-core/src/raw_module.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
use crate::{
asset::{Asset, AssetContentVc, AssetVc},
ident::AssetIdentVc,
module::{Module, ModuleVc},
};

#[turbo_tasks::value]
pub struct RawModule {
source: AssetVc,
}

#[turbo_tasks::value_impl]
impl Module for RawModule {}

#[turbo_tasks::value_impl]
impl Asset for RawModule {
#[turbo_tasks::function]
fn ident(&self) -> AssetIdentVc {
self.source.ident()
}

#[turbo_tasks::function]
fn content(&self) -> AssetContentVc {
self.source.content()
}
}

#[turbo_tasks::value_impl]
impl RawModuleVc {
#[turbo_tasks::function]
pub fn new(source: AssetVc) -> RawModuleVc {
RawModule { source }.cell()
}
}
6 changes: 5 additions & 1 deletion crates/turbopack-css/src/global_asset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use turbopack_core::{
chunk::{PassthroughAsset, PassthroughAssetVc},
context::{AssetContext, AssetContextVc},
ident::AssetIdentVc,
module::{Module, ModuleVc},
reference::AssetReferencesVc,
reference_type::{CssReferenceSubType, ReferenceType},
};
Expand All @@ -30,7 +31,7 @@ impl GlobalCssAssetVc {
#[turbo_tasks::value_impl]
impl GlobalCssAssetVc {
#[turbo_tasks::function]
async fn inner(self) -> Result<AssetVc> {
async fn inner(self) -> Result<ModuleVc> {
let this = self.await?;
// The underlying CSS is processed through an internal CSS reference.
// This can then be picked up by other rules to treat CSS assets in
Expand Down Expand Up @@ -63,6 +64,9 @@ impl Asset for GlobalCssAsset {
}
}

#[turbo_tasks::value_impl]
impl Module for GlobalCssAsset {}

#[turbo_tasks::function]
fn modifier() -> StringVc {
StringVc::cell("global css".to_string())
Expand Down
2 changes: 1 addition & 1 deletion crates/turbopack-css/src/module_asset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ struct ModuleCssClasses(IndexMap<String, Vec<ModuleCssClass>>);
#[turbo_tasks::value_impl]
impl ModuleCssAssetVc {
#[turbo_tasks::function]
async fn inner(self) -> Result<AssetVc> {
async fn inner(self) -> Result<ModuleVc> {
let this = self.await?;
Ok(this.context.process(
this.source,
Expand Down
13 changes: 7 additions & 6 deletions crates/turbopack-css/src/references/internal.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use anyhow::Result;
use turbo_tasks::{primitives::StringVc, ValueToString, ValueToStringVc};
use turbopack_core::{
asset::{Asset, AssetVc},
asset::Asset,
chunk::{ChunkableAssetReference, ChunkableAssetReferenceVc},
module::ModuleVc,
reference::{AssetReference, AssetReferenceVc},
resolve::{ResolveResult, ResolveResultVc},
};
Expand All @@ -11,23 +12,23 @@ use turbopack_core::{
#[turbo_tasks::value]
#[derive(Hash, Debug)]
pub struct InternalCssAssetReference {
asset: AssetVc,
module: ModuleVc,
}

#[turbo_tasks::value_impl]
impl InternalCssAssetReferenceVc {
/// Creates a new [`InternalCssAssetReferenceVc`].
#[turbo_tasks::function]
pub fn new(asset: AssetVc) -> Self {
Self::cell(InternalCssAssetReference { asset })
pub fn new(module: ModuleVc) -> Self {
Self::cell(InternalCssAssetReference { module })
}
}

#[turbo_tasks::value_impl]
impl AssetReference for InternalCssAssetReference {
#[turbo_tasks::function]
fn resolve_reference(&self) -> ResolveResultVc {
ResolveResult::asset(self.asset).cell()
ResolveResult::asset(self.module.into()).cell()
}
}

Expand All @@ -37,7 +38,7 @@ impl ValueToString for InternalCssAssetReference {
async fn to_string(&self) -> Result<StringVc> {
Ok(StringVc::cell(format!(
"internal css {}",
self.asset.ident().to_string().await?
self.module.ident().to_string().await?
)))
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/turbopack-ecmascript/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ impl EcmascriptModuleAssetBuilder {
self
}

pub fn build(self) -> AssetVc {
pub fn build(self) -> ModuleVc {
let base = if let Some(inner_assets) = self.inner_assets {
EcmascriptModuleAssetVc::new_with_inner_assets(
self.source,
Expand Down
17 changes: 11 additions & 6 deletions crates/turbopack-ecmascript/src/references/typescript.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,17 @@ impl AssetReference for TsReferencePathAssetReference {
.try_join(&self.path)
.await?
{
ResolveResult::asset(self.origin.context().process(
SourceAssetVc::new(*path).into(),
Value::new(ReferenceType::TypeScript(
TypeScriptReferenceSubType::Undefined,
)),
))
ResolveResult::asset(
self.origin
.context()
.process(
SourceAssetVc::new(*path).into(),
Value::new(ReferenceType::TypeScript(
TypeScriptReferenceSubType::Undefined,
)),
)
.into(),
)
.into()
} else {
ResolveResult::unresolveable().into()
Expand Down
2 changes: 1 addition & 1 deletion crates/turbopack-node/src/evaluate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ pub async fn get_evaluate_pool(
.into(),
Value::new(ReferenceType::Internal(InnerAssetsVc::cell(indexmap! {
"INNER".to_string() => module_asset,
"RUNTIME".to_string() => runtime_asset
"RUNTIME".to_string() => runtime_asset.into()
}))),
);

Expand Down
27 changes: 17 additions & 10 deletions crates/turbopack-node/src/transforms/postcss.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use turbopack_core::{
context::{AssetContext, AssetContextVc},
ident::AssetIdentVc,
issue::IssueContextExt,
module::ModuleVc,
reference_type::{EntryReferenceSubType, InnerAssetsVc, ReferenceType},
resolve::{find_context_file, FindContextFileResult},
source_asset::SourceAssetVc,
Expand Down Expand Up @@ -143,10 +144,14 @@ async fn extra_configs(
.map(|path| async move {
Ok(
matches!(&*path.get_type().await?, FileSystemEntryType::File).then(|| {
any_content_changed(context.process(
SourceAssetVc::new(path).into(),
Value::new(ReferenceType::Internal(InnerAssetsVc::empty())),
))
any_content_changed(
context
.process(
SourceAssetVc::new(path).into(),
Value::new(ReferenceType::Internal(InnerAssetsVc::empty())),
)
.into(),
)
}),
)
})
Expand All @@ -160,11 +165,13 @@ async fn extra_configs(
}

#[turbo_tasks::function]
fn postcss_executor(context: AssetContextVc, postcss_config_path: FileSystemPathVc) -> AssetVc {
let config_asset = context.process(
SourceAssetVc::new(postcss_config_path).into(),
Value::new(ReferenceType::Entry(EntryReferenceSubType::Undefined)),
);
fn postcss_executor(context: AssetContextVc, postcss_config_path: FileSystemPathVc) -> ModuleVc {
let config_asset = context
.process(
SourceAssetVc::new(postcss_config_path).into(),
Value::new(ReferenceType::Entry(EntryReferenceSubType::Undefined)),
)
.into();

context.process(
VirtualAssetVc::new(
Expand Down Expand Up @@ -218,7 +225,7 @@ impl PostCssTransformedAssetVc {
let css_path = css_fs_path.path.as_str();

let config_value = evaluate(
postcss_executor,
postcss_executor.into(),
project_path,
env,
this.source.ident(),
Expand Down
5 changes: 3 additions & 2 deletions crates/turbopack-node/src/transforms/webpack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use turbopack_core::{
asset::{Asset, AssetContent, AssetContentVc, AssetVc},
context::{AssetContext, AssetContextVc},
ident::AssetIdentVc,
module::ModuleVc,
reference_type::{InnerAssetsVc, ReferenceType},
source_asset::SourceAssetVc,
source_transform::{SourceTransform, SourceTransformVc},
Expand Down Expand Up @@ -115,7 +116,7 @@ struct ProcessWebpackLoadersResult {
}

#[turbo_tasks::function]
fn webpack_loaders_executor(context: AssetContextVc) -> AssetVc {
fn webpack_loaders_executor(context: AssetContextVc) -> ModuleVc {
context.process(
SourceAssetVc::new(embed_file_path("transforms/webpack-loaders.ts")).into(),
Value::new(ReferenceType::Internal(InnerAssetsVc::empty())),
Expand Down Expand Up @@ -152,7 +153,7 @@ impl WebpackLoadersProcessedAssetVc {
let resource_path = resource_fs_path.path.as_str();
let loaders = transform.loaders.await?;
let config_value = evaluate(
webpack_loaders_executor,
webpack_loaders_executor.into(),
project_path,
env,
this.source.ident(),
Expand Down
11 changes: 6 additions & 5 deletions crates/turbopack-tests/tests/execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,14 @@ use turbopack::{
ModuleAssetContextVc,
};
use turbopack_core::{
asset::{Asset, AssetVc},
asset::Asset,
chunk::{EvaluatableAssetVc, EvaluatableAssetsVc},
compile_time_defines,
compile_time_info::CompileTimeInfo,
context::{AssetContext, AssetContextVc},
environment::{EnvironmentVc, ExecutionEnvironment, NodeJsEnvironment},
issue::IssueVc,
module::ModuleVc,
reference_type::{EntryReferenceSubType, ReferenceType},
source_asset::SourceAssetVc,
};
Expand Down Expand Up @@ -246,15 +247,15 @@ async fn run_test(resource: &str) -> Result<RunTestResultVc> {
let test_asset = process_path_to_asset(test_path, context);

let res = evaluate(
jest_entry_asset,
jest_entry_asset.into(),
chunk_root_path,
CommandLineProcessEnvVc::new().into(),
test_asset.ident(),
context,
chunking_context,
Some(EvaluatableAssetsVc::many(vec![
EvaluatableAssetVc::from_asset(jest_runtime_asset, context),
EvaluatableAssetVc::from_asset(test_asset, context),
EvaluatableAssetVc::from_asset(jest_runtime_asset.into(), context),
EvaluatableAssetVc::from_asset(test_asset.into(), context),
])),
vec![],
CompletionVc::immutable(),
Expand Down Expand Up @@ -308,7 +309,7 @@ async fn snapshot_issues(run_result: RunTestResultVc) -> Result<NothingVc> {
}

#[turbo_tasks::function]
fn process_path_to_asset(path: FileSystemPathVc, context: AssetContextVc) -> AssetVc {
fn process_path_to_asset(path: FileSystemPathVc, context: AssetContextVc) -> ModuleVc {
context.process(
SourceAssetVc::new(path).into(),
Value::new(ReferenceType::Entry(EntryReferenceSubType::Undefined)),
Expand Down
2 changes: 1 addition & 1 deletion crates/turbopack/benches/node_file_trace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ fn bench_emit(b: &mut Bencher, bench_input: &BenchInput) {
.cell(),
);
let module = context.process(source.into(), Value::new(ReferenceType::Undefined));
let rebased = RebasedAssetVc::new(module, input_dir, output_dir);
let rebased = RebasedAssetVc::new(module.into(), input_dir, output_dir);

emit_with_completion(rebased.into(), output_dir).await?;

Expand Down
2 changes: 1 addition & 1 deletion crates/turbopack/examples/turbopack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ async fn main() -> Result<()> {
source.into(),
Value::new(turbopack_core::reference_type::ReferenceType::Undefined),
);
let rebased = RebasedAssetVc::new(module, input, output);
let rebased = RebasedAssetVc::new(module.into(), input, output);
emit_with_completion(rebased.into(), output).await?;

Ok(NothingVc::new().into())
Expand Down
Loading

0 comments on commit a4b8034

Please sign in to comment.