@eaglesong/task-panorama
Version:
Eaglesong task for Panorama
51 lines • 2.29 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const helper_task_1 = require("@eaglesong/helper-task");
const webpack_sources_1 = require("webpack-sources");
const Template_1 = tslib_1.__importDefault(require("webpack/lib/Template"));
function wrapComment(str) {
if (!str.includes('\n')) {
return Template_1.default.toComment(str);
}
return `/*!\n * ${str
.replace(/\*\//g, '* /')
.split('\n')
.join('\n * ')
.replace(/\s+\n/g, '\n')
.trimRight()}\n */`;
}
function wrapXmlComment(str) {
return `<!--\n${str}\n-->`;
}
class OutputHeaderWebpackPlugin {
constructor(options) {
this.options = options;
}
apply(compiler) {
compiler.hooks.compilation.tap('OutputHeaderWebpackPlugin', compilation => {
compilation.hooks.optimizeChunkAssets.tap('OutputHeaderWebpackPlugin', chunks => {
var _a;
for (const chunk of chunks) {
for (const file of chunk.files) {
// html-webpack-plugin passes JavaScript code under .xml extension
// Note: compilation.compiler !== compiler
// TODO: Add header to custom_ui_manifest.xml too
if ((_a = compilation.compiler.name) === null || _a === void 0 ? void 0 : _a.startsWith('html-webpack-plugin for'))
return;
if (!(file.endsWith('.js') || file.endsWith('.css') || file.endsWith('.xml')))
return;
const header = helper_task_1.getOutputHeader(this.options, file);
if (header === undefined)
continue;
const prefix = file.endsWith('.xml') ? wrapXmlComment(header) : wrapComment(header);
// @ts-ignore compilation.updateAsset is not typed
compilation.updateAsset(file, (old) => new webpack_sources_1.ConcatSource(prefix, '\n', old));
}
}
});
});
}
}
exports.OutputHeaderWebpackPlugin = OutputHeaderWebpackPlugin;
//# sourceMappingURL=OutputHeaderWebpackPlugin.js.map
;