const rollupPluginHtml = require('@rollup/plugin-html'); const makeHtmlAttributes = (attributes) => { if (!attributes) { return ''; } const keys = Object.keys(attributes); // eslint-disable-next-line no-param-reassign // eslint-disable-next-line no-return-assign return keys.reduce((result, key) => (result += ` ${key}="${attributes[key]}"`), ''); }; const genHtmlTemplateFunc = (contentOrContentFn) => ({ attributes, files, meta, publicPath, title, }) => { const scripts = (files.js || []) .map( ({ fileName }) => `` ) .join('\n'); const links = (files.css || []) .map( ({ fileName }) => `` ) .join('\n'); const metas = meta.map((input) => ``).join('\n'); return ` ${metas} ${title} ${links} ${(typeof contentOrContentFn === 'function' ? contentOrContentFn() : contentOrContentFn) || ''} ${scripts}
`; }; module.exports = (title, fileName, getHtmlContent) => rollupPluginHtml({ title, fileName, template: genHtmlTemplateFunc(getHtmlContent), meta: [{ charset: 'utf-8' }, { 'http-equiv': 'X-UA-Compatible', content: 'IE=edge' }], });