Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: (WIP) started conversion of tapable to TypeScript #13

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
159 changes: 111 additions & 48 deletions lib/Tapable.js → lib/Tapable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,120 @@
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) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the old style that naming function with number is very confusing. better to be self-explained.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So changing the actual name of the function would require a huge changes to webpack/core, but once I've gotten this fully migrated, I can add a comment to explain how some are ussing call and some use apply thus the strange name.

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)
to[key] = from[key];
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;
Expand All @@ -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];
Expand All @@ -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++) {
Expand All @@ -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();
Expand All @@ -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();
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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();
Expand All @@ -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();
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
};
8 changes: 8 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"compilerOptions": {
"module": "commonjs",
"target": "es5",
"noImplicitAny": false,
"sourceMap": false
}
}