Skip to content

Commit d00e214

Browse files
ndbroadbentSimenB
authored andcommitted
feat: add "includeRelated" option to also copy all related files (#63)
This will allow us to generically all "related files", such as gzipped ones, instead of just source maps. BREAKING CHANGE: Remove `includeSourcemap` option
1 parent 21a27af commit d00e214

File tree

10 files changed

+124
-76
lines changed

10 files changed

+124
-76
lines changed

CHANGELOG.md

Lines changed: 39 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ https://github.com/SimenB/add-asset-html-webpack-plugin/releases
88

99
## [Unreleased]
1010

11+
### Features
12+
13+
- Replace `includeSourcemap` with a more generic `includeRelatedFiles` which
14+
support adding all sort of files, not just source maps
15+
1116
### Changed
1217

1318
- Drop support for Node 4 and html-webpack-plugin@2
@@ -153,20 +158,37 @@ https://github.com/SimenB/add-asset-html-webpack-plugin/releases
153158

154159
Initial release
155160

156-
[unreleased]: https://github.com/SimenB/add-asset-html-webpack-plugin/compare/v2.1.3...HEAD
157-
[2.1.3]: https://github.com/SimenB/add-asset-html-webpack-plugin/compare/v2.1.2...v2.1.3
158-
[2.1.2]: https://github.com/SimenB/add-asset-html-webpack-plugin/compare/v2.1.1...v2.1.2
159-
[2.1.1]: https://github.com/SimenB/add-asset-html-webpack-plugin/compare/v2.1.0...v2.1.1
160-
[2.1.0]: https://github.com/SimenB/add-asset-html-webpack-plugin/compare/v2.0.1...v2.1.0
161-
[2.0.1]: https://github.com/SimenB/add-asset-html-webpack-plugin/compare/v2.0.0...v2.0.1
162-
[2.0.0]: https://github.com/SimenB/add-asset-html-webpack-plugin/compare/v1.0.2...v2.0.0
163-
[1.0.2]: https://github.com/SimenB/add-asset-html-webpack-plugin/compare/v1.0.1...v1.0.2
164-
[1.0.1]: https://github.com/SimenB/add-asset-html-webpack-plugin/compare/v1.0.0...v1.0.1
165-
[1.0.0]: https://github.com/SimenB/add-asset-html-webpack-plugin/compare/v0.4.0...v1.0.0
166-
[0.4.0]: https://github.com/SimenB/add-asset-html-webpack-plugin/compare/v0.3.0...v0.4.0
167-
[0.3.0]: https://github.com/SimenB/add-asset-html-webpack-plugin/compare/v0.2.0...v0.3.0
168-
[0.2.0]: https://github.com/SimenB/add-asset-html-webpack-plugin/compare/v0.1.0...v0.2.0
169-
[0.1.0]: https://github.com/SimenB/add-asset-html-webpack-plugin/compare/v0.0.3...v0.1.0
170-
[0.0.3]: https://github.com/SimenB/add-asset-html-webpack-plugin/compare/v0.0.2...v0.0.3
171-
[0.0.2]: https://github.com/SimenB/add-asset-html-webpack-plugin/compare/v0.0.1...v0.0.2
172-
[0.0.1]: https://github.com/SimenB/add-asset-html-webpack-plugin/commit/02e262d47b56934b714f71d92b557ba3204eae22
161+
[unreleased]:
162+
https://github.com/SimenB/add-asset-html-webpack-plugin/compare/v2.1.3...HEAD
163+
[2.1.3]:
164+
https://github.com/SimenB/add-asset-html-webpack-plugin/compare/v2.1.2...v2.1.3
165+
[2.1.2]:
166+
https://github.com/SimenB/add-asset-html-webpack-plugin/compare/v2.1.1...v2.1.2
167+
[2.1.1]:
168+
https://github.com/SimenB/add-asset-html-webpack-plugin/compare/v2.1.0...v2.1.1
169+
[2.1.0]:
170+
https://github.com/SimenB/add-asset-html-webpack-plugin/compare/v2.0.1...v2.1.0
171+
[2.0.1]:
172+
https://github.com/SimenB/add-asset-html-webpack-plugin/compare/v2.0.0...v2.0.1
173+
[2.0.0]:
174+
https://github.com/SimenB/add-asset-html-webpack-plugin/compare/v1.0.2...v2.0.0
175+
[1.0.2]:
176+
https://github.com/SimenB/add-asset-html-webpack-plugin/compare/v1.0.1...v1.0.2
177+
[1.0.1]:
178+
https://github.com/SimenB/add-asset-html-webpack-plugin/compare/v1.0.0...v1.0.1
179+
[1.0.0]:
180+
https://github.com/SimenB/add-asset-html-webpack-plugin/compare/v0.4.0...v1.0.0
181+
[0.4.0]:
182+
https://github.com/SimenB/add-asset-html-webpack-plugin/compare/v0.3.0...v0.4.0
183+
[0.3.0]:
184+
https://github.com/SimenB/add-asset-html-webpack-plugin/compare/v0.2.0...v0.3.0
185+
[0.2.0]:
186+
https://github.com/SimenB/add-asset-html-webpack-plugin/compare/v0.1.0...v0.2.0
187+
[0.1.0]:
188+
https://github.com/SimenB/add-asset-html-webpack-plugin/compare/v0.0.3...v0.1.0
189+
[0.0.3]:
190+
https://github.com/SimenB/add-asset-html-webpack-plugin/compare/v0.0.2...v0.0.3
191+
[0.0.2]:
192+
https://github.com/SimenB/add-asset-html-webpack-plugin/compare/v0.0.1...v0.0.2
193+
[0.0.1]:
194+
https://github.com/SimenB/add-asset-html-webpack-plugin/commit/02e262d47b56934b714f71d92b557ba3204eae22

README.md

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -108,11 +108,12 @@ Type: `boolean`, default: `false`
108108
If `true`, will append a unique hash of the file to the filename. This is useful
109109
for cache busting.
110110

111-
#### `includeSourcemap`
111+
#### `includeRelatedFiles`
112112

113113
Type: `boolean`, default: `true`
114114

115-
If `true`, will add `filepath + '.map'` to the compilation as well.
115+
If `true`, will add `filepath + '.*'` to the compilation as well. E.g
116+
`filepath.map` and `filepath.gz`.
116117

117118
#### `outputPath`
118119

@@ -200,16 +201,26 @@ const webpackConfig = {
200201
[npm-url]: https://npmjs.org/package/add-asset-html-webpack-plugin
201202
[npm-image]: https://img.shields.io/npm/v/add-asset-html-webpack-plugin.svg
202203
[travis-url]: https://travis-ci.org/SimenB/add-asset-html-webpack-plugin
203-
[travis-image]: https://img.shields.io/travis/SimenB/add-asset-html-webpack-plugin/master.svg
204-
[appveyor-url]: https://ci.appveyor.com/project/SimenB/add-asset-html-webpack-plugin
205-
[appveyor-image]: https://ci.appveyor.com/api/projects/status/dim5hcl49h3pi332/branch/master?svg=true
204+
[travis-image]:
205+
https://img.shields.io/travis/SimenB/add-asset-html-webpack-plugin/master.svg
206+
[appveyor-url]:
207+
https://ci.appveyor.com/project/SimenB/add-asset-html-webpack-plugin
208+
[appveyor-image]:
209+
https://ci.appveyor.com/api/projects/status/dim5hcl49h3pi332/branch/master?svg=true
206210
[codecov-url]: https://codecov.io/gh/SimenB/add-asset-html-webpack-plugin
207-
[codecov-image]: https://img.shields.io/codecov/c/github/SimenB/add-asset-html-webpack-plugin/master.svg
211+
[codecov-image]:
212+
https://img.shields.io/codecov/c/github/SimenB/add-asset-html-webpack-plugin/master.svg
208213
[david-url]: https://david-dm.org/SimenB/add-asset-html-webpack-plugin
209-
[david-image]: https://img.shields.io/david/SimenB/add-asset-html-webpack-plugin.svg
210-
[david-dev-url]: https://david-dm.org/SimenB/add-asset-html-webpack-plugin?type=dev
211-
[david-dev-image]: https://img.shields.io/david/dev/SimenB/add-asset-html-webpack-plugin.svg
212-
[david-peer-url]: https://david-dm.org/SimenB/add-asset-html-webpack-plugin?type=peer
213-
[david-peer-image]: https://img.shields.io/david/peer/SimenB/add-asset-html-webpack-plugin.svg
214+
[david-image]:
215+
https://img.shields.io/david/SimenB/add-asset-html-webpack-plugin.svg
216+
[david-dev-url]:
217+
https://david-dm.org/SimenB/add-asset-html-webpack-plugin?type=dev
218+
[david-dev-image]:
219+
https://img.shields.io/david/dev/SimenB/add-asset-html-webpack-plugin.svg
220+
[david-peer-url]:
221+
https://david-dm.org/SimenB/add-asset-html-webpack-plugin?type=peer
222+
[david-peer-image]:
223+
https://img.shields.io/david/peer/SimenB/add-asset-html-webpack-plugin.svg
214224
[greenkeeper-url]: https://greenkeeper.io/
215-
[greenkeeper-image]: https://badges.greenkeeper.io/SimenB/add-asset-html-webpack-plugin.svg
225+
[greenkeeper-image]:
226+
https://badges.greenkeeper.io/SimenB/add-asset-html-webpack-plugin.svg

__snapshots__/test.js.snap

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ Object {
4343
}
4444
`;
4545

46-
exports[`should add sourcemap to compilation 1`] = `
46+
exports[`should add sourcemap and gzipped files to compilation 1`] = `
4747
Object {
4848
"css": Array [],
4949
"js": Array [
@@ -86,12 +86,12 @@ exports[`should replace compilation assets key if \`outputPath\` is set 1`] = `
8686
Object {
8787
"css": Array [],
8888
"js": Array [
89-
"my-file.js",
89+
"some-file.js",
9090
],
9191
}
9292
`;
9393

94-
exports[`should skip adding sourcemap to compilation if set to false 1`] = `
94+
exports[`should skip adding sourcemap and gzipped files to compilation if set to false 1`] = `
9595
Object {
9696
"css": Array [],
9797
"js": Array [

fixture/some-file.js

Whitespace-only changes.

fixture/some-file.js.gz

Whitespace-only changes.

fixture/some-file.js.map

Whitespace-only changes.

index.d.ts

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,52 @@
11
declare namespace AddAssetHtmlPlugin {
22
interface Options {
33
/**
4-
* The absolute path of the file you want to add to the compilation, and resulting HTML file.
5-
* Mandatory.
6-
*/
4+
* The absolute path of the file you want to add to the compilation, and resulting HTML file.
5+
* Mandatory.
6+
*/
77
filepath: string;
88

99
/**
10-
* Files that the assets will be added to.
11-
* By default the assets will be included in all files. If files are defined, the
12-
* assets will only be included in specified file globs.
13-
*/
10+
* Files that the assets will be added to.
11+
* By default the assets will be included in all files. If files are defined, the
12+
* assets will only be included in specified file globs.
13+
*/
1414
files?: string | string[];
1515

1616
/**
17-
* If true, will append a unique hash of the file to the filename. This is useful for cache busting.
18-
* Default false.
19-
*/
17+
* If true, will append a unique hash of the file to the filename. This is useful for cache busting.
18+
* Default false.
19+
*/
2020
hash?: boolean;
2121

2222
/**
23-
* If true, will add filepath + '.map' to the compilation as well.
24-
* Default true.
25-
*/
26-
includeSourcemap?: boolean;
23+
* If `true`, will add `filepath + '.*'` to the compilation as well. E.g `filepath.map` and `filepath.gz`.
24+
* Default true.
25+
*/
26+
includeRelatedFiles?: boolean;
2727

2828
/**
29-
* If set, will be used as the output directory of the file.
30-
*/
29+
* If set, will be used as the output directory of the file.
30+
*/
3131
outputPath?: string;
3232

3333
/**
34-
* If set, will be used as the public path of the script or link tag.
35-
*/
34+
* If set, will be used as the public path of the script or link tag.
35+
*/
3636
publicPath?: string;
3737

3838
/**
39-
* Can be set to css to create a link-tag instead of a script-tag.
40-
* Default 'js'
41-
*/
39+
* Can be set to css to create a link-tag instead of a script-tag.
40+
* Default 'js'
41+
*/
4242
typeOfAsset?: string;
4343
}
4444
}
4545

4646
declare class AddAssetHtmlPlugin {
47-
constructor(options: AddAssetHtmlPlugin.Options | AddAssetHtmlPlugin.Options[]);
47+
constructor(
48+
options: AddAssetHtmlPlugin.Options | AddAssetHtmlPlugin.Options[],
49+
);
4850
apply(compiler: any): void;
4951
}
5052

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
"licensor": "^4.0.0",
6464
"lint-staged": "^7.0.0",
6565
"prettier": "^1.8.2",
66+
"slash": "^2.0.0",
6667
"webpack": "^4.0.0",
6768
"webpack-cli": "^3.1.0"
6869
},
@@ -92,7 +93,7 @@
9293
"eslint --fix",
9394
"git add"
9495
],
95-
"*.{md,json}": [
96+
"*.{md,json,ts}": [
9697
"prettier --write",
9798
"git add"
9899
],
@@ -104,6 +105,7 @@
104105
"prettier": {
105106
"singleQuote": true,
106107
"trailingComma": "all",
108+
"proseWrap": "always",
107109
"overrides": [
108110
{
109111
"files": [

src/addAllAssetsToCompilation.js

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import path from 'path';
22
import crypto from 'crypto';
33
import pEachSeries from 'p-each-series';
44
import micromatch from 'micromatch';
5+
import globby from 'globby';
56
import handleUrl from './handleUrl';
67

78
function ensureTrailingSlash(string) {
@@ -39,7 +40,7 @@ async function addFileToAssets(
3940
{
4041
filepath,
4142
typeOfAsset = 'js',
42-
includeSourcemap = true,
43+
includeRelatedFiles = true,
4344
hash = false,
4445
publicPath,
4546
outputPath,
@@ -86,12 +87,17 @@ async function addFileToAssets(
8687

8788
resolveOutput(compilation, addedFilename, outputPath);
8889

89-
if (includeSourcemap) {
90-
const addedMapFilename = await htmlPluginData.plugin.addFileToAssets(
91-
`${filepath}.map`,
92-
compilation,
90+
if (includeRelatedFiles) {
91+
const relatedFiles = await globby(`${filepath}.*`);
92+
await Promise.all(
93+
relatedFiles.sort().map(async relatedFile => {
94+
const addedMapFilename = await htmlPluginData.plugin.addFileToAssets(
95+
relatedFile,
96+
compilation,
97+
);
98+
resolveOutput(compilation, addedMapFilename, outputPath);
99+
}),
93100
);
94-
resolveOutput(compilation, addedMapFilename, outputPath);
95101
}
96102

97103
return Promise.resolve(null);

test.js

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
import path from 'path';
2+
import slash from 'slash';
23
import AddAssetHtmlPlugin from './src/index';
34
import addAllAssetsToCompilation from './src/addAllAssetsToCompilation';
45
import handleUrl from './src/handleUrl';
56

7+
const testFile = slash(require.resolve('./fixture/some-file'));
8+
69
const pluginMock = {
710
plugin: {
811
addFileToAssets: filename => Promise.resolve(path.basename(filename)),
@@ -20,7 +23,7 @@ test('assets should should be reversed', () => {
2023
expect(new AddAssetHtmlPlugin(['a', 'b']).assets).toEqual(['b', 'a']);
2124
});
2225

23-
test('should not reject success', async () => {
26+
test('should not reject on success', async () => {
2427
expect(await addAllAssetsToCompilation([], {}, pluginMock)).toEqual(
2528
pluginMock,
2629
);
@@ -90,7 +93,7 @@ test('should add file missing "/" to public path', async () => {
9093
expect(pluginData.assets).toMatchSnapshot();
9194
});
9295

93-
test('should add sourcemap to compilation', async () => {
96+
test('should add sourcemap and gzipped files to compilation', async () => {
9497
const addFileToAssetsStub = jest.fn();
9598
const compilation = { options: { output: {} } };
9699
const pluginData = {
@@ -100,25 +103,26 @@ test('should add sourcemap to compilation', async () => {
100103
addFileToAssetsStub.mockReturnValue(Promise.resolve('my-file.js'));
101104

102105
await addAllAssetsToCompilation(
103-
[{ filepath: 'my-file.js' }],
106+
[{ filepath: testFile }],
104107
compilation,
105108
pluginData,
106109
);
107110

108111
expect(pluginData.assets).toMatchSnapshot();
109112

110-
expect(addFileToAssetsStub).toHaveBeenCalledTimes(2);
111-
expect(addFileToAssetsStub.mock.calls[0]).toEqual([
112-
'my-file.js',
113+
expect(addFileToAssetsStub).toHaveBeenCalledTimes(3);
114+
expect(addFileToAssetsStub.mock.calls[0]).toEqual([testFile, compilation]);
115+
expect(addFileToAssetsStub.mock.calls[1]).toEqual([
116+
`${testFile}.gz`,
113117
compilation,
114118
]);
115-
expect(addFileToAssetsStub.mock.calls[1]).toEqual([
116-
'my-file.js.map',
119+
expect(addFileToAssetsStub.mock.calls[2]).toEqual([
120+
`${testFile}.map`,
117121
compilation,
118122
]);
119123
});
120124

121-
test('should skip adding sourcemap to compilation if set to false', async () => {
125+
test('should skip adding sourcemap and gzipped files to compilation if set to false', async () => {
122126
const addFileToAssetsStub = jest.fn();
123127
const compilation = { options: { output: {} } };
124128
const pluginData = {
@@ -128,7 +132,7 @@ test('should skip adding sourcemap to compilation if set to false', async () =>
128132
addFileToAssetsStub.mockReturnValue(Promise.resolve('my-file.js'));
129133

130134
await addAllAssetsToCompilation(
131-
[{ filepath: 'my-file.js', includeSourcemap: false }],
135+
[{ filepath: 'my-file.js', includeRelatedFiles: false }],
132136
compilation,
133137
pluginData,
134138
);
@@ -192,17 +196,18 @@ test('should replace compilation assets key if `outputPath` is set', async () =>
192196
};
193197

194198
await addAllAssetsToCompilation(
195-
[{ filepath: 'my-file.js', outputPath: 'assets' }],
199+
[{ filepath: testFile, outputPath: 'assets' }],
196200
compilation,
197201
pluginData,
198202
);
199203

200204
expect(pluginData.assets).toMatchSnapshot();
201205

202-
expect(compilation.assets['my-file.js']).toBeUndefined();
203-
expect(compilation.assets['assets/my-file.js']).toEqual(source);
204-
expect(compilation.assets['my-file.js.map']).toBeUndefined();
205-
expect(compilation.assets['assets/my-file.js.map']).toEqual(source);
206+
expect(compilation.assets).toEqual({
207+
'assets/some-file.js': source,
208+
'assets/some-file.js.gz': source,
209+
'assets/some-file.js.map': source,
210+
});
206211
});
207212

208213
test('filter option should exclude some files', async () => {

0 commit comments

Comments
 (0)