Skip to content

Commit 6853c6f

Browse files
kdy1sokra
andauthored
fix(turbopack): Pass resourceQuery to loaders (#69703)
### What? Pass `resourceQuery` to loaders correctly. ### Why? To be consistent with webpack ### How? Closes #69502 --------- Co-authored-by: Tobias Koppers <[email protected]>
1 parent 8662ba0 commit 6853c6f

File tree

9 files changed

+90
-36
lines changed

9 files changed

+90
-36
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { ReactNode } from 'react'
2+
export default function Root({ children }: { children: ReactNode }) {
3+
return (
4+
<html>
5+
<body>{children}</body>
6+
</html>
7+
)
8+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// @ts-expect-error -- ignore
2+
import { v } from './test.mdx?test=hi'
3+
4+
export default function Page() {
5+
console.log(v)
6+
return <p>hello world</p>
7+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
## Hello World
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/** @type {import('next').NextConfig} */
2+
const nextConfig = {
3+
experimental: {
4+
turbo: {
5+
rules: {
6+
'*.mdx': {
7+
loaders: ['test-loader.js'],
8+
as: '*.js',
9+
},
10+
},
11+
},
12+
},
13+
}
14+
15+
module.exports = nextConfig

test/e2e/app-dir/turbopack-loader-resource-query/node_modules/test-loader.js

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { nextTestSetup } from 'e2e-utils'
2+
;(process.env.TURBOPACK ? describe : describe.skip)(
3+
'turbopack-loader-resource-query',
4+
() => {
5+
const { next } = nextTestSetup({
6+
files: __dirname,
7+
})
8+
9+
// Recommended for tests that check HTML. Cheerio is a HTML parser that has a jQuery like API.
10+
it('should pass query to loader', async () => {
11+
await next.render$('/')
12+
13+
expect(next.cliOutput).toContain('resource query: ?test=hi')
14+
})
15+
}
16+
)

turbopack/crates/turbopack-core/src/ident.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ impl ValueToString for AssetIdent {
5454

5555
let query = self.query.await?;
5656
if !query.is_empty() {
57-
write!(s, "?{}", &*query)?;
57+
write!(s, "{}", &*query)?;
5858
}
5959

6060
if let Some(fragment) = &self.fragment {

turbopack/crates/turbopack-node/js/src/transforms/webpack-loaders.ts

Lines changed: 36 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -19,30 +19,30 @@ import { type StructuredError } from "src/ipc";
1919

2020
export type IpcInfoMessage =
2121
| {
22-
type: "fileDependency";
23-
path: string;
24-
}
22+
type: "fileDependency";
23+
path: string;
24+
}
2525
| {
26-
type: "buildDependency";
27-
path: string;
28-
}
26+
type: "buildDependency";
27+
path: string;
28+
}
2929
| {
30-
type: "dirDependency";
31-
path: string;
32-
glob: string;
33-
}
30+
type: "dirDependency";
31+
path: string;
32+
glob: string;
33+
}
3434
| {
35-
type: "emittedError";
36-
severity: "warning" | "error";
37-
error: StructuredError;
38-
}
35+
type: "emittedError";
36+
severity: "warning" | "error";
37+
error: StructuredError;
38+
}
3939
| {
40-
type: "log";
41-
time: number;
42-
logType: string;
43-
args: any[];
44-
trace?: StackFrame[];
45-
};
40+
type: "log";
41+
time: number;
42+
logType: string;
43+
args: any[];
44+
trace?: StackFrame[];
45+
};
4646

4747
export type IpcRequestMessage = {
4848
type: "resolve";
@@ -54,9 +54,9 @@ export type IpcRequestMessage = {
5454
type LoaderConfig =
5555
| string
5656
| {
57-
loader: string;
58-
options: { [k: string]: unknown };
59-
};
57+
loader: string;
58+
options: { [k: string]: unknown };
59+
};
6060

6161
let runLoaders: typeof import("loader-runner")["runLoaders"];
6262
try {
@@ -168,6 +168,7 @@ const transform = (
168168
ipc: Ipc<IpcInfoMessage, IpcRequestMessage>,
169169
content: string,
170170
name: string,
171+
query: string,
171172
loaders: LoaderConfig[]
172173
) => {
173174
return new Promise((resolve, reject) => {
@@ -180,7 +181,7 @@ const transform = (
180181

181182
runLoaders(
182183
{
183-
resource,
184+
resource: resource + query,
184185
context: {
185186
_module: {
186187
// For debugging purpose, if someone find context is not full compatible to
@@ -468,13 +469,13 @@ const transform = (
468469
if (!result.result) return reject(new Error("No result from loaders"));
469470
const [source, map] = result.result;
470471
resolve({
471-
source: Buffer.isBuffer(source) ? {binary: source.toString('base64')} : source,
472+
source: Buffer.isBuffer(source) ? { binary: source.toString('base64') } : source,
472473
map:
473474
typeof map === "string"
474475
? map
475476
: typeof map === "object"
476-
? JSON.stringify(map)
477-
: undefined,
477+
? JSON.stringify(map)
478+
: undefined,
478479
});
479480
}
480481
);
@@ -494,15 +495,15 @@ function makeErrorEmitter(
494495
error:
495496
error instanceof Error
496497
? {
497-
name: error.name,
498-
message: error.message,
499-
stack: parseStackTrace(error.stack),
500-
}
498+
name: error.name,
499+
message: error.message,
500+
stack: parseStackTrace(error.stack),
501+
}
501502
: {
502-
name: "Error",
503-
message: error,
504-
stack: [],
505-
},
503+
name: "Error",
504+
message: error,
505+
stack: [],
506+
},
506507
});
507508
};
508509
}

turbopack/crates/turbopack-node/src/transforms/webpack.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,9 @@ impl WebpackLoadersProcessedAsset {
232232
resolve_options_context: Some(transform.resolve_options_context),
233233
args: vec![
234234
Vc::cell(content.into()),
235+
// We need to pass the query string to the loader
235236
Vc::cell(resource_path.to_string().into()),
237+
Vc::cell(this.source.ident().query().await?.to_string().into()),
236238
Vc::cell(json!(*loaders)),
237239
],
238240
additional_invalidation: Completion::immutable(),

0 commit comments

Comments
 (0)