|
| 1 | +# warning-loader |
| 2 | + |
| 3 | +A webpack loader that produces warnings according to script rules. |
| 4 | + |
| 5 | +### Installation |
| 6 | + |
| 7 | + npm install --save-dev warning-loader |
| 8 | + |
| 9 | +### Usage |
| 10 | + |
| 11 | +Implement a warning function using the API at the bottom: |
| 12 | + |
| 13 | + var customWarnings = function () { |
| 14 | + if (/\/error.js$/.test(filename)) { |
| 15 | + this.error('The file error.js should never be required.'); |
| 16 | + } |
| 17 | + |
| 18 | + if (this.text === '') { |
| 19 | + this.warning('Module is empty'); |
| 20 | + } |
| 21 | + |
| 22 | + if (this.testRegex(/todo/i)) { |
| 23 | + this.warning('File contains a TODO'); |
| 24 | + } |
| 25 | + }; |
| 26 | + |
| 27 | +In webpack's config, add the loader: |
| 28 | + |
| 29 | + use: { |
| 30 | + loader: 'warning-loader', |
| 31 | + options: { |
| 32 | + any: customWarnings |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | +The key within the `options` (`any` in this example) does not matter; if multiple are present, |
| 37 | +all are called, so we can use multiple test functions. |
| 38 | + |
| 39 | +Note that this loader will never modify its input, and is always cacheable. This means your function |
| 40 | +may not be called again for the same input (during the same webpack run or when using the caching |
| 41 | +plugin [HardSourceWebpackPlugin](https://github.com/mzgoddard/hard-source-webpack-plugin)) - warnings |
| 42 | +will remain the same as the last time the module was compiled. |
| 43 | + |
| 44 | +### API |
| 45 | + |
| 46 | +Within the warning function, the context (`this`) is given a simple API: |
| 47 | + |
| 48 | + this.warning('Message'): emits a warning |
| 49 | + this.error('Message'): emits an error |
| 50 | + |
| 51 | + this.filename: The current module's filename |
| 52 | + this.text: The current module's text, as a string |
| 53 | + this.includes('text'): Returns true if any line in the current module includes the string |
| 54 | + this.testRegex(/regex/): Returns true if any line in the current module matches the regex |
0 commit comments