UNPKG

webpack-chunk-report-plugin

Version:

Webpack Chunk Report Plugin

28 lines (27 loc) 1.09 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.addScriptToHtml = addScriptToHtml; const fs_1 = __importDefault(require("fs")); const path_1 = __importDefault(require("path")); const jsdom_1 = require("jsdom"); async function addScriptToHtml(content, outputPath) { try { const html = fs_1.default.readFileSync(path_1.default.resolve(__dirname, "..", "public", "index.html"), "utf-8"); // Parse HTML with JSDOM const dom = new jsdom_1.JSDOM(html); const document = dom.window.document; // Create the script element const script = document.createElement("script"); script.textContent = content; document.head.appendChild(script); // Serialize and save the modified HTML const modifiedHtml = dom.serialize(); fs_1.default.writeFileSync(outputPath, modifiedHtml); } catch (error) { console.log(error); } }