Skip to content

Commit cefdcb8

Browse files
committed
Add test, fix callback.apply.
1 parent 9146e9d commit cefdcb8

File tree

5 files changed

+108
-125
lines changed

5 files changed

+108
-125
lines changed

README.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,48 @@
55
`$ npm install custom-loader`
66

77
## Usage
8+
This module is useful for those times you want a loader but cant find or want to write one. How about just writing a function instead?
9+
10+
### Example 1
11+
Performs a simple string replace.
12+
13+
```
14+
{
15+
module: {
16+
rules: [{
17+
test: /\.js$/,
18+
use: {
19+
loader: 'custom-loader',
20+
options: {
21+
callback: (source) => {
22+
return source.replace('foo', 'bar');
23+
}
24+
}
25+
}
26+
}]
27+
}
28+
};
29+
```
30+
31+
### Example 2
32+
Using `[sharp](https://www.npmjs.com/package/sharp)`, returns the grayscale version of the resource.
33+
834
```
35+
{
36+
module: {
37+
rules: [{
38+
test: /\.(gif|jpe?g|png|svg|tiff|webp)(\?.*)?$/,
39+
use: {
40+
loader: 'custom-loader',
41+
options: {
42+
callback: (source, sourceMap, callback) => {
43+
sharp().grayscale().toBuffer(callback);
44+
}
45+
}
46+
}
47+
}]
48+
}
49+
};
950
```
1051

1152
## Changelog

index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ module.exports = function (source, map) {
4343
}
4444

4545
// Invoke custom loader.
46-
return config.callback.apply(context, source, map, context.callback);
46+
return config.callback.call(context, source, map, context.callback);
4747
};
4848

4949
// Mark as raw.

main.js

Lines changed: 0 additions & 107 deletions
This file was deleted.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
"mocha" : "3.2.x",
2020
"semistandard" : "9.2.x",
2121
"snazzy" : "6.0.x",
22+
"tmp" : "0.0.x",
2223
"webpack" : "2.2.x"
2324
},
2425
"engines": {

test/index.js

Lines changed: 65 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* global describe, it */
1+
/* global describe, it, after */
22
/*!
33
* The MIT License (MIT)
44
*
@@ -25,30 +25,78 @@
2525
// Strict mode.
2626
'use strict';
2727

28+
// Standard lib.
29+
const fs = require('fs');
30+
const path = require('path');
31+
2832
// Package modules.
33+
const expect = require('chai').expect;
34+
const tmp = require('tmp');
2935
const webpack = require('webpack');
3036

37+
// Configure.
38+
const customLoader = require.resolve('../');
39+
3140
// Test suite.
3241
describe('custom-loader', () => {
33-
// Default webpack config.
34-
const config = {
35-
context: __dirname,
36-
entry: './fixtures/entry',
37-
// output: { filename: '[name].js' },
38-
// module: {
39-
// rules: [
40-
// {
41-
// test: /.js$/,
42-
// use: 'custom-loader'
43-
// }
44-
// ]
45-
// }
46-
};
42+
// Output.
43+
const output = tmp.fileSync({ postfix: '.js' });
44+
after(output.removeCallback);
4745

4846
// Tests.
4947
it('should allow a custom loader', (done) => {
50-
webpack(config, (err, stats) => {
51-
console.log(err, stats);
48+
const replacement = `module.exports = ${Math.random()};`;
49+
webpack({
50+
context: __dirname,
51+
entry: './fixtures/entry.js',
52+
output: {
53+
filename: path.basename(output.name),
54+
path: path.dirname(output.name)
55+
},
56+
module: {
57+
rules: [
58+
{
59+
test: /.js$/,
60+
use: {
61+
loader: customLoader,
62+
options: {
63+
callback: (source) => replacement
64+
}
65+
}
66+
}
67+
]
68+
}
69+
}, (err, stats) => {
70+
if (stats.compilation.errors && stats.compilation.errors.length) {
71+
err = stats.compilation.errors[0].error;
72+
}
73+
74+
// Check file contents.
75+
const contents = fs.readFileSync(output.name, { encoding: 'utf8' });
76+
expect(contents).to.contain(replacement);
77+
78+
done(err);
79+
});
80+
});
81+
it('should fail if a callback is not specified', (done) => {
82+
webpack({
83+
context: __dirname,
84+
entry: './fixtures/entry.js',
85+
output: {
86+
filename: path.basename(output.name),
87+
path: path.dirname(output.name)
88+
},
89+
module: {
90+
rules: [
91+
{
92+
test: /.js$/,
93+
use: customLoader
94+
}
95+
]
96+
}
97+
}, (err, stats) => {
98+
expect(stats).to.have.deep.property('compilation.errors[0].error.message');
99+
expect(stats.compilation.errors[0].error.message).to.contain('callback');
52100
done(err);
53101
});
54102
});

0 commit comments

Comments
 (0)