UNPKG

webpack-inject-plugin

Version:

A webpack plugin to dynamically inject code into the bundle.

84 lines 3.26 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); var path_1 = __importDefault(require("path")); var FAKE_LOADER_NAME = 'webpack-inject-plugin.loader'; exports.registry = {}; var uniqueIDCounter = 0; function getUniqueID() { var id = (++uniqueIDCounter).toString(16); return "webpack-inject-module-" + id; } function injectToArray(originalEntry, newEntry, entryOrder) { if (entryOrder === void 0) { entryOrder = 3 /* NotLast */; } if (entryOrder === 1 /* First */) { return [newEntry].concat(originalEntry); } if (entryOrder === 2 /* Last */) { return originalEntry.concat([newEntry]); } return originalEntry.splice(0, originalEntry.length - 1).concat([ newEntry ], originalEntry.splice(originalEntry.length - 1)); } function createEntryFilter(filterOption) { if (filterOption === null || filterOption === undefined) { return function () { return true; }; } if (typeof filterOption === 'string') { return function (entryName) { return filterOption === entryName; }; } if (typeof filterOption === 'function') { return filterOption; } throw new Error("Unknown entry filter: " + typeof filterOption); } function injectEntry(originalEntry, newEntry, options) { if (originalEntry === undefined) { return newEntry; } var filterFunc = createEntryFilter(options.entryName); // Last module in an array gets exported, so the injected one must not be // last. https://webpack.github.io/docs/configuration.html#entry if (typeof originalEntry === 'string') { return injectToArray([originalEntry], newEntry, options.entryOrder); } if (Array.isArray(originalEntry)) { return injectToArray(originalEntry, newEntry, options.entryOrder); } if (originalEntry === Object(originalEntry)) { return Object.entries(originalEntry).reduce(function (a, _a) { var key = _a[0], entry = _a[1]; if (filterFunc(key)) { a[key] = injectEntry(entry, newEntry, options); } else { a[key] = entry; } return a; }, {}); } return originalEntry; } exports.injectEntry = injectEntry; var WebpackInjectPlugin = /** @class */ (function () { function WebpackInjectPlugin(loader, options) { this.loader = loader; this.options = { entryName: (options && options.entryName) || undefined, entryOrder: (options && options.entryOrder) || 3 /* NotLast */, loaderID: (options && options.loaderID) || getUniqueID() }; } WebpackInjectPlugin.prototype.apply = function (compiler) { var id = this.options.loaderID; var newEntry = path_1.default.resolve(__dirname, FAKE_LOADER_NAME + "?id=" + id + "!"); exports.registry[id] = this.loader; compiler.options.entry = injectEntry(compiler.options.entry, newEntry, this.options); }; return WebpackInjectPlugin; }()); exports.default = WebpackInjectPlugin; //# sourceMappingURL=main.js.map