From 4c9efbe056434d840d7c9ad7c684faa010c58db0 Mon Sep 17 00:00:00 2001 From: bohan Date: Mon, 27 Nov 2023 14:46:13 +0800 Subject: [PATCH] fix: keep the query passed to loader (#4782) --- packages/rspack/src/config/adapter.ts | 5 +- packages/rspack/src/config/adapterRuleUse.ts | 6 +- packages/rspack/src/loader-runner/index.ts | 8 ++- .../configCases/module/issue-4777/index.js | 18 +++++ .../configCases/module/issue-4777/loader.js | 3 + .../configCases/module/issue-4777/sub/a.js | 1 + .../configCases/module/issue-4777/sub/b.js | 1 + .../configCases/module/issue-4777/sub/c.js | 1 + .../configCases/module/issue-4777/sub/d.js | 1 + .../configCases/module/issue-4777/sub/e.js | 1 + .../configCases/module/issue-4777/sub/f.js | 1 + .../configCases/module/issue-4777/sub/g.js | 1 + .../configCases/module/issue-4777/sub/h.js | 1 + .../module/issue-4777/webpack.config.js | 70 +++++++++++++++++++ .../loaders/issue-3320/test.filter.js | 1 - 15 files changed, 110 insertions(+), 9 deletions(-) create mode 100644 packages/rspack/tests/configCases/module/issue-4777/index.js create mode 100644 packages/rspack/tests/configCases/module/issue-4777/loader.js create mode 100644 packages/rspack/tests/configCases/module/issue-4777/sub/a.js create mode 100644 packages/rspack/tests/configCases/module/issue-4777/sub/b.js create mode 100644 packages/rspack/tests/configCases/module/issue-4777/sub/c.js create mode 100644 packages/rspack/tests/configCases/module/issue-4777/sub/d.js create mode 100644 packages/rspack/tests/configCases/module/issue-4777/sub/e.js create mode 100644 packages/rspack/tests/configCases/module/issue-4777/sub/f.js create mode 100644 packages/rspack/tests/configCases/module/issue-4777/sub/g.js create mode 100644 packages/rspack/tests/configCases/module/issue-4777/sub/h.js create mode 100644 packages/rspack/tests/configCases/module/issue-4777/webpack.config.js delete mode 100644 webpack-test/configCases/loaders/issue-3320/test.filter.js diff --git a/packages/rspack/src/config/adapter.ts b/packages/rspack/src/config/adapter.ts index a7d20b1dbbb..4c2a0a9b878 100644 --- a/packages/rspack/src/config/adapter.ts +++ b/packages/rspack/src/config/adapter.ts @@ -18,7 +18,8 @@ import type { RawRspackFuture, RawLibraryName, RawLibraryOptions, - JsModule + JsModule, + RawModuleRuleUse } from "@rspack/binding"; import assert from "assert"; import { Compiler } from "../Compiler"; @@ -378,7 +379,7 @@ const getRawModuleRule = ( } ]; } - let funcUse; + let funcUse: undefined | ((rawContext: RawFuncUseCtx) => RawModuleRuleUse[]); if (typeof rule.use === "function") { funcUse = (rawContext: RawFuncUseCtx) => { const context = { diff --git a/packages/rspack/src/config/adapterRuleUse.ts b/packages/rspack/src/config/adapterRuleUse.ts index 37ee4b2df87..a19c06daba3 100644 --- a/packages/rspack/src/config/adapterRuleUse.ts +++ b/packages/rspack/src/config/adapterRuleUse.ts @@ -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); diff --git a/packages/rspack/src/loader-runner/index.ts b/packages/rspack/src/loader-runner/index.ts index ec64b034c82..4b59b0f6def 100644 --- a/packages/rspack/src/loader-runner/index.ts +++ b/packages/rspack/src/loader-runner/index.ts @@ -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) @@ -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; } diff --git a/packages/rspack/tests/configCases/module/issue-4777/index.js b/packages/rspack/tests/configCases/module/issue-4777/index.js new file mode 100644 index 00000000000..ed18a0d7540 --- /dev/null +++ b/packages/rspack/tests/configCases/module/issue-4777/index.js @@ -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" + }); +}); diff --git a/packages/rspack/tests/configCases/module/issue-4777/loader.js b/packages/rspack/tests/configCases/module/issue-4777/loader.js new file mode 100644 index 00000000000..b414ad9f945 --- /dev/null +++ b/packages/rspack/tests/configCases/module/issue-4777/loader.js @@ -0,0 +1,3 @@ +module.exports = function () { + return `module.exports=${JSON.stringify(this.query)}`; +}; diff --git a/packages/rspack/tests/configCases/module/issue-4777/sub/a.js b/packages/rspack/tests/configCases/module/issue-4777/sub/a.js new file mode 100644 index 00000000000..38b08af1d77 --- /dev/null +++ b/packages/rspack/tests/configCases/module/issue-4777/sub/a.js @@ -0,0 +1 @@ +module.exports = ""; diff --git a/packages/rspack/tests/configCases/module/issue-4777/sub/b.js b/packages/rspack/tests/configCases/module/issue-4777/sub/b.js new file mode 100644 index 00000000000..38b08af1d77 --- /dev/null +++ b/packages/rspack/tests/configCases/module/issue-4777/sub/b.js @@ -0,0 +1 @@ +module.exports = ""; diff --git a/packages/rspack/tests/configCases/module/issue-4777/sub/c.js b/packages/rspack/tests/configCases/module/issue-4777/sub/c.js new file mode 100644 index 00000000000..38b08af1d77 --- /dev/null +++ b/packages/rspack/tests/configCases/module/issue-4777/sub/c.js @@ -0,0 +1 @@ +module.exports = ""; diff --git a/packages/rspack/tests/configCases/module/issue-4777/sub/d.js b/packages/rspack/tests/configCases/module/issue-4777/sub/d.js new file mode 100644 index 00000000000..38b08af1d77 --- /dev/null +++ b/packages/rspack/tests/configCases/module/issue-4777/sub/d.js @@ -0,0 +1 @@ +module.exports = ""; diff --git a/packages/rspack/tests/configCases/module/issue-4777/sub/e.js b/packages/rspack/tests/configCases/module/issue-4777/sub/e.js new file mode 100644 index 00000000000..38b08af1d77 --- /dev/null +++ b/packages/rspack/tests/configCases/module/issue-4777/sub/e.js @@ -0,0 +1 @@ +module.exports = ""; diff --git a/packages/rspack/tests/configCases/module/issue-4777/sub/f.js b/packages/rspack/tests/configCases/module/issue-4777/sub/f.js new file mode 100644 index 00000000000..38b08af1d77 --- /dev/null +++ b/packages/rspack/tests/configCases/module/issue-4777/sub/f.js @@ -0,0 +1 @@ +module.exports = ""; diff --git a/packages/rspack/tests/configCases/module/issue-4777/sub/g.js b/packages/rspack/tests/configCases/module/issue-4777/sub/g.js new file mode 100644 index 00000000000..38b08af1d77 --- /dev/null +++ b/packages/rspack/tests/configCases/module/issue-4777/sub/g.js @@ -0,0 +1 @@ +module.exports = ""; diff --git a/packages/rspack/tests/configCases/module/issue-4777/sub/h.js b/packages/rspack/tests/configCases/module/issue-4777/sub/h.js new file mode 100644 index 00000000000..38b08af1d77 --- /dev/null +++ b/packages/rspack/tests/configCases/module/issue-4777/sub/h.js @@ -0,0 +1 @@ +module.exports = ""; diff --git a/packages/rspack/tests/configCases/module/issue-4777/webpack.config.js b/packages/rspack/tests/configCases/module/issue-4777/webpack.config.js new file mode 100644 index 00000000000..85eca0bfb3d --- /dev/null +++ b/packages/rspack/tests/configCases/module/issue-4777/webpack.config.js @@ -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" + } + } + } + ] + } +}; diff --git a/webpack-test/configCases/loaders/issue-3320/test.filter.js b/webpack-test/configCases/loaders/issue-3320/test.filter.js deleted file mode 100644 index 4108ffb37d9..00000000000 --- a/webpack-test/configCases/loaders/issue-3320/test.filter.js +++ /dev/null @@ -1 +0,0 @@ -module.exports = () => {return true} \ No newline at end of file