UNPKG

webpack-userscript

Version:
48 lines 1.86 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Interpolater = void 0; const feature_1 = require("./feature"); class Interpolater extends feature_1.Feature { constructor() { super(...arguments); this.name = 'Interpolater'; } apply({ hooks }) { hooks.headers.tap(this.name, (headers, ctx) => this.interpolate(headers, this.getVariables(ctx))); hooks.proxyHeaders.tap(this.name, (headers, ctx) => this.interpolate(headers, this.getVariables(ctx))); hooks.proxyScriptFile.tap(this.name, (filepath, ctx) => this.interpolate(filepath, this.getVariables(ctx))); } getVariables({ fileInfo: { chunk, originalFile, filename, basename, query, dirname, extname, }, buildNo, buildTime, }) { var _a; return { name: (_a = chunk.name) !== null && _a !== void 0 ? _a : '', file: originalFile, filename, basename, query, dirname, extname, buildNo: buildNo.toString(), buildTime: buildTime.toISOString(), }; } interpolate(data, info) { if (typeof data === 'string') { return Object.entries(info).reduce((value, [dataKey, dataVal]) => { return value.replace(new RegExp(`\\[${dataKey}\\]`, 'g'), `${dataVal}`); }, data); } if (Array.isArray(data)) { return data.map((item) => this.interpolate(item, info)); } if (typeof data === 'object' && data !== null) { return Object.fromEntries(Object.entries(data).map(([k, v]) => [ this.interpolate(k, info), this.interpolate(v, info), ])); } return data; } } exports.Interpolater = Interpolater; //# sourceMappingURL=interpolater.js.map