Skip to content

Commit 7e3f856

Browse files
GenuifxSimenB
authored andcommitted
[enhancement] analyze filepath via globby (#78)
* feat: analyze filepath with globby * chores: change test suits name * chores: review and update code * chores: changelog, readme and drop useless code * 2.0.2 * Revert "2.0.2" This reverts commit 43aecb1. * Move changelog entry to unreleased * Simplify glob promises
1 parent 191a20e commit 7e3f856

File tree

9 files changed

+63
-8
lines changed

9 files changed

+63
-8
lines changed

.vscode/settings.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"eslint.enable": true
3+
}

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ This project adheres to [Semantic Versioning](http://semver.org/).
55
Latest version of this document will always be available on https://github.com/SimenB/add-asset-html-webpack-plugin/releases
66

77
## [Unreleased]
8-
Nothing
8+
### Added
9+
- Support globby string as filepath option
910

1011
## [2.0.1] - 2017-04-23
1112
### Fixes

README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ below, just pass multiple objects as an array.
6464
new AddAssetHtmlPlugin([
6565
{ filepath: require.resolve('./some-file') },
6666
{ filepath: require.resolve('./some-other-file') },
67+
// Glob to match all of the dll file
68+
{ filepath: require.resolve('./**/*.dll.js') },
6769
]);
6870
```
6971

@@ -75,10 +77,10 @@ new AddAssetHtmlPlugin({ filepath: require.resolve('./some-file') });
7577
```
7678

7779
#### `filepath`
78-
Type: `string`, mandatory
80+
Type: `string|Glob`, mandatory
7981

8082
The absolute path of the file you want to add to the compilation, and resulting
81-
HTML file.
83+
HTML file. Also support globby string.
8284

8385
#### `filter`
8486
Type: `string|Array<string>`, default `[]
@@ -169,7 +171,7 @@ const webpackConfig = {
169171
}),
170172
new HtmlWebpackPlugin(),
171173
new AddAssetHtmlPlugin({
172-
filepath: require.resolve('./build/vendor.dll.js'),
174+
filepath: path.resolve(__dirname,'./build/*.dll.js'),
173175
}),
174176
],
175177
};

example/webpack.config.dll.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@ module.exports = {
66
context: __dirname,
77
entry: {
88
vendor: ['classnames'],
9+
lib: ['classnames'],
910
},
1011
devtool: '#source-map',
1112
output: {
1213
path: path.join(__dirname, 'build'),
13-
filename: '[name].dll.js',
14+
filename: '[name].[hash:4].dll.js',
1415
library: '[name]_[hash]',
1516
},
1617
plugins: [

example/webpack.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ module.exports = {
2020
}),
2121
new HtmlWebpackPlugin(),
2222
new AddAssetHtmlPlugin({
23-
filepath: require.resolve('./build/vendor.dll.js'),
23+
filepath: path.resolve(__dirname, './build/*.dll.js'),
2424
}),
2525
],
2626
};

package.json

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

src/addAllAssetsToCompilation.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import path from 'path';
22
import crypto from 'crypto';
33
import Promise from 'bluebird';
44
import minimatch from 'minimatch';
5+
import handleUrl from './handleUrl';
56

67
function ensureTrailingSlash(string) {
78
if (string.length && string.substr(-1, 1) !== '/') {
@@ -99,7 +100,8 @@ async function addFileToAssets(
99100
// Visible for testing
100101
export default async function(assets, compilation, htmlPluginData, callback) {
101102
try {
102-
await Promise.mapSeries(assets, asset =>
103+
const handledAssets = await handleUrl(assets);
104+
await Promise.mapSeries(handledAssets, asset =>
103105
addFileToAssets(compilation, htmlPluginData, asset)
104106
);
105107

src/handleUrl.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import globby from 'globby';
2+
3+
/**
4+
* handle globby filepath and return an array with all matched assets.
5+
*
6+
* @export
7+
* @param {Array} assets
8+
* @returns
9+
*/
10+
export default async function(assets) {
11+
const globbyAssets = [];
12+
const normalAssets = [];
13+
// if filepath is null or undefined, just bubble up.
14+
assets.forEach(
15+
asset =>
16+
asset.filepath && globby.hasMagic(asset.filepath)
17+
? globbyAssets.push(asset)
18+
: normalAssets.push(asset)
19+
);
20+
const ret = [];
21+
await Promise.all(
22+
globbyAssets.map(asset =>
23+
globby(asset.filepath).then(paths =>
24+
paths.forEach(path =>
25+
ret.push(Object.assign({}, asset, { filepath: path }))
26+
)
27+
)
28+
)
29+
);
30+
31+
return ret.concat(normalAssets);
32+
}

test.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import path from 'path';
44
import Promise from 'bluebird';
55
import AddAssetHtmlPlugin from './src/index';
66
import addAllAssetsToCompilation from './src/addAllAssetsToCompilation';
7+
import handleUrl from './src/handleUrl';
78

89
const pluginMock = {
910
plugin: {
@@ -295,3 +296,15 @@ test('filter option should include some files with string option', async () => {
295296
expect(callback).toHaveBeenCalledTimes(1);
296297
expect(callback).toHaveBeenCalledWith(null, pluginData);
297298
});
299+
300+
test('use globby to find multi file', async () => {
301+
const assets = [{ filepath: './src/*.js' }];
302+
const ret = await handleUrl(assets);
303+
expect(ret).toHaveLength(3);
304+
});
305+
306+
test('filepath without globbyMagic should just return', async () => {
307+
const assets = [{ filepath: path.join(__dirname, 'my-file.js') }];
308+
const ret = await handleUrl(assets);
309+
expect(ret).toHaveLength(1);
310+
});

0 commit comments

Comments
 (0)