Skip to content

Commit 7265aba

Browse files
authored
fix: update globby to v11 (#248)
Fixes #231 BREAKING CHANGE: `filename` no longer accepts a glob pattern, that must be passed as `glob` instead.
1 parent b9bacc2 commit 7265aba

File tree

8 files changed

+57
-911
lines changed

8 files changed

+57
-911
lines changed

README.md

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,8 @@ below, just pass multiple objects as an array.
6767
new AddAssetHtmlPlugin([
6868
{ filepath: require.resolve('./some-file') },
6969
{ filepath: require.resolve('./some-other-file') },
70-
// Glob to match all of the dll file
71-
{ filepath: require.resolve('./**/*.dll.js') },
70+
// Glob to match all of the dll file, make sure to use forward slashes on Windows
71+
{ glob: require.resolve('./**/*.dll.js') },
7272
]);
7373
```
7474

@@ -82,10 +82,17 @@ new AddAssetHtmlPlugin({ filepath: require.resolve('./some-file') });
8282

8383
#### `filepath`
8484

85-
Type: `string|Glob`, mandatory
85+
Type: `string`, mandatory unless `glob` is defined
8686

8787
The absolute path of the file you want to add to the compilation, and resulting
88-
HTML file. Also support globby string.
88+
HTML file.
89+
90+
#### `glob`
91+
92+
Type: `string`, mandatory unless `filepath` is defined
93+
94+
A glob used to locate files to add to the compilation. See
95+
[`globby`'s docs](https://github.com/sindresorhus/globby) for how to use it.
8996

9097
#### `files`
9198

__snapshots__/test.js.snap

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,12 +73,14 @@ Object {
7373
exports[`should invoke callback on error 1`] = `
7474
Array [
7575
[Error: No filepath defined],
76+
[Error: No filepath defined],
7677
]
7778
`;
7879

7980
exports[`should reject on error 1`] = `
8081
Array [
8182
[Error: No filepath defined],
83+
[Error: No filepath defined],
8284
]
8385
`;
8486

@@ -117,3 +119,5 @@ Object {
117119
],
118120
}
119121
`;
122+
123+
exports[`throws of both filepath and glob is defined 1`] = `"Both filepath and glob defined in {\\"filepath\\":\\"my-file.js\\",\\"glob\\":\\"my-file.js\\"} - only use one of them"`;

example/dll/webpack.config.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const path = require('path');
22
const webpack = require('webpack');
3+
const slash = require('slash');
34
const HtmlWebpackPlugin = require('html-webpack-plugin');
45
const AddAssetHtmlPlugin = require('../../');
56

@@ -22,7 +23,8 @@ module.exports = {
2223
}),
2324
new HtmlWebpackPlugin(),
2425
new AddAssetHtmlPlugin({
25-
filepath: path.resolve(__dirname, './build/*.dll.js'),
26+
// glob needs to use forward slashes
27+
glob: `${slash(path.resolve(__dirname, './build'))}/*.dll.js`,
2628
}),
2729
],
2830
};

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
},
3535
"homepage": "https://github.com/SimenB/add-asset-html-webpack-plugin#readme",
3636
"dependencies": {
37-
"globby": "^9.2.0",
37+
"globby": "^11.1.0",
3838
"micromatch": "^4.0.4"
3939
},
4040
"devDependencies": {

src/index.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,15 @@ module.exports = class AddAssetHtmlPlugin {
8686
}
8787

8888
async addAllAssetsToCompilation(compilation, htmlPluginData) {
89-
const handledAssets = await handleUrl(this.assets);
90-
// eslint-disable-next-line no-restricted-syntax
91-
for (const asset of handledAssets) {
92-
await this.addFileToAssets(compilation, htmlPluginData, asset);
89+
try {
90+
const handledAssets = await handleUrl(this.assets);
91+
// eslint-disable-next-line no-restricted-syntax
92+
for (const asset of handledAssets) {
93+
await this.addFileToAssets(compilation, htmlPluginData, asset);
94+
}
95+
} catch (error) {
96+
compilation.errors.push(error);
97+
throw error;
9398
}
9499
return htmlPluginData;
95100
}

src/utils.js

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,16 +40,21 @@ function resolveOutput(compilation, addedFilename, outputPath) {
4040
async function handleUrl(assets) {
4141
const globbyAssets = [];
4242
const normalAssets = [];
43-
// if filepath is null or undefined, just bubble up.
44-
assets.forEach(asset =>
45-
asset.filepath && globby.hasMagic(asset.filepath)
46-
? globbyAssets.push(asset)
47-
: normalAssets.push(asset),
48-
);
43+
assets.forEach(asset => {
44+
if (asset.filepath && asset.glob) {
45+
throw new Error(
46+
`Both filepath and glob defined in ${JSON.stringify(
47+
asset,
48+
)} - only use one of them`,
49+
);
50+
}
51+
52+
return asset.glob ? globbyAssets.push(asset) : normalAssets.push(asset);
53+
});
4954
const ret = [];
5055
await Promise.all(
5156
globbyAssets.map(asset =>
52-
globby(asset.filepath).then(paths =>
57+
globby(asset.glob).then(paths =>
5358
paths.forEach(filepath =>
5459
ret.push(Object.assign({}, asset, { filepath })),
5560
),

test.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ test('filter option should include some files with string option', async () => {
292292
});
293293

294294
test('use globby to find multi file', async () => {
295-
const assets = [{ filepath: './src/*.js' }];
295+
const assets = [{ glob: './src/*.js' }];
296296
const ret = await handleUrl(assets);
297297
expect(ret).toHaveLength(2);
298298
});
@@ -302,3 +302,8 @@ test('filepath without globbyMagic should just return', async () => {
302302
const ret = await handleUrl(assets);
303303
expect(ret).toHaveLength(1);
304304
});
305+
306+
test('throws of both filepath and glob is defined', async () => {
307+
const assets = [{ filepath: 'my-file.js', glob: 'my-file.js' }];
308+
await expect(handleUrl(assets)).rejects.toThrowErrorMatchingSnapshot();
309+
});

0 commit comments

Comments
 (0)