Skip to content

Commit

Permalink
feat(turbopack-core): parse query params of resolve requests (#5677)
Browse files Browse the repository at this point in the history
  • Loading branch information
ForsakenHarmony committed Aug 10, 2023
1 parent 10c0a63 commit 4c650d6
Show file tree
Hide file tree
Showing 14 changed files with 474 additions and 250 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Expand Up @@ -599,7 +599,7 @@ jobs:

- name: Run cargo clippy
run: |
RUSTFLAGS="-D warnings -A deprecated" cargo groups clippy turbopack --features rustls-tls
RUSTFLAGS="-D warnings -A deprecated -Aclippy::too_many_arguments" cargo groups clippy turbopack --features rustls-tls
- name: Run ast-grep lints
run: |
Expand Down
11 changes: 7 additions & 4 deletions crates/turbopack-cli/src/build/mod.rs
Expand Up @@ -24,7 +24,6 @@ use turbopack_core::{
resolve::{
origin::{PlainResolveOrigin, ResolveOriginExt},
parse::Request,
pattern::QueryMap,
},
};
use turbopack_env::dotenv::load_env;
Expand Down Expand Up @@ -204,10 +203,14 @@ 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::Module(m, p) => {
Request::module(m.clone(), Value::new(p.clone().into()), QueryMap::none())
EntryRequest::Relative(p) => {
Request::relative(Value::new(p.clone().into()), Vc::<String>::empty(), false)
}
EntryRequest::Module(m, p) => Request::module(
m.clone(),
Value::new(p.clone().into()),
Vc::<String>::empty(),
),
})
})
.try_join()
Expand Down
12 changes: 8 additions & 4 deletions crates/turbopack-cli/src/dev/mod.rs
Expand Up @@ -23,7 +23,7 @@ use turbopack_cli_utils::issue::{ConsoleUi, LogOptions};
use turbopack_core::{
environment::ServerAddr,
issue::{IssueReporter, IssueSeverity},
resolve::{parse::Request, pattern::QueryMap},
resolve::parse::Request,
server_fs::ServerFileSystem,
};
use turbopack_dev::DevChunkingContext;
Expand Down Expand Up @@ -268,10 +268,14 @@ 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::Module(m, p) => {
Request::module(m.clone(), Value::new(p.clone().into()), QueryMap::none())
EntryRequest::Relative(p) => {
Request::relative(Value::new(p.clone().into()), Vc::<String>::empty(), false)
}
EntryRequest::Module(m, p) => Request::module(
m.clone(),
Value::new(p.clone().into()),
Vc::<String>::empty(),
),
})
.collect();

Expand Down
13 changes: 11 additions & 2 deletions crates/turbopack-core/src/file_source.rs
Expand Up @@ -13,21 +13,30 @@ use crate::{
#[turbo_tasks::value]
pub struct FileSource {
pub path: Vc<FileSystemPath>,
pub query: Vc<String>,
}

#[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: Vc::<String>::empty(),
})
}

#[turbo_tasks::function]
pub fn new_with_query(path: Vc<FileSystemPath>, query: Vc<String>) -> 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
71 changes: 49 additions & 22 deletions crates/turbopack-core/src/ident.rs
Expand Up @@ -13,7 +13,7 @@ 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<String>,
/// 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,25 +49,34 @@ 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.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))
}
}
Expand All @@ -84,7 +93,7 @@ impl AssetIdent {
pub fn from_path(path: Vc<FileSystemPath>) -> Vc<Self> {
Self::new(Value::new(AssetIdent {
path,
query: None,
query: Vc::<String>::empty(),
fragment: None,
assets: Vec::new(),
modifiers: Vec::new(),
Expand All @@ -93,29 +102,48 @@ impl AssetIdent {
}

#[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<String>) -> Vc<Self> {
let mut this = self.clone();
this.query = query;
Self::new(Value::new(this))
}

#[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)))
Self::new(Value::new(this))
}

#[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)))
Self::new(Value::new(this))
}

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

#[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)))
}

#[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<String> {
self.query
}

/// Computes a unique output asset name for the given asset identifier.
Expand All @@ -124,18 +152,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,10 +187,11 @@ impl AssetIdent {
assets,
modifiers,
part,
} = this;
if let Some(query) = query {
} = self;
let query = query.await?;
if !query.is_empty() {
0_u8.deterministic_hash(&mut hasher);
query.await?.deterministic_hash(&mut hasher);
query.deterministic_hash(&mut hasher);
has_hash = true;
}
if let Some(fragment) = fragment {
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 4c650d6

Please sign in to comment.