Skip to content

Commit e944fc2

Browse files
committed
v1.0.0
1 parent 1785e9f commit e944fc2

File tree

3 files changed

+115
-0
lines changed

3 files changed

+115
-0
lines changed

README.md

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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

index.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
"use strict";
2+
3+
module.exports = function (source, inputSourceMap) {
4+
var loader = this;
5+
6+
this.cacheable();
7+
8+
var query = this.query;
9+
10+
var context = {
11+
warning(message) {
12+
loader.emitWarning(new Error(message));
13+
},
14+
15+
error(message) {
16+
loader.emitError(new Error(message));
17+
},
18+
19+
get filename() {
20+
return loader.resourcePath;
21+
},
22+
23+
get text() {
24+
return source;
25+
},
26+
27+
includes(str) {
28+
return source.includes(str);
29+
},
30+
31+
testRegex(regex) {
32+
if (!regex.flags.includes('m')) {
33+
regex = new RegExp(regex, regex.flags + 'm');
34+
}
35+
36+
return regex.test(source);
37+
}
38+
};
39+
40+
for (var key in query) {
41+
query[key].apply(context);
42+
}
43+
44+
return source;
45+
};

package.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"name": "warning-loader",
3+
"version": "1.0.0",
4+
"description": "A webpack loader that produces warnings according to script rules",
5+
"main": "index.js",
6+
"repository": {
7+
"type": "git",
8+
"url": "git+https://github.com/configurator/warning-loader.git"
9+
},
10+
"author": "Dor Kleiman <[email protected]>",
11+
"license": "MIT",
12+
"bugs": {
13+
"url": "https://github.com/configurator/warning-loader/issues"
14+
},
15+
"homepage": "https://github.com/configurator/warning-loader#readme"
16+
}

0 commit comments

Comments
 (0)