Skip to content

Commit 0ae8d22

Browse files
committed
Add files option
Closes #61
1 parent 1e4334a commit 0ae8d22

4 files changed

Lines changed: 73 additions & 11 deletions

File tree

README.md

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ $ npm i add-asset-html-webpack-plugin -D
2121
NOTE: This plugin requires `html-webpack-plugin@^2.10.0`.
2222

2323
## Basic Usage
24-
The plugin will add the given JS or CSS file to the files Webpack knows about, and put it into the
25-
list of assets `html-webpack-plugin` injects into the generated html. Add the plugin the your
26-
config, providing it a filepath:
24+
The plugin will add the given JS or CSS file to the files Webpack knows about,
25+
and put it into the list of assets `html-webpack-plugin` injects into the
26+
generated html. Add the plugin the your config, providing it a filepath:
2727

2828
```js
2929
const HtmlWebpackPlugin = require('html-webpack-plugin');
@@ -41,7 +41,8 @@ const webpackConfig = {
4141
};
4242
```
4343

44-
This will add a script tag to the HTML generated by `html-webpack-plugin`, and look like:
44+
This will add a script tag to the HTML generated by `html-webpack-plugin`, and
45+
look like:
4546
```html
4647
<!DOCTYPE html>
4748
<html>
@@ -56,8 +57,8 @@ This will add a script tag to the HTML generated by `html-webpack-plugin`, and l
5657
</html>
5758
```
5859

59-
NOTE: You can also pass an array of assets to be added. Same API as mentioned below, just pass
60-
multiple objects as an array.
60+
NOTE: You can also pass an array of assets to be added. Same API as mentioned
61+
below, just pass multiple objects as an array.
6162

6263
```js
6364
new AddAssetHtmlPlugin([
@@ -76,12 +77,22 @@ new AddAssetHtmlPlugin({ filepath: require.resolve('./some-file') });
7677
#### `filepath`
7778
Type: `string`, mandatory
7879

79-
The absolute path of the file you want to add to the compilation, and resulting HTML file.
80+
The absolute path of the file you want to add to the compilation, and resulting
81+
HTML file.
82+
83+
#### `filter`
84+
Type: `string|Array<string>`, default `[]
85+
86+
Files that the assets will be added to.
87+
88+
By default the assets will be included in all files. If files are defined, the
89+
assets will only be included in specified file globs.
8090

8191
#### `hash`
8292
Type: `boolean`, default: `false`
8393

84-
If `true`, will append a unique hash of the file to the filename. This is useful for cache busting.
94+
If `true`, will append a unique hash of the file to the filename. This is
95+
useful for cache busting.
8596

8697
#### `includeSourcemap`
8798
Type: `boolean`, default: `true`

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@
3838
},
3939
"homepage": "https://github.com/SimenB/add-asset-html-webpack-plugin#readme",
4040
"dependencies": {
41-
"bluebird": "^3.4.6"
41+
"bluebird": "^3.4.6",
42+
"minimatch": "^3.0.3"
4243
},
4344
"devDependencies": {
4445
"babel-cli": "^6.18.0",

src/addAllAssetsToCompilation.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import path from 'path';
22
import crypto from 'crypto';
33
import Promise from 'bluebird';
4+
import minimatch from 'minimatch';
45

56
function ensureTrailingSlash(string) {
67
if (string.length && string.substr(-1, 1) !== '/') {
@@ -30,14 +31,24 @@ function resolveOutput(compilation, addedFilename, outputPath) {
3031
async function addFileToAssets(
3132
compilation,
3233
htmlPluginData,
33-
{ filepath, typeOfAsset = 'js', includeSourcemap = true, hash = false, publicPath, outputPath }
34+
{ filepath, typeOfAsset = 'js', includeSourcemap = true, hash = false, publicPath, outputPath, files = [] }
3435
) {
3536
if (!filepath) {
3637
const error = new Error('No filepath defined');
3738
compilation.errors.push(error);
3839
return Promise.reject(error);
3940
}
4041

42+
const filters = Array.isArray(files) ? files : [files];
43+
44+
if (filters.length > 0) {
45+
const shouldSkip = !files.some(file => minimatch(htmlPluginData.outputName, file));
46+
47+
if (shouldSkip) {
48+
return Promise.resolve(null);
49+
}
50+
}
51+
4152
const addedFilename = await htmlPluginData.plugin.addFileToAssets(filepath, compilation);
4253

4354
let suffix = '';

test.js

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const pluginMock = {
99
plugin: {
1010
addFileToAssets: filename => Promise.resolve(path.basename(filename)),
1111
},
12+
outputName: 'index.html',
1213
};
1314

1415
test('assets should always be an array', () => {
@@ -76,7 +77,7 @@ test.concurrent('should used passed in publicPath', async () => {
7677
expect(callback).toHaveBeenCalledWith(null, pluginData);
7778
});
7879

79-
// No idea what this does, actually... Coverage currently hits it, but the logic is untested.
80+
// TODO: No idea what this does, actually... Coverage currently hits it, but the logic is untested.
8081
test('should handle missing `publicPath`');
8182

8283
test.concurrent('should add file missing "/" to public path', async () => {
@@ -200,3 +201,41 @@ test.concurrent('should replace compilation assets key if `outputPath` is set',
200201
expect(compilation.assets['my-file.js.map']).toBeUndefined();
201202
expect(compilation.assets['assets/my-file.js.map']).toEqual(source);
202203
});
204+
205+
test.concurrent('filter option should exclude some files', async () => {
206+
const callback = jest.fn();
207+
const compilation = { options: { output: { publicPath: 'vendor/' } } };
208+
const pluginData = Object.assign({ assets: { js: [], css: [] } }, pluginMock);
209+
210+
await addAllAssetsToCompilation(
211+
[{ filepath: path.join(__dirname, 'my-file.js'), files: ['something-weird'] }],
212+
compilation,
213+
pluginData,
214+
callback
215+
);
216+
217+
expect(pluginData.assets.css).toEqual([]);
218+
expect(pluginData.assets.js).toEqual([]);
219+
220+
expect(callback).toHaveBeenCalledTimes(1);
221+
expect(callback).toHaveBeenCalledWith(null, pluginData);
222+
});
223+
224+
test.concurrent('filter option should include some files', async () => {
225+
const callback = jest.fn();
226+
const compilation = { options: { output: { publicPath: 'vendor/' } } };
227+
const pluginData = Object.assign({ assets: { js: [], css: [] } }, pluginMock);
228+
229+
await addAllAssetsToCompilation(
230+
[{ filepath: path.join(__dirname, 'my-file.js'), files: ['index.*'] }],
231+
compilation,
232+
pluginData,
233+
callback
234+
);
235+
236+
expect(pluginData.assets.css).toEqual([]);
237+
expect(pluginData.assets.js).toEqual(['vendor/my-file.js']);
238+
239+
expect(callback).toHaveBeenCalledTimes(1);
240+
expect(callback).toHaveBeenCalledWith(null, pluginData);
241+
});

0 commit comments

Comments
 (0)