@@ -4,7 +4,7 @@ import path from 'path';
4
4
import webpack from 'webpack' ;
5
5
import sources from 'webpack-sources' ;
6
6
7
- const thing = path . resolve ( __dirname , './hotLoader.js' ) ;
7
+ const hotLoader = path . resolve ( __dirname , './hotLoader.js' ) ;
8
8
9
9
const { ConcatSource, SourceMapSource, OriginalSource } = sources ;
10
10
const { Template, util : { createHash } } = webpack ;
@@ -17,6 +17,20 @@ const REGEXP_CHUNKHASH = /\[chunkhash(?::(\d+))?\]/i;
17
17
const REGEXP_CONTENTHASH = / \[ c o n t e n t h a s h (?: : ( \d + ) ) ? \] / i;
18
18
const REGEXP_NAME = / \[ n a m e \] / i;
19
19
20
+ const isHMR = ( compiler ) => {
21
+ if ( compiler && compiler . options ) {
22
+ if ( compiler . options . devServer && compiler . options . devServer . hot ) {
23
+ return true ;
24
+ }
25
+
26
+ if ( compiler . options . entry ) {
27
+ const entryString = JSON . stringify ( compiler . options . entry ) ;
28
+ return entryString . includes ( 'hot' ) || entryString . includes ( 'hmr' ) ;
29
+ }
30
+ }
31
+ return false ;
32
+ } ;
33
+
20
34
class CssDependency extends webpack . Dependency {
21
35
constructor (
22
36
{ identifier, content, media, sourceMap } ,
@@ -132,24 +146,31 @@ class ExtractCssChunks {
132
146
}
133
147
134
148
apply ( compiler ) {
135
- console . log ( 'DevServerHot:' , compiler . options . devServer . hot )
136
- // console.log(compiler.options)
137
- const updatedRules = compiler . options . module . rules . reduce ( ( rules , rule ) => {
138
- if ( rule . use && Array . isArray ( rule . use ) ) {
139
- const isMiniCss = rule . use . some ( ( l ) => {
140
- const needle = l . loader || l ;
141
- return needle . includes ( pluginName ) ;
142
- } ) ;
143
- if ( isMiniCss ) {
144
- rule . use . unshift ( thing ) ;
145
- }
146
- }
147
- rules . push ( rule ) ;
149
+ try {
150
+ const isHOT = isHMR ( compiler ) ;
148
151
149
- return rules ;
150
- } , [ ] ) ;
152
+ if ( isHOT && compiler . options . module && compiler . options . module . rules ) {
153
+ const updatedRules = compiler . options . module . rules . reduce ( ( rules , rule ) => {
154
+ if ( rule . use && Array . isArray ( rule . use ) ) {
155
+ const isMiniCss = rule . use . some ( ( l ) => {
156
+ const needle = l . loader || l ;
157
+ return needle . includes ( pluginName ) ;
158
+ } ) ;
159
+ if ( isMiniCss ) {
160
+ rule . use . unshift ( hotLoader ) ;
161
+ }
162
+ }
163
+ rules . push ( rule ) ;
164
+
165
+ return rules ;
166
+ } , [ ] ) ;
167
+
168
+ compiler . options . module . rules = updatedRules ;
169
+ }
170
+ } catch ( e ) {
171
+ console . error ( 'Something went wrong: contact the author' , e ) ;
172
+ }
151
173
152
- compiler . options . module . rules = updatedRules ;
153
174
154
175
compiler . hooks . thisCompilation . tap ( pluginName , ( compilation ) => {
155
176
compilation . hooks . normalModuleLoader . tap ( pluginName , ( lc , m ) => {
0 commit comments