Skip to content

Commit 1f17c32

Browse files
authored
Merge pull request #134 from webpack/bugfix/concat-buffer
add support for concatenating Buffer RawSources
2 parents c15d951 + e283bef commit 1f17c32

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

lib/RawSource.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ class RawSource extends Source {
1818
this._valueIsBuffer = !convertToString && isBuffer;
1919
this._value = convertToString && isBuffer ? undefined : value;
2020
this._valueAsBuffer = isBuffer ? value : undefined;
21+
this._valueAsString = isBuffer ? undefined : value;
2122
}
2223

2324
isBuffer() {
@@ -51,10 +52,16 @@ class RawSource extends Source {
5152
*/
5253
streamChunks(options, onChunk, onSource, onName) {
5354
if (this._value === undefined) {
54-
this._value = this._valueAsBuffer.toString("utf-8");
55+
this._value = Buffer.from(this._valueAsBuffer, "utf-8");
56+
}
57+
if (this._valueAsString === undefined) {
58+
this._valueAsString =
59+
typeof this._value === "string"
60+
? this._value
61+
: this._value.toString("utf-8");
5562
}
5663
return streamChunksOfRawSource(
57-
this._value,
64+
this._valueAsString,
5865
onChunk,
5966
onSource,
6067
onName,

test/ConcatSource.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,4 +194,14 @@ describe("ConcatSource", () => {
194194
}
195195
`);
196196
});
197+
198+
it("should allow to concat buffer sources", () => {
199+
const source = new ConcatSource("a", new RawSource(Buffer.from("b")), "c");
200+
expect(source.sourceAndMap()).toMatchInlineSnapshot(`
201+
Object {
202+
"map": null,
203+
"source": "abc",
204+
}
205+
`);
206+
});
197207
});

0 commit comments

Comments
 (0)