Skip to content

Commit 9ea84a0

Browse files
author
Gerald Yeo
committed
start tests
1 parent 967779f commit 9ea84a0

6 files changed

Lines changed: 142 additions & 36 deletions

File tree

README.md

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,13 @@
55
[![npm][npm-badge]][npm-link]
66
[![Build Status][circle-badge]][circle-link]
77

8-
This is a webpack plugin that allows you to filter the final output files
9-
before they are being written to disk. This is useful if you're creating
10-
multiple output bundles with common assets. As such, you can use this
11-
to omit it in other runs.
8+
This webpack plugin allows you to filter the list of output files before
9+
they are being written / emitted to disk. It does not prevent files
10+
from `import` or `require` from being processed and bundled, keeping the
11+
file references like image assets in your bundled code.
1212

13-
Unlike the [IgnorePlugin][ignore-plugin-package], this does not prevent
14-
an `import` or `require` from being bundled, but instead, omits certain
15-
files from being written to disk, ensuring that any update of references
16-
due to hashing or other processing will still happen.
13+
This is useful if you're creating multiple output bundles with common assets.
14+
As such, you can use this to omit it in other runs.
1715

1816
## Installation
1917

@@ -35,18 +33,18 @@ const webpackConfig = {
3533
output: {
3634
path: path.join(__dirname, 'dist'),
3735
filename: 'assets/app.[chunkhash].js',
38-
chunkFilename: 'assets/[id].app.[chunkhash].js',
36+
chunkFilename: 'assets/[id].app.[chunkhash].js'
3937
},
4038
module: {
4139
rules: [{
42-
test: /\.(svg|woff2?|ttf|eot|jpe?g|png|gif)(\?.*)?$/i,
40+
test: /\.svg$/,
4341
use: {
4442
loader: 'file-loader?name=[path][name]_[hash].[ext]',
4543
options: {
46-
outputPath: 'assets/images/',
44+
outputPath: 'assets/images/'
4745
}
4846
}
49-
}],
47+
}]
5048
},
5149
plugins: [
5250
new ExtractTextPlugin({
@@ -55,7 +53,7 @@ const webpackConfig = {
5553
}),
5654
new FilterChunkWebpackPlugin({
5755
patterns: [
58-
'assets/*'
56+
'assets/*',
5957
'!assets/css/*'
6058
]
6159
})
@@ -75,15 +73,16 @@ assets/css/css.98a5a.css
7573
but not
7674

7775
```
78-
assets/images/a5b912cd3.png
76+
assets/images/a5b912cd3.svg
7977
```
8078

8179
## Options
8280

83-
| option | type | default | description |
84-
| -------- | ------- | ------- | --------------------------------------------------------------------------------------------------------------------- |
85-
| patterns | array | `[]` | a list of pattern types that are supported by [multimatch][multimatch-package] |
86-
| include | boolean | `false` | by default, this plugin will omit the matched result. Setting it to true will select only the matched result instead. |
81+
| options | type | defaults | description |
82+
| -------- | ------- | -------- | ---------------------------------------------------------------------------------------------------------------------------- |
83+
| patterns | array | `[]` | A list of pattern types that are supported by [multimatch][multimatch-package] |
84+
| include | boolean | `false` | By default, this plugin will omit the matched result. Setting it to true will include the matched result instead of omitting |
85+
| preview | boolean | `false` | To print the list of chunks that matches the patterns without applying the changes |
8786

8887
## License
8988

fixtures/test.png

1.07 KB
Loading

fixtures/test.svg

Lines changed: 14 additions & 0 deletions
Loading

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
"main": "index.js",
66
"scripts": {
77
"build": "babel src/index.js --out-file index.js",
8-
"test": "exit 1"
8+
"test": "jest --coverage",
9+
"test:watch": "jest --coverage --watch"
910
},
1011
"repository": {
1112
"type": "git",
@@ -28,6 +29,7 @@
2829
"babel-cli": "^6.26.0",
2930
"babel-core": "^6.26.0",
3031
"babel-preset-es2015": "^6.24.1",
32+
"babel-register": "^6.26.0",
3133
"css-loader": "^0.28.7",
3234
"eslint": "^4.6.1",
3335
"extract-text-webpack-plugin": "^3.0.0",

src/index.js

Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,39 +3,55 @@ import omit from 'lodash.omit';
33
import pick from 'lodash.pick';
44

55
class FilterChunkWebpackPlugin {
6-
constructor(options) {
6+
constructor(options = {}) {
77
if (!Array.isArray(options.patterns)) {
8-
throw new Error('[FilterChunkWebpackPlugin] The "patterns" option should be an array');
8+
throw new Error('The "patterns" option should be an array');
99
}
1010

1111
this.options = Object.assign({
12+
debug: this.log,
1213
include: false,
1314
patterns: [],
1415
preview: false
1516
}, options);
1617
}
17-
18+
19+
log(...args) {
20+
// eslint-disable-next-line no-console
21+
console.log(...args);
22+
}
23+
24+
filter(...args) {
25+
if (this.options.include === true) {
26+
return pick(...args);
27+
}
28+
29+
return omit(...args);
30+
}
31+
1832
previewMatchedFiles(matchedFiles) {
1933
const action = this.options.include === true ? 'included' : 'excluded';
2034

21-
console.log(`${matchedFiles.length} file(s) that will be ${action}`);
22-
matchedFiles.forEach((file) => console.log(' %s', file));
23-
console.log('');
35+
this.options.debug(`${matchedFiles.length} file(s) that will be ${action}`);
36+
matchedFiles.forEach((file) => {
37+
this.options.debug(file);
38+
});
39+
this.options.debug('');
2440
}
2541

2642
apply(compiler) {
27-
const filter = this.options.include === true ? pick : omit;
28-
29-
3043
compiler.plugin('emit', (compilation, callback) => {
31-
const files = Object.keys(compilation.assets);
32-
const matchedFiles = multimatch(files, this.options.patterns);
33-
34-
if (this.options.preview) {
35-
this.previewMatchedFiles(matchedFiles);
36-
} else {
37-
// eslint-disable-next-line no-param-reassign
38-
compilation.assets = filter(compilation.assets, matchedFiles);
44+
45+
if (this.options.patterns.length > 0) {
46+
const files = Object.keys(compilation.assets);
47+
const matchedFiles = multimatch(files, this.options.patterns);
48+
49+
if (this.options.preview) {
50+
this.previewMatchedFiles(matchedFiles);
51+
} else {
52+
// eslint-disable-next-line no-param-reassign
53+
compilation.assets = this.filter(compilation.assets, matchedFiles);
54+
}
3955
}
4056

4157
callback();

src/index.spec.js

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
// import path from 'path';
2+
// import fs from 'fs';
3+
// import webpack from 'webpack';
4+
5+
import FilterChunkWebpackPlugin from './index';
6+
7+
describe('FilterChunkWebpackPlugin', function () {
8+
const PATTERN_ERROR = 'The "patterns" option should be an array';
9+
10+
it('should set options from constructor', function () {
11+
const plugin = new FilterChunkWebpackPlugin({
12+
include: true,
13+
preview: true,
14+
patterns: ['**/**']
15+
});
16+
expect(plugin.options.include).toBe(true);
17+
expect(plugin.options.preview).toBe(true);
18+
expect(plugin.options.patterns).toEqual(['**/**']);
19+
});
20+
21+
it('should throw an Error when patterns is not array', function () {
22+
const plugin = () => new FilterChunkWebpackPlugin({
23+
patterns: 'test'
24+
});
25+
expect(plugin).toThrow(PATTERN_ERROR)
26+
});
27+
28+
it('should throw an Error when options not set', function () {
29+
const plugin = () => new FilterChunkWebpackPlugin();
30+
expect(plugin).toThrow(PATTERN_ERROR)
31+
});
32+
33+
// describe('when deleting files', () => {
34+
// let unlinkSync;
35+
// const outputPath = path.resolve(__dirname, 'assets');
36+
// const webpackConfig = {
37+
// entry: path.join(__dirname, 'entry.js'),
38+
// output: {
39+
// path: outputPath,
40+
// filename: 'bundle.js',
41+
// },
42+
// };
43+
44+
// beforeEach(() => { unlinkSync = stub(fs, 'unlinkSync'); });
45+
// afterEach(() => { unlinkSync.restore(); });
46+
47+
// it('should delete the extraneous files from the output path', (done) => {
48+
// const compiler = webpack({
49+
// ...webpackConfig,
50+
// plugins: [new WebpackCleanupPlugin({ quiet: true })],
51+
// });
52+
// compiler.run(() => {
53+
// expect(unlinkSync).to.have.been.calledWith(njoin(outputPath, 'a.txt'));
54+
// expect(unlinkSync).to.have.been.calledWith(njoin(outputPath, 'b.txt'));
55+
// expect(unlinkSync).to.not.have.been.calledWith(njoin(outputPath, 'bundle.js'));
56+
// expect(unlinkSync).to.have.been.calledWith(njoin(outputPath, 'foo.json'));
57+
// expect(unlinkSync).to.have.been.calledWith(njoin(outputPath, 'z.txt'));
58+
// done();
59+
// });
60+
// });
61+
62+
// it('should not delete when previewing files', (done) => {
63+
// const compiler = webpack({
64+
// ...webpackConfig,
65+
// plugins: [
66+
// new WebpackCleanupPlugin({ preview: true }),
67+
// ],
68+
// });
69+
// compiler.run(() => {
70+
// expect(unlinkSync).to.not.have.been.called;
71+
// done();
72+
// });
73+
// });
74+
// });
75+
});

0 commit comments

Comments
 (0)