Unverified Commit 98fd3456 authored by Tobias Koppers's avatar Tobias Koppers Committed by GitHub

Turbopack: query conditions in rules follow-ups (#88801)

Rebase of https://github.com/vercel/next.js/pull/87291
parent 9e933be7
...@@ -2106,6 +2106,13 @@ mod tests { ...@@ -2106,6 +2106,13 @@ mod tests {
"browser", "browser",
{ {
"path": { "type": "glob", "value": "*.svg"}, "path": { "type": "glob", "value": "*.svg"},
"query": {
"type": "regex",
"value": {
"source": "@someQuery",
"flags": ""
}
},
"content": { "content": {
"source": "@someTag", "source": "@someTag",
"flags": "" "flags": ""
...@@ -2140,7 +2147,10 @@ mod tests { ...@@ -2140,7 +2147,10 @@ mod tests {
source: rcstr!("@someTag"), source: rcstr!("@someTag"),
flags: rcstr!(""), flags: rcstr!(""),
}), }),
query: None, query: Some(ConfigConditionQuery::Regex(RegexComponents {
source: rcstr!("@someQuery"),
flags: rcstr!(""),
})),
}, },
] ]
.into(), .into(),
......
...@@ -197,6 +197,9 @@ module.exports = { ...@@ -197,6 +197,9 @@ module.exports = {
{ {
any: [ any: [
{ path: '*.svg' }, { path: '*.svg' },
// 'query' matches anywhere in the full query string,
// which can be empty, or start with `?`.
{ query: /[?&]svgr(?=&|$)/ },
// 'content' is always a RegExp, and can match // 'content' is always a RegExp, and can match
// anywhere in the file. // anywhere in the file.
{ content: /\<svg\W/ }, { content: /\<svg\W/ },
......
import { NextResponse } from 'next/server' import { NextResponse } from 'next/server'
import foo from '../../foo.js' import foo from '../../foo.js'
import bar from '../../bar.js' // @ts-expect-error -- ignore
import bar from '../../bar.js?test=hi'
export async function GET(_req) { export async function GET(_req) {
return NextResponse.json( return NextResponse.json(
......
...@@ -35,7 +35,10 @@ export default { ...@@ -35,7 +35,10 @@ export default {
condition: { condition: {
any: [ any: [
{ {
all: ['development', { not: { not: { content: /export/ } } }], all: [
'development',
{ not: { not: { query: /test=hi/, content: /export/ } } },
],
}, },
], ],
}, },
...@@ -49,7 +52,10 @@ export default { ...@@ -49,7 +52,10 @@ export default {
// this should match on production // this should match on production
{ {
condition: { condition: {
all: [{ not: 'development' }, { content: /export/ }], all: [
{ not: 'development' },
{ query: /test=hi/, content: /export/ },
],
}, },
loaders: [ loaders: [
{ {
......
...@@ -132,9 +132,6 @@ async fn rule_condition_from_webpack_condition( ...@@ -132,9 +132,6 @@ async fn rule_condition_from_webpack_condition(
} }
None => {} None => {}
} }
if let Some(content) = content {
rule_conditions.push(RuleCondition::ResourceContentEsRegex(content.await?));
}
match &query { match &query {
Some(ConditionQuery::Constant(value)) => { Some(ConditionQuery::Constant(value)) => {
rule_conditions.push(RuleCondition::ResourceQueryEquals(value.clone().into())); rule_conditions.push(RuleCondition::ResourceQueryEquals(value.clone().into()));
...@@ -144,6 +141,10 @@ async fn rule_condition_from_webpack_condition( ...@@ -144,6 +141,10 @@ async fn rule_condition_from_webpack_condition(
} }
None => {} None => {}
} }
// Add the content condition last since matching requires a more expensive file read.
if let Some(content) = content {
rule_conditions.push(RuleCondition::ResourceContentEsRegex(content.await?));
}
RuleCondition::All(rule_conditions) RuleCondition::All(rule_conditions)
} }
}) })
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment