Files
cypress/packages/rewriter/lib/html.ts
Lachlan Miller ab401ecd35 chore: use import type syntax (#17864)
* chore: use import type across repo

* chore: use import type across repo

* chore: use import type across repo

* chore: use import type across repo

* update exports

* update test

* update import type

* update types

* use import type in driver

* correctly export function

* revert test

* remove unrelated code

* revert code

* improve type imports

* override for reporter
2021-08-25 09:11:56 +10:00

33 lines
946 B
TypeScript

import RewritingStream from 'parse5-html-rewriting-stream'
import * as htmlRules from './html-rules'
import type stream from 'stream'
import type { DeferSourceMapRewriteFn } from './js'
// the HTML rewriter passes inline JS to the JS rewriter, hence
// the lack of basic `rewriteHtml` or `HtmlRewriter` exports here
export function HtmlJsRewriter (url: string, deferSourceMapRewrite?: DeferSourceMapRewriteFn): stream.Transform {
const rewriter = new RewritingStream()
htmlRules.install(url, rewriter, deferSourceMapRewrite)
return rewriter
}
export function rewriteHtmlJs (url: string, html: string, deferSourceMapRewrite?: DeferSourceMapRewriteFn): Promise<string> {
let out = ''
const rewriter = HtmlJsRewriter(url, deferSourceMapRewrite)
rewriter.on('data', (chunk) => {
out += chunk
})
rewriter.end(html)
return new Promise<string>((resolve) => {
rewriter.on('end', () => {
resolve(out)
})
})
}