mirror of
https://github.com/angular/angular-cli.git
synced 2025-05-23 15:36:23 +08:00
refactor(@angular-devkit/build-angular): use async pipeline helper in HTML rewriting stream
The Node.js async `pipeline` helper function reduces the amount of infrastructure code needed to pipe HTML content through the parse5 transform stream.
This commit is contained in:
parent
ffeb3eb74e
commit
b55bbde9c3
@ -6,7 +6,8 @@
|
|||||||
* found in the LICENSE file at https://angular.io/license
|
* found in the LICENSE file at https://angular.io/license
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import { Readable, Writable } from 'stream';
|
import { Readable } from 'node:stream';
|
||||||
|
import { pipeline } from 'node:stream/promises';
|
||||||
import { loadEsmModule } from '../load-esm';
|
import { loadEsmModule } from '../load-esm';
|
||||||
|
|
||||||
export async function htmlRewritingStream(content: string): Promise<{
|
export async function htmlRewritingStream(content: string): Promise<{
|
||||||
@ -16,42 +17,18 @@ export async function htmlRewritingStream(content: string): Promise<{
|
|||||||
const { RewritingStream } = await loadEsmModule<typeof import('parse5-html-rewriting-stream')>(
|
const { RewritingStream } = await loadEsmModule<typeof import('parse5-html-rewriting-stream')>(
|
||||||
'parse5-html-rewriting-stream',
|
'parse5-html-rewriting-stream',
|
||||||
);
|
);
|
||||||
const chunks: Buffer[] = [];
|
|
||||||
const rewriter = new RewritingStream();
|
const rewriter = new RewritingStream();
|
||||||
|
|
||||||
return {
|
return {
|
||||||
rewriter,
|
rewriter,
|
||||||
transformedContent: () => {
|
transformedContent: () =>
|
||||||
return new Promise((resolve) => {
|
pipeline(Readable.from(content), rewriter, async function (source) {
|
||||||
new Readable({
|
const chunks = [];
|
||||||
encoding: 'utf8',
|
for await (const chunk of source) {
|
||||||
read(): void {
|
chunks.push(Buffer.from(chunk));
|
||||||
this.push(Buffer.from(content));
|
}
|
||||||
this.push(null);
|
|
||||||
},
|
return Buffer.concat(chunks).toString('utf-8');
|
||||||
})
|
}),
|
||||||
.pipe(rewriter)
|
|
||||||
.pipe(
|
|
||||||
new Writable({
|
|
||||||
write(
|
|
||||||
chunk: string | Buffer,
|
|
||||||
encoding: string | undefined,
|
|
||||||
callback: Function,
|
|
||||||
): void {
|
|
||||||
chunks.push(
|
|
||||||
typeof chunk === 'string'
|
|
||||||
? Buffer.from(chunk, encoding as BufferEncoding)
|
|
||||||
: chunk,
|
|
||||||
);
|
|
||||||
callback();
|
|
||||||
},
|
|
||||||
final(callback: (error?: Error) => void): void {
|
|
||||||
callback();
|
|
||||||
resolve(Buffer.concat(chunks).toString());
|
|
||||||
},
|
|
||||||
}),
|
|
||||||
);
|
|
||||||
});
|
|
||||||
},
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user