Skip to content

Commit

Permalink
Merge pull request #151 from webpack/feature/browser-support
Browse files Browse the repository at this point in the history
add browser support
  • Loading branch information
sokra authored Dec 4, 2020
2 parents a3cb3ae + eee843a commit 549db33
Show file tree
Hide file tree
Showing 5 changed files with 172 additions and 149 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
48 changes: 26 additions & 22 deletions lib/HookCodeFactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,22 +55,23 @@ 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";
}
code += "});\n";
code += "}));\n";
fn = new Function(this.args(), code);
break;
}
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 @@ -239,7 +243,7 @@ class HookCodeFactory {
cbCode += onDone();
}
cbCode += "}\n";
cbCode += "}";
cbCode += "})";
code += `_fn${tapIndex}(${this.args({
before: tap.context ? "_context" : undefined,
after: cbCode
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 @@ -363,7 +367,7 @@ class HookCodeFactory {
code += "} while(_loop);\n";
if (!syncOnly) {
code += "_loopAsync = true;\n";
code += "};\n";
code += "});\n";
code += "_looper();\n";
}
return code;
Expand All @@ -388,9 +392,9 @@ 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";
code += "});\n";
}
for (let i = 0; i < this.options.taps.length; i++) {
const done = () => {
Expand Down
Loading

0 comments on commit 549db33

Please sign in to comment.