1
- import { Compiler , Configuration } from ' webpack' ;
1
+ import { Compiler , Configuration } from " webpack" ;
2
2
3
3
type File = {
4
- [ key : string ] : string
4
+ [ key : string ] : string ;
5
5
} ;
6
6
7
7
type Asset = {
8
- source ( ) : string
9
- size ( ) : number
8
+ source ( ) : string ;
9
+ size ( ) : number ;
10
10
} ;
11
11
12
12
interface Compilation {
13
- assets : { [ key : string ] : Asset }
13
+ assets : { [ key : string ] : Asset } ;
14
14
}
15
15
16
16
interface ReplaceConfig {
17
- position ?: ' before' | ' after'
18
- removeTarget ?: boolean
19
- target : string ,
20
- leaveCssFile ?: boolean
17
+ position ?: " before" | " after" ;
18
+ removeTarget ?: boolean ;
19
+ target : string ;
20
+ leaveCssFile ?: boolean ;
21
21
}
22
22
23
23
interface Config {
24
- filter ?( fileName : string ) : boolean
25
- replace ?: ReplaceConfig
24
+ filter ?( fileName : string ) : boolean ;
25
+ replace ?: ReplaceConfig ;
26
26
}
27
27
28
28
const DEFAULT_REPLACE_CONFIG : ReplaceConfig = {
29
- target : ' </head>'
29
+ target : " </head>"
30
30
} ;
31
31
32
- export default class Plugin
33
- {
32
+ export default class Plugin {
34
33
static addStyle ( html : string , style : string , replaceConfig : ReplaceConfig ) {
35
34
const styleString = `<style type="text/css">${ style } </style>` ;
36
35
const replaceValues = [ styleString , replaceConfig . target ] ;
37
36
38
- if ( replaceConfig . position === ' after' ) {
39
- replaceValues . reverse ( )
37
+ if ( replaceConfig . position === " after" ) {
38
+ replaceValues . reverse ( ) ;
40
39
}
41
40
42
- return html . replace ( replaceConfig . target , replaceValues . join ( '' ) ) ;
41
+ html = String ( html ) ;
42
+
43
+ return html . replace ( replaceConfig . target , replaceValues . join ( "" ) ) ;
43
44
}
44
45
45
46
static removeLinkTag ( html : string , cssFileName : string ) {
47
+ html = String ( html ) ;
48
+
46
49
return html . replace (
47
50
new RegExp ( `<link[^>]+href=['"]${ cssFileName } ['"][^>]+(>|\/>|><\/link>)` ) ,
48
- '' ,
51
+ ""
49
52
) ;
50
53
}
51
54
52
55
static cleanUp ( html : string , replaceConfig : ReplaceConfig ) {
56
+ html = String ( html ) ;
57
+
53
58
return replaceConfig . removeTarget
54
- ? html . replace ( replaceConfig . target , '' )
59
+ ? html . replace ( replaceConfig . target , "" )
55
60
: html ;
56
61
}
57
62
@@ -62,25 +67,25 @@ export default class Plugin
62
67
constructor ( private readonly config : Config = { } ) { }
63
68
64
69
private filter ( fileName : string ) : boolean {
65
- if ( typeof this . config . filter === ' function' ) {
70
+ if ( typeof this . config . filter === " function" ) {
66
71
return this . config . filter ( fileName ) ;
67
72
} else {
68
73
return true ;
69
74
}
70
75
}
71
76
72
77
private prepare ( { assets } : Compilation ) {
73
- const isCSS = is ( ' css' ) ;
74
- const isHTML = is ( ' html' ) ;
78
+ const isCSS = is ( " css" ) ;
79
+ const isHTML = is ( " html" ) ;
75
80
const { replace : replaceConfig = DEFAULT_REPLACE_CONFIG } = this . config ;
76
81
77
- Object . keys ( assets ) . forEach ( ( fileName ) => {
82
+ Object . keys ( assets ) . forEach ( fileName => {
78
83
if ( isCSS ( fileName ) ) {
79
84
const isCurrentFileNeedsToBeInlined = this . filter ( fileName ) ;
80
85
if ( isCurrentFileNeedsToBeInlined ) {
81
86
this . css [ fileName ] = assets [ fileName ] . source ( ) ;
82
87
if ( ! replaceConfig . leaveCssFile ) {
83
- delete assets [ fileName ]
88
+ delete assets [ fileName ] ;
84
89
}
85
90
}
86
91
} else if ( isHTML ( fileName ) ) {
@@ -90,32 +95,39 @@ export default class Plugin
90
95
}
91
96
92
97
private process ( { assets } : Compilation , { output } : Configuration ) {
93
- const publicPath = ( output && output . publicPath ) || '' ;
98
+ const publicPath = ( output && output . publicPath ) || "" ;
94
99
const { replace : replaceConfig = DEFAULT_REPLACE_CONFIG } = this . config ;
95
100
96
- Object . keys ( this . html ) . forEach ( ( htmlFileName ) => {
101
+ Object . keys ( this . html ) . forEach ( htmlFileName => {
97
102
let html = this . html [ htmlFileName ] ;
98
103
99
- Object . keys ( this . css ) . forEach ( ( key ) => {
104
+ Object . keys ( this . css ) . forEach ( key => {
100
105
html = Plugin . addStyle ( html , this . css [ key ] , replaceConfig ) ;
101
106
html = Plugin . removeLinkTag ( html , publicPath + key ) ;
102
107
} ) ;
103
108
104
109
html = Plugin . cleanUp ( html , replaceConfig ) ;
105
110
106
111
assets [ htmlFileName ] = {
107
- source ( ) { return html } ,
108
- size ( ) { return html . length } ,
112
+ source ( ) {
113
+ return html ;
114
+ } ,
115
+ size ( ) {
116
+ return html . length ;
117
+ }
109
118
} ;
110
119
} ) ;
111
120
}
112
121
113
122
apply ( compiler : Compiler ) {
114
- compiler . hooks . emit . tapAsync ( 'html-inline-css-webpack-plugin' , ( compilation : Compilation , callback : ( ) => void ) => {
115
- this . prepare ( compilation ) ;
116
- this . process ( compilation , compiler . options ) ;
117
- callback ( ) ;
118
- } ) ;
123
+ compiler . hooks . emit . tapAsync (
124
+ "html-inline-css-webpack-plugin" ,
125
+ ( compilation : Compilation , callback : ( ) => void ) => {
126
+ this . prepare ( compilation ) ;
127
+ this . process ( compilation , compiler . options ) ;
128
+ callback ( ) ;
129
+ }
130
+ ) ;
119
131
}
120
132
}
121
133
0 commit comments