| 25 | import { Schema as PwaOptions } from './schema'; |
| 26 | |
| 27 | function updateIndexFile(path: string): Rule { |
| 28 | return async (host: Tree) => { |
| 29 | const originalContent = host.readText(path); |
| 30 | |
| 31 | const { RewritingStream } = await import('parse5-html-rewriting-stream'); |
| 32 | |
| 33 | const rewriter = new RewritingStream(); |
| 34 | let needsNoScript = true; |
| 35 | rewriter.on('startTag', (startTag) => { |
| 36 | if (startTag.tagName === 'noscript') { |
| 37 | needsNoScript = false; |
| 38 | } |
| 39 | |
| 40 | rewriter.emitStartTag(startTag); |
| 41 | }); |
| 42 | |
| 43 | rewriter.on('endTag', (endTag) => { |
| 44 | if (endTag.tagName === 'head') { |
| 45 | rewriter.emitRaw(' <link rel="manifest" href="manifest.webmanifest">\n'); |
| 46 | } else if (endTag.tagName === 'body' && needsNoScript) { |
| 47 | rewriter.emitRaw( |
| 48 | ' <noscript>Please enable JavaScript to continue using this application.</noscript>\n', |
| 49 | ); |
| 50 | } |
| 51 | |
| 52 | rewriter.emitEndTag(endTag); |
| 53 | }); |
| 54 | |
| 55 | return pipeline(Readable.from(originalContent), rewriter, async function (source) { |
| 56 | const chunks = []; |
| 57 | for await (const chunk of source) { |
| 58 | chunks.push(Buffer.from(chunk)); |
| 59 | } |
| 60 | host.overwrite(path, Buffer.concat(chunks)); |
| 61 | }); |
| 62 | }; |
| 63 | } |
| 64 | |
| 65 | export default function (options: PwaOptions): Rule { |
| 66 | return async (host) => { |