UNPKG

piral-cli-webpack5

Version:

Provides debug and build capabilities for pilets and Piral instances using Webpack v5.

40 lines 1.63 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.html5EntryWebpackConfigEnhancer = void 0; const HtmlWebpackPlugin = require("html-webpack-plugin"); const cheerio_1 = require("cheerio"); const fs_1 = require("fs"); const path_1 = require("path"); const helpers_1 = require("./helpers"); const html5EntryWebpackConfigEnhancer = (options) => (compilerOptions) => { const entry = compilerOptions.entry; const [template] = (0, helpers_1.getTemplates)(entry); if (template) { const src = (0, path_1.dirname)(template); const html = (0, fs_1.readFileSync)(template, 'utf8'); const templateContent = (0, cheerio_1.load)( // try to replace ejs tags, if any html.replace(/<%=([\w\W]*?)%>/g, function (match, group) { try { return eval(group); } catch { return match; } })); const entries = (0, helpers_1.extractParts)(templateContent).map((entry) => (0, path_1.join)(src, entry)); const plugins = [ new HtmlWebpackPlugin({ ...options, templateContent: templateContent.html(), }), ]; if (!entries.length) throw new Error('Template entries expected to be not empty'); (0, helpers_1.setEntries)(compilerOptions, template, entries); compilerOptions.plugins = [...compilerOptions.plugins, ...plugins]; } return compilerOptions; }; exports.html5EntryWebpackConfigEnhancer = html5EntryWebpackConfigEnhancer; //# sourceMappingURL=index.js.map