From 47f29e6d4c8bb2a844c8057ae58cd8a5efa91494 Mon Sep 17 00:00:00 2001 From: Sean Larkin Date: Tue, 20 Sep 2016 23:29:03 -0500 Subject: [PATCH] feat: (WIP) started conversion of tapable to TypeScript --- lib/{Tapable.js => Tapable.ts} | 159 +++++++++++++++++++++++---------- tsconfig.json | 8 ++ 2 files changed, 119 insertions(+), 48 deletions(-) rename lib/{Tapable.js => Tapable.ts} (55%) create mode 100644 tsconfig.json diff --git a/lib/Tapable.js b/lib/Tapable.ts similarity index 55% rename from lib/Tapable.js rename to lib/Tapable.ts index 2dc5cca..25440c2 100644 --- a/lib/Tapable.js +++ b/lib/Tapable.ts @@ -2,10 +2,73 @@ MIT License http://www.opensource.org/licenses/mit-license.php Author Tobias Koppers @sokra */ -function Tapable() { + +// interface Tapable { +// compilation(compilation, data): void; +// } + +// this.plugin("compilation", function (compilation, data) { ... }); + +interface Plugin { + apply(): void; +} + +export abstract class Tapable { + private _plugins: any = {}; + static mixin(property: string): void { + copyProperties(Tapable.prototype, property); + } + + public apply(...plugins: Plugin[]) { + for(let i = 0; i < plugins.length; i++) { + plugins[i].apply(this); + } + } + + public plugin(names: string|string[], fn: Function ) { + if(Array.isArray(names)) { + names.forEach(function(name: string){ + this.plugin(name, fn); + }, this); + return; + } + if(!this._plugins[name]) this._plugins[name] = [fn]; + else this._plugins[name].push(fn); + } + + protected applyPlugins(name: string, ...args: any[]) { + if(!this._plugins[name]) return; + + let plugins = this._plugins[name]; + for(let i = 0; i < plugins.length; i++) + plugins[i].apply(this, args); + } + + protected applyPluginsWaterfall(name: string, init: any, ...args: any[]) { + if(!this._plugins[name]) return init; + + let plugins = this._plugins[name]; + let current = init; + for(let i = 0; i < plugins.length; i++) + current = plugins[i].apply(this, [current].concat(args)); + return current; + } + + protected applyPluginsWaterfall0(name: string, init: any) { + 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; + } +} + + +function TapableOld() { this._plugins = {}; } -module.exports = Tapable; +module.exports = TapableOld; function copyProperties(from, to) { for(var key in from) @@ -13,29 +76,46 @@ function copyProperties(from, to) { return to; } -Tapable.mixin = function mixinTapable(pt) { - copyProperties(Tapable.prototype, pt); -}; +// TapableOld.mixin = function mixinTapableOld(pt) { +// copyProperties(TapableOld.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); -}; +// TapableOld.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.applyPluginsWaterfall = function applyPluginsWaterfall(name, init) { - if(!this._plugins[name]) return init; - var args = Array.prototype.slice.call(arguments, 2); - var plugins = this._plugins[name]; - var current = init; - for(var i = 0; i < plugins.length; i++) - current = plugins[i].apply(this, [current].concat(args)); - return current; -}; +// TapableOld.prototype.apply = function apply() { +// for(var i = 0; i < arguments.length; i++) { +// arguments[i].apply(this); +// } +// }; + +// TapableOld.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.applyPluginsWaterfall0 = function applyPluginsWaterfall0(name, init) { +// TapableOld.prototype.applyPluginsWaterfall = function applyPluginsWaterfall(name, init) { +// if(!this._plugins[name]) return init; +// var args = Array.prototype.slice.call(arguments, 2); +// var plugins = this._plugins[name]; +// var current = init; +// for(var i = 0; i < plugins.length; i++) +// current = plugins[i].apply(this, [current].concat(args)); +// return current; +// }; + +TapableOld.prototype.applyPluginsWaterfall0 = function applyPluginsWaterfall0(name, init) { var plugins = this._plugins[name]; if(!plugins) return init; var current = init; @@ -44,7 +124,7 @@ Tapable.prototype.applyPluginsWaterfall0 = function applyPluginsWaterfall0(name, return current; }; -Tapable.prototype.applyPluginsBailResult = function applyPluginsBailResult(name) { +TapableOld.prototype.applyPluginsBailResult = function applyPluginsBailResult(name) { if(!this._plugins[name]) return; var args = Array.prototype.slice.call(arguments, 1); var plugins = this._plugins[name]; @@ -56,7 +136,7 @@ Tapable.prototype.applyPluginsBailResult = function applyPluginsBailResult(name) } }; -Tapable.prototype.applyPluginsBailResult1 = function applyPluginsBailResult1(name, param) { +TapableOld.prototype.applyPluginsBailResult1 = function applyPluginsBailResult1(name, param) { if(!this._plugins[name]) return; var plugins = this._plugins[name]; for(var i = 0; i < plugins.length; i++) { @@ -67,7 +147,7 @@ Tapable.prototype.applyPluginsBailResult1 = function applyPluginsBailResult1(nam } }; -Tapable.prototype.applyPluginsAsyncSeries = Tapable.prototype.applyPluginsAsync = function applyPluginsAsync(name) { +TapableOld.prototype.applyPluginsAsyncSeries = TapableOld.prototype.applyPluginsAsync = function applyPluginsAsync(name) { var args = Array.prototype.slice.call(arguments, 1); var callback = args.pop(); if(!this._plugins[name] || this._plugins[name].length === 0) return callback(); @@ -85,7 +165,7 @@ Tapable.prototype.applyPluginsAsyncSeries = Tapable.prototype.applyPluginsAsync plugins[0].apply(this, args); }; -Tapable.prototype.applyPluginsAsyncSeriesBailResult = function applyPluginsAsyncSeriesBailResult(name) { +TapableOld.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(); @@ -103,7 +183,7 @@ Tapable.prototype.applyPluginsAsyncSeriesBailResult = function applyPluginsAsync plugins[0].apply(this, args); }; -Tapable.prototype.applyPluginsAsyncSeriesBailResult1 = function applyPluginsAsyncSeriesBailResult1(name, param, callback) { +TapableOld.prototype.applyPluginsAsyncSeriesBailResult1 = function applyPluginsAsyncSeriesBailResult1(name, param, callback) { var plugins = this._plugins[name]; if(!plugins || plugins.length === 0) return callback(); var i = 0; @@ -119,7 +199,7 @@ Tapable.prototype.applyPluginsAsyncSeriesBailResult1 = function applyPluginsAsyn plugins[0].call(this, param, innerCallback); }; -Tapable.prototype.applyPluginsAsyncWaterfall = function applyPluginsAsyncWaterfall(name, init, callback) { +TapableOld.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; @@ -135,7 +215,7 @@ Tapable.prototype.applyPluginsAsyncWaterfall = function applyPluginsAsyncWaterfa plugins[0].call(this, init, next); }; -Tapable.prototype.applyPluginsParallel = function applyPluginsParallel(name) { +TapableOld.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(); @@ -158,7 +238,7 @@ Tapable.prototype.applyPluginsParallel = function applyPluginsParallel(name) { } }; -Tapable.prototype.applyPluginsParallelBailResult = function applyPluginsParallelBailResult(name) { +TapableOld.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(); @@ -188,7 +268,7 @@ Tapable.prototype.applyPluginsParallelBailResult = function applyPluginsParallel } }; -Tapable.prototype.applyPluginsParallelBailResult1 = function applyPluginsParallelBailResult1(name, param, callback) { +TapableOld.prototype.applyPluginsParallelBailResult1 = function applyPluginsParallelBailResult1(name, param, callback) { var plugins = this._plugins[name]; if(!plugins || plugins.length === 0) return callback(); var currentPos = plugins.length; @@ -216,20 +296,3 @@ Tapable.prototype.applyPluginsParallelBailResult1 = function applyPluginsParalle } }; - -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); - } -}; diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000..ea89196 --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,8 @@ +{ + "compilerOptions": { + "module": "commonjs", + "target": "es5", + "noImplicitAny": false, + "sourceMap": false + } +} \ No newline at end of file