-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrollup-html.js
More file actions
64 lines (60 loc) · 1.57 KB
/
rollup-html.js
File metadata and controls
64 lines (60 loc) · 1.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/**
* Шаблон HTML-файла.
* @link https://github.com/rollup/plugins/tree/master/packages/html#template
* @link https://github.com/rollup/plugins/blob/master/packages/html/lib/index.js#L27
* @param attributes
* @param attributes.script
* @param attributes.link
* @param attributes.html
* @param files
* @param files.js
* @param files.css
* @param publicPath
* @param title
* @returns {Promise<string>}
*/
export async function htmlTemplate({attributes, files, publicPath, title}) {
const scripts = (
(files.js || [])
.map(({ fileName }) => {
const attrs = makeHtmlAttributes(attributes.script);
return `<script src="${publicPath}${fileName}" ${attrs}></script>`;
})
.join('\n')
);
const links = (
(files.css || [])
.map(({ fileName }) => {
const attrs = makeHtmlAttributes(attributes.link);
return `<link href="${publicPath}${fileName}" rel="stylesheet" ${attrs}>`;
})
.join('\n')
);
return (`
<!doctype html>
<html${makeHtmlAttributes(attributes.html)}>
<head>
<meta charset="utf-8">
<title>${title}</title>
<script>
window.addEventListener('unhandledrejection', promiseRejectionEvent => {
console.error(promiseRejectionEvent);
});
</script>
${links}
</head>
<body>
<div id='root'></div>
${scripts}
</body>
</html>
`);
}
function makeHtmlAttributes(attributes) {
if (!attributes) {
return '';
}
const keys = Object.keys(attributes);
// eslint-disable-next-line no-param-reassign
return keys.reduce((result, key) => (result += ` ${key}="${attributes[key]}"`), '');
}