Skip to content

Commit

Permalink
parse query from imports
Browse files Browse the repository at this point in the history
  • Loading branch information
ForsakenHarmony committed Aug 7, 2023
1 parent d0aafab commit f2ca09c
Show file tree
Hide file tree
Showing 23 changed files with 549 additions and 288 deletions.
6 changes: 4 additions & 2 deletions crates/turbopack-cli/src/build/mod.rs
Expand Up @@ -204,9 +204,11 @@ async fn build_internal(
.cloned()
.map(|r| async move {
Ok(match &*r.await? {
EntryRequest::Relative(p) => Request::relative(Value::new(p.clone().into()), false),
EntryRequest::Relative(p) => {
Request::relative(p.clone().into(), QueryMap::empty(), false)
}
EntryRequest::Module(m, p) => {
Request::module(m.clone(), Value::new(p.clone().into()), QueryMap::none())
Request::module(m.clone(), p.clone().into(), QueryMap::empty())
}
})
})
Expand Down
8 changes: 5 additions & 3 deletions crates/turbopack-cli/src/dev/mod.rs
Expand Up @@ -13,7 +13,7 @@ use anyhow::{Context, Result};
use owo_colors::OwoColorize;
use turbo_tasks::{
util::{FormatBytes, FormatDuration},
StatsType, TransientInstance, TurboTasks, TurboTasksBackendApi, UpdateInfo, Value, Vc,
StatsType, TransientInstance, TurboTasks, TurboTasksBackendApi, UpdateInfo, Vc,
};
use turbo_tasks_fs::FileSystem;
use turbo_tasks_malloc::TurboMalloc;
Expand Down Expand Up @@ -268,9 +268,11 @@ async fn source(
let entry_requests = entry_requests
.iter()
.map(|r| match r {
EntryRequest::Relative(p) => Request::relative(Value::new(p.clone().into()), false),
EntryRequest::Relative(p) => {
Request::relative(p.clone().into(), QueryMap::empty(), false)
}
EntryRequest::Module(m, p) => {
Request::module(m.clone(), Value::new(p.clone().into()), QueryMap::none())
Request::module(m.clone(), p.clone().into(), QueryMap::empty())
}
})
.collect();
Expand Down
14 changes: 12 additions & 2 deletions crates/turbopack-core/src/file_source.rs
Expand Up @@ -5,6 +5,7 @@ use turbo_tasks_fs::{FileContent, FileSystemEntryType, FileSystemPath, LinkConte
use crate::{
asset::{Asset, AssetContent},
ident::AssetIdent,
resolve::pattern::QueryMap,
source::Source,
};

Expand All @@ -13,21 +14,30 @@ use crate::{
#[turbo_tasks::value]
pub struct FileSource {
pub path: Vc<FileSystemPath>,
pub query: Vc<QueryMap>,
}

#[turbo_tasks::value_impl]
impl FileSource {
#[turbo_tasks::function]
pub fn new(path: Vc<FileSystemPath>) -> Vc<Self> {
Self::cell(FileSource { path })
Self::cell(FileSource {
path,
query: QueryMap::empty(),
})
}

#[turbo_tasks::function]
pub fn new_with_query(path: Vc<FileSystemPath>, query: Vc<QueryMap>) -> Vc<Self> {
Self::cell(FileSource { path, query })
}
}

#[turbo_tasks::value_impl]
impl Source for FileSource {
#[turbo_tasks::function]
fn ident(&self) -> Vc<AssetIdent> {
AssetIdent::from_path(self.path)
AssetIdent::from_path(self.path).with_query(self.query)
}
}

Expand Down
92 changes: 59 additions & 33 deletions crates/turbopack-core/src/ident.rs
@@ -1,19 +1,19 @@
use std::fmt::Write;

use anyhow::Result;
use turbo_tasks::{Value, ValueToString, Vc};
use turbo_tasks::{ValueToString, Vc};
use turbo_tasks_fs::FileSystemPath;
use turbo_tasks_hash::{encode_hex, hash_xxh3_hash64, DeterministicHash, Xxh3Hash64Hasher};

use crate::resolve::ModulePart;
use crate::resolve::{pattern::QueryMap, ModulePart};

#[turbo_tasks::value(serialization = "auto_for_input")]
#[turbo_tasks::value(serialization = "auto_for_input", shared)]
#[derive(Clone, Debug, PartialOrd, Ord, Hash)]
pub struct AssetIdent {
/// The primary path of the asset
pub path: Vc<FileSystemPath>,
/// The query string of the asset (e.g. `?foo=bar`)
pub query: Option<Vc<String>>,
pub query: Vc<QueryMap>,
/// The fragment of the asset (e.g. `#foo`)
pub fragment: Option<Vc<String>>,
/// The assets that are nested in this asset
Expand Down Expand Up @@ -49,73 +49,96 @@ impl ValueToString for AssetIdent {
#[turbo_tasks::function]
async fn to_string(&self) -> Result<Vc<String>> {
let mut s = self.path.to_string().await?.clone_value();
if let Some(query) = &self.query {
write!(s, "?{}", query.await?)?;

let query = self.query.to_string().await?;
if !query.is_empty() {
write!(s, "?{}", &*query)?;
}

if let Some(fragment) = &self.fragment {
write!(s, "#{}", fragment.await?)?;
}

for (key, asset) in &self.assets {
write!(s, "/({})/{}", key.await?, asset.to_string().await?)?;
}

if !self.modifiers.is_empty() {
s.push_str(" (");

for (i, modifier) in self.modifiers.iter().enumerate() {
if i > 0 {
s.push_str(", ");
}

s.push_str(&modifier.await?);
}

s.push(')');
}

Ok(Vc::cell(s))
}
}

#[turbo_tasks::value_impl]
impl AssetIdent {
#[turbo_tasks::function]
pub fn new(ident: Value<AssetIdent>) -> Vc<Self> {
ident.into_value().cell()
}

/// Creates an [AssetIdent] from a [Vc<FileSystemPath>]
#[turbo_tasks::function]
pub fn from_path(path: Vc<FileSystemPath>) -> Vc<Self> {
Self::new(Value::new(AssetIdent {
Self::cell(AssetIdent {
path,
query: None,
query: QueryMap::empty(),
fragment: None,
assets: Vec::new(),
modifiers: Vec::new(),
part: None,
}))
})
}

#[turbo_tasks::function]
pub async fn with_modifier(self: Vc<Self>, modifier: Vc<String>) -> Result<Vc<Self>> {
let mut this = self.await?.clone_value();
pub fn with_query(&self, query: Vc<QueryMap>) -> Vc<Self> {
let mut this = self.clone();
this.query = this.query.merge(query);
this.cell()
}

#[turbo_tasks::function]
pub fn with_modifier(&self, modifier: Vc<String>) -> Vc<Self> {
let mut this = self.clone();
this.add_modifier(modifier);
Ok(Self::new(Value::new(this)))
this.cell()
}

#[turbo_tasks::function]
pub async fn with_part(self: Vc<Self>, part: Vc<ModulePart>) -> Result<Vc<Self>> {
let mut this = self.await?.clone_value();
pub fn with_part(&self, part: Vc<ModulePart>) -> Vc<Self> {
let mut this = self.clone();
this.part = Some(part);
Ok(Self::new(Value::new(this)))
this.cell()
}

#[turbo_tasks::function]
pub fn with_path(&self, path: Vc<FileSystemPath>) -> Vc<Self> {
let mut this = self.clone();
this.path = path;
this.cell()
}

#[turbo_tasks::function]
pub async fn rename_as(self: Vc<Self>, pattern: String) -> Result<Vc<Self>> {
let mut this = self.await?.clone_value();
pub async fn rename_as(&self, pattern: String) -> Result<Vc<Self>> {
let mut this = self.clone();
this.rename_as_ref(&pattern).await?;
Ok(Self::new(Value::new(this)))
Ok(this.cell())
}

#[turbo_tasks::function]
pub async fn path(self: Vc<Self>) -> Result<Vc<FileSystemPath>> {
Ok(self.await?.path)
pub fn path(&self) -> Vc<FileSystemPath> {
self.path
}

#[turbo_tasks::function]
pub fn query(&self) -> Vc<QueryMap> {
self.query
}

/// Computes a unique output asset name for the given asset identifier.
Expand All @@ -124,18 +147,16 @@ impl AssetIdent {
/// name generation logic.
#[turbo_tasks::function]
pub async fn output_name(
self: Vc<Self>,
&self,
context_path: Vc<FileSystemPath>,
expected_extension: String,
) -> Result<Vc<String>> {
let this = &*self.await?;

// For clippy -- This explicit deref is necessary
let path = &*this.path.await?;
let path = &*self.path.await?;
let mut name = if let Some(inner) = context_path.await?.get_path_to(path) {
clean_separators(inner)
} else {
clean_separators(&this.path.to_string().await?)
clean_separators(&self.path.to_string().await?)
};
let removed_extension = name.ends_with(&expected_extension);
if removed_extension {
Expand All @@ -161,12 +182,17 @@ impl AssetIdent {
assets,
modifiers,
part,
} = this;
if let Some(query) = query {
} = self;
for (key, value) in &*query.await? {
0_u8.deterministic_hash(&mut hasher);
query.await?.deterministic_hash(&mut hasher);
key.deterministic_hash(&mut hasher);
value.deterministic_hash(&mut hasher);
has_hash = true;
}
// if let Some(query) = &*query.await? {
// for (key, value) in query {
// }
// }
if let Some(fragment) = fragment {
1_u8.deterministic_hash(&mut hasher);
fragment.await?.deterministic_hash(&mut hasher);
Expand Down
1 change: 1 addition & 0 deletions crates/turbopack-core/src/lib.rs
Expand Up @@ -6,6 +6,7 @@
#![feature(lint_reasons)]
#![feature(async_fn_in_trait)]
#![feature(arbitrary_self_types)]
#![feature(iter_intersperse)]

pub mod asset;
pub mod changed;
Expand Down

0 comments on commit f2ca09c

Please sign in to comment.