UNPKG

@alicloud/console-toolkit-plugin-react

Version:

console toolkit plugin for base react app

118 lines (117 loc) 5.18 kB
"use strict"; var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.htmlInjectPlugin = void 0; var url = require("url"); var fs = require("fs-extra"); var path = require("path"); var cheerio = require("cheerio"); var utils_1 = require("../../utils"); var HtmlInjectPlugin = /** @class */ (function () { function HtmlInjectPlugin(options) { this.data = options.data; this.cors = !!options.cors; } HtmlInjectPlugin.prototype.apply = function (compiler) { var _this = this; compiler.hooks.compilation.tap('HtmlInjectPlugin', function (compilation) { // @ts-ignore if (_this.cors && compilation.hooks.htmlWebpackPluginAlterAssetTags) { // @ts-ignore compilation.hooks.htmlWebpackPluginAlterAssetTags.tap("HtmlInjectPlugin", function (htmlPluginData, callback) { var _a; if ((_a = htmlPluginData.body) === null || _a === void 0 ? void 0 : _a.length) { htmlPluginData.body.forEach(function (tag) { if (tag.tagName === 'script') { tag.attributes.crossorigin = 'anonymous'; } }); } if (typeof callback === 'function') { callback(null, htmlPluginData); } else { return htmlPluginData; } }); } // @ts-ignore if (!compilation.hooks.htmlWebpackPluginAfterHtmlProcessing) { return; } // @ts-ignore compilation.hooks.htmlWebpackPluginBeforeHtmlProcessing.tapAsync('HtmlInjectPlugin', function (data, callback) { var $ = cheerio.load(data.html, { xml: { decodeEntities: false, xmlMode: false, } }); // const dom = new JSDOM(data.html); // const document = dom.window.document; var body = $('body'); var head = $('head'); var publicPath = ''; if (compiler) { var output = compiler.options.output; if (output && output.publicPath) { publicPath = output.publicPath; } } _this.data.metas.forEach(function (script) { head.append(script); }); _this.data.headscripts.forEach(function (script) { head.append(script); }); _this.data.scripts.forEach(function (script) { body.append(script); }); _this.data.prescripts.forEach(function (script) { body.prepend(script); }); _this._processScripts($, publicPath); data.html = $.html(); callback(); }); // @ts-ignore compilation.hooks.htmlWebpackPluginAfterHtmlProcessing.tapAsync('HtmlInjectPlugin', function (data, callback) { var _a, _b; if (data.html) { var oneHtmlPath = path.resolve(((_a = compiler.options.output) === null || _a === void 0 ? void 0 : _a.path) || '', 'one.html'); var idptHtmlPath = path.resolve(((_b = compiler.options.output) === null || _b === void 0 ? void 0 : _b.path) || '', 'idpt.html'); fs.ensureFileSync(oneHtmlPath); fs.ensureFileSync(idptHtmlPath); fs.writeFileSync(oneHtmlPath, data.html.replace(/\.alicdn.com/g, '.{{{MAIN_RESOURCE_CDN}}}')); fs.writeFileSync(idptHtmlPath, data.html.replace(/dev\.g\.alicdn\.com/g, 'dev-g.{{{MAIN_RESOURCE_CDN}}}').replace(/\.alicdn.com/g, '.{{{MAIN_RESOURCE_CDN}}}')); } callback(); }); }); }; HtmlInjectPlugin.prototype._processScripts = function ($, publicPath) { var scripts = $('script'); scripts.each(function (_index, script) { var src = script.attribs['src']; if (src && src.startsWith('/') && !src.startsWith('//')) { src = url.resolve(publicPath, src.slice(1, src.length)); } $(script).attr('src', src); }); }; return HtmlInjectPlugin; }()); function htmlInjectPlugin(config, options) { (0, utils_1.createPlugin)(config, 'HtmlInjectPlugin', HtmlInjectPlugin, __assign({}, options)); } exports.htmlInjectPlugin = htmlInjectPlugin;