@@ -17,6 +17,7 @@ const WebpackError = require("./WebpackError");
1717const Module = require ( "./Module" ) ;
1818const ModuleParseError = require ( "./ModuleParseError" ) ;
1919const ModuleBuildError = require ( "./ModuleBuildError" ) ;
20+ const ModuleError = require ( "./ModuleError" ) ;
2021const ModuleWarning = require ( "./ModuleWarning" ) ;
2122
2223const runLoaders = require ( "loader-runner" ) . runLoaders ;
@@ -135,32 +136,17 @@ class NormalModule extends Module {
135136 }
136137
137138 createLoaderContext ( resolver , options , compilation , fs ) {
138- const addErrorFrom = ( error ) => {
139- if ( ! error . from ) {
140- const currentLoaderObject = loaderContext . loaders && loaderContext . loaders . length ?
141- loaderContext . loaders [ loaderContext . loaderIndex ] :
142- null ;
143- if ( currentLoaderObject ) {
144- const loaderName = currentLoaderObject . origin || currentLoaderObject . path ;
145- error . from = / \/ / . test ( loaderName ) ?
146- path . relative ( loaderContext . rootContext , loaderName ) :
147- loaderName ;
148- }
149- }
150- } ;
151139 const loaderContext = {
152140 version : 2 ,
153141 emitWarning : ( warning ) => {
154142 if ( ! ( warning instanceof Error ) )
155143 warning = new NonErrorEmittedError ( warning ) ;
156- addErrorFrom ( warning ) ;
157- this . warnings . push ( new ModuleWarning ( this , warning ) ) ;
144+ this . warnings . push ( new ModuleWarning ( this , warning , this . getCurrentLoaderName ( loaderContext ) ) ) ;
158145 } ,
159146 emitError : ( error ) => {
160147 if ( ! ( error instanceof Error ) )
161148 error = new NonErrorEmittedError ( error ) ;
162- addErrorFrom ( error ) ;
163- this . errors . push ( new ModuleBuildError ( this , error ) ) ;
149+ this . errors . push ( new ModuleError ( this , error , this . getCurrentLoaderName ( loaderContext ) ) ) ;
164150 } ,
165151 exec : ( code , filename ) => {
166152 const module = new NativeModule ( filename , this ) ;
@@ -192,6 +178,19 @@ class NormalModule extends Module {
192178 return loaderContext ;
193179 }
194180
181+ getCurrentLoaderName ( loaderContext ) {
182+ const currentLoaderObject = this . loaders && this . loaders . length ?
183+ this . loaders [ loaderContext . loaderIndex ] :
184+ null ;
185+ if ( currentLoaderObject ) {
186+ const loaderName = currentLoaderObject . origin || currentLoaderObject . path ;
187+ return / \/ | \\ / . test ( loaderName ) ?
188+ path . relative ( loaderContext . rootContext , loaderName ) :
189+ loaderName ;
190+ }
191+ return null ;
192+ }
193+
195194 createSource ( source , resourceBuffer , sourceMap ) {
196195 // if there is no identifier return raw source
197196 if ( ! this . identifier ) {
@@ -219,7 +218,6 @@ class NormalModule extends Module {
219218
220219 doBuild ( options , compilation , resolver , fs , callback ) {
221220 const loaderContext = this . createLoaderContext ( resolver , options , compilation , fs ) ;
222-
223221 runLoaders ( {
224222 resource : this . resource ,
225223 loaders : this . loaders ,
@@ -233,7 +231,7 @@ class NormalModule extends Module {
233231 }
234232
235233 if ( err ) {
236- const error = new ModuleBuildError ( this , err ) ;
234+ const error = new ModuleBuildError ( this , err , this . getCurrentLoaderName ( loaderContext ) ) ;
237235 return callback ( error ) ;
238236 }
239237
0 commit comments