Skip to content

Commit

Permalink
fix: update globby to v11 (#248)
Browse files Browse the repository at this point in the history
Fixes #231

BREAKING CHANGE: `filename` no longer accepts a glob pattern, that must be passed as `glob` instead.
  • Loading branch information
SimenB committed Mar 8, 2022
1 parent b9bacc2 commit 7265aba
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 911 deletions.
15 changes: 11 additions & 4 deletions README.md
Expand Up @@ -67,8 +67,8 @@ below, just pass multiple objects as an array.
new AddAssetHtmlPlugin([
{ filepath: require.resolve('./some-file') },
{ filepath: require.resolve('./some-other-file') },
// Glob to match all of the dll file
{ filepath: require.resolve('./**/*.dll.js') },
// Glob to match all of the dll file, make sure to use forward slashes on Windows
{ glob: require.resolve('./**/*.dll.js') },
]);
```

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

#### `filepath`

Type: `string|Glob`, mandatory
Type: `string`, mandatory unless `glob` is defined

The absolute path of the file you want to add to the compilation, and resulting
HTML file. Also support globby string.
HTML file.

#### `glob`

Type: `string`, mandatory unless `filepath` is defined

A glob used to locate files to add to the compilation. See
[`globby`'s docs](https://github.com/sindresorhus/globby) for how to use it.

#### `files`

Expand Down
4 changes: 4 additions & 0 deletions __snapshots__/test.js.snap
Expand Up @@ -73,12 +73,14 @@ Object {
exports[`should invoke callback on error 1`] = `
Array [
[Error: No filepath defined],
[Error: No filepath defined],
]
`;

exports[`should reject on error 1`] = `
Array [
[Error: No filepath defined],
[Error: No filepath defined],
]
`;

Expand Down Expand Up @@ -117,3 +119,5 @@ Object {
],
}
`;

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"`;
4 changes: 3 additions & 1 deletion example/dll/webpack.config.js
@@ -1,5 +1,6 @@
const path = require('path');
const webpack = require('webpack');
const slash = require('slash');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const AddAssetHtmlPlugin = require('../../');

Expand All @@ -22,7 +23,8 @@ module.exports = {
}),
new HtmlWebpackPlugin(),
new AddAssetHtmlPlugin({
filepath: path.resolve(__dirname, './build/*.dll.js'),
// glob needs to use forward slashes
glob: `${slash(path.resolve(__dirname, './build'))}/*.dll.js`,
}),
],
};
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -34,7 +34,7 @@
},
"homepage": "https://github.com/SimenB/add-asset-html-webpack-plugin#readme",
"dependencies": {
"globby": "^9.2.0",
"globby": "^11.1.0",
"micromatch": "^4.0.4"
},
"devDependencies": {
Expand Down
13 changes: 9 additions & 4 deletions src/index.js
Expand Up @@ -86,10 +86,15 @@ module.exports = class AddAssetHtmlPlugin {
}

async addAllAssetsToCompilation(compilation, htmlPluginData) {
const handledAssets = await handleUrl(this.assets);
// eslint-disable-next-line no-restricted-syntax
for (const asset of handledAssets) {
await this.addFileToAssets(compilation, htmlPluginData, asset);
try {
const handledAssets = await handleUrl(this.assets);
// eslint-disable-next-line no-restricted-syntax
for (const asset of handledAssets) {
await this.addFileToAssets(compilation, htmlPluginData, asset);
}
} catch (error) {
compilation.errors.push(error);
throw error;
}
return htmlPluginData;
}
Expand Down
19 changes: 12 additions & 7 deletions src/utils.js
Expand Up @@ -40,16 +40,21 @@ function resolveOutput(compilation, addedFilename, outputPath) {
async function handleUrl(assets) {
const globbyAssets = [];
const normalAssets = [];
// if filepath is null or undefined, just bubble up.
assets.forEach(asset =>
asset.filepath && globby.hasMagic(asset.filepath)
? globbyAssets.push(asset)
: normalAssets.push(asset),
);
assets.forEach(asset => {
if (asset.filepath && asset.glob) {
throw new Error(
`Both filepath and glob defined in ${JSON.stringify(
asset,
)} - only use one of them`,
);
}

return asset.glob ? globbyAssets.push(asset) : normalAssets.push(asset);
});
const ret = [];
await Promise.all(
globbyAssets.map(asset =>
globby(asset.filepath).then(paths =>
globby(asset.glob).then(paths =>
paths.forEach(filepath =>
ret.push(Object.assign({}, asset, { filepath })),
),
Expand Down
7 changes: 6 additions & 1 deletion test.js
Expand Up @@ -292,7 +292,7 @@ test('filter option should include some files with string option', async () => {
});

test('use globby to find multi file', async () => {
const assets = [{ filepath: './src/*.js' }];
const assets = [{ glob: './src/*.js' }];
const ret = await handleUrl(assets);
expect(ret).toHaveLength(2);
});
Expand All @@ -302,3 +302,8 @@ test('filepath without globbyMagic should just return', async () => {
const ret = await handleUrl(assets);
expect(ret).toHaveLength(1);
});

test('throws of both filepath and glob is defined', async () => {
const assets = [{ filepath: 'my-file.js', glob: 'my-file.js' }];
await expect(handleUrl(assets)).rejects.toThrowErrorMatchingSnapshot();
});

0 comments on commit 7265aba

Please sign in to comment.