Skip to content

Commit 21a27af

Browse files
committed
fix: utilize new plugin API
1 parent 5162ecf commit 21a27af

File tree

6 files changed

+17
-95
lines changed

6 files changed

+17
-95
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ https://github.com/SimenB/add-asset-html-webpack-plugin/releases
1010

1111
### Changed
1212

13-
- Drop support for Node 4
13+
- Drop support for Node 4 and html-webpack-plugin@2
1414

1515
## [2.1.3] - 2018-03-03
1616

example/webpack.config.dll.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ module.exports = {
66
context: __dirname,
77
entry: {
88
vendor: ['classnames'],
9-
lib: ['classnames'],
109
},
1110
devtool: '#source-map',
11+
mode: 'development',
1212
output: {
1313
path: path.join(__dirname, 'build'),
1414
filename: '[name].[hash:4].dll.js',

example/webpack.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ module.exports = {
88
context: __dirname,
99
entry: path.join(__dirname, 'entry.js'),
1010
devtool: '#source-map',
11+
mode: 'development',
1112
output: {
1213
path: path.join(__dirname, 'dist'),
1314
filename: 'index_bundle.js',

src/addAllAssetsToCompilation.js

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -98,20 +98,10 @@ async function addFileToAssets(
9898
}
9999

100100
// Visible for testing
101-
export default async function(assets, compilation, htmlPluginData, callback) {
102-
try {
103-
const handledAssets = await handleUrl(assets);
104-
await pEachSeries(handledAssets, asset =>
105-
addFileToAssets(compilation, htmlPluginData, asset),
106-
);
107-
if (callback) {
108-
return callback(null, htmlPluginData);
109-
}
110-
return htmlPluginData;
111-
} catch (e) {
112-
if (callback) {
113-
return callback(e, htmlPluginData);
114-
}
115-
throw new Error(e);
116-
}
101+
export default async function(assets, compilation, htmlPluginData) {
102+
const handledAssets = await handleUrl(assets);
103+
await pEachSeries(handledAssets, asset =>
104+
addFileToAssets(compilation, htmlPluginData, asset),
105+
);
106+
return htmlPluginData;
117107
}

src/index.js

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,11 @@ export default class AddAssetHtmlPlugin {
77

88
/* istanbul ignore next: this would be integration tests */
99
apply(compiler) {
10-
compiler.plugin('compilation', compilation => {
11-
compilation.plugin(
12-
'html-webpack-plugin-before-html-generation',
13-
async (htmlPluginData, callback) => {
14-
await addAllAssetsToCompilation(
15-
this.assets,
16-
compilation,
17-
htmlPluginData,
18-
callback,
19-
);
20-
},
10+
compiler.hooks.compilation.tap('AddAssetHtmlPlugin', compilation => {
11+
compilation.hooks.htmlWebpackPluginBeforeHtmlGeneration.tapPromise(
12+
'AddAssetHtmlPlugin',
13+
htmlPluginData =>
14+
addAllAssetsToCompilation(this.assets, compilation, htmlPluginData),
2115
);
2216
});
2317
}

test.js

Lines changed: 3 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -20,31 +20,20 @@ test('assets should should be reversed', () => {
2020
expect(new AddAssetHtmlPlugin(['a', 'b']).assets).toEqual(['b', 'a']);
2121
});
2222

23-
test('should invoke callback on success', async () => {
24-
const callback = jest.fn();
25-
26-
await addAllAssetsToCompilation([], {}, pluginMock, callback);
27-
28-
expect(callback).toHaveBeenCalledTimes(1);
29-
expect(callback).toHaveBeenCalledWith(null, pluginMock);
30-
});
31-
3223
test('should not reject success', async () => {
3324
expect(await addAllAssetsToCompilation([], {}, pluginMock)).toEqual(
3425
pluginMock,
3526
);
3627
});
3728

3829
test('should invoke callback on error', async () => {
39-
const callback = jest.fn();
4030
const compilation = { errors: [] };
4131

42-
await addAllAssetsToCompilation([{}], compilation, pluginMock, callback);
32+
await expect(
33+
addAllAssetsToCompilation([{}], compilation, pluginMock),
34+
).rejects.toThrowError();
4335

4436
expect(compilation.errors).toMatchSnapshot();
45-
46-
expect(callback).toHaveBeenCalledTimes(1);
47-
expect(callback).toHaveBeenCalledWith(compilation.errors[0], pluginMock);
4837
});
4938

5039
test('should reject on error', async () => {
@@ -60,64 +49,48 @@ test('should reject on error', async () => {
6049
});
6150

6251
test("should add file using compilation's publicPath", async () => {
63-
const callback = jest.fn();
6452
const compilation = { options: { output: { publicPath: 'vendor/' } } };
6553
const pluginData = Object.assign({ assets: { js: [], css: [] } }, pluginMock);
6654

6755
await addAllAssetsToCompilation(
6856
[{ filepath: path.join(__dirname, 'my-file.js') }],
6957
compilation,
7058
pluginData,
71-
callback,
7259
);
7360

7461
expect(pluginData.assets).toMatchSnapshot();
75-
76-
expect(callback).toHaveBeenCalledTimes(1);
77-
expect(callback).toHaveBeenCalledWith(null, pluginData);
7862
});
7963

8064
test('should used passed in publicPath', async () => {
81-
const callback = jest.fn();
8265
const compilation = { options: { output: { publicPath: 'vendor/' } } };
8366
const pluginData = Object.assign({ assets: { js: [], css: [] } }, pluginMock);
8467

8568
await addAllAssetsToCompilation(
8669
[{ filepath: 'my-file.js', publicPath: 'pp' }],
8770
compilation,
8871
pluginData,
89-
callback,
9072
);
9173

9274
expect(pluginData.assets).toMatchSnapshot();
93-
94-
expect(callback).toHaveBeenCalledTimes(1);
95-
expect(callback).toHaveBeenCalledWith(null, pluginData);
9675
});
9776

9877
// TODO: No idea what this does, actually... Coverage currently hits it, but the logic is untested.
9978
test.skip('should handle missing `publicPath`', () => {});
10079

10180
test('should add file missing "/" to public path', async () => {
102-
const callback = jest.fn();
10381
const compilation = { options: { output: { publicPath: 'vendor' } } };
10482
const pluginData = Object.assign({ assets: { js: [], css: [] } }, pluginMock);
10583

10684
await addAllAssetsToCompilation(
10785
[{ filepath: 'my-file.js' }],
10886
compilation,
10987
pluginData,
110-
callback,
11188
);
11289

11390
expect(pluginData.assets).toMatchSnapshot();
114-
115-
expect(callback).toHaveBeenCalledTimes(1);
116-
expect(callback).toHaveBeenCalledWith(null, pluginData);
11791
});
11892

11993
test('should add sourcemap to compilation', async () => {
120-
const callback = jest.fn();
12194
const addFileToAssetsStub = jest.fn();
12295
const compilation = { options: { output: {} } };
12396
const pluginData = {
@@ -130,14 +103,10 @@ test('should add sourcemap to compilation', async () => {
130103
[{ filepath: 'my-file.js' }],
131104
compilation,
132105
pluginData,
133-
callback,
134106
);
135107

136108
expect(pluginData.assets).toMatchSnapshot();
137109

138-
expect(callback).toHaveBeenCalledTimes(1);
139-
expect(callback).toHaveBeenCalledWith(null, pluginData);
140-
141110
expect(addFileToAssetsStub).toHaveBeenCalledTimes(2);
142111
expect(addFileToAssetsStub.mock.calls[0]).toEqual([
143112
'my-file.js',
@@ -150,7 +119,6 @@ test('should add sourcemap to compilation', async () => {
150119
});
151120

152121
test('should skip adding sourcemap to compilation if set to false', async () => {
153-
const callback = jest.fn();
154122
const addFileToAssetsStub = jest.fn();
155123
const compilation = { options: { output: {} } };
156124
const pluginData = {
@@ -163,20 +131,15 @@ test('should skip adding sourcemap to compilation if set to false', async () =>
163131
[{ filepath: 'my-file.js', includeSourcemap: false }],
164132
compilation,
165133
pluginData,
166-
callback,
167134
);
168135

169136
expect(pluginData.assets).toMatchSnapshot();
170137

171-
expect(callback).toHaveBeenCalledTimes(1);
172-
expect(callback).toHaveBeenCalledWith(null, pluginData);
173-
174138
expect(addFileToAssetsStub).toHaveBeenCalledTimes(1);
175139
expect(addFileToAssetsStub).toHaveBeenCalledWith('my-file.js', compilation);
176140
});
177141

178142
test('should include hash of file content if option is set', async () => {
179-
const callback = jest.fn();
180143
const compilation = {
181144
options: { output: {} },
182145
assets: {
@@ -189,17 +152,12 @@ test('should include hash of file content if option is set', async () => {
189152
[{ filepath: 'my-file.js', hash: true }],
190153
compilation,
191154
pluginData,
192-
callback,
193155
);
194156

195157
expect(pluginData.assets).toMatchSnapshot();
196-
197-
expect(callback).toHaveBeenCalledTimes(1);
198-
expect(callback).toHaveBeenCalledWith(null, pluginData);
199158
});
200159

201160
test('should add to css if `typeOfAsset` is css', async () => {
202-
const callback = jest.fn();
203161
const compilation = {
204162
options: { output: {} },
205163
assets: {
@@ -212,17 +170,12 @@ test('should add to css if `typeOfAsset` is css', async () => {
212170
[{ filepath: 'my-file.css', typeOfAsset: 'css' }],
213171
compilation,
214172
pluginData,
215-
callback,
216173
);
217174

218175
expect(pluginData.assets).toMatchSnapshot();
219-
220-
expect(callback).toHaveBeenCalledTimes(1);
221-
expect(callback).toHaveBeenCalledWith(null, pluginData);
222176
});
223177

224178
test('should replace compilation assets key if `outputPath` is set', async () => {
225-
const callback = jest.fn();
226179
const source = { source: () => 'test' };
227180
const addFileToAssetsMock = (filename, compilation) => {
228181
const name = path.basename(filename);
@@ -242,7 +195,6 @@ test('should replace compilation assets key if `outputPath` is set', async () =>
242195
[{ filepath: 'my-file.js', outputPath: 'assets' }],
243196
compilation,
244197
pluginData,
245-
callback,
246198
);
247199

248200
expect(pluginData.assets).toMatchSnapshot();
@@ -254,7 +206,6 @@ test('should replace compilation assets key if `outputPath` is set', async () =>
254206
});
255207

256208
test('filter option should exclude some files', async () => {
257-
const callback = jest.fn();
258209
const compilation = { options: { output: { publicPath: 'vendor/' } } };
259210
const pluginData = Object.assign({ assets: { js: [], css: [] } }, pluginMock);
260211

@@ -267,49 +218,35 @@ test('filter option should exclude some files', async () => {
267218
],
268219
compilation,
269220
pluginData,
270-
callback,
271221
);
272222

273223
expect(pluginData.assets).toMatchSnapshot();
274-
275-
expect(callback).toHaveBeenCalledTimes(1);
276-
expect(callback).toHaveBeenCalledWith(null, pluginData);
277224
});
278225

279226
test('filter option should include some files', async () => {
280-
const callback = jest.fn();
281227
const compilation = { options: { output: { publicPath: 'vendor/' } } };
282228
const pluginData = Object.assign({ assets: { js: [], css: [] } }, pluginMock);
283229

284230
await addAllAssetsToCompilation(
285231
[{ filepath: path.join(__dirname, 'my-file.js'), files: ['index.*'] }],
286232
compilation,
287233
pluginData,
288-
callback,
289234
);
290235

291236
expect(pluginData.assets).toMatchSnapshot();
292-
293-
expect(callback).toHaveBeenCalledTimes(1);
294-
expect(callback).toHaveBeenCalledWith(null, pluginData);
295237
});
296238

297239
test('filter option should include some files with string option', async () => {
298-
const callback = jest.fn();
299240
const compilation = { options: { output: { publicPath: 'vendor/' } } };
300241
const pluginData = Object.assign({ assets: { js: [], css: [] } }, pluginMock);
301242

302243
await addAllAssetsToCompilation(
303244
[{ filepath: path.join(__dirname, 'my-file.js'), files: 'index.*' }],
304245
compilation,
305246
pluginData,
306-
callback,
307247
);
308248

309249
expect(pluginData.assets).toMatchSnapshot();
310-
311-
expect(callback).toHaveBeenCalledTimes(1);
312-
expect(callback).toHaveBeenCalledWith(null, pluginData);
313250
});
314251

315252
test('use globby to find multi file', async () => {

0 commit comments

Comments
 (0)