@@ -3,12 +3,10 @@ import path from 'path';
33import loaderUtils from 'loader-utils' ;
44import { validate } from 'schema-utils' ;
55
6- import { findModuleById , evalModuleCode } from './utils' ;
6+ import { findModuleById , evalModuleCode , provideLoaderContext } from './utils' ;
77import schema from './loader-options.json' ;
88
9- import MiniCssExtractPlugin from './index' ;
10-
11- const pluginName = 'mini-css-extract-plugin' ;
9+ import MiniCssExtractPlugin , { pluginName , pluginSymbol } from './index' ;
1210
1311function hotLoader ( content , context ) {
1412 const accept = context . locals
@@ -39,6 +37,12 @@ export function pitch(request) {
3937 baseDataPath : 'options' ,
4038 } ) ;
4139
40+ if ( ! this [ pluginSymbol ] ) {
41+ throw new Error (
42+ "You forgot to add 'mini-css-extract-plugin' plugin (i.e. `{ plugins: [new MiniCssExtractPlugin()] }`), please read https://github.com/webpack-contrib/mini-css-extract-plugin#getting-started"
43+ ) ;
44+ }
45+
4246 const loaders = this . loaders . slice ( this . loaderIndex + 1 ) ;
4347
4448 this . addDependency ( this . resourcePath ) ;
@@ -104,33 +108,18 @@ export function pitch(request) {
104108
105109 new LimitChunkCountPlugin ( { maxChunks : 1 } ) . apply ( childCompiler ) ;
106110
107- const NormalModule = webpack . NormalModule
108- ? webpack . NormalModule
109- : // eslint-disable-next-line global-require
110- require ( 'webpack/lib/NormalModule' ) ;
111-
112- childCompiler . hooks . thisCompilation . tap (
113- `${ pluginName } loader` ,
114- ( compilation ) => {
115- const normalModuleHook =
116- typeof NormalModule . getCompilationHooks !== 'undefined'
117- ? NormalModule . getCompilationHooks ( compilation ) . loader
118- : compilation . hooks . normalModuleLoader ;
119-
120- normalModuleHook . tap ( `${ pluginName } loader` , ( loaderContext , module ) => {
121- if ( module . request === request ) {
122- // eslint-disable-next-line no-param-reassign
123- module . loaders = loaders . map ( ( loader ) => {
124- return {
125- loader : loader . path ,
126- options : loader . options ,
127- ident : loader . ident ,
128- } ;
129- } ) ;
130- }
111+ provideLoaderContext ( childCompiler , `${ pluginName } loader` , ( _ , module ) => {
112+ if ( module . request === request ) {
113+ // eslint-disable-next-line no-param-reassign
114+ module . loaders = loaders . map ( ( loader ) => {
115+ return {
116+ loader : loader . path ,
117+ options : loader . options ,
118+ ident : loader . ident ,
119+ } ;
131120 } ) ;
132121 }
133- ) ;
122+ } ) ;
134123
135124 let source ;
136125
@@ -203,12 +192,6 @@ export function pitch(request) {
203192 const count = identifierCountMap . get ( dependency . identifier ) || 0 ;
204193 const CssDependency = MiniCssExtractPlugin . getCssDependency ( webpack ) ;
205194
206- if ( ! CssDependency ) {
207- throw new Error (
208- "You forgot to add 'mini-css-extract-plugin' plugin (i.e. `{ plugins: [new MiniCssExtractPlugin()] }`), please read https://github.com/webpack-contrib/mini-css-extract-plugin#getting-started"
209- ) ;
210- }
211-
212195 this . _module . addDependency (
213196 ( lastDep = new CssDependency ( dependency , dependency . context , count ) )
214197 ) ;
0 commit comments