|
| 1 | +import { SyncHook } from 'tapable' |
| 2 | +import { Compiler } from 'webpack' |
| 3 | +import HTMLWebpackPlugin = require('html-webpack-plugin') |
| 4 | + |
| 5 | +import { TAP_KEY_PREFIX } from '../types' |
| 6 | +import { BasePlugin } from './base-plugin' |
| 7 | + |
| 8 | +interface BeforeAssetTagGenerationData { |
| 9 | + outputName: string |
| 10 | + assets: { |
| 11 | + publicPath: string |
| 12 | + css: string[] |
| 13 | + } |
| 14 | + plugin: HTMLWebpackPlugin |
| 15 | +} |
| 16 | + |
| 17 | +interface BeforeEmitData { |
| 18 | + html: string |
| 19 | + outputName: string |
| 20 | + plugin: HTMLWebpackPlugin |
| 21 | +} |
| 22 | + |
| 23 | +interface HTMLWebpackPluginHooks { |
| 24 | + beforeAssetTagGeneration: SyncHook<BeforeAssetTagGenerationData> |
| 25 | + beforeEmit: SyncHook<BeforeEmitData> |
| 26 | +} |
| 27 | + |
| 28 | +type CSSStyle = string |
| 29 | + |
| 30 | +export class PluginForHtmlWebpackPluginV4 extends BasePlugin { |
| 31 | + // Using object reference to distinguish styles for multiple files |
| 32 | + private cssStyleMap: Map<HTMLWebpackPlugin, CSSStyle[]> = new Map() |
| 33 | + |
| 34 | + private prepareCSSStyle(data: BeforeAssetTagGenerationData) { |
| 35 | + data.assets.css.forEach((cssLink, index) => { |
| 36 | + if (this.isCurrentFileNeedsToBeInlined(cssLink)) { |
| 37 | + const style = this.getCSSStyle({ |
| 38 | + cssLink, |
| 39 | + publicPath: data.assets.publicPath, |
| 40 | + }) |
| 41 | + |
| 42 | + if (style) { |
| 43 | + if (this.cssStyleMap.has(data.plugin)) { |
| 44 | + this.cssStyleMap.get(data.plugin)!.push(style) |
| 45 | + } else { |
| 46 | + this.cssStyleMap.set(data.plugin, [style]) |
| 47 | + } |
| 48 | + |
| 49 | + // prevent generate <link /> tag |
| 50 | + data.assets.css.splice(index, 1) |
| 51 | + } |
| 52 | + } |
| 53 | + }) |
| 54 | + } |
| 55 | + |
| 56 | + private process(data: BeforeEmitData) { |
| 57 | + // check if current html needs to be inlined |
| 58 | + if (this.isCurrentFileNeedsToBeInlined(data.outputName)) { |
| 59 | + const cssStyles = this.cssStyleMap.get(data.plugin) || [] |
| 60 | + |
| 61 | + cssStyles.forEach((style) => { |
| 62 | + data.html = this.addStyle({ |
| 63 | + style, |
| 64 | + html: data.html, |
| 65 | + htmlFileName: data.outputName, |
| 66 | + }) |
| 67 | + }) |
| 68 | + |
| 69 | + data.html = this.cleanUp(data.html) |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + apply(compiler: Compiler) { |
| 74 | + compiler.hooks.compilation.tap( |
| 75 | + `${TAP_KEY_PREFIX}_compilation`, |
| 76 | + (compilation) => { |
| 77 | + const hooks: HTMLWebpackPluginHooks = (HTMLWebpackPlugin as any).getHooks( |
| 78 | + compilation, |
| 79 | + ) |
| 80 | + |
| 81 | + hooks.beforeAssetTagGeneration.tap( |
| 82 | + `${TAP_KEY_PREFIX}_beforeAssetTagGeneration`, |
| 83 | + (data) => { |
| 84 | + this.prepare(compilation) |
| 85 | + this.prepareCSSStyle(data) |
| 86 | + }, |
| 87 | + ) |
| 88 | + |
| 89 | + hooks.beforeEmit.tap(`${TAP_KEY_PREFIX}_beforeEmit`, (data) => { |
| 90 | + this.process(data) |
| 91 | + }) |
| 92 | + }, |
| 93 | + ) |
| 94 | + } |
| 95 | +} |
0 commit comments