-
-
Notifications
You must be signed in to change notification settings - Fork 587
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support passing query to loader (#4418)
* feat: pass loader test `option` * test: more test cases * chore: cleanup * test * use correct version css-loader --------- Co-authored-by: ahabhgk <ahabhgk@gmail.com>
- Loading branch information
Showing
36 changed files
with
794 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
packages/rspack/tests/configCases/builtin-swc-loader/source-map/a.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
let foo: string = "f1"; | ||
let bar: string = "b1"; | ||
let baz: string = "b2"; | ||
const boo = <string>"abc"; |
51 changes: 51 additions & 0 deletions
51
packages/rspack/tests/configCases/builtin-swc-loader/source-map/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
require("./a"); | ||
|
||
it("should generate correct sourceMap", async () => { | ||
const path = require("path"); | ||
const fs = require("fs"); | ||
const source = fs.readFileSync(__filename + ".map", "utf-8"); | ||
const map = JSON.parse(source); | ||
const sourceContent = fs.readFileSync( | ||
path.resolve(__dirname, "../a.ts"), | ||
"utf-8" | ||
); | ||
expect(map.sources).toContain("./a.ts"); | ||
expect(map.sourcesContent[1]).toEqual(sourceContent); | ||
|
||
checkStub("fo" + "o", sourceContent); | ||
checkStub("ba" + "r", sourceContent); | ||
checkStub("ba" + "z", sourceContent); | ||
checkStub(wrap("f" + 1), sourceContent); | ||
checkStub(wrap("b" + 1), sourceContent); | ||
checkStub(wrap("b" + 2), sourceContent); | ||
checkStub(wrap("ab" + "c"), sourceContent); | ||
}); | ||
|
||
const wrap = v => `"${v}"`; | ||
const checkStub = async (stub, sourceContent) => { | ||
const fs = require("fs"); | ||
const { SourceMapConsumer } = require("source-map"); | ||
|
||
const source = fs.readFileSync(__filename + ".map", "utf-8"); | ||
const map = JSON.parse(source); | ||
const consumer = await new SourceMapConsumer(map); | ||
const generated = fs.readFileSync(__filename, "utf-8"); | ||
const { line, column } = consumer.originalPositionFor( | ||
positionFor(generated, stub) | ||
); | ||
const { line: originalLine, column: originalColumn } = positionFor( | ||
sourceContent, | ||
stub | ||
); | ||
expect(line).toBe(originalLine); | ||
expect(column).toBe(originalColumn); | ||
}; | ||
|
||
const positionFor = (content, text) => { | ||
let lines = content.split(/\r?\n/); | ||
for (let i = 0; i < lines.length; i++) { | ||
const column = lines[i].indexOf(text); | ||
if (column >= 0) return { line: i + 1, column }; | ||
} | ||
return null; | ||
}; |
30 changes: 30 additions & 0 deletions
30
packages/rspack/tests/configCases/builtin-swc-loader/source-map/webpack.config.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
module.exports = { | ||
devtool: "source-map", | ||
externals: ["source-map"], | ||
externalsType: "commonjs", | ||
module: { | ||
rules: [ | ||
{ | ||
test: /\.ts$/, | ||
use: [ | ||
{ | ||
loader: "builtin:swc-loader", | ||
options: { | ||
jsc: { | ||
parser: { | ||
syntax: "typescript" | ||
} | ||
} | ||
} | ||
} | ||
], | ||
type: "javascript/auto" | ||
} | ||
] | ||
}, | ||
experiments: { | ||
rspackFuture: { | ||
disableTransformByDefault: true | ||
} | ||
} | ||
}; |
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
it("should get options", function () { | ||
expect(require("./a")).toStrictEqual({ | ||
arg: true, | ||
arg1: null, | ||
arg3: 1234567890, | ||
arg4: "string", | ||
arg5: [1, 2, 3], | ||
arg6: { foo: "value", bar: { baz: "other-value" } } | ||
}); | ||
expect(require("./b")).toStrictEqual({ | ||
arg: true, | ||
arg1: null, | ||
arg3: 1234567890, | ||
arg4: "string", | ||
arg5: [1, 2, 3], | ||
arg6: { foo: "value", bar: { baz: "other-value" } } | ||
}); | ||
expect(require("./c")).toStrictEqual({ | ||
arg: true, | ||
arg1: null, | ||
arg3: 1234567890, | ||
arg4: "string", | ||
arg5: [1, 2, 3], | ||
arg6: { foo: "value", bar: { baz: "other-value" } } | ||
}); | ||
expect(require("./d")).toStrictEqual({ | ||
arg4: "text" | ||
}); | ||
expect(require("./e")).toStrictEqual({}); | ||
expect(require("./f")).toStrictEqual({ | ||
delicious: "", | ||
name: "cheesecake", | ||
slices: "8", | ||
warm: "false" | ||
}); | ||
expect(require("./g")).toStrictEqual({ | ||
"=": "=" | ||
}); | ||
expect(require("./h")).toStrictEqual({ | ||
foo: "bar" | ||
}); | ||
expect(require("./i")).toStrictEqual({ | ||
foo: "bar" | ||
}); | ||
}); |
3 changes: 3 additions & 0 deletions
3
packages/rspack/tests/configCases/loader/options/infrastructure-log.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module.exports = [ | ||
/^Pack got invalid because of write to: Compilation\/modules.+loaders[/\\]options[/\\]error1\.js$/ | ||
]; |
12 changes: 12 additions & 0 deletions
12
packages/rspack/tests/configCases/loader/options/loader-1.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
const schema = require("./loader-1.options.json"); | ||
|
||
/** @type {import("@rspack/core").LoaderDefinition} */ | ||
module.exports = function () { | ||
const options = this.getOptions(schema); | ||
|
||
const json = JSON.stringify(options) | ||
.replace(/\u2028/g, "\\u2028") | ||
.replace(/\u2029/g, "\\u2029"); | ||
|
||
return `module.exports = ${json}`; | ||
}; |
43 changes: 43 additions & 0 deletions
43
packages/rspack/tests/configCases/loader/options/loader-1.options.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
{ | ||
"additionalProperties": false, | ||
"properties": { | ||
"arg": { | ||
"type": "boolean" | ||
}, | ||
"arg1": { | ||
"type": "null" | ||
}, | ||
"arg2": {}, | ||
"arg3": { | ||
"type": "number" | ||
}, | ||
"arg4": { | ||
"type": "string" | ||
}, | ||
"arg5": { | ||
"type": "array", | ||
"items": { | ||
"type": "number" | ||
} | ||
}, | ||
"arg6": { | ||
"type": "object", | ||
"properties": { | ||
"foo": { | ||
"type": "string" | ||
}, | ||
"bar": { | ||
"type": "object", | ||
"properties": { | ||
"baz": { | ||
"type": "string" | ||
} | ||
}, | ||
"additionalProperties": false | ||
} | ||
}, | ||
"additionalProperties": false | ||
} | ||
}, | ||
"type": "object" | ||
} |
12 changes: 12 additions & 0 deletions
12
packages/rspack/tests/configCases/loader/options/loader-2.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
const schema = require("./loader-2.options.json"); | ||
|
||
/** @type {import("@rspack/core").LoaderDefinition} */ | ||
module.exports = function () { | ||
const options = this.getOptions(schema); | ||
|
||
const json = JSON.stringify(options) | ||
.replace(/\u2028/g, "\\u2028") | ||
.replace(/\u2029/g, "\\u2029"); | ||
|
||
return `module.exports = ${json}`; | ||
}; |
10 changes: 10 additions & 0 deletions
10
packages/rspack/tests/configCases/loader/options/loader-2.options.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"title": "Custom Loader Name configuration", | ||
"additionalProperties": false, | ||
"properties": { | ||
"arg": { | ||
"enum": [true] | ||
} | ||
}, | ||
"type": "object" | ||
} |
10 changes: 10 additions & 0 deletions
10
packages/rspack/tests/configCases/loader/options/loader.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
/** @type {import("@rspack/core").LoaderDefinition} */ | ||
module.exports = function () { | ||
const options = this.getOptions(); | ||
|
||
const json = JSON.stringify(options) | ||
.replace(/\u2028/g, "\\u2028") | ||
.replace(/\u2029/g, "\\u2029"); | ||
|
||
return `module.exports = ${json}`; | ||
}; |
Oops, something went wrong.