-
-
Notifications
You must be signed in to change notification settings - Fork 855
fix(loader): preserve string BOM across native boundaries #15632
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
136fa4b
fix(loader): preserve string BOM across native boundaries
intellild 47b99e2
fix(loader): preserve UTF-8 hint for pitch results
intellild 47688a8
fix(loader): distinguish content types in cache keys
intellild 4d7a08a
refactor(loader): preserve content types across native boundaries
intellild 5b1431c
fix(loader): preserve content types during source map extraction
intellild 9252ebe
test(loader): isolate loader cache matrix outputs
intellild File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
30 changes: 30 additions & 0 deletions
30
tests/rspack-test/configCases/loader/utf8-hint-bom/index.js
This file contains hidden or 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 @@ | ||
| it("should preserve a string BOM across loader boundaries", () => { | ||
| expect(require("./input.js?string-normal")).toBe("\ufeffhello"); | ||
| }); | ||
|
|
||
| it("should strip a Buffer BOM when converting to a string", () => { | ||
| expect(require("./input.js?buffer-normal")).toBe("hello"); | ||
| }); | ||
|
|
||
| it("should preserve BOM bytes for raw loaders", () => { | ||
| expect(require("./input.js?string-raw")).toBe("efbbbf68656c6c6f"); | ||
| expect(require("./input.js?buffer-raw")).toBe("efbbbf68656c6c6f"); | ||
| }); | ||
|
|
||
| it("should strip a resource BOM for normal loaders without a source map", () => { | ||
| expect(require("./resource.txt?resource-plain-normal")).toBe("hello\n"); | ||
| expect(require("./resource.txt?resource-extract-normal")).toBe("hello\n"); | ||
| }); | ||
|
|
||
| it("should preserve resource BOM bytes for raw loaders without a source map", () => { | ||
| expect(require("./resource.txt?resource-plain-raw")).toBe("efbbbf68656c6c6f0a"); | ||
| expect(require("./resource.txt?resource-extract-raw")).toBe("efbbbf68656c6c6f0a"); | ||
| }); | ||
|
|
||
| it("should strip a resource BOM for normal loaders after extracting a source map", () => { | ||
| expect(require("./resource-with-map.txt?resource-extract-normal")).toBe("hello\n"); | ||
| }); | ||
|
|
||
| it("should preserve resource BOM bytes for raw loaders after extracting a source map", () => { | ||
| expect(require("./resource-with-map.txt?resource-extract-raw")).toBe("efbbbf68656c6c6f0a"); | ||
| }); |
This file contains hidden or 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 @@ | ||
| // Replaced by producer-loader. |
3 changes: 3 additions & 0 deletions
3
tests/rspack-test/configCases/loader/utf8-hint-bom/normal-loader.js
This file contains hidden or 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 = function (content) { | ||
| return `module.exports = ${JSON.stringify(content)};`; | ||
| }; |
5 changes: 5 additions & 0 deletions
5
tests/rspack-test/configCases/loader/utf8-hint-bom/pitch-loader.js
This file contains hidden or 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,5 @@ | ||
| module.exports = function () { | ||
| throw new Error("A pitch result should skip the producer normal function"); | ||
| }; | ||
|
|
||
| module.exports.pitch = require("./producer-loader"); |
4 changes: 4 additions & 0 deletions
4
tests/rspack-test/configCases/loader/utf8-hint-bom/producer-loader.js
This file contains hidden or 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 @@ | ||
| module.exports = function () { | ||
| const content = "\ufeffhello"; | ||
| return this.getOptions().kind === "string" ? content : Buffer.from(content); | ||
| }; |
5 changes: 5 additions & 0 deletions
5
tests/rspack-test/configCases/loader/utf8-hint-bom/raw-loader.js
This file contains hidden or 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,5 @@ | ||
| module.exports = function (content) { | ||
| if (!Buffer.isBuffer(content)) throw new Error("Expected a Buffer"); | ||
| return `module.exports = ${JSON.stringify(content.toString("hex"))};`; | ||
| }; | ||
| module.exports.raw = true; |
2 changes: 2 additions & 0 deletions
2
tests/rspack-test/configCases/loader/utf8-hint-bom/resource-with-map.txt
This file contains hidden or 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,2 @@ | ||
| hello | ||
| //# sourceMappingURL=resource.map |
1 change: 1 addition & 0 deletions
1
tests/rspack-test/configCases/loader/utf8-hint-bom/resource.map
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
1 change: 1 addition & 0 deletions
1
tests/rspack-test/configCases/loader/utf8-hint-bom/resource.txt
This file contains hidden or 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 @@ | ||
| hello |
53 changes: 53 additions & 0 deletions
53
tests/rspack-test/configCases/loader/utf8-hint-bom/rspack.config.js
This file contains hidden or 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,53 @@ | ||
| module.exports = [false, true].flatMap((pitch) => | ||
| [false, true].flatMap((parallel) => | ||
| [false, true].map((mixed) => ({ | ||
| module: { | ||
| rules: [ | ||
| ...['string', 'buffer'].flatMap((kind) => | ||
| [false, true].map((raw) => ({ | ||
| resourceQuery: new RegExp( | ||
| `^\\?${kind}-${raw ? 'raw' : 'normal'}$`, | ||
| ), | ||
| use: [ | ||
| { | ||
| loader: require.resolve( | ||
| raw ? './raw-loader' : './normal-loader', | ||
| ), | ||
| options: {}, | ||
| parallel: parallel ? { maxWorkers: 1 } : false, | ||
| }, | ||
| ...(mixed ? ['builtin:test-passthrough-loader'] : []), | ||
| { | ||
| loader: require.resolve( | ||
| pitch ? './pitch-loader' : './producer-loader', | ||
| ), | ||
| options: { kind }, | ||
| parallel: parallel ? { maxWorkers: 1 } : false, | ||
| }, | ||
| ], | ||
| })), | ||
| ), | ||
| ...[false, true].flatMap((extractSourceMap) => | ||
| [false, true].map((raw) => ({ | ||
| resourceQuery: new RegExp( | ||
| `^\\?resource-${extractSourceMap ? 'extract' : 'plain'}-${raw ? 'raw' : 'normal'}$`, | ||
| ), | ||
| type: 'javascript/auto', | ||
| extractSourceMap, | ||
| use: [ | ||
| { | ||
| loader: require.resolve( | ||
| raw ? './raw-loader' : './normal-loader', | ||
| ), | ||
| options: {}, | ||
| parallel: parallel ? { maxWorkers: 1 } : false, | ||
| }, | ||
| ...(mixed ? ['builtin:test-passthrough-loader'] : []), | ||
| ], | ||
| })), | ||
| ), | ||
| ], | ||
| }, | ||
| })), | ||
| ), | ||
| ); |
15 changes: 15 additions & 0 deletions
15
tests/rspack-test/watchCases/loaders/loader-cache-content-type/0/index.js
This file contains hidden or 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,15 @@ | ||
| const stringFirst = require("./input.txt?string-first"); | ||
| const bufferFirst = require("./input.txt?buffer-first"); | ||
|
|
||
| it("should distinguish equal bytes with different content types in the loader cache", () => { | ||
| const step = +WATCH_STEP; | ||
| const runs = LOADER_CACHE_ENABLED ? (step < 2 ? 1 : 2) : step + 1; | ||
| expect(stringFirst).toEqual({ | ||
| content: step < 2 ? "\uFEFFhello" : "hello", | ||
| runs, | ||
| }); | ||
| expect(bufferFirst).toEqual({ | ||
| content: step < 2 ? "hello" : "\uFEFFhello", | ||
| runs, | ||
| }); | ||
| }); |
1 change: 1 addition & 0 deletions
1
tests/rspack-test/watchCases/loaders/loader-cache-content-type/0/input.txt
This file contains hidden or 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 @@ | ||
| 0 |
1 change: 1 addition & 0 deletions
1
tests/rspack-test/watchCases/loaders/loader-cache-content-type/1/input.txt
This file contains hidden or 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 @@ | ||
| 1 |
1 change: 1 addition & 0 deletions
1
tests/rspack-test/watchCases/loaders/loader-cache-content-type/2/input.txt
This file contains hidden or 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 @@ | ||
| 2 |
1 change: 1 addition & 0 deletions
1
tests/rspack-test/watchCases/loaders/loader-cache-content-type/3/input.txt
This file contains hidden or 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 |
8 changes: 8 additions & 0 deletions
8
tests/rspack-test/watchCases/loaders/loader-cache-content-type/consumer-loader.js
This file contains hidden or 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,8 @@ | ||
| const runs = new Map(); | ||
|
|
||
| module.exports = function (source) { | ||
| const key = `${this.getOptions().name}:${this.resource}`; | ||
| const count = (runs.get(key) || 0) + 1; | ||
| runs.set(key, count); | ||
| return `module.exports = ${JSON.stringify({ content: source, runs: count })};`; | ||
| }; |
6 changes: 6 additions & 0 deletions
6
tests/rspack-test/watchCases/loaders/loader-cache-content-type/producer-loader.js
This file contains hidden or 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,6 @@ | ||
| module.exports = function (source) { | ||
| const firstType = this.resourceQuery === "?string-first"; | ||
| const isString = Number(source.trim()) < 2 ? firstType : !firstType; | ||
| const content = "\uFEFFhello"; | ||
| return isString ? content : Buffer.from(content); | ||
| }; |
42 changes: 42 additions & 0 deletions
42
tests/rspack-test/watchCases/loaders/loader-cache-content-type/rspack.config.js
This file contains hidden or 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,42 @@ | ||
| const { rspack } = require('@rspack/core'); | ||
|
|
||
| module.exports = [false, true].flatMap((cache) => | ||
| [false, true].flatMap((parallel) => | ||
| [false, true].map((mixed) => ({ | ||
| mode: 'development', | ||
| output: { | ||
| filename: `bundle-${cache}-${parallel}-${mixed}.js`, | ||
| }, | ||
| incremental: false, | ||
| cache: cache ? { type: 'memory' } : false, | ||
| experiments: { | ||
| newCache: { | ||
| codeGeneration: false, | ||
| loader: cache, | ||
| minimize: false, | ||
| }, | ||
| }, | ||
| module: { | ||
| rules: [ | ||
| { | ||
| test: /input\.txt$/, | ||
| type: 'javascript/auto', | ||
| use: [ | ||
| { | ||
| loader: require.resolve('./consumer-loader'), | ||
| options: { name: `${cache}-${parallel}-${mixed}` }, | ||
| cache, | ||
| parallel: parallel ? { maxWorkers: 1 } : false, | ||
| }, | ||
| ...(mixed | ||
| ? [{ loader: 'builtin:test-passthrough-loader', cache }] | ||
| : []), | ||
| { loader: require.resolve('./producer-loader') }, | ||
| ], | ||
| }, | ||
| ], | ||
| }, | ||
| plugins: [new rspack.DefinePlugin({ LOADER_CACHE_ENABLED: cache })], | ||
| })), | ||
| ), | ||
| ); |
5 changes: 5 additions & 0 deletions
5
tests/rspack-test/watchCases/loaders/loader-cache-content-type/test.config.js
This file contains hidden or 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,5 @@ | ||
| module.exports = { | ||
| findBundle(_index, options) { | ||
| return options.output.filename; | ||
| }, | ||
| }; |
1 change: 1 addition & 0 deletions
1
tests/rspack-test/watchCases/loaders/loader-cache-content-type/test.filter.js
This file contains hidden or 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 @@ | ||
| module.exports = () => !process.env.WASM; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.