Skip to content

Commit

Permalink
Add query structure to module requests
Browse files Browse the repository at this point in the history
  • Loading branch information
wbinnssmith committed Nov 17, 2022
1 parent a8a75b6 commit 1561db5
Show file tree
Hide file tree
Showing 17 changed files with 99 additions and 49 deletions.
10 changes: 10 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions crates/turbopack-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ futures = "0.3.24"
indexmap = { workspace = true }
lazy_static = "1.4.0"
patricia_tree = "0.3.1"
qstring = "0.7.2"
rand = "0.8.5"
regex = "1.5.4"
serde = { version = "1.0.136", features = ["rc"] }
Expand Down
10 changes: 7 additions & 3 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::{
},
origin::ResolveOriginVc,
parse::{Request, RequestVc},
pattern::QueryMapVc,
};
use crate::{
asset::{AssetVc, AssetsVc},
Expand Down Expand Up @@ -649,9 +650,11 @@ pub async fn resolve(
options,
)
}
Request::Module { module, path } => {
resolve_module_request(context, options, options_value, module, path).await?
}
Request::Module {
module,
path,
query,
} => resolve_module_request(context, options, options_value, module, path, query).await?,
Request::ServerRelative { path } => {
let mut new_pat = path.clone();
new_pat.push_front(".".to_string().into());
Expand Down Expand Up @@ -810,6 +813,7 @@ async fn resolve_module_request(
options_value: &ResolveOptions,
module: &str,
path: &Pattern,
_: &QueryMapVc,
) -> Result<ResolveResultVc> {
let result = find_package(
context,
Expand Down
29 changes: 23 additions & 6 deletions crates/turbopack-core/src/resolve/parse.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use anyhow::Result;
use indexmap::IndexMap;
use lazy_static::lazy_static;
use regex::Regex;
use turbo_tasks::{primitives::StringVc, TryJoinIterExt, Value, ValueToString, ValueToStringVc};

use super::pattern::Pattern;
use super::pattern::{Pattern, QueryMapVc};

#[turbo_tasks::value]
#[derive(Hash, Clone, Debug)]
Expand All @@ -19,6 +20,7 @@ pub enum Request {
Module {
module: String,
path: Pattern,
query: QueryMapVc,
},
ServerRelative {
path: Pattern,
Expand Down Expand Up @@ -57,6 +59,7 @@ impl Request {
Request::Module {
module,
path: Pattern::Constant(path),
query: QueryMapVc,
} => format!("{module}{path}"),
Request::ServerRelative {
path: Pattern::Constant(path),
Expand Down Expand Up @@ -101,7 +104,7 @@ impl Request {
Regex::new(r"^([A-Za-z]:\\|\\\\)").unwrap();
static ref URI_PATH: Regex = Regex::new(r"^([^/\\]+:)(.+)").unwrap();
static ref MODULE_PATH: Regex =
Regex::new(r"^((?:@[^/]+/)?[^/]+)(.*)").unwrap();
Regex::new(r"^((?:@[^/]+/)?[^/]+)([^?]*)(.*)").unwrap();
}
if WINDOWS_PATH.is_match(r) {
return Request::Windows { path: request };
Expand All @@ -116,10 +119,15 @@ impl Request {
}
}
if let Some(caps) = MODULE_PATH.captures(r) {
if let (Some(module), Some(path)) = (caps.get(1), caps.get(2)) {
if let (Some(module), Some(path), query) =
(caps.get(1), caps.get(2), caps.get(3))
{
return Request::Module {
module: module.as_str().to_string(),
path: path.as_str().to_string().into(),
query: QueryMapVc::cell(query.map(|q| {
IndexMap::from_iter(qstring::QString::from(q.as_str()))
})),
};
}
}
Expand All @@ -137,7 +145,11 @@ impl Request {
Request::Relative { path, .. } => {
path.extend(iter);
}
Request::Module { module: _, path } => {
Request::Module {
module: _,
path,
query: _,
} => {
path.extend(iter);
}
Request::ServerRelative { path } => {
Expand Down Expand Up @@ -205,10 +217,11 @@ impl RequestVc {
}

#[turbo_tasks::function]
pub fn module(module: String, path: Value<Pattern>) -> Self {
pub fn module(module: String, path: Value<Pattern>, query: QueryMapVc) -> Self {
Self::cell(Request::Module {
module,
path: path.into_value(),
query,
})
}
}
Expand Down Expand Up @@ -238,7 +251,11 @@ impl ValueToString for Request {
format!("relative {path}")
}
}
Request::Module { module, path } => {
Request::Module {
module,
path,
query: _,
} => {
if path.could_match_others("") {
format!("module \"{module}\" with subpath {path}")
} else {
Expand Down
17 changes: 17 additions & 0 deletions crates/turbopack-core/src/resolve/pattern.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::{collections::HashSet, fmt::Display, mem::take};

use anyhow::Result;
use indexmap::IndexMap;
use lazy_static::lazy_static;
use regex::Regex;
use serde::{Deserialize, Serialize};
Expand All @@ -11,6 +12,22 @@ use turbo_tasks_fs::{
DirectoryContent, DirectoryEntry, FileSystemEntryType, FileSystemPathVc, LinkContent, LinkType,
};

#[turbo_tasks::value(transparent)]
pub struct QueryMap(#[turbo_tasks(trace_ignore)] Option<IndexMap<String, String>>);

#[turbo_tasks::value_impl]
impl QueryMapVc {
#[turbo_tasks::function]
pub fn new() -> Self {
Self::cell(Some(IndexMap::new()))
}

#[turbo_tasks::function]
pub fn none() -> Self {
Self::cell(None)
}
}

#[turbo_tasks::value(shared, serialization = "auto_for_input")]
#[derive(PartialOrd, Ord, Hash, Clone, Debug)]
pub enum Pattern {
Expand Down
11 changes: 9 additions & 2 deletions crates/turbopack-ecmascript/src/references/typescript.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ use turbo_tasks::{primitives::StringVc, Value, ValueToString, ValueToStringVc};
use turbo_tasks_fs::FileSystemPathVc;
use turbopack_core::{
reference::{AssetReference, AssetReferenceVc},
resolve::{origin::ResolveOriginVc, parse::RequestVc, ResolveResult, ResolveResultVc},
resolve::{
origin::ResolveOriginVc, parse::RequestVc, pattern::QueryMapVc, ResolveResult,
ResolveResultVc,
},
source_asset::SourceAssetVc,
};

Expand Down Expand Up @@ -121,7 +124,11 @@ impl AssetReference for TsReferenceTypeAssetReference {
fn resolve_reference(&self) -> ResolveResultVc {
type_resolve(
self.origin,
RequestVc::module(self.module.clone(), Value::new("".to_string().into())),
RequestVc::module(
self.module.clone(),
Value::new("".to_string().into()),
QueryMapVc::none(),
),
)
}
}
Expand Down
11 changes: 9 additions & 2 deletions crates/turbopack-ecmascript/src/typescript/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ use turbo_tasks_fs::FileSystemPathVc;
use turbopack_core::{
asset::{Asset, AssetContentVc, AssetVc},
reference::{AssetReference, AssetReferenceVc, AssetReferencesVc},
resolve::{origin::ResolveOriginVc, parse::RequestVc, ResolveResult, ResolveResultVc},
resolve::{
origin::ResolveOriginVc, parse::RequestVc, pattern::QueryMapVc, ResolveResult,
ResolveResultVc,
},
};

use self::resolve::{read_from_tsconfigs, read_tsconfigs, type_resolve};
Expand Down Expand Up @@ -114,7 +117,11 @@ impl Asset for TsConfigModuleAsset {
references.push(
TsConfigTypesReferenceVc::new(
self.origin,
RequestVc::module(name, Value::new("".to_string().into())),
RequestVc::module(
name,
Value::new("".to_string().into()),
QueryMapVc::none(),
),
)
.into(),
);
Expand Down
9 changes: 8 additions & 1 deletion crates/turbopack-ecmascript/src/typescript/resolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use turbopack_core::{
},
origin::ResolveOriginVc,
parse::{Request, RequestVc},
pattern::QueryMapVc,
resolve, AliasPattern, ResolveResult, ResolveResultVc,
},
source_asset::SourceAssetVc,
Expand Down Expand Up @@ -235,7 +236,12 @@ pub async fn type_resolve(origin: ResolveOriginVc, request: RequestVc) -> Result
let context_path = origin.origin_path().parent();
let options = origin.resolve_options();
let options = apply_typescript_types_options(options);
let types_request = if let Request::Module { module: m, path: p } = &*request.await? {
let types_request = if let Request::Module {
module: m,
path: p,
query: _,
} = &*request.await?
{
let m = if let Some(stripped) = m.strip_prefix('@') {
stripped.replace('/', "__")
} else {
Expand All @@ -244,6 +250,7 @@ pub async fn type_resolve(origin: ResolveOriginVc, request: RequestVc) -> Result
Some(RequestVc::module(
format!("@types/{m}"),
Value::new(p.clone()),
QueryMapVc::none(),
))
} else {
None
Expand Down

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

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

This file was deleted.

This file was deleted.

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

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

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

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

7 changes: 6 additions & 1 deletion crates/turbopack/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -521,7 +521,12 @@ async fn warn_on_unsupported_modules(
request: RequestVc,
origin_path: FileSystemPathVc,
) -> Result<()> {
if let Request::Module { module, path } = &*request.await? {
if let Request::Module {
module,
path,
query: _,
} = &*request.await?
{
// Warn if the package is known not to be supported by Turbopack at the moment.
if UNSUPPORTED_PACKAGES.contains(module) {
UnsupportedModuleIssue {
Expand Down

0 comments on commit 1561db5

Please sign in to comment.