-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathv3.ts
More file actions
68 lines (60 loc) · 1.88 KB
/
Copy pathv3.ts
File metadata and controls
68 lines (60 loc) · 1.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import type { SyncHook } from 'tapable'
import type { Compiler } from 'webpack'
import { TAP_KEY_PREFIX } from '../types'
import { BasePlugin } from './base-plugin'
interface HtmlWebpackPluginData {
html: string
outputName: string
assets: {
publicPath: string
css: string[]
}
}
export class PluginForHtmlWebpackPluginV3 extends BasePlugin {
private process(data: HtmlWebpackPluginData) {
// check if current html needs to be inlined
if (this.isCurrentFileNeedsToBeInlined(data.outputName)) {
const [...cssAssets] = data.assets.css
cssAssets.forEach((cssLink) => {
const style = this.getCSSStyle({
cssLink,
publicPath: data.assets.publicPath,
})
if (style) {
data.html = this.addStyle({
html: data.html,
htmlFileName: data.outputName,
style: style,
})
const cssLinkIndex = data.assets.css.indexOf(cssLink)
// prevent generate <link /> tag
if (cssLinkIndex !== -1) {
data.assets.css.splice(cssLinkIndex, 1)
}
}
})
data.html = this.cleanUp(data.html)
}
}
apply(compiler: Compiler) {
compiler.hooks.compilation.tap(
`${TAP_KEY_PREFIX}_compilation`,
(compilation) => {
if ('htmlWebpackPluginBeforeHtmlProcessing' in compilation.hooks) {
const hook = compilation.hooks.htmlWebpackPluginBeforeHtmlProcessing as SyncHook<HtmlWebpackPluginData>
hook.tap(
`${TAP_KEY_PREFIX}_htmlWebpackPluginBeforeHtmlProcessing`,
(data: HtmlWebpackPluginData) => {
this.prepare(compilation)
this.process(data)
},
)
} else {
throw new Error(
'`html-webpack-plugin` should be ordered first before html-inline-css-webpack-plugin',
)
}
},
)
}
}