UNPKG

@web/rollup-plugin-html

Version:
92 lines 4.04 kB
"use strict"; var __rest = (this && this.__rest) || function (s, e) { var t = {}; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p]; if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]]; } return t; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.getOutputHTML = void 0; const injectBundles_js_1 = require("./injectBundles.js"); const parse5_1 = require("parse5"); const html_minifier_terser_1 = require("html-minifier-terser"); const injectedUpdatedAssetPaths_js_1 = require("./injectedUpdatedAssetPaths.js"); const injectAbsoluteBaseUrl_js_1 = require("./injectAbsoluteBaseUrl.js"); const hashInlineScripts_js_1 = require("./hashInlineScripts.js"); const injectServiceWorkerRegistration_js_1 = require("./injectServiceWorkerRegistration.js"); async function getOutputHTML(params) { const { pluginOptions, entrypointBundles, externalTransformHtmlFns, input, outputDir, emittedAssets, defaultInjectDisabled, serviceWorkerPath, injectServiceWorker, absolutePathPrefix, strictCSPInlineScripts, } = params; const { default: defaultBundle } = entrypointBundles, multiBundles = __rest(entrypointBundles, ["default"]); const { absoluteSocialMediaUrls = true, rootDir = process.cwd() } = pluginOptions; // inject rollup output into HTML let document = (0, parse5_1.parse)(input.html); if (pluginOptions.extractAssets !== false) { (0, injectedUpdatedAssetPaths_js_1.injectedUpdatedAssetPaths)({ document, input, outputDir, rootDir, emittedAssets, externalAssets: pluginOptions.externalAssets, absolutePathPrefix, publicPath: pluginOptions.publicPath, }); } if (!defaultInjectDisabled) { (0, injectBundles_js_1.injectBundles)(document, entrypointBundles); } if (absoluteSocialMediaUrls && pluginOptions.absoluteBaseUrl) { (0, injectAbsoluteBaseUrl_js_1.injectAbsoluteBaseUrl)(document, pluginOptions.absoluteBaseUrl); } if (injectServiceWorker && serviceWorkerPath) { (0, injectServiceWorkerRegistration_js_1.injectServiceWorkerRegistration)({ document, outputDir, serviceWorkerPath, htmlFileName: input.name, }); } let outputHtml = (0, parse5_1.serialize)(document); const transforms = [...(externalTransformHtmlFns !== null && externalTransformHtmlFns !== void 0 ? externalTransformHtmlFns : [])]; if (pluginOptions.transformHtml) { if (Array.isArray(pluginOptions.transformHtml)) { transforms.push(...pluginOptions.transformHtml); } else { transforms.push(pluginOptions.transformHtml); } } // run transform functions on output HTML for (const transform of transforms) { outputHtml = await transform(outputHtml, { bundle: defaultBundle, bundles: multiBundles, htmlFileName: input.name, }); } if (pluginOptions.minify) { outputHtml = await (0, html_minifier_terser_1.minify)(outputHtml, { collapseWhitespace: true, removeComments: true, removeRedundantAttributes: true, removeScriptTypeAttributes: true, removeStyleLinkTypeAttributes: true, useShortDoctype: true, minifyCSS: true, minifyJS: true, }); } if (strictCSPInlineScripts) { document = (0, parse5_1.parse)(outputHtml); (0, hashInlineScripts_js_1.hashInlineScripts)(document); outputHtml = (0, parse5_1.serialize)(document); } return outputHtml; } exports.getOutputHTML = getOutputHTML; //# sourceMappingURL=getOutputHTML.js.map