UNPKG

@eaglesong/task-panorama

Version:
47 lines 2.15 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const tslib_1 = require("tslib"); const html_webpack_plugin_1 = tslib_1.__importDefault(require("html-webpack-plugin")); const path_1 = tslib_1.__importDefault(require("path")); const url_1 = require("url"); class HtmlWebpackXmlPlugin { apply(compiler) { compiler.hooks.compilation.tap(this.constructor.name, compilation => { const hooks = html_webpack_plugin_1.default.getHooks(compilation); const { publicPath } = compilation.outputOptions; hooks.beforeAssetTagGeneration.tap(this.constructor.name, args => { const xmlAssets = []; // eslint-disable-next-line prefer-destructuring const chunks = compilation.chunks; for (const chunk of chunks) { for (const file of chunk.files) { if (!file.endsWith('.xml') || chunk.__type == null) continue; xmlAssets.push({ file: args.assets.publicPath + file, type: chunk.__type, }); } } args.assets.xml = xmlAssets; return args; }); hooks.beforeEmit.tap(this.constructor.name, args => { const images = Object.keys(compilation.assets) .filter(assetName => /\.(png|je?pg)$/.test(assetName)) .map(assetName => { const url = new url_1.URL(publicPath); // TODO: Move publicPath higher? url.pathname = path_1.default.posix.resolve(url.pathname, assetName); return url.toString(); }); if (images.length > 0) { args.html = `<!--\n${images.map(x => `"${x}"`).join('\n')}\n-->\n${args.html}`; } return args; }); }); } } exports.HtmlWebpackXmlPlugin = HtmlWebpackXmlPlugin; //# sourceMappingURL=HtmlWebpackXmlPlugin.js.map