Unverified Commit 6853c6f5 authored by Donny/강동윤's avatar Donny/강동윤 Committed by GitHub

fix(turbopack): Pass `resourceQuery` to loaders (#69703)

### What?

Pass `resourceQuery` to loaders correctly.

### Why?

To be consistent with webpack

### How?

Closes https://github.com/vercel/next.js/issues/69502

---------
Co-authored-by: 's avatarTobias Koppers <tobias.koppers@googlemail.com>
parent 8662ba05
import { ReactNode } from 'react'
export default function Root({ children }: { children: ReactNode }) {
return (
<html>
<body>{children}</body>
</html>
)
}
// @ts-expect-error -- ignore
import { v } from './test.mdx?test=hi'
export default function Page() {
console.log(v)
return <p>hello world</p>
}
/** @type {import('next').NextConfig} */
const nextConfig = {
experimental: {
turbo: {
rules: {
'*.mdx': {
loaders: ['test-loader.js'],
as: '*.js',
},
},
},
},
}
module.exports = nextConfig
module.exports = function loader(code) {
console.log('resource query: ', this.resourceQuery)
return 'export const v = "anything"'
}
import { nextTestSetup } from 'e2e-utils'
;(process.env.TURBOPACK ? describe : describe.skip)(
'turbopack-loader-resource-query',
() => {
const { next } = nextTestSetup({
files: __dirname,
})
// Recommended for tests that check HTML. Cheerio is a HTML parser that has a jQuery like API.
it('should pass query to loader', async () => {
await next.render$('/')
expect(next.cliOutput).toContain('resource query: ?test=hi')
})
}
)
......@@ -54,7 +54,7 @@ impl ValueToString for AssetIdent {
let query = self.query.await?;
if !query.is_empty() {
write!(s, "?{}", &*query)?;
write!(s, "{}", &*query)?;
}
if let Some(fragment) = &self.fragment {
......
......@@ -19,30 +19,30 @@ import { type StructuredError } from "src/ipc";
export type IpcInfoMessage =
| {
type: "fileDependency";
path: string;
}
type: "fileDependency";
path: string;
}
| {
type: "buildDependency";
path: string;
}
type: "buildDependency";
path: string;
}
| {
type: "dirDependency";
path: string;
glob: string;
}
type: "dirDependency";
path: string;
glob: string;
}
| {
type: "emittedError";
severity: "warning" | "error";
error: StructuredError;
}
type: "emittedError";
severity: "warning" | "error";
error: StructuredError;
}
| {
type: "log";
time: number;
logType: string;
args: any[];
trace?: StackFrame[];
};
type: "log";
time: number;
logType: string;
args: any[];
trace?: StackFrame[];
};
export type IpcRequestMessage = {
type: "resolve";
......@@ -54,9 +54,9 @@ export type IpcRequestMessage = {
type LoaderConfig =
| string
| {
loader: string;
options: { [k: string]: unknown };
};
loader: string;
options: { [k: string]: unknown };
};
let runLoaders: typeof import("loader-runner")["runLoaders"];
try {
......@@ -168,6 +168,7 @@ const transform = (
ipc: Ipc<IpcInfoMessage, IpcRequestMessage>,
content: string,
name: string,
query: string,
loaders: LoaderConfig[]
) => {
return new Promise((resolve, reject) => {
......@@ -180,7 +181,7 @@ const transform = (
runLoaders(
{
resource,
resource: resource + query,
context: {
_module: {
// For debugging purpose, if someone find context is not full compatible to
......@@ -468,13 +469,13 @@ const transform = (
if (!result.result) return reject(new Error("No result from loaders"));
const [source, map] = result.result;
resolve({
source: Buffer.isBuffer(source) ? {binary: source.toString('base64')} : source,
source: Buffer.isBuffer(source) ? { binary: source.toString('base64') } : source,
map:
typeof map === "string"
? map
: typeof map === "object"
? JSON.stringify(map)
: undefined,
? JSON.stringify(map)
: undefined,
});
}
);
......@@ -494,15 +495,15 @@ function makeErrorEmitter(
error:
error instanceof Error
? {
name: error.name,
message: error.message,
stack: parseStackTrace(error.stack),
}
name: error.name,
message: error.message,
stack: parseStackTrace(error.stack),
}
: {
name: "Error",
message: error,
stack: [],
},
name: "Error",
message: error,
stack: [],
},
});
};
}
......@@ -232,7 +232,9 @@ impl WebpackLoadersProcessedAsset {
resolve_options_context: Some(transform.resolve_options_context),
args: vec![
Vc::cell(content.into()),
// We need to pass the query string to the loader
Vc::cell(resource_path.to_string().into()),
Vc::cell(this.source.ident().query().await?.to_string().into()),
Vc::cell(json!(*loaders)),
],
additional_invalidation: Completion::immutable(),
......
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