Skip to content

Commit

Permalink
feat: Add support for head and body parameters (#20)
Browse files Browse the repository at this point in the history
Add support for `body` and `head`  to default template.

Closes #19.
  • Loading branch information
bebraw authored Jan 6, 2020
1 parent cad6072 commit f16bf21
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 2 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ const config = {
title: 'Webpack demo',
// Optional, defaults to `{ lang: 'en' }`
htmlAttributes: { lang: 'en' },
// Optional, any additional HTML attached within <head>
head: '',
// Optional, any additional HTML attached within <body>
body: '',
// Optional
cssAttributes: { rel: 'preload' },
// Optional
Expand Down
28 changes: 28 additions & 0 deletions __snapshots__/test.js.snap
Original file line number Diff line number Diff line change
@@ -1,5 +1,33 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`additional body 1`] = `
"<!DOCTYPE html>
<html lang=\\"en\\">
<head>
<meta charset=\\"UTF-8\\">
<title></title>
</head>
<body>
<div>Demo</div><script src=\\"runtime~main.js\\"></script><script src=\\"main.js\\"></script>
</body>
</html>"
`;
exports[`additional head 1`] = `
"<!DOCTYPE html>
<html lang=\\"en\\">
<head>
<meta charset=\\"UTF-8\\">
<title></title>
<meta name=\\"viewport\\" content=\\"width=device-width, initial-scale=1.0\\" />
</head>
<body>
<script src=\\"runtime~main.js\\"></script><script src=\\"main.js\\"></script>
</body>
</html>"
`;
exports[`custom chunks 1`] = `
"<!DOCTYPE html>
<html lang=\\"en\\">
Expand Down
6 changes: 4 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ function defaultTemplate({
htmlAttributes = {
lang: 'en',
},
head = '',
body = '',
cssAttributes = {},
jsAttributes = {},
}) {
Expand All @@ -103,10 +105,10 @@ function defaultTemplate({
<head>
<meta charset="UTF-8">
<title>${title}</title>
${cssTags}
${head}${cssTags}
</head>
<body>
${jsTags}
${body}${jsTags}
</body>
</html>`;
}
Expand Down
22 changes: 22 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,28 @@ test('custom lang', () => {
});
});

test('additional head', () => {
return compiler(
{},
getConfig({
context: {
head:
'<meta name="viewport" content="width=device-width, initial-scale=1.0" />',
},
})
).then(result => {
expect(result.compilation.assets['index.html']._value).toMatchSnapshot();
});
});

test('additional body', () => {
return compiler({}, getConfig({ context: { body: '<div>Demo</div>' } })).then(
result => {
expect(result.compilation.assets['index.html']._value).toMatchSnapshot();
}
);
});

test('custom js attribute', () => {
return compiler(
{},
Expand Down

0 comments on commit f16bf21

Please sign in to comment.