Skip to content

Commit 7ee4bc7

Browse files
J-Kallunkisapegin
authored andcommitted
Feat: Add htmlAttributes option and generate <html lang="en"> by default (#8)
BREAKING CHANGE: * Node 8.12 is the minimum required version.
1 parent a6300e2 commit 7ee4bc7

File tree

4 files changed

+44
-9
lines changed

4 files changed

+44
-9
lines changed

README.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ const config = {
1919
plugins: [
2020
new MiniHtmlWebpackPlugin({
2121
context: {
22-
title: 'Webpack demo'
22+
title: 'Webpack demo',
23+
htmlAttributes: { lang: 'en' } // Optional, defaults to { lang: 'en' }
2324
},
2425
filename: 'demo.html' // Optional, defaults to `index.html`
2526
})
@@ -65,9 +66,11 @@ const config = {
6566
context: {
6667
title: 'Custom template' // Available in the context below
6768
},
68-
template: ({ css, js, title, publicPath }) =>
69+
template: ({ css, js, title, htmlAttributes, publicPath }) =>
6970
`<!DOCTYPE html>
70-
<html>
71+
<html ${Object.entries(htmlAttributes)
72+
.map(attribute => `${attribute[0]}="${attribute[1]}"`)
73+
.join(' ')}>
7174
<head>
7275
<meta charset="UTF-8">
7376
<title>${title}</title>

__snapshots__/test.js.snap

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,22 @@
22

33
exports[`custom filename 1`] = `
44
"<!DOCTYPE html>
5-
<html>
5+
<html lang=\\"en\\">
6+
<head>
7+
<meta charset=\\"UTF-8\\">
8+
<title></title>
9+
10+
11+
</head>
12+
<body>
13+
<script src=\\"runtime~main.js\\"></script><script src=\\"main.js\\"></script>
14+
</body>
15+
</html>"
16+
`;
17+
18+
exports[`custom lang 1`] = `
19+
"<!DOCTYPE html>
20+
<html lang=\\"ru\\">
621
<head>
722
<meta charset=\\"UTF-8\\">
823
<title></title>
@@ -19,7 +34,7 @@ exports[`custom template 1`] = `"<div>Pizza</div>"`;
1934
2035
exports[`custom title 1`] = `
2136
"<!DOCTYPE html>
22-
<html>
37+
<html lang=\\"en\\">
2338
<head>
2439
<meta charset=\\"UTF-8\\">
2540
<title>Pizza</title>
@@ -34,7 +49,7 @@ exports[`custom title 1`] = `
3449
3550
exports[`default options 1`] = `
3651
"<!DOCTYPE html>
37-
<html>
52+
<html lang=\\"en\\">
3853
<head>
3954
<meta charset=\\"UTF-8\\">
4055
<title></title>

index.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,17 @@ function normalizeEntrypoints(entrypoints) {
6161
return Object.keys(entrypoints).map(name => entrypoints[name]);
6262
}
6363

64-
function defaultTemplate({ css, js, title = '', publicPath }) {
64+
function defaultTemplate({
65+
css,
66+
js,
67+
title = '',
68+
htmlAttributes = { lang: 'en' },
69+
publicPath,
70+
}) {
6571
return `<!DOCTYPE html>
66-
<html>
72+
<html ${Object.entries(htmlAttributes)
73+
.map(attribute => `${attribute[0]}="${attribute[1]}"`)
74+
.join(' ')}>
6775
<head>
6876
<meta charset="UTF-8">
6977
<title>${title}</title>

test.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,20 @@ test('custom title', () => {
2424
);
2525
});
2626

27+
test('custom lang', () => {
28+
return compiler(
29+
{},
30+
getConfig({ context: { htmlAttributes: { lang: 'ru' } } })
31+
).then(result => {
32+
expect(result.compilation.assets['index.html']._value).toMatchSnapshot();
33+
});
34+
});
35+
2736
test('custom template', () => {
2837
return compiler(
2938
{},
3039
getConfig({
31-
context: { title: 'Pizza' },
40+
context: { title: 'Pizza', htmlAttributes: { lang: 'it' } },
3241
template: ({ title }) => `<div>${title}</div>`,
3342
})
3443
).then(result => {

0 commit comments

Comments
 (0)