diff options
author | Florian Dold <florian.dold@gmail.com> | 2018-09-20 02:56:13 +0200 |
---|---|---|
committer | Florian Dold <florian.dold@gmail.com> | 2018-09-20 02:56:13 +0200 |
commit | bbff7403fbf46f9ad92240ac213df8d30ef31b64 (patch) | |
tree | c58400ec5124da1c7d56b01aea83309f80a56c3b /node_modules/tapable | |
parent | 003fb34971cf63466184351b4db5f7c67df4f444 (diff) |
update packages
Diffstat (limited to 'node_modules/tapable')
-rw-r--r-- | node_modules/tapable/README.md | 439 | ||||
-rw-r--r-- | node_modules/tapable/lib/Tapable.js | 458 | ||||
-rw-r--r-- | node_modules/tapable/package.json | 24 |
3 files changed, 387 insertions, 534 deletions
diff --git a/node_modules/tapable/README.md b/node_modules/tapable/README.md index 820cd2432..c68e7d09d 100644 --- a/node_modules/tapable/README.md +++ b/node_modules/tapable/README.md @@ -1,151 +1,288 @@ -# Tapable
-
-``` javascript
-var Tapable = require("tapable");
-```
-
-`Tapable` is a class for plugin binding and applying.
-
-Just extend it.
-
-``` javascript
-function MyClass() {
- Tapable.call(this);
-}
-
-MyClass.prototype = Object.create(Tapable.prototype);
-
-MyClass.prototype.method = function() {};
-```
-
-Or mix it in.
-
-``` javascript
-function MyClass2() {
- EventEmitter.call(this);
- Tapable.call(this);
-}
-
-MyClass2.prototype = Object.create(EventEmitter.prototype);
-Tapable.mixin(MyClass2.prototype);
-
-MyClass2.prototype.method = function() {};
-```
-
-## Public functions
-
-### apply
-
-``` javascript
-void apply(plugins: Plugin...)
-```
-
-Attaches all plugins passed as arguments to the instance, by calling `apply` on them.
-
-### plugin
-
-``` javascript
-void plugin(names: string|string[], handler: Function)
-```
-
-`names` are the names (or a single name) of the plugin interfaces the class provides.
-
-`handler` is a callback function. The signature depends on the class. `this` is the instance of the class.
-
-## Protected functions
-
-### applyPlugins
-
-``` javascript
-void applyPlugins(name: string, args: any...)
-```
-
-Synchronously applies all registered handlers for `name`. The handler functions are called with all args.
-
-### applyPluginsWaterfall
-
-``` javascript
-any applyPluginsWaterfall(name: string, init: any, args: any...)
-```
-
-Synchronously applies all registered handlers for `name`. The handler functions are called with the return value of the previous handler and all args. For the first handler `init` is used and the return value of the last handler is return by `applyPluginsWaterfall`
-
-### applyPluginsAsync
-
-``` javascript
-void applyPluginsAsync(
- name: string,
- args: any...,
- callback: (err?: Error) -> void
-)
-```
-
-Asynchronously applies all registered handlers for `name`. The handler functions are called with all args and a callback function with the signature `(err?: Error) -> void`. The handler functions are called in order of registration.
-
-`callback` is called after all handlers are called.
-
-### applyPluginsBailResult
-
-``` javascript
-any applyPluginsBailResult(name: string, args: any...)
-```
-
-Synchronously applies all registered handlers for `name`. The handler function are called with all args. If a handler function returns something `!== undefined`, the value is returned and no more handlers are applied.
-
-### applyPluginsAsyncWaterfall
-
-``` javascript
-applyPluginsAsyncWaterfall(
- name: string,
- init: any,
- callback: (err: Error, result: any) -> void
-)
-```
-
-Asynchronously applies all registered handlers for `name`. The handler functions are called with the current value and a callback function with the signature `(err: Error, nextValue: any) -> void`. When called, `nextValue` is the current value for the next handler. The current value for the first handler is `init`. After all handlers are applied, `callback` is called with the last value. If any handler passes a value for `err`, the `callback` is called with this error and no more handlers are called.
-
-### applyPluginsAsyncSeries
-
-``` javascript
-applyPluginsAsyncSeries(
- name: string,
- args: any...,
- callback: (err: Error, result: any) -> void
-)
-```
-
-Asynchronously applies all registered handlers for `name`. The handler functions are called with all `args` and a callback function with the signature `(err: Error) -> void`. The handlers are called in series, one at a time. After all handlers are applied, `callback` is called. If any handler passes a value for `err`, the `callback` is called with this error and no more handlers are called.
-
-### applyPluginsParallel
-
-``` javascript
-applyPluginsParallel(
- name: string,
- args: any...,
- callback: (err?: Error) -> void
-)
-```
-
-Applies all registered handlers for `name` in parallel. The handler functions are called with all args and a callback function with the signature `(err?: Error) -> void`. The `callback` function is called when all handlers have called the callback without `err`. If any handler calls the callback with `err`, `callback` is invoked with this error and the other handlers are ignored.
-
-### applyPluginsParallelBailResult
-
-``` javascript
-applyPluginsParallelBailResult(
- name: string,
- args: any...,
- callback: (err: Error, result: any) -> void
-)
-```
-
-Applies all registered handlers for `name` in parallel. The handler functions are called with all args and a callback function with the signature `(err?: Error) -> void`. Handler functions must call the callback. They can either pass an error, pass undefined, or pass a value. The first result (either error or value) which is not undefined is passed to the `callback`. The order is defined by registration, not by the speed of the handler function.
-
-### hasPlugins
-
-``` js
-hasPlugins(
- name: string
-)
-```
-
-Returns true, if plugins are registered for this name.
+# Tapable + +The tapable packages exposes many Hook classes, which can be used to create hooks for plugins. + +``` javascript +const { + SyncHook, + SyncBailHook, + SyncWaterfallHook, + SyncLoopHook, + AsyncParallelHook, + AsyncParallelBailHook, + AsyncSeriesHook, + AsyncSeriesBailHook, + AsyncSeriesWaterfallHook + } = require("tapable"); +``` + +## Usage + +All Hook constructors take one optional argument, which is a list of argument names as strings. + +``` js +const hook = new SyncHook(["arg1", "arg2", "arg3"]); +``` + +The best practice is to expose all hooks of a class in a `hooks` property: + +``` js +class Car { + constructor() { + this.hooks = { + accelerate: new SyncHook(["newSpeed"]), + break: new SyncHook(), + calculateRoutes: new AsyncParallelHook(["source", "target", "routesList"]) + }; + } + + /* ... */ +} +``` + +Other people can now use these hooks: + +``` js +const myCar = new Car(); + +// Use the tap method to add a consument +myCar.hooks.break.tap("WarningLampPlugin", () => warningLamp.on()); +``` + +It's required to pass a name to identify the plugin/reason. + +You may receive arguments: + +``` js +myCar.hooks.accelerate.tap("LoggerPlugin", newSpeed => console.log(`Accelerating to ${newSpeed}`)); +``` + +For sync hooks `tap` is the only valid method to add a plugin. Async hooks also support async plugins: + +``` js +myCar.hooks.calculateRoutes.tapPromise("GoogleMapsPlugin", (source, target, routesList) => { + // return a promise + return google.maps.findRoute(source, target).then(route => { + routesList.add(route); + }); +}); +myCar.hooks.calculateRoutes.tapAsync("BingMapsPlugin", (source, target, routesList, callback) => { + bing.findRoute(source, target, (err, route) => { + if(err) return callback(err); + routesList.add(route); + // call the callback + callback(); + }); +}); + +// You can still use sync plugins +myCar.hooks.calculateRoutes.tap("CachedRoutesPlugin", (source, target, routesList) => { + const cachedRoute = cache.get(source, target); + if(cachedRoute) + routesList.add(cachedRoute); +}) +``` + +The class declaring these hooks need to call them: + +``` js +class Car { + /* ... */ + + setSpeed(newSpeed) { + this.hooks.accelerate.call(newSpeed); + } + + useNavigationSystemPromise(source, target) { + const routesList = new List(); + return this.hooks.calculateRoutes.promise(source, target, routesList).then(() => { + return routesList.getRoutes(); + }); + } + + useNavigationSystemAsync(source, target, callback) { + const routesList = new List(); + this.hooks.calculateRoutes.callAsync(source, target, routesList, err => { + if(err) return callback(err); + callback(null, routesList.getRoutes()); + }); + } +} +``` + +The Hook will compile a method with the most efficient way of running your plugins. It generates code depending on: +* The number of registered plugins (none, one, many) +* The kind of registered plugins (sync, async, promise) +* The used call method (sync, async, promise) +* The number of arguments +* Whether interception is used + +This ensures fastest possible execution. + +## Hook types + +Each hook can be tapped with one or several functions. How they are executed depends on the hook type: + +* Basic hook (without “Waterfall”, “Bail” or “Loop” in its name). This hook simply calls every function it’s tapped with in a row. + +* __Waterfall__. A waterfall hook also calls each tapped function in a row. Unlike the basic hook, it passes a return value from each function to the next function. + +* __Bail__. A bail hook allows exitting early. When any of the tapped function returns anything, the bail hook will stop executing the remaining ones. + +* __Loop__. TODO + +Additionally, hooks can be synchronous or asynchronous. To reflect this, there’re “Sync”, “AsyncSeries” and “AsyncParallel” hook classes: + +* __Sync__. A sync hooks can only be tapped with synchronous functions (using `myHook.tap()`). + +* __AsyncSeries__. Async-series hooks can be tapped with synchronous, callback-based and promise-based functions (using `myHook.tap()`, `myHook.tapAsync()` and `myHook.tapPromise()`). They call each async method in a row. + +* __AsyncParallel__. Async-parallel hooks can also be tapped with synchronous, callback-based and promise-based functions (using `myHook.tap()`, `myHook.tapAsync()` and `myHook.tapPromise()`). However, they run each async method in parallel. + +The hook type is reflected in its class name. E.g., `AsyncSeriesWaterfallHook` allows asynchronous functions and runs them in series passing each function’s return value into the next function. + + +## Interception + +All Hooks offer an additional interception API: + +``` js +myCar.hooks.calculateRoutes.intercept({ + call: (source, target, routesList) => { + console.log("Starting to calculate routes"); + }, + register: (tapInfo) => { + // tapInfo = { type: "promise", name: "GoogleMapsPlugin", fn: ... } + console.log(`${tapInfo.name} is doing it's job`); + return tapInfo; // may return a new tapInfo object + } +}) +``` + +**call**: `(...args) => void` Adding `call` to your interceptor will trigger when hooks are triggered. You have access to the hooks arguments. + +**tap**: `(tap: Tap) => void` Adding `tap` to your interceptor will trigger when a plugin taps into a hook. Provided is the `Tap` object. `Tap` object can't be changed. + +**loop**: `(...args) => void` Adding `loop` to your interceptor will trigger for each loop of a looping hook. + +**register**: `(tap: Tap) => Tap | undefined` Adding `register` to your interceptor will trigger for each added `Tap` and allows to modify it. + +## Context + +Plugins and interceptors can opt-in to access an optional `context` object, which can be used to pass arbitrary values to subsequent plugins and interceptors. + +``` js +myCar.hooks.accelerate.intercept({ + context: true, + tap: (context, tapInfo) => { + // tapInfo = { type: "sync", name: "NoisePlugin", fn: ... } + console.log(`${tapInfo.name} is doing it's job`); + + // `context` starts as an empty object if at least one plugin uses `context: true`. + // If no plugins use `context: true`, then `context` is undefined. + if (context) { + // Arbitrary properties can be added to `context`, which plugins can then access. + context.hasMuffler = true; + } + } +}); + +myCar.hooks.accelerate.tap({ + name: "NoisePlugin", + context: true +}, (context, newSpeed) => { + if (context && context.hasMuffler) { + console.log("Silence..."); + } else { + console.log("Vroom!"); + } +}); +``` + +## HookMap + +A HookMap is a helper class for a Map with Hooks + +``` js +const keyedHook = new HookMap(key => new SyncHook(["arg"])) +``` + +``` js +keyedHook.tap("some-key", "MyPlugin", (arg) => { /* ... */ }); +keyedHook.tapAsync("some-key", "MyPlugin", (arg, callback) => { /* ... */ }); +keyedHook.tapPromise("some-key", "MyPlugin", (arg) => { /* ... */ }); +``` + +``` js +const hook = keyedHook.get("some-key"); +if(hook !== undefined) { + hook.callAsync("arg", err => { /* ... */ }); +} +``` + +## Hook/HookMap interface + +Public: + +``` ts +interface Hook { + tap: (name: string | Tap, fn: (context?, ...args) => Result) => void, + tapAsync: (name: string | Tap, fn: (context?, ...args, callback: (err, result: Result) => void) => void) => void, + tapPromise: (name: string | Tap, fn: (context?, ...args) => Promise<Result>) => void, + intercept: (interceptor: HookInterceptor) => void +} + +interface HookInterceptor { + call: (context?, ...args) => void, + loop: (context?, ...args) => void, + tap: (context?, tap: Tap) => void, + register: (tap: Tap) => Tap, + context: boolean +} + +interface HookMap { + for: (key: any) => Hook, + tap: (key: any, name: string | Tap, fn: (context?, ...args) => Result) => void, + tapAsync: (key: any, name: string | Tap, fn: (context?, ...args, callback: (err, result: Result) => void) => void) => void, + tapPromise: (key: any, name: string | Tap, fn: (context?, ...args) => Promise<Result>) => void, + intercept: (interceptor: HookMapInterceptor) => void +} + +interface HookMapInterceptor { + factory: (key: any, hook: Hook) => Hook +} + +interface Tap { + name: string, + type: string + fn: Function, + stage: number, + context: boolean +} +``` + +Protected (only for the class containing the hook): + +``` ts +interface Hook { + isUsed: () => boolean, + call: (...args) => Result, + promise: (...args) => Promise<Result>, + callAsync: (...args, callback: (err, result: Result) => void) => void, +} + +interface HookMap { + get: (key: any) => Hook | undefined, + for: (key: any) => Hook +} +``` + +## MultiHook + +A helper Hook-like class to redirect taps to multiple other hooks: + +``` js +const { MultiHook } = require("tapable"); + +this.hooks.allHooks = new MultiHook([this.hooks.hookA, this.hooks.hookB]); +``` diff --git a/node_modules/tapable/lib/Tapable.js b/node_modules/tapable/lib/Tapable.js index a8514c416..530fea356 100644 --- a/node_modules/tapable/lib/Tapable.js +++ b/node_modules/tapable/lib/Tapable.js @@ -1,377 +1,81 @@ -/*
- MIT License http://www.opensource.org/licenses/mit-license.php
- Author Tobias Koppers @sokra
-*/
-
-// polyfill from https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter
-// using the polyfill specifically to avoid the call to `Object.defineProperty` for performance reasons
-function fastFilter(fun/*, thisArg*/) {
- 'use strict';
-
- if (this === void 0 || this === null) {
- throw new TypeError();
- }
-
- var t = Object(this);
- var len = t.length >>> 0;
- if (typeof fun !== 'function') {
- throw new TypeError();
- }
-
- var res = [];
- var thisArg = arguments.length >= 2 ? arguments[1] : void 0;
- for (var i = 0; i < len; i++) {
- if (i in t) {
- var val = t[i];
-
- // NOTE: Technically this should Object.defineProperty at
- // the next index, as push can be affected by
- // properties on Object.prototype and Array.prototype.
- // But that method's new, and collisions should be
- // rare, so use the more-compatible alternative.
- if (fun.call(thisArg, val, i, t)) {
- res.push(val);
- }
- }
- }
-
- return res;
-}
-
-function Tapable() {
- this._plugins = {};
-}
-module.exports = Tapable;
-
-function copyProperties(from, to) {
- for(var key in from)
- to[key] = from[key];
- return to;
-}
-
-Tapable.mixin = function mixinTapable(pt) {
- copyProperties(Tapable.prototype, pt);
-};
-
-Tapable.prototype.applyPlugins = function applyPlugins(name) {
- if(!this._plugins[name]) return;
- var args = Array.prototype.slice.call(arguments, 1);
- var plugins = this._plugins[name];
- for(var i = 0; i < plugins.length; i++)
- plugins[i].apply(this, args);
-};
-
-Tapable.prototype.applyPlugins0 = function applyPlugins0(name) {
- var plugins = this._plugins[name];
- if(!plugins) return;
- for(var i = 0; i < plugins.length; i++)
- plugins[i].call(this);
-};
-
-Tapable.prototype.applyPlugins1 = function applyPlugins1(name, param) {
- var plugins = this._plugins[name];
- if(!plugins) return;
- for(var i = 0; i < plugins.length; i++)
- plugins[i].call(this, param);
-};
-
-Tapable.prototype.applyPlugins2 = function applyPlugins2(name, param1, param2) {
- var plugins = this._plugins[name];
- if(!plugins) return;
- for(var i = 0; i < plugins.length; i++)
- plugins[i].call(this, param1, param2);
-};
-
-Tapable.prototype.applyPluginsWaterfall = function applyPluginsWaterfall(name, init) {
- if(!this._plugins[name]) return init;
- var args = Array.prototype.slice.call(arguments, 1);
- var plugins = this._plugins[name];
- var current = init;
- for(var i = 0; i < plugins.length; i++) {
- args[0] = current;
- current = plugins[i].apply(this, args);
- }
- return current;
-};
-
-Tapable.prototype.applyPluginsWaterfall0 = function applyPluginsWaterfall0(name, init) {
- var plugins = this._plugins[name];
- if(!plugins) return init;
- var current = init;
- for(var i = 0; i < plugins.length; i++)
- current = plugins[i].call(this, current);
- return current;
-};
-
-Tapable.prototype.applyPluginsWaterfall1 = function applyPluginsWaterfall1(name, init, param) {
- var plugins = this._plugins[name];
- if(!plugins) return init;
- var current = init;
- for(var i = 0; i < plugins.length; i++)
- current = plugins[i].call(this, current, param);
- return current;
-};
-
-Tapable.prototype.applyPluginsWaterfall2 = function applyPluginsWaterfall2(name, init, param1, param2) {
- var plugins = this._plugins[name];
- if(!plugins) return init;
- var current = init;
- for(var i = 0; i < plugins.length; i++)
- current = plugins[i].call(this, current, param1, param2);
- return current;
-};
-
-Tapable.prototype.applyPluginsBailResult = function applyPluginsBailResult(name) {
- if(!this._plugins[name]) return;
- var args = Array.prototype.slice.call(arguments, 1);
- var plugins = this._plugins[name];
- for(var i = 0; i < plugins.length; i++) {
- var result = plugins[i].apply(this, args);
- if(typeof result !== "undefined") {
- return result;
- }
- }
-};
-
-Tapable.prototype.applyPluginsBailResult1 = function applyPluginsBailResult1(name, param) {
- if(!this._plugins[name]) return;
- var plugins = this._plugins[name];
- for(var i = 0; i < plugins.length; i++) {
- var result = plugins[i].call(this, param);
- if(typeof result !== "undefined") {
- return result;
- }
- }
-};
-
-Tapable.prototype.applyPluginsBailResult2 = function applyPluginsBailResult2(name, param1, param2) {
- if(!this._plugins[name]) return;
- var plugins = this._plugins[name];
- for(var i = 0; i < plugins.length; i++) {
- var result = plugins[i].call(this, param1, param2);
- if(typeof result !== "undefined") {
- return result;
- }
- }
-};
-
-Tapable.prototype.applyPluginsBailResult3 = function applyPluginsBailResult3(name, param1, param2, param3) {
- if(!this._plugins[name]) return;
- var plugins = this._plugins[name];
- for(var i = 0; i < plugins.length; i++) {
- var result = plugins[i].call(this, param1, param2, param3);
- if(typeof result !== "undefined") {
- return result;
- }
- }
-};
-
-Tapable.prototype.applyPluginsBailResult4 = function applyPluginsBailResult4(name, param1, param2, param3, param4) {
- if(!this._plugins[name]) return;
- var plugins = this._plugins[name];
- for(var i = 0; i < plugins.length; i++) {
- var result = plugins[i].call(this, param1, param2, param3, param4);
- if(typeof result !== "undefined") {
- return result;
- }
- }
-};
-
-Tapable.prototype.applyPluginsBailResult5 = function applyPluginsBailResult5(name, param1, param2, param3, param4, param5) {
- if(!this._plugins[name]) return;
- var plugins = this._plugins[name];
- for(var i = 0; i < plugins.length; i++) {
- var result = plugins[i].call(this, param1, param2, param3, param4, param5);
- if(typeof result !== "undefined") {
- return result;
- }
- }
-};
-
-Tapable.prototype.applyPluginsAsyncSeries = Tapable.prototype.applyPluginsAsync = function applyPluginsAsyncSeries(name) {
- var args = Array.prototype.slice.call(arguments, 1);
- var callback = args.pop();
- var plugins = this._plugins[name];
- if(!plugins || plugins.length === 0) return callback();
- var i = 0;
- var _this = this;
- args.push(copyProperties(callback, function next(err) {
- if(err) return callback(err);
- i++;
- if(i >= plugins.length) {
- return callback();
- }
- plugins[i].apply(_this, args);
- }));
- plugins[0].apply(this, args);
-};
-
-Tapable.prototype.applyPluginsAsyncSeries1 = function applyPluginsAsyncSeries1(name, param, callback) {
- var plugins = this._plugins[name];
- if(!plugins || plugins.length === 0) return callback();
- var i = 0;
- var _this = this;
- var innerCallback = copyProperties(callback, function next(err) {
- if(err) return callback(err);
- i++;
- if(i >= plugins.length) {
- return callback();
- }
- plugins[i].call(_this, param, innerCallback);
- });
- plugins[0].call(this, param, innerCallback);
-};
-
-Tapable.prototype.applyPluginsAsyncSeriesBailResult = function applyPluginsAsyncSeriesBailResult(name) {
- var args = Array.prototype.slice.call(arguments, 1);
- var callback = args.pop();
- if(!this._plugins[name] || this._plugins[name].length === 0) return callback();
- var plugins = this._plugins[name];
- var i = 0;
- var _this = this;
- args.push(copyProperties(callback, function next() {
- if(arguments.length > 0) return callback.apply(null, arguments);
- i++;
- if(i >= plugins.length) {
- return callback();
- }
- plugins[i].apply(_this, args);
- }));
- plugins[0].apply(this, args);
-};
-
-Tapable.prototype.applyPluginsAsyncSeriesBailResult1 = function applyPluginsAsyncSeriesBailResult1(name, param, callback) {
- var plugins = this._plugins[name];
- if(!plugins || plugins.length === 0) return callback();
- var i = 0;
- var _this = this;
- var innerCallback = copyProperties(callback, function next(err, result) {
- if(arguments.length > 0) return callback(err, result);
- i++;
- if(i >= plugins.length) {
- return callback();
- }
- plugins[i].call(_this, param, innerCallback);
- });
- plugins[0].call(this, param, innerCallback);
-};
-
-Tapable.prototype.applyPluginsAsyncWaterfall = function applyPluginsAsyncWaterfall(name, init, callback) {
- if(!this._plugins[name] || this._plugins[name].length === 0) return callback(null, init);
- var plugins = this._plugins[name];
- var i = 0;
- var _this = this;
- var next = copyProperties(callback, function(err, value) {
- if(err) return callback(err);
- i++;
- if(i >= plugins.length) {
- return callback(null, value);
- }
- plugins[i].call(_this, value, next);
- });
- plugins[0].call(this, init, next);
-};
-
-Tapable.prototype.applyPluginsParallel = function applyPluginsParallel(name) {
- var args = Array.prototype.slice.call(arguments, 1);
- var callback = args.pop();
- if(!this._plugins[name] || this._plugins[name].length === 0) return callback();
- var plugins = this._plugins[name];
- var remaining = plugins.length;
- args.push(copyProperties(callback, function(err) {
- if(remaining < 0) return; // ignore
- if(err) {
- remaining = -1;
- return callback(err);
- }
- remaining--;
- if(remaining === 0) {
- return callback();
- }
- }));
- for(var i = 0; i < plugins.length; i++) {
- plugins[i].apply(this, args);
- if(remaining < 0) return;
- }
-};
-
-Tapable.prototype.applyPluginsParallelBailResult = function applyPluginsParallelBailResult(name) {
- var args = Array.prototype.slice.call(arguments, 1);
- var callback = args[args.length - 1];
- if(!this._plugins[name] || this._plugins[name].length === 0) return callback();
- var plugins = this._plugins[name];
- var currentPos = plugins.length;
- var currentResult;
- var done = [];
- for(var i = 0; i < plugins.length; i++) {
- args[args.length - 1] = (function(i) {
- return copyProperties(callback, function() {
- if(i >= currentPos) return; // ignore
- done.push(i);
- if(arguments.length > 0) {
- currentPos = i + 1;
- done = fastFilter.call(done, function(item) {
- return item <= i;
- });
- currentResult = Array.prototype.slice.call(arguments);
- }
- if(done.length === currentPos) {
- callback.apply(null, currentResult);
- currentPos = 0;
- }
- });
- }(i));
- plugins[i].apply(this, args);
- }
-};
-
-Tapable.prototype.applyPluginsParallelBailResult1 = function applyPluginsParallelBailResult1(name, param, callback) {
- var plugins = this._plugins[name];
- if(!plugins || plugins.length === 0) return callback();
- var currentPos = plugins.length;
- var currentResult;
- var done = [];
- for(var i = 0; i < plugins.length; i++) {
- var innerCallback = (function(i) {
- return copyProperties(callback, function() {
- if(i >= currentPos) return; // ignore
- done.push(i);
- if(arguments.length > 0) {
- currentPos = i + 1;
- done = fastFilter.call(done, function(item) {
- return item <= i;
- });
- currentResult = Array.prototype.slice.call(arguments);
- }
- if(done.length === currentPos) {
- callback.apply(null, currentResult);
- currentPos = 0;
- }
- });
- }(i));
- plugins[i].call(this, param, innerCallback);
- }
-};
-
-Tapable.prototype.hasPlugins = function hasPlugins(name) {
- var plugins = this._plugins[name];
- return plugins && plugins.length > 0;
-};
-
-
-Tapable.prototype.plugin = function plugin(name, fn) {
- if(Array.isArray(name)) {
- name.forEach(function(name) {
- this.plugin(name, fn);
- }, this);
- return;
- }
- if(!this._plugins[name]) this._plugins[name] = [fn];
- else this._plugins[name].push(fn);
-};
-
-Tapable.prototype.apply = function apply() {
- for(var i = 0; i < arguments.length; i++) {
- arguments[i].apply(this);
- }
-};
+/* + MIT License http://www.opensource.org/licenses/mit-license.php + Author Tobias Koppers @sokra +*/ +"use strict"; + +const util = require("util"); +const SyncBailHook = require("./SyncBailHook"); + +function Tapable() { + this._pluginCompat = new SyncBailHook(["options"]); + this._pluginCompat.tap( + { + name: "Tapable camelCase", + stage: 100 + }, + options => { + options.names.add( + options.name.replace(/[- ]([a-z])/g, (str, ch) => ch.toUpperCase()) + ); + } + ); + this._pluginCompat.tap( + { + name: "Tapable this.hooks", + stage: 200 + }, + options => { + let hook; + for (const name of options.names) { + hook = this.hooks[name]; + if (hook !== undefined) { + break; + } + } + if (hook !== undefined) { + const tapOpt = { + name: options.fn.name || "unnamed compat plugin", + stage: options.stage || 0 + }; + if (options.async) hook.tapAsync(tapOpt, options.fn); + else hook.tap(tapOpt, options.fn); + return true; + } + } + ); +} +module.exports = Tapable; + +Tapable.addCompatLayer = function addCompatLayer(instance) { + Tapable.call(instance); + instance.plugin = Tapable.prototype.plugin; + instance.apply = Tapable.prototype.apply; +}; + +Tapable.prototype.plugin = util.deprecate(function plugin(name, fn) { + if (Array.isArray(name)) { + name.forEach(function(name) { + this.plugin(name, fn); + }, this); + return; + } + const result = this._pluginCompat.call({ + name: name, + fn: fn, + names: new Set([name]) + }); + if (!result) { + throw new Error( + `Plugin could not be registered at '${name}'. Hook was not found.\n` + + "BREAKING CHANGE: There need to exist a hook at 'this.hooks'. " + + "To create a compatibility layer for this hook, hook into 'this._pluginCompat'." + ); + } +}, "Tapable.plugin is deprecated. Use new API on `.hooks` instead"); + +Tapable.prototype.apply = util.deprecate(function apply() { + for (var i = 0; i < arguments.length; i++) { + arguments[i].apply(this); + } +}, "Tapable.apply is deprecated. Call apply on the plugin directly instead"); diff --git a/node_modules/tapable/package.json b/node_modules/tapable/package.json index fe0710a89..bb232aee6 100644 --- a/node_modules/tapable/package.json +++ b/node_modules/tapable/package.json @@ -1,6 +1,6 @@ { "name": "tapable", - "version": "0.2.8", + "version": "1.1.0", "author": "Tobias Koppers @sokra", "description": "Just a little module for plugins.", "license": "MIT", @@ -9,18 +9,30 @@ "url": "http://github.com/webpack/tapable.git" }, "devDependencies": { - "mocha": "^2.2.4", - "should": "^5.2.0" + "babel-core": "^6.26.0", + "babel-jest": "^21.0.2", + "babel-polyfill": "^6.26.0", + "babel-preset-env": "^1.6.0", + "codecov": "^2.3.0", + "jest": "^21.0.4", + "prettier": "^1.13.2" }, "engines": { - "node": ">=0.6" + "node": ">=6" }, "files": [ "lib" ], "homepage": "https://github.com/webpack/tapable", - "main": "lib/Tapable.js", + "main": "lib/index.js", "scripts": { - "test": "mocha --reporter spec" + "test": "jest", + "travis": "jest --coverage && codecov", + "pretty": "prettier --write lib/*.js lib/__tests__/*.js" + }, + "jest": { + "transform": { + "__tests__[\\\\/].+\\.js$": "babel-jest" + } } } |