Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 20 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,18 @@ const config = {
context: {
title: 'Webpack demo',
// Optional, defaults to `{ lang: 'en' }`
htmlAttributes: { lang: 'en' },
htmlAttributes: {
lang: 'en'
},
// Optional
cssAttributes: { rel: 'preload' },
cssAttributes: {
rel: 'preload',
as: 'style'
},
// Optional
jsAttributes: { defer: 'defer' }
jsAttributes: {
defer: true
}
}
})
]
Expand Down Expand Up @@ -77,9 +84,16 @@ const config = {
// `context` is available in `template` below
context: {
title: 'Webpack demo',
htmlAttributes: { lang: 'en' },
cssAttributes: { rel: 'preload' },
jsAttributes: { defer: 'defer' }
htmlAttributes: {
lang: 'en'
},
cssAttributes: {
rel: 'preload',
as: 'style'
},
jsAttributes: {
defer: true
}
},
template: ({
css,
Expand Down
8 changes: 4 additions & 4 deletions __snapshots__/test.js.snap
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`custom filename 1`] = `
exports[`custom attributes 1`] = `
"<!DOCTYPE html>
<html lang=\\"en\\">
<head>
Expand All @@ -9,12 +9,12 @@ exports[`custom filename 1`] = `

</head>
<body>
<script src=\\"runtime~main.js\\"></script><script src=\\"main.js\\"></script>
<script src=\\"runtime~main.js\\" defer></script><script src=\\"main.js\\" defer></script>
</body>
</html>"
`;

exports[`custom js attribute 1`] = `
exports[`custom filename 1`] = `
"<!DOCTYPE html>
<html lang=\\"en\\">
<head>
Expand All @@ -23,7 +23,7 @@ exports[`custom js attribute 1`] = `

</head>
<body>
<script src=\\"runtime~main.js\\" defer=\\"defer\\"></script><script src=\\"main.js\\" defer=\\"defer\\"></script>
<script src=\\"runtime~main.js\\"></script><script src=\\"main.js\\"></script>
</body>
</html>"
`;
Expand Down
22 changes: 16 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class MiniHtmlWebpackPlugin {
plugin(compilation, callback) {
const {
filename = 'index.html',
publicPath = '',
publicPath,
template,
context,
} = this.options;
Expand Down Expand Up @@ -108,12 +108,15 @@ function generateCSSReferences({
publicPath = '',
attributes = {},
}) {
attributes = generateAttributes(attributes);
const allAttributes = {
...attributes,
rel: 'rel' in attributes ? attributes.rel : 'stylesheet',
};

attributes = generateAttributes(allAttributes);

return files
.map(
file => `<link href="${publicPath}${file}" rel="stylesheet"${attributes}>`
)
.map(file => `<link href="${publicPath}${file}"${attributes}>`)
.join('');
}

Expand All @@ -138,7 +141,14 @@ function generateAttributes(attributes = {}) {

return (
' ' +
attributes.map(attribute => `${attribute[0]}="${attribute[1]}"`).join(' ')
attributes
.map(attr => {
if (attr[1] === true) {
return attr[0];
}
return `${attr[0]}="${attr[1]}"`;
})
.join(' ')
);
}

Expand Down
1 change: 1 addition & 0 deletions main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import './style.css';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's move these files to some folder so it's clear they are test files, they shouldn't be in the root folder.

Loading