Skip to content

Commit

Permalink
allow to use tapable in browser
Browse files Browse the repository at this point in the history
avoid arrow functions in favor of normal functions
  • Loading branch information
sokra committed Dec 4, 2020
1 parent a3cb3ae commit d677b60
Show file tree
Hide file tree
Showing 5 changed files with 146 additions and 123 deletions.
2 changes: 1 addition & 1 deletion lib/AsyncParallelBailHook.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class AsyncParallelBailHookCodeFactory extends HookCodeFactory {
content({ onError, onResult, onDone }) {
let code = "";
code += `var _results = new Array(${this.options.taps.length});\n`;
code += "var _checkDone = () => {\n";
code += "var _checkDone = function() {\n";
code += "for(var i = 0; i < _results.length; i++) {\n";
code += "var item = _results[i];\n";
code += "if(item === undefined) return false;\n";
Expand Down
40 changes: 22 additions & 18 deletions lib/HookCodeFactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,17 +55,18 @@ class HookCodeFactory {
});
let code = "";
code += '"use strict";\n';
code += "return new Promise((_resolve, _reject) => {\n";
code += this.header();
code += "return new Promise(function(_resolve, _reject) {\n";
if (errorHelperUsed) {
code += "var _sync = true;\n";
code += "function _error(_err) {\n";
code += "if(_sync)\n";
code += "_resolve(Promise.resolve().then(() => { throw _err; }));\n";
code +=
"_resolve(Promise.resolve().then(function() { throw _err; }));\n";
code += "else\n";
code += "_reject(_err);\n";
code += "};\n";
}
code += this.header();
code += content;
if (errorHelperUsed) {
code += "_sync = false;\n";
Expand Down Expand Up @@ -100,7 +101,16 @@ class HookCodeFactory {
const onError = options.onError;
const onResult = options.onResult;
const onDone = options.onDone;
return this.content(
let code = "";
for (let i = 0; i < this.options.interceptors.length; i++) {
const interceptor = this.options.interceptors[i];
if (interceptor.call) {
code += `${this.getInterceptor(i)}.call(${this.args({
before: interceptor.context ? "_context" : undefined
})});\n`;
}
}
code += this.content(
Object.assign(options, {
onError:
onError &&
Expand Down Expand Up @@ -143,6 +153,7 @@ class HookCodeFactory {
})
})
);
return code;
} else {
return this.content(options);
}
Expand All @@ -160,14 +171,6 @@ class HookCodeFactory {
code += "var _taps = this.taps;\n";
code += "var _interceptors = this.interceptors;\n";
}
for (let i = 0; i < this.options.interceptors.length; i++) {
const interceptor = this.options.interceptors[i];
if (interceptor.call) {
code += `${this.getInterceptor(i)}.call(${this.args({
before: interceptor.context ? "_context" : undefined
})});\n`;
}
}
return code;
}

Expand Down Expand Up @@ -227,8 +230,9 @@ class HookCodeFactory {
break;
case "async":
let cbCode = "";
if (onResult) cbCode += `(_err${tapIndex}, _result${tapIndex}) => {\n`;
else cbCode += `_err${tapIndex} => {\n`;
if (onResult)
cbCode += `function(_err${tapIndex}, _result${tapIndex}) {\n`;
else cbCode += `function(_err${tapIndex}) {\n`;
cbCode += `if(_err${tapIndex}) {\n`;
cbCode += onError(`_err${tapIndex}`);
cbCode += "} else {\n";
Expand All @@ -252,15 +256,15 @@ class HookCodeFactory {
})});\n`;
code += `if (!_promise${tapIndex} || !_promise${tapIndex}.then)\n`;
code += ` throw new Error('Tap function (tapPromise) did not return promise (returned ' + _promise${tapIndex} + ')');\n`;
code += `_promise${tapIndex}.then(_result${tapIndex} => {\n`;
code += `_promise${tapIndex}.then(function(_result${tapIndex}) {\n`;
code += `_hasResult${tapIndex} = true;\n`;
if (onResult) {
code += onResult(`_result${tapIndex}`);
}
if (onDone) {
code += onDone();
}
code += `}, _err${tapIndex} => {\n`;
code += `}, function(_err${tapIndex}) {\n`;
code += `if(_hasResult${tapIndex}) throw _err${tapIndex};\n`;
code += onError(`_err${tapIndex}`);
code += "});\n";
Expand Down Expand Up @@ -322,7 +326,7 @@ class HookCodeFactory {
const syncOnly = this.options.taps.every(t => t.type === "sync");
let code = "";
if (!syncOnly) {
code += "var _looper = () => {\n";
code += "var _looper = function() {\n";
code += "var _loopAsync = false;\n";
}
code += "var _loop;\n";
Expand Down Expand Up @@ -388,7 +392,7 @@ class HookCodeFactory {
code += "do {\n";
code += `var _counter = ${this.options.taps.length};\n`;
if (onDone) {
code += "var _done = () => {\n";
code += "var _done = function() {\n";
code += onDone();
code += "};\n";
}
Expand Down
Loading

0 comments on commit d677b60

Please sign in to comment.