Skip to content

Commit d4b2b53

Browse files
author
Gerald Yeo
committed
added tests
1 parent 9ea84a0 commit d4b2b53

12 files changed

Lines changed: 247 additions & 111 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,4 @@ typings/
6060
/lib
6161
/index.js
6262
temp
63+
/.spec_output/*

.npmignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
.eslintrc.json
66
.nyc_output
77
.nycrc
8+
.spec_output
89
.travis.yml
910

1011
/rollup.config.js

CONTRIBUTING.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# Contributing
2+
3+
[![PRs Welcome][pr-welcome-badge]][pr-welcome-link]
4+
5+
Thank you for opening (and reading) this document. :)
6+
We are open to, and grateful for, any contributions made.
7+
8+
## Semantic Versioning
9+
10+
This repository generally follows semantic versioning. We release patch versions for bug fixes, minor versions for new features, and major versions for any breaking changes.
11+
12+
All releases to npm or any other supported distribution channels will corresponding to a tagged commit.
13+
14+
## `yarn` vs `npm`
15+
16+
This repository currently uses `yarn` as a development tool, but you may use `npm` instead.
17+
18+
## Testing
19+
20+
To only run linting:
21+
22+
`yarn run lint`
23+
24+
To run tests and coverage:
25+
26+
`yarn test`
27+
28+
To continuously watch and run tests, run the following:
29+
30+
`yarn test:watch`
31+
32+
## Sending a Pull Request
33+
34+
If you send a pull request, please do it against the master branch.
35+
36+
Please __do not__ bump the version and tag your pull request with a v\[number\] as it corresponds to a release.
37+
38+
Before submitting a pull request, please make sure the following is done:
39+
40+
- Fork the repository and create your branch from master.
41+
- If you've added code that should be tested, add tests!
42+
- Ensure the test suite passes (`yarn test`).
43+
- Make sure your code lints (`yarn lint`).
44+
45+
Thank you for contributing!
46+
47+
[pr-welcome-badge]: https://img.shields.io/badge/PRs-Welcome-ff69b4.svg?style=flat-square
48+
[pr-welcome-link]: https://github.com/yeojz/filter-chunk-webpack-plugin/blob/master/CONTRIBUTING.md

README.md

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# filter-chunk-webpack-plugin
22

3-
> Include or exclude files / chunks from the final output based on a list of patterns
3+
> Include or exclude files / chunks from the final webpack output based on a list of patterns
44
55
[![npm][npm-badge]][npm-link]
66
[![Build Status][circle-badge]][circle-link]
@@ -32,15 +32,15 @@ const webpackConfig = {
3232
entry: 'index.js',
3333
output: {
3434
path: path.join(__dirname, 'dist'),
35-
filename: 'assets/app.[chunkhash].js',
36-
chunkFilename: 'assets/[id].app.[chunkhash].js'
35+
filename: 'assets/app.[chunkhash].js'
3736
},
3837
module: {
3938
rules: [{
4039
test: /\.svg$/,
4140
use: {
42-
loader: 'file-loader?name=[path][name]_[hash].[ext]',
41+
loader: 'file-loader',
4342
options: {
43+
name: '[hash].[ext]',
4444
outputPath: 'assets/images/'
4545
}
4646
}
@@ -65,8 +65,6 @@ This should generate files like
6565

6666
```
6767
assets/app.12ab3c.js
68-
assets/1.app.3bcd45.js
69-
7068
assets/css/css.98a5a.css
7169
```
7270

@@ -76,13 +74,14 @@ but not
7674
assets/images/a5b912cd3.svg
7775
```
7876

77+
For more info, check out the [usage.spec.js](./src/usage.spec.js) for more info.
78+
7979
## Options
8080

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 |
81+
| options | type | default | description |
82+
| -------- | ------- | ------- | ----------------------------------------------------------------------------------------------------------------------------------- |
83+
| patterns | array | `[]` | A list of pattern types that are supported by [multimatch][multimatch-package] |
84+
| select | boolean | `false` | By default, this plugin will omit the matched result. Setting this to true will include the matched result instead of omitting them |
8685

8786
## License
8887

@@ -95,4 +94,3 @@ assets/images/a5b912cd3.svg
9594
[circle-link]: https://circleci.com/gh/yeojz/filter-chunk-webpack-plugin
9695

9796
[multimatch-package]: https://github.com/sindresorhus/multimatch
98-
[ignore-plugin-package]: https://webpack.js.org/plugins/ignore-plugin/

circle.yml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
machine:
2+
node:
3+
version: "6"
4+
environment:
5+
PATH: "${PATH}:${HOME}/${CIRCLE_PROJECT_REPONAME}/node_modules/.bin"
6+
general:
7+
branches:
8+
ignore:
9+
- gh-pages
10+
dependencies:
11+
override:
12+
- yarn
13+
cache_directories:
14+
- ~/.cache/yarn
15+
test:
16+
pre:
17+
- yarn run clean
18+
override:
19+
- yarn test
20+
- yarn run lint
21+
- yarn run coverage
22+
deployment:
23+
release:
24+
tag: /^v[0-9]+(\.[0-9]+)*$/
25+
owner: yeojz
26+
commands:
27+
- yarn run clean
28+
- yarn run build
29+
- echo -e "$NPM_USER\n$NPM_PASS\n$NPM_EMAIL" | npm login
30+
- npm publish
31+
- npm logout

fixtures/app.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
const png = require('./test.png');
2+
const svg = require('./test.svg');
3+
4+
require('./style.css');
5+
6+
function entry() {
7+
return [png, svg];
8+
}
9+
10+
module.exports = entry;

fixtures/style.css

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
html,
2+
body {
3+
margin: 0;
4+
padding: 0;
5+
}

package.json

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
{
22
"name": "filter-chunk-webpack-plugin",
33
"version": "0.0.0",
4-
"description": "Include or exclude files / chunks from the final output based on a list of patterns",
4+
"description": "Include or exclude files / chunks from the final webpack output based on a list of patterns",
55
"main": "index.js",
66
"scripts": {
77
"build": "babel src/index.js --out-file index.js",
8+
"clean": "rimraf index.js coverage .spec_output/it-* .nyc_output",
9+
"coverage:upload": "codecov",
10+
"lint": "eslint --ext js src",
811
"test": "jest --coverage",
912
"test:watch": "jest --coverage --watch"
1013
},
@@ -29,15 +32,14 @@
2932
"babel-cli": "^6.26.0",
3033
"babel-core": "^6.26.0",
3134
"babel-preset-es2015": "^6.24.1",
32-
"babel-register": "^6.26.0",
35+
"codecov": "^2.3.0",
3336
"css-loader": "^0.28.7",
3437
"eslint": "^4.6.1",
3538
"extract-text-webpack-plugin": "^3.0.0",
3639
"file-loader": "^0.11.2",
3740
"jest": "^21.0.2",
3841
"rimraf": "^2.6.2",
3942
"style-loader": "^0.18.2",
40-
"url-loader": "^0.5.9",
4143
"webpack": "^3.5.6"
4244
},
4345
"dependencies": {

src/index.js

Lines changed: 6 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -4,54 +4,27 @@ import pick from 'lodash.pick';
44

55
class FilterChunkWebpackPlugin {
66
constructor(options = {}) {
7-
if (!Array.isArray(options.patterns)) {
7+
if (typeof options.patterns !== 'undefined' && !Array.isArray(options.patterns)) {
88
throw new Error('The "patterns" option should be an array');
99
}
1010

1111
this.options = Object.assign({
12-
debug: this.log,
13-
include: false,
12+
select: false,
1413
patterns: [],
15-
preview: false
1614
}, options);
1715
}
1816

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-
32-
previewMatchedFiles(matchedFiles) {
33-
const action = this.options.include === true ? 'included' : 'excluded';
34-
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('');
40-
}
41-
4217
apply(compiler) {
18+
const filter = this.options.select === true ? pick : omit;
19+
4320
compiler.plugin('emit', (compilation, callback) => {
4421

4522
if (this.options.patterns.length > 0) {
4623
const files = Object.keys(compilation.assets);
4724
const matchedFiles = multimatch(files, this.options.patterns);
4825

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-
}
26+
// eslint-disable-next-line no-param-reassign
27+
compilation.assets = filter(compilation.assets, matchedFiles);
5528
}
5629

5730
callback();

src/index.spec.js

Lines changed: 1 addition & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
// import path from 'path';
2-
// import fs from 'fs';
3-
// import webpack from 'webpack';
4-
51
import FilterChunkWebpackPlugin from './index';
62

73
describe('FilterChunkWebpackPlugin', function () {
@@ -13,6 +9,7 @@ describe('FilterChunkWebpackPlugin', function () {
139
preview: true,
1410
patterns: ['**/**']
1511
});
12+
1613
expect(plugin.options.include).toBe(true);
1714
expect(plugin.options.preview).toBe(true);
1815
expect(plugin.options.patterns).toEqual(['**/**']);
@@ -22,54 +19,7 @@ describe('FilterChunkWebpackPlugin', function () {
2219
const plugin = () => new FilterChunkWebpackPlugin({
2320
patterns: 'test'
2421
});
25-
expect(plugin).toThrow(PATTERN_ERROR)
26-
});
2722

28-
it('should throw an Error when options not set', function () {
29-
const plugin = () => new FilterChunkWebpackPlugin();
3023
expect(plugin).toThrow(PATTERN_ERROR)
3124
});
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-
// });
7525
});

0 commit comments

Comments
 (0)