Skip to content

Commit 3d36f33

Browse files
bebrawsapegin
authored andcommitted
feat: Add support for chunks (#16)
This feature is similar to the one in html-webpack-plugin and it's required for multi-page setups.
1 parent 05d2bd2 commit 3d36f33

File tree

5 files changed

+99
-3
lines changed

5 files changed

+99
-3
lines changed

README.md

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,42 @@ const config = {
3030
cssAttributes: { rel: 'preload' },
3131
// Optional
3232
jsAttributes: { defer: 'defer' }
33-
}
33+
},
34+
// Optional, use this for choosing chunks to include to your page.
35+
// See the expanded example below.
36+
chunks: ['app']
3437
})
3538
]
3639
};
3740
```
3841

42+
### Multiple pages
43+
44+
It's possible to use `MiniHtmlWebpackPlugin` to develop sites with multiple pages. It can be combined with webpack's bundle splitting so you can share common code across different pages.
45+
46+
To achieve this, you'll have to define `entry` against each the code for each page and define `MiniHtmlWebpackPlugin` to match them. In practice you might want to abstract this pairing but to give you the full idea, consider the example below.
47+
48+
```javascript
49+
const MiniHtmlWebpackPlugin = require('mini-html-webpack-plugin');
50+
51+
const config = {
52+
entry: {
53+
app: './app.js',
54+
another: './another.js'
55+
},
56+
plugins: [
57+
new MiniHtmlWebpackPlugin({
58+
filename: 'index.html',
59+
chunks: ['app'],
60+
}),
61+
new MiniHtmlWebpackPlugin({
62+
filename: 'another.html',
63+
chunks: ['another'],
64+
},
65+
],
66+
};
67+
```
68+
3969
### HTML minification
4070
4171
```javascript

__snapshots__/test.js.snap

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,33 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3+
exports[`custom chunks 1`] = `
4+
"<!DOCTYPE html>
5+
<html lang=\\"en\\">
6+
<head>
7+
<meta charset=\\"UTF-8\\">
8+
<title></title>
9+
10+
</head>
11+
<body>
12+
<script src=\\"runtime~index.js\\"></script><script src=\\"index.js\\"></script>
13+
</body>
14+
</html>"
15+
`;
16+
17+
exports[`custom chunks 2`] = `
18+
"<!DOCTYPE html>
19+
<html lang=\\"en\\">
20+
<head>
21+
<meta charset=\\"UTF-8\\">
22+
<title></title>
23+
24+
</head>
25+
<body>
26+
<script src=\\"runtime~another.js\\"></script><script src=\\"another.js\\"></script>
27+
</body>
28+
</html>"
29+
`;
30+
331
exports[`custom filename 1`] = `
432
"<!DOCTYPE html>
533
<html lang=\\"en\\">

index.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,13 @@ class MiniHtmlWebpackPlugin {
1313
publicPath = '',
1414
template,
1515
context,
16+
chunks,
1617
} = this.options;
1718

18-
const files = getFiles(normalizeEntrypoints(compilation.entrypoints));
19+
const files = getFiles(
20+
normalizeEntrypoints(compilation.entrypoints),
21+
chunks
22+
);
1923

2024
const options = Object.assign({}, { publicPath }, context, files);
2125

@@ -37,10 +41,14 @@ class MiniHtmlWebpackPlugin {
3741
}
3842
}
3943

40-
function getFiles(entrypoints) {
44+
function getFiles(entrypoints, chunks) {
4145
const ret = {};
4246

4347
entrypoints.forEach(entry => {
48+
if (chunks && !chunks.includes(entry.name)) {
49+
return;
50+
}
51+
4452
entry.getFiles().forEach(file => {
4553
const extension = path.extname(file).replace(/\./, '');
4654

test.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,36 @@ test('default options', () => {
1616
});
1717
});
1818

19+
test('custom chunks', () => {
20+
return compiler(
21+
{},
22+
{
23+
entry: {
24+
index: './index.js',
25+
another: './another.js',
26+
},
27+
plugins: [
28+
new MiniHtmlWebpackPlugin({
29+
filename: 'index.html',
30+
chunks: ['index'],
31+
}),
32+
new MiniHtmlWebpackPlugin({
33+
filename: 'another.html',
34+
chunks: ['another'],
35+
}),
36+
],
37+
}
38+
).then(result => {
39+
// This should contain only reference to the index chunk and the related
40+
// runtime.
41+
expect(result.compilation.assets['index.html']._value).toMatchSnapshot();
42+
43+
// This should contain only reference to the another chunk and the related
44+
// runtime.
45+
expect(result.compilation.assets['another.html']._value).toMatchSnapshot();
46+
});
47+
});
48+
1949
test('custom title', () => {
2050
return compiler({}, getConfig({ context: { title: 'Pizza' } })).then(
2151
result => {

test/fixtures/another.js

Whitespace-only changes.

0 commit comments

Comments
 (0)