Skip to content

Commit 88798d1

Browse files
authored
fix: Prevent two rel attributes in the same link tag (#22)
Credit: @pldg. Closes #12.
1 parent fb60136 commit 88798d1

File tree

8 files changed

+326
-35
lines changed

8 files changed

+326
-35
lines changed

README.md

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,22 @@ const config = {
2525
context: {
2626
title: 'Webpack demo',
2727
// Optional, defaults to `{ lang: 'en' }`
28-
htmlAttributes: { lang: 'en' },
28+
htmlAttributes: {
29+
lang: 'en'
30+
},
2931
// Optional, any additional HTML attached within <head>
3032
head: '',
3133
// Optional, any additional HTML attached within <body>
3234
body: '',
3335
// Optional
34-
cssAttributes: { rel: 'preload' },
36+
cssAttributes: {
37+
rel: 'preload',
38+
as: 'style'
39+
},
3540
// Optional
36-
jsAttributes: { defer: 'defer' }
41+
jsAttributes: {
42+
defer: true
43+
}
3744
},
3845
// Optional, use this for choosing chunks to include to your page.
3946
// See the expanded example below.
@@ -113,9 +120,16 @@ const config = {
113120
// `context` is available in `template` below
114121
context: {
115122
title: 'Webpack demo',
116-
htmlAttributes: { lang: 'en' },
117-
cssAttributes: { rel: 'preload' },
118-
jsAttributes: { defer: 'defer' }
123+
htmlAttributes: {
124+
lang: 'en'
125+
},
126+
cssAttributes: {
127+
rel: 'preload',
128+
as: 'style'
129+
},
130+
jsAttributes: {
131+
defer: true
132+
}
119133
},
120134
template: ({
121135
css,

__snapshots__/test.js.snap

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ exports[`additional head 1`] = `
3030
3131
exports[`custom async template 1`] = `"<div>Pizza</div>"`;
3232
33-
exports[`custom chunks 1`] = `
33+
exports[`custom attributes 1`] = `
3434
"<!DOCTYPE html>
3535
<html lang=\\"en\\">
3636
<head>
@@ -39,12 +39,12 @@ exports[`custom chunks 1`] = `
3939
4040
</head>
4141
<body>
42-
<script src=\\"runtime~index.js\\"></script><script src=\\"index.js\\"></script>
42+
<script src=\\"runtime~main.js\\" defer></script><script src=\\"main.js\\" defer></script>
4343
</body>
4444
</html>"
4545
`;
4646

47-
exports[`custom chunks 2`] = `
47+
exports[`custom chunks 1`] = `
4848
"<!DOCTYPE html>
4949
<html lang=\\"en\\">
5050
<head>
@@ -53,12 +53,12 @@ exports[`custom chunks 2`] = `
5353
5454
</head>
5555
<body>
56-
<script src=\\"runtime~another.js\\"></script><script src=\\"another.js\\"></script>
56+
<script src=\\"runtime~index.js\\"></script><script src=\\"index.js\\"></script>
5757
</body>
5858
</html>"
5959
`;
6060
61-
exports[`custom filename 1`] = `
61+
exports[`custom chunks 2`] = `
6262
"<!DOCTYPE html>
6363
<html lang=\\"en\\">
6464
<head>
@@ -67,12 +67,12 @@ exports[`custom filename 1`] = `
6767
6868
</head>
6969
<body>
70-
<script src=\\"runtime~main.js\\"></script><script src=\\"main.js\\"></script>
70+
<script src=\\"runtime~another.js\\"></script><script src=\\"another.js\\"></script>
7171
</body>
7272
</html>"
7373
`;
7474
75-
exports[`custom js attribute 1`] = `
75+
exports[`custom filename 1`] = `
7676
"<!DOCTYPE html>
7777
<html lang=\\"en\\">
7878
<head>
@@ -81,7 +81,7 @@ exports[`custom js attribute 1`] = `
8181
8282
</head>
8383
<body>
84-
<script src=\\"runtime~main.js\\" defer=\\"defer\\"></script><script src=\\"main.js\\" defer=\\"defer\\"></script>
84+
<script src=\\"runtime~main.js\\"></script><script src=\\"main.js\\"></script>
8585
</body>
8686
</html>"
8787
`;

index.js

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -112,17 +112,16 @@ function defaultTemplate({
112112
</html>`;
113113
}
114114

115-
function generateCSSReferences({
116-
files = [],
117-
publicPath = '',
118-
attributes = {},
119-
}) {
120-
attributes = generateAttributes(attributes);
115+
function generateCSSReferences({ files = [], publicPath, attributes = {} }) {
116+
const allAttributes = {
117+
...attributes,
118+
rel: 'rel' in attributes ? attributes.rel : 'stylesheet',
119+
};
120+
121+
attributes = generateAttributes(allAttributes);
121122

122123
return files
123-
.map(
124-
file => `<link href="${publicPath}${file}" rel="stylesheet"${attributes}>`
125-
)
124+
.map(file => `<link href="${publicPath}${file}"${attributes}>`)
126125
.join('');
127126
}
128127

@@ -147,7 +146,14 @@ function generateAttributes(attributes = {}) {
147146

148147
return (
149148
' ' +
150-
attributes.map(attribute => `${attribute[0]}="${attribute[1]}"`).join(' ')
149+
attributes
150+
.map(attr => {
151+
if (attr[1] === true) {
152+
return attr[0];
153+
}
154+
return `${attr[0]}="${attr[1]}"`;
155+
})
156+
.join(' ')
151157
);
152158
}
153159

0 commit comments

Comments
 (0)