You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -79,7 +79,7 @@ By default, the resource file is converted to a UTF-8 string and passed to the l
79
79
80
80
__raw-loader.js__
81
81
82
-
```js
82
+
```javascript
83
83
module.exports=function(content) {
84
84
assert(content instanceof Buffer);
85
85
returnsomeSyncOperation(content);
@@ -94,7 +94,7 @@ module.exports.raw = true;
94
94
95
95
Loaders are __always__ called from right to left. There are some instances where the loader only cares about the __metadata__ behind a request and can ignore the results of the previous loader. The `pitch` method on loaders is called from __left to right__ before the loaders are actually executed (from right to left). For the following [`use`](/configuration/module#rule-use) configuration:
96
96
97
-
```js
97
+
```javascript
98
98
module.exports= {
99
99
//...
100
100
module: {
@@ -128,7 +128,7 @@ So why might a loader take advantage of the "pitching" phase?
128
128
129
129
First, the `data` passed to the `pitch` method is exposed in the execution phase as well under `this.data` and could be useful for capturing and sharing information from earlier in the cycle.
Second, if a loader delivers a result in the `pitch` method the process turns around and skips the remaining loaders. In our example above, if the `b-loader`s `pitch` method returned something:
142
142
143
-
```js
143
+
```javascript
144
144
module.exports=function(content) {
145
145
returnsomeSyncOperation(content);
146
146
};
@@ -170,7 +170,7 @@ The loader context represents the properties that are available inside of a load
170
170
Given the following example this require call is used:
171
171
In `/abc/file.js`:
172
172
173
-
```js
173
+
```javascript
174
174
require('./loader1?xyz!loader2!./resource?rrr');
175
175
```
176
176
@@ -213,7 +213,7 @@ A function that can be called synchronously or asynchronously in order to return
213
213
214
214
<!-- eslint-skip -->
215
215
216
-
```js
216
+
```javascript
217
217
this.callback(
218
218
err:Error|null,
219
219
content: string | Buffer,
@@ -261,13 +261,13 @@ An array of all the loaders. It is writeable in the pitch phase.
@@ -338,16 +338,33 @@ Should a source map be generated. Since generating source maps can be an expensi
338
338
emitWarning(warning:Error)
339
339
```
340
340
341
-
Emit a warning.
341
+
Emit a warning that will be displayed in the output like the following:
342
342
343
+
``` bash
344
+
WARNING in ./src/lib.js (./src/loader.js!./src/lib.js)
345
+
Module Warning (from ./src/loader.js):
346
+
Here is a Warning!
347
+
@ ./src/index.js 1:0-25
348
+
```
349
+
350
+
T> Note that the warnings will not be displayed if `stats.warnings` is set to `false`, or some other omit setting is used to `stats` such as `none` or `errors-only`. See the [stats configuration](/configuration/stats/#stats).
343
351
344
352
### `this.emitError`
345
353
346
354
``` typescript
347
355
emitError(error: Error)
348
356
```
349
357
350
-
Emit an error.
358
+
Emit an error that also can be displayed in the output.
359
+
360
+
``` bash
361
+
ERROR in ./src/lib.js (./src/loader.js!./src/lib.js)
362
+
Module Error (from ./src/loader.js):
363
+
Here is an Error!
364
+
@ ./src/index.js 1:0-25
365
+
```
366
+
367
+
T> Unlike throwing an Error directly, it will NOT interrupt the compilation process of the current module.
351
368
352
369
353
370
### `this.loadModule`
@@ -471,3 +488,80 @@ Hacky access to the Compiler object of webpack.
471
488
### `this._module`
472
489
473
490
Hacky access to the Module object being loaded.
491
+
492
+
493
+
## Error Reporting
494
+
495
+
You can report errors from inside a loader by:
496
+
497
+
- Using [this.emitError](/api/loaders/#this-emiterror). Will report the errors without interrupting module's compilation.
498
+
- Using `throw` (or other uncaught exception). Throwing an error while a loader is running will cause current module compilation failure.
499
+
- Using `callback` (in async mode). Pass an error to the callback will also cause module compilation failure.
500
+
501
+
For example:
502
+
503
+
__./src/index.js__
504
+
505
+
``` javascript
506
+
require('./loader!./lib');
507
+
```
508
+
509
+
Throwing an error from loader:
510
+
511
+
__./src/loader.js__
512
+
513
+
``` javascript
514
+
module.exports=function(source) {
515
+
thrownewError('This is a Fatal Error!');
516
+
};
517
+
```
518
+
519
+
Or pass an error to the callback in async mode:
520
+
521
+
__./src/loader.js__
522
+
523
+
``` javascript
524
+
module.exports=function(source) {
525
+
constcallback=this.async();
526
+
//...
527
+
callback(newError('This is a Fatal Error!'), source);
528
+
};
529
+
```
530
+
531
+
The module will get bundled like this:
532
+
533
+
<!-- eslint-skip -->
534
+
535
+
``` javascript
536
+
/***/"./src/loader.js!./src/lib.js":
537
+
/*!************************************!*\
538
+
!*** ./src/loader.js!./src/lib.js ***!
539
+
\************************************/
540
+
/*! no static exports found */
541
+
/***/ (function(module, exports) {
542
+
543
+
thrownewError("Module build failed (from ./src/loader.js):\nError: This is a Fatal Error!\n at Object.module.exports (/workspace/src/loader.js:3:9)");
544
+
545
+
/***/ })
546
+
```
547
+
548
+
Then the build output will also display the error (Similar to `this.emitError`):
0 commit comments