Skip to content

Commit

Permalink
fix: keep the query passed to loader (#4782)
Browse files Browse the repository at this point in the history
  • Loading branch information
bvanjoi committed Nov 27, 2023
1 parent ecde5ad commit 4c9efbe
Show file tree
Hide file tree
Showing 15 changed files with 110 additions and 9 deletions.
5 changes: 3 additions & 2 deletions packages/rspack/src/config/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ import type {
RawRspackFuture,
RawLibraryName,
RawLibraryOptions,
JsModule
JsModule,
RawModuleRuleUse
} from "@rspack/binding";
import assert from "assert";
import { Compiler } from "../Compiler";
Expand Down Expand Up @@ -378,7 +379,7 @@ const getRawModuleRule = (
}
];
}
let funcUse;
let funcUse: undefined | ((rawContext: RawFuncUseCtx) => RawModuleRuleUse[]);
if (typeof rule.use === "function") {
funcUse = (rawContext: RawFuncUseCtx) => {
const context = {
Expand Down
6 changes: 3 additions & 3 deletions packages/rspack/src/config/adapterRuleUse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -318,9 +318,9 @@ function resolveStringifyLoaders(
const obj = parsePathQueryFragment(use.loader);
let ident: string | null = null;

if (use.options === null) obj.query = "";
else if (use.options === undefined) obj.query = "";
else if (typeof use.options === "string") obj.query = "?" + use.options;
if (use.options === null) {
} else if (use.options === undefined) {
} else if (typeof use.options === "string") obj.query = "?" + use.options;
else if (use.ident) obj.query = "??" + (ident = use.ident);
else if (typeof use.options === "object" && use.options.ident)
obj.query = "??" + (ident = use.options.ident);
Expand Down
8 changes: 5 additions & 3 deletions packages/rspack/src/loader-runner/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,8 @@ function createLoaderObject(loader: any, compiler: Compiler): LoaderObject {
obj.type = value.type;
obj.options = value.options;
obj.ident = value.ident;
if (obj.options === null) obj.query = "";
else if (obj.options === undefined) obj.query = "";
if (obj.options === null) obj.query = value.query;
else if (obj.options === undefined) obj.query = value.query;
else if (typeof obj.options === "string") obj.query = "?" + obj.options;
else if (obj.ident) obj.query = "??" + obj.ident;
else if (typeof obj.options === "object" && obj.options.ident)
Expand Down Expand Up @@ -187,7 +187,9 @@ export async function runLoaders(
}
obj.options = compiler.ruleSet.references.get(ident);
if (obj.options === undefined) {
throw new Error("Invalid ident is provided by referenced loader");
throw new Error(
`Invalid ident("${ident}") is provided by referenced loader`
);
}
obj.ident = ident;
}
Expand Down
18 changes: 18 additions & 0 deletions packages/rspack/tests/configCases/module/issue-4777/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
it("should loader the correctly query", () => {
expect(require("./sub/a")).toBe("?query=a");
// FIXME: should return `query` and `fragment` in rust side
// expect(require('./sub/b')).toBe('?query=alias')
// FIXME: should return `query` and `fragment` in rust side
// expect(require('./sub/c')).toBe('?query=alias')
expect(require("./sub/d")).toBe("?query=d");
expect(require("./sub/e")).toBe("?query=options-e");
expect(require("./sub/f")).toStrictEqual({
query: "options-object-f"
});
expect(require("./sub/g")).toStrictEqual({
query: "options-object-g"
});
expect(require("./sub/h")).toStrictEqual({
query: "options-object-h"
});
});
3 changes: 3 additions & 0 deletions packages/rspack/tests/configCases/module/issue-4777/loader.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = function () {
return `module.exports=${JSON.stringify(this.query)}`;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = "";
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = "";
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = "";
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = "";
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = "";
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = "";
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = "";
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = "";
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
module.exports = {
resolveLoader: {
alias: {
"my-loader": "./loader.js?query=alias"
}
},
module: {
rules: [
{
test: /a\.js$/,
use: {
loader: "./loader?query=a"
}
},
{
test: /b\.js$/,
use: {
loader: "my-loader"
}
},
{
test: /c\.js$/,
use: {
loader: "my-loader?query=c"
}
},
{
test: /d\.js$/,
use: {
loader: "./loader",
options: "query=d"
}
},
{
test: /e\.js$/,
use: {
loader: "./loader?query=e",
options: "query=options-e"
}
},
{
test: /f\.js$/,
use: {
loader: "./loader?query=f",
options: {
query: "options-object-f"
}
}
},
{
test: /g\.js$/,
use: {
loader: "my-loader",
options: {
query: "options-object-g"
}
}
},
{
test: /h\.js$/,
use: {
loader: "my-loader?query=h",
options: {
query: "options-object-h"
}
}
}
]
}
};
1 change: 0 additions & 1 deletion webpack-test/configCases/loaders/issue-3320/test.filter.js

This file was deleted.

0 comments on commit 4c9efbe

Please sign in to comment.