|
| 1 | +import { RawSource } from 'webpack-sources'; |
| 2 | + |
| 3 | +const parse5 = require('parse5'); |
| 4 | + |
| 5 | +export interface IndexHtmlWebpackPluginOptions { |
| 6 | + input: string; |
| 7 | + output: string; |
| 8 | + baseHref?: string; |
| 9 | + entrypoints: string[]; |
| 10 | + deployUrl?: string; |
| 11 | +} |
| 12 | + |
| 13 | +function readFile(filename: string, compilation: any): Promise<string> { |
| 14 | + return new Promise<string>((resolve, reject) => { |
| 15 | + compilation.inputFileSystem.readFile(filename, (err: Error, data: Buffer) => { |
| 16 | + if (err) { |
| 17 | + reject(err); |
| 18 | + return; |
| 19 | + } |
| 20 | + |
| 21 | + const content = data.toString(); |
| 22 | + resolve(content); |
| 23 | + }); |
| 24 | + }); |
| 25 | +} |
| 26 | + |
| 27 | +export class IndexHtmlWebpackPlugin { |
| 28 | + private _options: IndexHtmlWebpackPluginOptions; |
| 29 | + |
| 30 | + constructor(options?: Partial<IndexHtmlWebpackPluginOptions>) { |
| 31 | + this._options = { |
| 32 | + input: 'index.html', |
| 33 | + output: 'index.html', |
| 34 | + entrypoints: ['polyfills', 'main'], |
| 35 | + ...options |
| 36 | + }; |
| 37 | + } |
| 38 | + |
| 39 | + apply(compiler: any) { |
| 40 | + compiler.hooks.emit.tapPromise('index-html-webpack-plugin', async (compilation: any) => { |
| 41 | + // Get input html file |
| 42 | + const inputContent = await readFile(this._options.input, compilation); |
| 43 | + compilation.fileDependencies.add(this._options.input); |
| 44 | + |
| 45 | + |
| 46 | + // Get all files for selected entrypoints |
| 47 | + const unfilteredSortedFiles: string[] = []; |
| 48 | + for (const entryName of this._options.entrypoints) { |
| 49 | + const entrypoint = compilation.entrypoints.get(entryName); |
| 50 | + if (entrypoint) { |
| 51 | + unfilteredSortedFiles.push(...entrypoint.getFiles()); |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + // Filter files |
| 56 | + const existingFiles = new Set<string>(); |
| 57 | + const stylesheets: string[] = []; |
| 58 | + const scripts: string[] = []; |
| 59 | + for (const file of unfilteredSortedFiles) { |
| 60 | + if (existingFiles.has(file)) { |
| 61 | + continue; |
| 62 | + } |
| 63 | + existingFiles.add(file); |
| 64 | + |
| 65 | + if (file.endsWith('.js')) { |
| 66 | + scripts.push(file); |
| 67 | + } else if (file.endsWith('.css')) { |
| 68 | + stylesheets.push(file); |
| 69 | + } |
| 70 | + |
| 71 | + } |
| 72 | + |
| 73 | + // Find the head and body elements |
| 74 | + const treeAdapter = parse5.treeAdapters.default; |
| 75 | + const document = parse5.parse(inputContent, { treeAdapter }); |
| 76 | + let headElement; |
| 77 | + let bodyElement; |
| 78 | + for (const topNode of document.childNodes) { |
| 79 | + if (topNode.tagName === 'html') { |
| 80 | + for (const htmlNode of topNode.childNodes) { |
| 81 | + if (htmlNode.tagName === 'head') { |
| 82 | + headElement = htmlNode; |
| 83 | + } |
| 84 | + if (htmlNode.tagName === 'body') { |
| 85 | + bodyElement = htmlNode; |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + // Inject into the html |
| 92 | + |
| 93 | + if (!headElement || !bodyElement) { |
| 94 | + throw new Error('Missing head and/or body elements'); |
| 95 | + } |
| 96 | + |
| 97 | + for (const script of scripts) { |
| 98 | + const element = treeAdapter.createElement( |
| 99 | + 'script', |
| 100 | + undefined, |
| 101 | + [ |
| 102 | + { name: 'type', value: 'text/javascript' }, |
| 103 | + { name: 'src', value: (this._options.deployUrl || '') + script }, |
| 104 | + ] |
| 105 | + ); |
| 106 | + treeAdapter.appendChild(bodyElement, element); |
| 107 | + } |
| 108 | + |
| 109 | + // Adjust base href if specified |
| 110 | + if (this._options.baseHref != undefined) { |
| 111 | + let baseElement; |
| 112 | + for (const node of headElement.childNodes) { |
| 113 | + if (node.tagName === 'base') { |
| 114 | + baseElement = node; |
| 115 | + break; |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + if (!baseElement) { |
| 120 | + const element = treeAdapter.createElement( |
| 121 | + 'base', |
| 122 | + undefined, |
| 123 | + [ |
| 124 | + { name: 'href', value: this._options.baseHref }, |
| 125 | + ] |
| 126 | + ); |
| 127 | + treeAdapter.appendChild(headElement, element); |
| 128 | + } else { |
| 129 | + let hrefAttribute; |
| 130 | + for (const attribute of baseElement.attrs) { |
| 131 | + if (attribute.name === 'href') { |
| 132 | + hrefAttribute = attribute; |
| 133 | + } |
| 134 | + } |
| 135 | + if (hrefAttribute) { |
| 136 | + hrefAttribute.value = this._options.baseHref; |
| 137 | + } else { |
| 138 | + baseElement.attrs.push({ name: 'href', value: this._options.baseHref }); |
| 139 | + } |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + for (const stylesheet of stylesheets) { |
| 144 | + const element = treeAdapter.createElement( |
| 145 | + 'link', |
| 146 | + undefined, |
| 147 | + [ |
| 148 | + { name: 'rel', value: 'stylesheet' }, |
| 149 | + { name: 'href', value: (this._options.deployUrl || '') + stylesheet }, |
| 150 | + ] |
| 151 | + ); |
| 152 | + treeAdapter.appendChild(headElement, element); |
| 153 | + } |
| 154 | + |
| 155 | + // Add to compilation assets |
| 156 | + const outputContent = parse5.serialize(document, { treeAdapter }); |
| 157 | + compilation.assets[this._options.output] = new RawSource(outputContent); |
| 158 | + }); |
| 159 | + } |
| 160 | +} |
0 commit comments