UNPKG

webpack-genius

Version:

**For chinese developers:** you'd better add taobao mirror before everything start. Or you may fail to install. ```bash yarn config set registry http://registry.npm.taobao.org ```

125 lines (124 loc) 4.16 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.RuleHandle = void 0; var RuleHandle = /** @class */ (function () { function RuleHandle(genius) { this.status = true; this.rule = {}; this.genius = genius; this.rule.test = this.test(); this.rule.exclude = this.exclude(); this.rule.include = this.include(); this.rule.use = this.loaders(); this.onInit(); } RuleHandle.prototype.onInit = function () { // }; RuleHandle.prototype.removeLoader = function (name) { var _this = this; this.rule.use = this.rule.use.filter(function (item) { return !_this.isLoaderEqual(item.loader, name); }); return this; }; RuleHandle.prototype.addLoaderBefore = function (loader, beforeLoaderName) { var _this = this; var _a, _b; if (this.exists(loader.loader)) { return this; } var index = this.rule.use.findIndex(function (item) { return _this.isLoaderEqual(item.loader, beforeLoaderName); }); if (index === -1) { (_a = this.rule.use) === null || _a === void 0 ? void 0 : _a.unshift(loader); } else { (_b = this.rule.use) === null || _b === void 0 ? void 0 : _b.splice(index, 0, loader); } return this; }; RuleHandle.prototype.addLoaderAfter = function (loader, afterLoaderName) { var _this = this; var _a, _b; if (this.exists(loader.loader)) { return this; } var index = this.rule.use.findIndex(function (item) { return _this.isLoaderEqual(item.loader, afterLoaderName); }); if (index === -1) { (_a = this.rule.use) === null || _a === void 0 ? void 0 : _a.push(loader); } else { (_b = this.rule.use) === null || _b === void 0 ? void 0 : _b.splice(index + 1, 0, loader); } return this; }; RuleHandle.prototype.enable = function (enable) { this.status = enable; return this; }; RuleHandle.prototype.isUsed = function () { return this.status; }; RuleHandle.prototype.setInclude = function (value) { this.rule.include = value; return this; }; RuleHandle.prototype.setExclude = function (value) { this.rule.exclude = value; return this; }; RuleHandle.prototype.setOptions = function (loaderName, fn) { var _this = this; var loader = this.rule.use.find(function (item) { return _this.isLoaderEqual(item.loader, loaderName); }); if (!loader) { throw new Error('Loader `' + loaderName + '` is not found.'); } var result = fn(loader.options); if (result) { loader.options = result; } return this; }; ; RuleHandle.prototype.disableLoader = function (loaderName) { var _this = this; this.rule.use = this.rule.use.filter(function (item) { return !_this.isLoaderEqual(item.loader, loaderName); }); return this; }; RuleHandle.prototype.exclude = function () { return /node_modules/; }; RuleHandle.prototype.include = function () { return undefined; }; RuleHandle.prototype.collect = function () { var _a; (_a = this.rule.use) === null || _a === void 0 ? void 0 : _a.forEach(function (item) { item.loader = require.resolve(item.loader); }); return this.rule; }; RuleHandle.prototype.exists = function (loader) { var _this = this; var index = this.rule.use.findIndex(function (item) { return _this.isLoaderEqual(item.loader, loader); }); return index >= 0; }; RuleHandle.prototype.isLoaderEqual = function (loader, search) { if (!search) { return false; } return loader === search || loader === require.resolve(String(search)); }; return RuleHandle; }()); exports.RuleHandle = RuleHandle;