mm
This commit is contained in:
+140
@@ -0,0 +1,140 @@
|
||||
/*
|
||||
MIT License http://www.opensource.org/licenses/mit-license.php
|
||||
Author Tobias Koppers @sokra
|
||||
*/
|
||||
"use strict";
|
||||
|
||||
const Hook = require("./Hook");
|
||||
const HookCodeFactory = require("./HookCodeFactory");
|
||||
|
||||
/** @typedef {import("./Hook").EXPECTED_ANY} EXPECTED_ANY */
|
||||
/** @typedef {import("./Hook").EXPECTED_FUNCTION} EXPECTED_FUNCTION */
|
||||
/** @typedef {import("./Hook").UnsetAdditionalOptions} UnsetAdditionalOptions */
|
||||
/** @typedef {import("./Hook").CompileOptions<EXPECTED_ANY, EXPECTED_ANY, EXPECTED_ANY>} CompileOptions */
|
||||
/** @typedef {import("./HookCodeFactory").ContentOptions} ContentOptions */
|
||||
/**
|
||||
* @template {EXPECTED_ANY[]} T
|
||||
* @typedef {import("./Hook").ArgumentNames<T>} ArgumentNames
|
||||
*/
|
||||
/**
|
||||
* @template T
|
||||
* @typedef {import("./Hook").AsArray<T>} AsArray
|
||||
*/
|
||||
|
||||
/**
|
||||
* Mirror of the `AsyncParallelBailHook` class declared in `tapable.d.ts` for
|
||||
* use as the factory function's return type, so `new AsyncParallelBailHook(...)`
|
||||
* resolves to the specific subclass type instead of the generic `Hook`.
|
||||
* @template T
|
||||
* @template R
|
||||
* @template [AdditionalOptions=UnsetAdditionalOptions]
|
||||
* @typedef {import("../tapable").AsyncParallelBailHook<T, R, AdditionalOptions>} AsyncParallelBailHookType
|
||||
*/
|
||||
|
||||
class AsyncParallelBailHookCodeFactory extends HookCodeFactory {
|
||||
/**
|
||||
* @param {ContentOptions} options content generation options
|
||||
* @returns {string} generated body code
|
||||
*/
|
||||
content({ onError, onResult, onDone }) {
|
||||
const opts = /** @type {CompileOptions} */ (this.options);
|
||||
let code = "";
|
||||
code += `var _results = new Array(${opts.taps.length});\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";
|
||||
code += "if(item.result !== undefined) {\n";
|
||||
code += onResult("item.result");
|
||||
code += "return true;\n";
|
||||
code += "}\n";
|
||||
code += "if(item.error) {\n";
|
||||
code += onError("item.error");
|
||||
code += "return true;\n";
|
||||
code += "}\n";
|
||||
code += "}\n";
|
||||
code += "return false;\n";
|
||||
code += "}\n";
|
||||
code += this.callTapsParallel({
|
||||
onError: (i, err, done, doneBreak) => {
|
||||
let code = "";
|
||||
code += `if(${i} < _results.length && ((_results.length = ${
|
||||
i + 1
|
||||
}), (_results[${i}] = { error: ${err} }), _checkDone())) {\n`;
|
||||
code += doneBreak(true);
|
||||
code += "} else {\n";
|
||||
code += done();
|
||||
code += "}\n";
|
||||
return code;
|
||||
},
|
||||
onResult: (i, result, done, doneBreak) => {
|
||||
let code = "";
|
||||
code += `if(${i} < _results.length && (${result} !== undefined && (_results.length = ${
|
||||
i + 1
|
||||
}), (_results[${i}] = { result: ${result} }), _checkDone())) {\n`;
|
||||
code += doneBreak(true);
|
||||
code += "} else {\n";
|
||||
code += done();
|
||||
code += "}\n";
|
||||
return code;
|
||||
},
|
||||
onTap: (i, run, done, _doneBreak) => {
|
||||
let code = "";
|
||||
if (i > 0) {
|
||||
code += `if(${i} >= _results.length) {\n`;
|
||||
code += done();
|
||||
code += "} else {\n";
|
||||
}
|
||||
code += run();
|
||||
if (i > 0) code += "}\n";
|
||||
return code;
|
||||
},
|
||||
onDone
|
||||
});
|
||||
return code;
|
||||
}
|
||||
}
|
||||
|
||||
const factory = new AsyncParallelBailHookCodeFactory();
|
||||
|
||||
/**
|
||||
* @template T
|
||||
* @template R
|
||||
* @template [AdditionalOptions=UnsetAdditionalOptions]
|
||||
* @this {Hook<T, R, AdditionalOptions>}
|
||||
* @param {CompileOptions} options compile options
|
||||
* @returns {EXPECTED_FUNCTION} the compiled call function
|
||||
*/
|
||||
function COMPILE(options) {
|
||||
factory.setup(this, options);
|
||||
return factory.create(options);
|
||||
}
|
||||
|
||||
/**
|
||||
* @constructor
|
||||
* @template T
|
||||
* @template R
|
||||
* @template [AdditionalOptions=UnsetAdditionalOptions]
|
||||
* @param {ArgumentNames<AsArray<T>>=} args argument names of the hook
|
||||
* @param {string=} name name of the hook
|
||||
* @returns {AsyncParallelBailHookType<T, R, AdditionalOptions>} a new AsyncParallelBailHook instance
|
||||
*/
|
||||
function AsyncParallelBailHook(
|
||||
args = /** @type {ArgumentNames<AsArray<T>>} */ (/** @type {unknown} */ ([])),
|
||||
name = undefined
|
||||
) {
|
||||
const hook = new Hook(args, name);
|
||||
hook.constructor = AsyncParallelBailHook;
|
||||
hook.compile = COMPILE;
|
||||
// @ts-expect-error for performance reasons
|
||||
hook._call = undefined;
|
||||
hook.call = undefined;
|
||||
return /** @type {AsyncParallelBailHookType<T, R, AdditionalOptions>} */ (
|
||||
/** @type {unknown} */ (hook)
|
||||
);
|
||||
}
|
||||
|
||||
/** @type {EXPECTED_ANY} */
|
||||
AsyncParallelBailHook.prototype = null;
|
||||
|
||||
module.exports = AsyncParallelBailHook;
|
||||
+84
@@ -0,0 +1,84 @@
|
||||
/*
|
||||
MIT License http://www.opensource.org/licenses/mit-license.php
|
||||
Author Tobias Koppers @sokra
|
||||
*/
|
||||
"use strict";
|
||||
|
||||
const Hook = require("./Hook");
|
||||
const HookCodeFactory = require("./HookCodeFactory");
|
||||
|
||||
/** @typedef {import("./Hook").EXPECTED_ANY} EXPECTED_ANY */
|
||||
/** @typedef {import("./Hook").EXPECTED_FUNCTION} EXPECTED_FUNCTION */
|
||||
/** @typedef {import("./Hook").CompileOptions<EXPECTED_ANY, EXPECTED_ANY, EXPECTED_ANY>} CompileOptions */
|
||||
/** @typedef {import("./Hook").UnsetAdditionalOptions} UnsetAdditionalOptions */
|
||||
/** @typedef {import("./HookCodeFactory").ContentOptions} ContentOptions */
|
||||
/**
|
||||
* @template {EXPECTED_ANY[]} T
|
||||
* @typedef {import("./Hook").ArgumentNames<T>} ArgumentNames
|
||||
*/
|
||||
/**
|
||||
* @template T
|
||||
* @typedef {import("./Hook").AsArray<T>} AsArray
|
||||
*/
|
||||
|
||||
/**
|
||||
* Mirror of the `AsyncParallelHook` class declared in `tapable.d.ts` for use
|
||||
* as the factory function's return type, so `new AsyncParallelHook(...)`
|
||||
* resolves to the specific subclass type instead of the generic `Hook`.
|
||||
* @template T
|
||||
* @template [AdditionalOptions=UnsetAdditionalOptions]
|
||||
* @typedef {import("../tapable").AsyncParallelHook<T, AdditionalOptions>} AsyncParallelHookType
|
||||
*/
|
||||
|
||||
class AsyncParallelHookCodeFactory extends HookCodeFactory {
|
||||
/**
|
||||
* @param {ContentOptions} options content generation options
|
||||
* @returns {string} generated body code
|
||||
*/
|
||||
content({ onError, onDone }) {
|
||||
return this.callTapsParallel({
|
||||
onError: (i, err, done, doneBreak) => onError(err) + doneBreak(true),
|
||||
onDone
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
const factory = new AsyncParallelHookCodeFactory();
|
||||
|
||||
/**
|
||||
* @this {Hook<EXPECTED_ANY, EXPECTED_ANY, EXPECTED_ANY>}
|
||||
* @param {CompileOptions} options compile options
|
||||
* @returns {EXPECTED_FUNCTION} the compiled call function
|
||||
*/
|
||||
function COMPILE(options) {
|
||||
factory.setup(this, options);
|
||||
return factory.create(options);
|
||||
}
|
||||
|
||||
/**
|
||||
* @constructor
|
||||
* @template T
|
||||
* @template [AdditionalOptions=UnsetAdditionalOptions]
|
||||
* @param {ArgumentNames<AsArray<T>>=} args argument names of the hook
|
||||
* @param {string=} name name of the hook
|
||||
* @returns {AsyncParallelHookType<T, AdditionalOptions>} a new AsyncParallelHook instance
|
||||
*/
|
||||
function AsyncParallelHook(
|
||||
args = /** @type {ArgumentNames<AsArray<T>>} */ (/** @type {unknown} */ ([])),
|
||||
name = undefined
|
||||
) {
|
||||
const hook = new Hook(args, name);
|
||||
hook.constructor = AsyncParallelHook;
|
||||
hook.compile = COMPILE;
|
||||
// @ts-expect-error for performance reasons
|
||||
hook._call = undefined;
|
||||
hook.call = undefined;
|
||||
return /** @type {AsyncParallelHookType<T, AdditionalOptions>} */ (
|
||||
/** @type {unknown} */ (hook)
|
||||
);
|
||||
}
|
||||
|
||||
/** @type {EXPECTED_ANY} */
|
||||
AsyncParallelHook.prototype = null;
|
||||
|
||||
module.exports = AsyncParallelHook;
|
||||
+91
@@ -0,0 +1,91 @@
|
||||
/*
|
||||
MIT License http://www.opensource.org/licenses/mit-license.php
|
||||
Author Tobias Koppers @sokra
|
||||
*/
|
||||
"use strict";
|
||||
|
||||
const Hook = require("./Hook");
|
||||
const HookCodeFactory = require("./HookCodeFactory");
|
||||
|
||||
/** @typedef {import("./Hook").EXPECTED_ANY} EXPECTED_ANY */
|
||||
/** @typedef {import("./Hook").EXPECTED_FUNCTION} EXPECTED_FUNCTION */
|
||||
/** @typedef {import("./Hook").CompileOptions<EXPECTED_ANY, EXPECTED_ANY, EXPECTED_ANY>} CompileOptions */
|
||||
/** @typedef {import("./Hook").UnsetAdditionalOptions} UnsetAdditionalOptions */
|
||||
/** @typedef {import("./HookCodeFactory").ContentOptions} ContentOptions */
|
||||
/**
|
||||
* @template {EXPECTED_ANY[]} T
|
||||
* @typedef {import("./Hook").ArgumentNames<T>} ArgumentNames
|
||||
*/
|
||||
/**
|
||||
* @template T
|
||||
* @typedef {import("./Hook").AsArray<T>} AsArray
|
||||
*/
|
||||
|
||||
/**
|
||||
* Mirror of the `AsyncSeriesBailHook` class declared in `tapable.d.ts` for
|
||||
* use as the factory function's return type, so `new AsyncSeriesBailHook(...)`
|
||||
* resolves to the specific subclass type instead of the generic `Hook`.
|
||||
* @template T
|
||||
* @template R
|
||||
* @template [AdditionalOptions=UnsetAdditionalOptions]
|
||||
* @typedef {import("../tapable").AsyncSeriesBailHook<T, R, AdditionalOptions>} AsyncSeriesBailHookType
|
||||
*/
|
||||
|
||||
class AsyncSeriesBailHookCodeFactory extends HookCodeFactory {
|
||||
/**
|
||||
* @param {ContentOptions} options content generation options
|
||||
* @returns {string} generated body code
|
||||
*/
|
||||
content({ onError, onResult, resultReturns, onDone }) {
|
||||
return this.callTapsSeries({
|
||||
onError: (i, err, next, doneBreak) => onError(err) + doneBreak(true),
|
||||
onResult: (i, result, next) =>
|
||||
`if(${result} !== undefined) {\n${onResult(
|
||||
result
|
||||
)}\n} else {\n${next()}}\n`,
|
||||
resultReturns,
|
||||
onDone
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
const factory = new AsyncSeriesBailHookCodeFactory();
|
||||
|
||||
/**
|
||||
* @this {Hook<EXPECTED_ANY, EXPECTED_ANY, EXPECTED_ANY>}
|
||||
* @param {CompileOptions} options compile options
|
||||
* @returns {EXPECTED_FUNCTION} the compiled call function
|
||||
*/
|
||||
function COMPILE(options) {
|
||||
factory.setup(this, options);
|
||||
return factory.create(options);
|
||||
}
|
||||
|
||||
/**
|
||||
* @constructor
|
||||
* @template T
|
||||
* @template R
|
||||
* @template [AdditionalOptions=UnsetAdditionalOptions]
|
||||
* @param {ArgumentNames<AsArray<T>>=} args argument names of the hook
|
||||
* @param {string=} name name of the hook
|
||||
* @returns {AsyncSeriesBailHookType<T, R, AdditionalOptions>} a new AsyncSeriesBailHook instance
|
||||
*/
|
||||
function AsyncSeriesBailHook(
|
||||
args = /** @type {ArgumentNames<AsArray<T>>} */ (/** @type {unknown} */ ([])),
|
||||
name = undefined
|
||||
) {
|
||||
const hook = new Hook(args, name);
|
||||
hook.constructor = AsyncSeriesBailHook;
|
||||
hook.compile = COMPILE;
|
||||
// @ts-expect-error for performance reasons
|
||||
hook._call = undefined;
|
||||
hook.call = undefined;
|
||||
return /** @type {AsyncSeriesBailHookType<T, R, AdditionalOptions>} */ (
|
||||
/** @type {unknown} */ (hook)
|
||||
);
|
||||
}
|
||||
|
||||
/** @type {EXPECTED_ANY} */
|
||||
AsyncSeriesBailHook.prototype = null;
|
||||
|
||||
module.exports = AsyncSeriesBailHook;
|
||||
+84
@@ -0,0 +1,84 @@
|
||||
/*
|
||||
MIT License http://www.opensource.org/licenses/mit-license.php
|
||||
Author Tobias Koppers @sokra
|
||||
*/
|
||||
"use strict";
|
||||
|
||||
const Hook = require("./Hook");
|
||||
const HookCodeFactory = require("./HookCodeFactory");
|
||||
|
||||
/** @typedef {import("./Hook").EXPECTED_ANY} EXPECTED_ANY */
|
||||
/** @typedef {import("./Hook").EXPECTED_FUNCTION} EXPECTED_FUNCTION */
|
||||
/** @typedef {import("./Hook").CompileOptions<EXPECTED_ANY, EXPECTED_ANY, EXPECTED_ANY>} CompileOptions */
|
||||
/** @typedef {import("./Hook").UnsetAdditionalOptions} UnsetAdditionalOptions */
|
||||
/** @typedef {import("./HookCodeFactory").ContentOptions} ContentOptions */
|
||||
/**
|
||||
* @template {EXPECTED_ANY[]} T
|
||||
* @typedef {import("./Hook").ArgumentNames<T>} ArgumentNames
|
||||
*/
|
||||
/**
|
||||
* @template T
|
||||
* @typedef {import("./Hook").AsArray<T>} AsArray
|
||||
*/
|
||||
|
||||
/**
|
||||
* Mirror of the `AsyncSeriesHook` class declared in `tapable.d.ts` for use
|
||||
* as the factory function's return type, so `new AsyncSeriesHook(...)`
|
||||
* resolves to the specific subclass type instead of the generic `Hook`.
|
||||
* @template T
|
||||
* @template [AdditionalOptions=UnsetAdditionalOptions]
|
||||
* @typedef {import("../tapable").AsyncSeriesHook<T, AdditionalOptions>} AsyncSeriesHookType
|
||||
*/
|
||||
|
||||
class AsyncSeriesHookCodeFactory extends HookCodeFactory {
|
||||
/**
|
||||
* @param {ContentOptions} options content generation options
|
||||
* @returns {string} generated body code
|
||||
*/
|
||||
content({ onError, onDone }) {
|
||||
return this.callTapsSeries({
|
||||
onError: (i, err, next, doneBreak) => onError(err) + doneBreak(true),
|
||||
onDone
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
const factory = new AsyncSeriesHookCodeFactory();
|
||||
|
||||
/**
|
||||
* @this {Hook<EXPECTED_ANY, EXPECTED_ANY, EXPECTED_ANY>}
|
||||
* @param {CompileOptions} options compile options
|
||||
* @returns {EXPECTED_FUNCTION} the compiled call function
|
||||
*/
|
||||
function COMPILE(options) {
|
||||
factory.setup(this, options);
|
||||
return factory.create(options);
|
||||
}
|
||||
|
||||
/**
|
||||
* @constructor
|
||||
* @template T
|
||||
* @template [AdditionalOptions=UnsetAdditionalOptions]
|
||||
* @param {ArgumentNames<AsArray<T>>=} args argument names of the hook
|
||||
* @param {string=} name name of the hook
|
||||
* @returns {AsyncSeriesHookType<T, AdditionalOptions>} a new AsyncSeriesHook instance
|
||||
*/
|
||||
function AsyncSeriesHook(
|
||||
args = /** @type {ArgumentNames<AsArray<T>>} */ (/** @type {unknown} */ ([])),
|
||||
name = undefined
|
||||
) {
|
||||
const hook = new Hook(args, name);
|
||||
hook.constructor = AsyncSeriesHook;
|
||||
hook.compile = COMPILE;
|
||||
// @ts-expect-error for performance reasons
|
||||
hook._call = undefined;
|
||||
hook.call = undefined;
|
||||
return /** @type {AsyncSeriesHookType<T, AdditionalOptions>} */ (
|
||||
/** @type {unknown} */ (hook)
|
||||
);
|
||||
}
|
||||
|
||||
/** @type {EXPECTED_ANY} */
|
||||
AsyncSeriesHook.prototype = null;
|
||||
|
||||
module.exports = AsyncSeriesHook;
|
||||
+84
@@ -0,0 +1,84 @@
|
||||
/*
|
||||
MIT License http://www.opensource.org/licenses/mit-license.php
|
||||
Author Tobias Koppers @sokra
|
||||
*/
|
||||
"use strict";
|
||||
|
||||
const Hook = require("./Hook");
|
||||
const HookCodeFactory = require("./HookCodeFactory");
|
||||
|
||||
/** @typedef {import("./Hook").EXPECTED_ANY} EXPECTED_ANY */
|
||||
/** @typedef {import("./Hook").EXPECTED_FUNCTION} EXPECTED_FUNCTION */
|
||||
/** @typedef {import("./Hook").CompileOptions<EXPECTED_ANY, EXPECTED_ANY, EXPECTED_ANY>} CompileOptions */
|
||||
/** @typedef {import("./Hook").UnsetAdditionalOptions} UnsetAdditionalOptions */
|
||||
/** @typedef {import("./HookCodeFactory").ContentOptions} ContentOptions */
|
||||
/**
|
||||
* @template {EXPECTED_ANY[]} T
|
||||
* @typedef {import("./Hook").ArgumentNames<T>} ArgumentNames
|
||||
*/
|
||||
/**
|
||||
* @template T
|
||||
* @typedef {import("./Hook").AsArray<T>} AsArray
|
||||
*/
|
||||
|
||||
/**
|
||||
* Mirror of the `AsyncSeriesLoopHook` class declared in `tapable.d.ts` for
|
||||
* use as the factory function's return type, so `new AsyncSeriesLoopHook(...)`
|
||||
* resolves to the specific subclass type instead of the generic `Hook`.
|
||||
* @template T
|
||||
* @template [AdditionalOptions=UnsetAdditionalOptions]
|
||||
* @typedef {import("../tapable").AsyncSeriesLoopHook<T, AdditionalOptions>} AsyncSeriesLoopHookType
|
||||
*/
|
||||
|
||||
class AsyncSeriesLoopHookCodeFactory extends HookCodeFactory {
|
||||
/**
|
||||
* @param {ContentOptions} options content generation options
|
||||
* @returns {string} generated body code
|
||||
*/
|
||||
content({ onError, onDone }) {
|
||||
return this.callTapsLooping({
|
||||
onError: (i, err, next, doneBreak) => onError(err) + doneBreak(true),
|
||||
onDone
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
const factory = new AsyncSeriesLoopHookCodeFactory();
|
||||
|
||||
/**
|
||||
* @this {Hook<EXPECTED_ANY, EXPECTED_ANY, EXPECTED_ANY>}
|
||||
* @param {CompileOptions} options compile options
|
||||
* @returns {EXPECTED_FUNCTION} the compiled call function
|
||||
*/
|
||||
function COMPILE(options) {
|
||||
factory.setup(this, options);
|
||||
return factory.create(options);
|
||||
}
|
||||
|
||||
/**
|
||||
* @constructor
|
||||
* @template T
|
||||
* @template [AdditionalOptions=UnsetAdditionalOptions]
|
||||
* @param {ArgumentNames<AsArray<T>>=} args argument names of the hook
|
||||
* @param {string=} name name of the hook
|
||||
* @returns {AsyncSeriesLoopHookType<T, AdditionalOptions>} a new AsyncSeriesLoopHook instance
|
||||
*/
|
||||
function AsyncSeriesLoopHook(
|
||||
args = /** @type {ArgumentNames<AsArray<T>>} */ (/** @type {unknown} */ ([])),
|
||||
name = undefined
|
||||
) {
|
||||
const hook = new Hook(args, name);
|
||||
hook.constructor = AsyncSeriesLoopHook;
|
||||
hook.compile = COMPILE;
|
||||
// @ts-expect-error for performance reasons
|
||||
hook._call = undefined;
|
||||
hook.call = undefined;
|
||||
return /** @type {AsyncSeriesLoopHookType<T, AdditionalOptions>} */ (
|
||||
/** @type {unknown} */ (hook)
|
||||
);
|
||||
}
|
||||
|
||||
/** @type {EXPECTED_ANY} */
|
||||
AsyncSeriesLoopHook.prototype = null;
|
||||
|
||||
module.exports = AsyncSeriesLoopHook;
|
||||
+98
@@ -0,0 +1,98 @@
|
||||
/*
|
||||
MIT License http://www.opensource.org/licenses/mit-license.php
|
||||
Author Tobias Koppers @sokra
|
||||
*/
|
||||
"use strict";
|
||||
|
||||
const Hook = require("./Hook");
|
||||
const HookCodeFactory = require("./HookCodeFactory");
|
||||
|
||||
/** @typedef {import("./Hook").EXPECTED_ANY} EXPECTED_ANY */
|
||||
/** @typedef {import("./Hook").EXPECTED_FUNCTION} EXPECTED_FUNCTION */
|
||||
/** @typedef {import("./Hook").CompileOptions<EXPECTED_ANY, EXPECTED_ANY, EXPECTED_ANY>} CompileOptions */
|
||||
/** @typedef {import("./Hook").UnsetAdditionalOptions} UnsetAdditionalOptions */
|
||||
/** @typedef {import("./HookCodeFactory").ContentOptions} ContentOptions */
|
||||
/**
|
||||
* @template {EXPECTED_ANY[]} T
|
||||
* @typedef {import("./Hook").ArgumentNames<T>} ArgumentNames
|
||||
*/
|
||||
/**
|
||||
* @template T
|
||||
* @typedef {import("./Hook").AsArray<T>} AsArray
|
||||
*/
|
||||
|
||||
/**
|
||||
* Mirror of the `AsyncSeriesWaterfallHook` class declared in `tapable.d.ts`
|
||||
* for use as the factory function's return type, so
|
||||
* `new AsyncSeriesWaterfallHook(...)` resolves to the specific subclass type
|
||||
* instead of the generic `Hook`.
|
||||
* @template T
|
||||
* @template [R=AsArray<T>[0]]
|
||||
* @template [AdditionalOptions=UnsetAdditionalOptions]
|
||||
* @typedef {import("../tapable").AsyncSeriesWaterfallHook<T, R, AdditionalOptions>} AsyncSeriesWaterfallHookType
|
||||
*/
|
||||
|
||||
class AsyncSeriesWaterfallHookCodeFactory extends HookCodeFactory {
|
||||
/**
|
||||
* @param {ContentOptions} options content generation options
|
||||
* @returns {string} generated body code
|
||||
*/
|
||||
content({ onError, onResult }) {
|
||||
return this.callTapsSeries({
|
||||
onError: (i, err, next, doneBreak) => onError(err) + doneBreak(true),
|
||||
onResult: (i, result, next) => {
|
||||
let code = "";
|
||||
code += `if(${result} !== undefined) {\n`;
|
||||
code += `${/** @type {string[]} */ (this._args)[0]} = ${result};\n`;
|
||||
code += "}\n";
|
||||
code += next();
|
||||
return code;
|
||||
},
|
||||
onDone: () => onResult(/** @type {string[]} */ (this._args)[0])
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
const factory = new AsyncSeriesWaterfallHookCodeFactory();
|
||||
|
||||
/**
|
||||
* @this {Hook<EXPECTED_ANY, EXPECTED_ANY, EXPECTED_ANY>}
|
||||
* @param {CompileOptions} options compile options
|
||||
* @returns {EXPECTED_FUNCTION} the compiled call function
|
||||
*/
|
||||
function COMPILE(options) {
|
||||
factory.setup(this, options);
|
||||
return factory.create(options);
|
||||
}
|
||||
|
||||
/**
|
||||
* @constructor
|
||||
* @template T
|
||||
* @template [R=AsArray<T>[0]]
|
||||
* @template [AdditionalOptions=UnsetAdditionalOptions]
|
||||
* @param {ArgumentNames<AsArray<T>>=} args argument names of the hook
|
||||
* @param {string=} name name of the hook
|
||||
* @returns {AsyncSeriesWaterfallHookType<T, R, AdditionalOptions>} a new AsyncSeriesWaterfallHook instance
|
||||
*/
|
||||
function AsyncSeriesWaterfallHook(
|
||||
args = /** @type {ArgumentNames<AsArray<T>>} */ (/** @type {unknown} */ ([])),
|
||||
name = undefined
|
||||
) {
|
||||
if (args.length < 1) {
|
||||
throw new Error("Waterfall hooks must have at least one argument");
|
||||
}
|
||||
const hook = new Hook(args, name);
|
||||
hook.constructor = AsyncSeriesWaterfallHook;
|
||||
hook.compile = COMPILE;
|
||||
// @ts-expect-error for performance reasons
|
||||
hook._call = undefined;
|
||||
hook.call = undefined;
|
||||
return /** @type {AsyncSeriesWaterfallHookType<T, R, AdditionalOptions>} */ (
|
||||
/** @type {unknown} */ (hook)
|
||||
);
|
||||
}
|
||||
|
||||
/** @type {EXPECTED_ANY} */
|
||||
AsyncSeriesWaterfallHook.prototype = null;
|
||||
|
||||
module.exports = AsyncSeriesWaterfallHook;
|
||||
+500
@@ -0,0 +1,500 @@
|
||||
/*
|
||||
MIT License http://www.opensource.org/licenses/mit-license.php
|
||||
Author Tobias Koppers @sokra
|
||||
*/
|
||||
"use strict";
|
||||
|
||||
const util = require("util");
|
||||
|
||||
// eslint-disable-next-line jsdoc/reject-function-type
|
||||
/** @typedef {Function} EXPECTED_FUNCTION */
|
||||
// eslint-disable-next-line jsdoc/reject-any-type
|
||||
/** @typedef {any} EXPECTED_ANY */
|
||||
/** @typedef {Record<string, EXPECTED_ANY>} EXPECTED_OBJECT */
|
||||
|
||||
/**
|
||||
* @template T
|
||||
* @typedef {T extends EXPECTED_ANY[] ? T : [T]} AsArray
|
||||
*/
|
||||
|
||||
/**
|
||||
* @template {number} T
|
||||
* @typedef {T extends 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 ? T : never} Measure
|
||||
*/
|
||||
|
||||
/**
|
||||
* @template {EXPECTED_ANY[]} T
|
||||
* @template U
|
||||
* @typedef {{
|
||||
* 0: [U];
|
||||
* 1: [T[0], U];
|
||||
* 2: [T[0], T[1], U];
|
||||
* 3: [T[0], T[1], T[2], U];
|
||||
* 4: [T[0], T[1], T[2], T[3], U];
|
||||
* 5: [T[0], T[1], T[2], T[3], T[4], U];
|
||||
* 6: [T[0], T[1], T[2], T[3], T[4], T[5], U];
|
||||
* 7: [T[0], T[1], T[2], T[3], T[4], T[5], T[6], U];
|
||||
* 8: [T[0], T[1], T[2], T[3], T[4], T[5], T[6], T[7], U];
|
||||
* }[Measure<T["length"]>]} Append
|
||||
*/
|
||||
|
||||
/* eslint-disable jsdoc/ts-no-empty-object-type */
|
||||
/**
|
||||
* @template X
|
||||
* @typedef {X extends UnsetAdditionalOptions ? { } : X} IfSet
|
||||
*/
|
||||
/* eslint-enable jsdoc/ts-no-empty-object-type */
|
||||
|
||||
/**
|
||||
* @template {number} T
|
||||
* @template U
|
||||
* @typedef {T extends 0 ? void[] : ReadonlyArray<U> & { 0: U, length: T }} FixedSizeArray
|
||||
*/
|
||||
|
||||
/**
|
||||
* @template {EXPECTED_ANY[]} T
|
||||
* @typedef {FixedSizeArray<T["length"], string>} ArgumentNames
|
||||
*/
|
||||
|
||||
/**
|
||||
* @typedef {object} TapOptions
|
||||
* @property {string=} before name of an earlier tap to insert this tap before
|
||||
* @property {number=} stage stage to schedule the tap in (lower runs earlier)
|
||||
*/
|
||||
|
||||
/**
|
||||
* @typedef {TapOptions & { name: string }} Tap
|
||||
*/
|
||||
|
||||
/**
|
||||
* @typedef {Tap & { type: "sync" | "async" | "promise", fn: EXPECTED_FUNCTION }} FullTap
|
||||
*/
|
||||
|
||||
/**
|
||||
* @template E, T
|
||||
* @typedef {(error: E | null, result?: T) => void} Callback
|
||||
*/
|
||||
|
||||
/**
|
||||
* @template E, T
|
||||
* @typedef {(error?: E | null | false, result?: T) => void} InnerCallback
|
||||
*/
|
||||
|
||||
// TODO remove context in the next major release, deprecated
|
||||
/**
|
||||
* @template T
|
||||
* @template R
|
||||
* @template [AdditionalOptions=UnsetAdditionalOptions]
|
||||
* @typedef {object} HookInterceptor
|
||||
* @property {string=} name optional name of the interceptor
|
||||
* @property {((tap: FullTap & IfSet<AdditionalOptions>) => void)=} tap called for each tap before it runs
|
||||
* @property {((...args: EXPECTED_ANY[]) => void)=} call called when the hook is called
|
||||
* @property {((...args: EXPECTED_ANY[]) => void)=} loop called for each loop iteration of looping hooks
|
||||
* @property {((err: Error) => void)=} error called when an error occurs
|
||||
* @property {((result: R) => void)=} result called with the hook's result
|
||||
* @property {(() => void)=} done called when the hook is fully done
|
||||
* @property {((tap: FullTap & IfSet<AdditionalOptions>) => FullTap & IfSet<AdditionalOptions>)=} register called whenever a tap is registered
|
||||
* @property {boolean=} context true to pass a `_context` argument to interceptor callbacks
|
||||
*/
|
||||
|
||||
/**
|
||||
* @typedef {object} UnsetAdditionalOptions
|
||||
* @property {true} _UnsetAdditionalOptions unset
|
||||
*/
|
||||
|
||||
/**
|
||||
* @template T
|
||||
* @template R
|
||||
* @template [AdditionalOptions=UnsetAdditionalOptions]
|
||||
* @typedef {object} CompileOptions
|
||||
* @property {FullTap[]} taps the registered taps
|
||||
* @property {HookInterceptor<T, R, AdditionalOptions>[]} interceptors the registered interceptors
|
||||
* @property {ArgumentNames<AsArray<T>>} args names of the hook arguments (used for code generation)
|
||||
* @property {"sync" | "async" | "promise"} type call type
|
||||
*/
|
||||
|
||||
const deprecateContext = util.deprecate(
|
||||
() => {},
|
||||
"Hook.context is deprecated and will be removed"
|
||||
);
|
||||
|
||||
/**
|
||||
* @template T, R
|
||||
* @callback CallDelegate
|
||||
* @param {...AsArray<T>} args
|
||||
* @returns {R}
|
||||
*/
|
||||
|
||||
/**
|
||||
* @template T, R
|
||||
* @type {CallDelegate<EXPECTED_ANY, EXPECTED_ANY>}
|
||||
* @this {Hook<T, R>}
|
||||
*/
|
||||
function CALL_DELEGATE(...args) {
|
||||
// @ts-expect-error for performance reasons
|
||||
this.call = this._createCall("sync");
|
||||
// @ts-expect-error for performance reasons
|
||||
return this.call(...args);
|
||||
}
|
||||
|
||||
/**
|
||||
* @template T, R
|
||||
* @callback CallAsyncDelegate
|
||||
* @param {...Append<AsArray<T>, Callback<Error, R>>} args
|
||||
* @returns void
|
||||
*/
|
||||
|
||||
/**
|
||||
* @template T, R
|
||||
* @type {CallAsyncDelegate<EXPECTED_ANY, EXPECTED_ANY>}
|
||||
* @this {Hook<T, R>}
|
||||
*/
|
||||
function CALL_ASYNC_DELEGATE(...args) {
|
||||
// @ts-expect-error for performance reasons
|
||||
this.callAsync = this._createCall("async");
|
||||
return this.callAsync(...args);
|
||||
}
|
||||
|
||||
/**
|
||||
* @template T, R
|
||||
* @callback PromiseDeledate
|
||||
* @param {...AsArray<T>} args
|
||||
* @returns {Promise<R>}
|
||||
*/
|
||||
|
||||
/**
|
||||
* @template T, R
|
||||
* @type {PromiseDeledate<EXPECTED_ANY, EXPECTED_ANY>}
|
||||
* @this {Hook<T, R>}
|
||||
*/
|
||||
function PROMISE_DELEGATE(...args) {
|
||||
// @ts-expect-error for performance reasons
|
||||
this.promise = this._createCall("promise");
|
||||
return this.promise(...args);
|
||||
}
|
||||
|
||||
/**
|
||||
* @template T
|
||||
* @template R
|
||||
* @template [AdditionalOptions=UnsetAdditionalOptions]
|
||||
*/
|
||||
class Hook {
|
||||
/**
|
||||
* @param {ArgumentNames<AsArray<T>>=} args argument names of the hook (used for code generation)
|
||||
* @param {string=} name name of the hook
|
||||
*/
|
||||
constructor(
|
||||
args = /** @type {ArgumentNames<AsArray<T>>} */ (
|
||||
/** @type {unknown} */ ([])
|
||||
),
|
||||
name = undefined
|
||||
) {
|
||||
/**
|
||||
* @private
|
||||
* @type {ArgumentNames<AsArray<T>>}
|
||||
*/
|
||||
this._args = args;
|
||||
/** @type {string | undefined} */
|
||||
this.name = name;
|
||||
/** @type {FullTap[]} */
|
||||
this.taps = [];
|
||||
/** @type {HookInterceptor<T, R, AdditionalOptions>[]} */
|
||||
this.interceptors = [];
|
||||
/**
|
||||
* @private
|
||||
* @type {CallDelegate<T, R> | undefined}
|
||||
*/
|
||||
this._call = CALL_DELEGATE;
|
||||
/** @type {CallDelegate<T, R> | undefined} */
|
||||
this.call = CALL_DELEGATE;
|
||||
/**
|
||||
* @private
|
||||
* @type {CallAsyncDelegate<T, R>}
|
||||
*/
|
||||
this._callAsync = CALL_ASYNC_DELEGATE;
|
||||
/** @type {CallAsyncDelegate<T, R>} */
|
||||
this.callAsync = CALL_ASYNC_DELEGATE;
|
||||
/**
|
||||
* @private
|
||||
* @type {PromiseDeledate<T, R>}
|
||||
*/
|
||||
this._promise = PROMISE_DELEGATE;
|
||||
/** @type {PromiseDeledate<T, R>} */
|
||||
this.promise = PROMISE_DELEGATE;
|
||||
/** @type {EXPECTED_FUNCTION[] | undefined} */
|
||||
this._x = undefined;
|
||||
|
||||
// eslint-disable-next-line no-self-assign
|
||||
this.compile = this.compile;
|
||||
// eslint-disable-next-line no-self-assign
|
||||
this.tap = this.tap;
|
||||
// eslint-disable-next-line no-self-assign
|
||||
this.tapAsync = this.tapAsync;
|
||||
// eslint-disable-next-line no-self-assign
|
||||
this.tapPromise = this.tapPromise;
|
||||
}
|
||||
|
||||
/**
|
||||
* @abstract
|
||||
* @param {CompileOptions<T, R, AdditionalOptions>} _options compile options
|
||||
* @returns {EXPECTED_FUNCTION} the compiled call function
|
||||
*/
|
||||
compile(_options) {
|
||||
throw new Error("Abstract: should be overridden");
|
||||
}
|
||||
|
||||
/**
|
||||
* @private
|
||||
* @param {"sync" | "async" | "promise"} type the call type
|
||||
* @returns {EXPECTED_FUNCTION} the compiled call function
|
||||
*/
|
||||
_createCall(type) {
|
||||
return this.compile({
|
||||
taps: this.taps,
|
||||
interceptors: this.interceptors,
|
||||
args: this._args,
|
||||
type
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @private
|
||||
* @param {"sync" | "async" | "promise"} type the tap type
|
||||
* @param {string | (Tap & IfSet<AdditionalOptions>)} options name or full tap options
|
||||
* @param {EXPECTED_FUNCTION} fn callback registered with the tap
|
||||
* @returns {void}
|
||||
*/
|
||||
_tap(type, options, fn) {
|
||||
if (typeof options === "string") {
|
||||
// Fast path: a string options ("name") is by far the most common
|
||||
// case. Build the final descriptor in a single allocation instead
|
||||
// of creating `{ name }` and then `Object.assign`ing it.
|
||||
const name = options.trim();
|
||||
if (name === "") {
|
||||
throw new Error("Missing name for tap");
|
||||
}
|
||||
options =
|
||||
/** @type {FullTap & IfSet<AdditionalOptions>} */
|
||||
({ type, fn, name });
|
||||
} else {
|
||||
if (typeof options !== "object" || options === null) {
|
||||
throw new Error("Invalid tap options");
|
||||
}
|
||||
let { name } = options;
|
||||
if (typeof name === "string") {
|
||||
name = name.trim();
|
||||
}
|
||||
if (typeof name !== "string" || name === "") {
|
||||
throw new Error("Missing name for tap");
|
||||
}
|
||||
if (
|
||||
typeof (/** @type {{ context?: unknown }} */ (options).context) !==
|
||||
"undefined"
|
||||
) {
|
||||
deprecateContext();
|
||||
}
|
||||
// Fast path: only `name` is set. Build the descriptor as a literal
|
||||
// so `_insert` and downstream consumers see the same hidden class
|
||||
// as the string-options path, avoiding a polymorphic call site.
|
||||
// Scan with `for...in` (cheaper than allocating `Object.keys`)
|
||||
// to verify no other user-provided properties exist - e.g.
|
||||
// webpack's `additionalAssets` - otherwise they'd be dropped.
|
||||
let onlyName = true;
|
||||
for (const key in options) {
|
||||
if (key !== "name") {
|
||||
onlyName = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (onlyName) {
|
||||
options =
|
||||
/** @type {FullTap & IfSet<AdditionalOptions>} */
|
||||
({ type, fn, name });
|
||||
} else {
|
||||
options.name = name;
|
||||
// Preserve previous precedence: user-provided keys win over the internal `type`/`fn`.
|
||||
options = Object.assign({ type, fn }, options);
|
||||
}
|
||||
}
|
||||
options = this._runRegisterInterceptors(
|
||||
/** @type {FullTap & IfSet<AdditionalOptions>} */
|
||||
(options)
|
||||
);
|
||||
this._insert(
|
||||
/** @type {FullTap & IfSet<AdditionalOptions>} */
|
||||
(options)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string | (Tap & IfSet<AdditionalOptions>)} options tap name or full tap options
|
||||
* @param {(...args: AsArray<T>) => R} fn the function to register
|
||||
* @returns {void}
|
||||
*/
|
||||
tap(options, fn) {
|
||||
this._tap("sync", options, fn);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string | (Tap & IfSet<AdditionalOptions>)} options tap name or full tap options
|
||||
* @param {(...args: Append<AsArray<T>, InnerCallback<Error, R>>) => void} fn the function to register
|
||||
* @returns {void}
|
||||
*/
|
||||
tapAsync(options, fn) {
|
||||
this._tap("async", options, fn);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string | (Tap & IfSet<AdditionalOptions>)} options tap name or full tap options
|
||||
* @param {(...args: AsArray<T>) => Promise<R>} fn the function to register
|
||||
* @returns {void}
|
||||
*/
|
||||
tapPromise(options, fn) {
|
||||
this._tap("promise", options, fn);
|
||||
}
|
||||
|
||||
/**
|
||||
* @private
|
||||
* @param {FullTap & IfSet<AdditionalOptions>} options the tap descriptor
|
||||
* @returns {FullTap & IfSet<AdditionalOptions>} possibly transformed options
|
||||
*/
|
||||
_runRegisterInterceptors(options) {
|
||||
const { interceptors } = this;
|
||||
const { length } = interceptors;
|
||||
// Common case: no interceptors.
|
||||
if (length === 0) return options;
|
||||
for (let i = 0; i < length; i++) {
|
||||
const interceptor = interceptors[i];
|
||||
if (interceptor.register) {
|
||||
const newOptions = interceptor.register(options);
|
||||
if (newOptions !== undefined) {
|
||||
options = newOptions;
|
||||
}
|
||||
}
|
||||
}
|
||||
return options;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {TapOptions & IfSet<AdditionalOptions>} options the options to merge into each tap
|
||||
* @returns {Omit<this, "call" | "callAsync" | "promise">} a wrapper that pre-applies the options
|
||||
*/
|
||||
withOptions(options) {
|
||||
/**
|
||||
* @param {string | (TapOptions & IfSet<AdditionalOptions>)} opt options
|
||||
* @returns {Tap & IfSet<AdditionalOptions>} merged options
|
||||
*/
|
||||
const mergeOptions = (opt) =>
|
||||
/** @type {Tap & IfSet<AdditionalOptions>} */ (
|
||||
Object.assign(
|
||||
{},
|
||||
options,
|
||||
typeof opt === "string" ? { name: opt } : opt
|
||||
)
|
||||
);
|
||||
|
||||
return /** @type {Omit<this, "call" | "callAsync" | "promise">} */ ({
|
||||
name: this.name,
|
||||
tap: (opt, fn) => this.tap(mergeOptions(opt), fn),
|
||||
tapAsync: (opt, fn) => this.tapAsync(mergeOptions(opt), fn),
|
||||
tapPromise: (opt, fn) => this.tapPromise(mergeOptions(opt), fn),
|
||||
intercept: (interceptor) => this.intercept(interceptor),
|
||||
isUsed: () => this.isUsed(),
|
||||
withOptions: (opt) => this.withOptions(mergeOptions(opt))
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @returns {boolean} true if the hook has any taps or interceptors registered
|
||||
*/
|
||||
isUsed() {
|
||||
return this.taps.length > 0 || this.interceptors.length > 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {HookInterceptor<T, R, AdditionalOptions>} interceptor the interceptor to register
|
||||
* @returns {void}
|
||||
*/
|
||||
intercept(interceptor) {
|
||||
this._resetCompilation();
|
||||
this.interceptors.push(Object.assign({}, interceptor));
|
||||
if (interceptor.register) {
|
||||
for (let i = 0; i < this.taps.length; i++) {
|
||||
this.taps[i] = interceptor.register(
|
||||
/** @type {FullTap & IfSet<AdditionalOptions>} */ (this.taps[i])
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @private
|
||||
* @returns {void}
|
||||
*/
|
||||
_resetCompilation() {
|
||||
this.call = this._call;
|
||||
this.callAsync = this._callAsync;
|
||||
this.promise = this._promise;
|
||||
}
|
||||
|
||||
/**
|
||||
* @private
|
||||
* @param {FullTap & IfSet<AdditionalOptions>} item the tap to insert into the ordered taps list
|
||||
* @returns {void}
|
||||
*/
|
||||
_insert(item) {
|
||||
this._resetCompilation();
|
||||
const { taps } = this;
|
||||
const stage = typeof item.stage === "number" ? item.stage : 0;
|
||||
|
||||
// Fast path: the overwhelmingly common `hook.tap("name", fn)` case
|
||||
// has no `before` and default stage 0. If the list is empty or the
|
||||
// last tap's stage is <= the new item's stage the item belongs at
|
||||
// the end - append in O(1), skipping the Set allocation and the
|
||||
// shift loop.
|
||||
if (!(typeof item.before === "string" || Array.isArray(item.before))) {
|
||||
const n = taps.length;
|
||||
if (n === 0 || (taps[n - 1].stage || 0) <= stage) {
|
||||
taps[n] = item;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
/** @type {Set<string> | undefined} */
|
||||
let before;
|
||||
|
||||
if (typeof item.before === "string") {
|
||||
before = new Set([item.before]);
|
||||
} else if (Array.isArray(item.before)) {
|
||||
before = new Set(item.before);
|
||||
}
|
||||
|
||||
let i = taps.length;
|
||||
|
||||
while (i > 0) {
|
||||
i--;
|
||||
const tap = taps[i];
|
||||
taps[i + 1] = tap;
|
||||
const xStage = tap.stage || 0;
|
||||
if (before) {
|
||||
if (before.has(tap.name)) {
|
||||
before.delete(tap.name);
|
||||
continue;
|
||||
}
|
||||
if (before.size > 0) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
if (xStage > stage) {
|
||||
continue;
|
||||
}
|
||||
i++;
|
||||
break;
|
||||
}
|
||||
taps[i] = item;
|
||||
}
|
||||
}
|
||||
|
||||
Object.setPrototypeOf(Hook.prototype, null);
|
||||
|
||||
module.exports = Hook;
|
||||
+649
@@ -0,0 +1,649 @@
|
||||
/*
|
||||
MIT License http://www.opensource.org/licenses/mit-license.php
|
||||
Author Tobias Koppers @sokra
|
||||
*/
|
||||
"use strict";
|
||||
|
||||
/** @typedef {import("./Hook").EXPECTED_ANY} EXPECTED_ANY */
|
||||
/** @typedef {import("./Hook").EXPECTED_OBJECT} EXPECTED_OBJECT */
|
||||
/** @typedef {import("./Hook").EXPECTED_FUNCTION} EXPECTED_FUNCTION */
|
||||
/** @typedef {import("./Hook").FullTap} FullTap */
|
||||
/** @typedef {import("./Hook").CompileOptions<EXPECTED_ANY, EXPECTED_ANY, EXPECTED_ANY>} CompileOptions */
|
||||
|
||||
/**
|
||||
* @template T, R, AdditionalOptions
|
||||
* @typedef {import("./Hook").HookInterceptor<T, R, AdditionalOptions>} HookInterceptor
|
||||
*/
|
||||
|
||||
/**
|
||||
* @template T, R, AdditionalOptions
|
||||
* @typedef {import("./Hook")<T, R, AdditionalOptions>} Hook
|
||||
*/
|
||||
|
||||
/**
|
||||
* @typedef {(error: string) => string} OnErrorCallback
|
||||
*/
|
||||
|
||||
/**
|
||||
* @typedef {(result: string) => string} OnResultCallback
|
||||
*/
|
||||
|
||||
/**
|
||||
* @typedef {() => string} OnDoneCallback
|
||||
*/
|
||||
|
||||
/**
|
||||
* @typedef {object} ContentOptions
|
||||
* @property {OnErrorCallback} onError generates code for handling an error
|
||||
* @property {OnResultCallback} onResult generates code for handling a result
|
||||
* @property {OnDoneCallback} onDone generates code for completion
|
||||
* @property {boolean=} resultReturns true if the generated function returns a result
|
||||
* @property {boolean=} doneReturns true if the generated function returns when done
|
||||
* @property {boolean=} rethrowIfPossible true to rethrow errors when safe
|
||||
*/
|
||||
|
||||
/**
|
||||
* @typedef {object} CallTapOptions
|
||||
* @property {OnErrorCallback} onError generates code for handling an error
|
||||
* @property {OnResultCallback=} onResult generates code for handling a result
|
||||
* @property {OnDoneCallback=} onDone generates code for completion
|
||||
* @property {boolean=} rethrowIfPossible true to rethrow errors when safe
|
||||
*/
|
||||
|
||||
/**
|
||||
* @typedef {object} CallTapsOptions
|
||||
* @property {((tapIndex: number, error: string, next: () => string, doneBreak: (skipDone: boolean) => string) => string)} onError generates code for handling an error
|
||||
* @property {((tapIndex: number, result: string, next: () => string, doneBreak: (skipDone: boolean) => string) => string)=} onResult generates code for handling a result
|
||||
* @property {OnDoneCallback} onDone generates code for completion
|
||||
* @property {boolean=} rethrowIfPossible true to rethrow errors when safe
|
||||
* @property {boolean=} doneReturns true if the generated function returns when done
|
||||
* @property {boolean=} resultReturns true if the generated function returns a result
|
||||
*/
|
||||
|
||||
/**
|
||||
* @typedef {object} CallTapsParallelOptions
|
||||
* @property {((tapIndex: number, error: string, next: () => string, doneBreak: (skipDone: boolean) => string) => string)} onError generates code for handling an error
|
||||
* @property {((tapIndex: number, result: string, next: () => string, doneBreak: (skipDone: boolean) => string) => string)=} onResult generates code for handling a result
|
||||
* @property {(tapIndex: number, run: () => string, done: () => string, doneBreak: (skipDone: boolean) => string) => string=} onTap options
|
||||
* @property {OnDoneCallback=} onDone generates code for completion
|
||||
* @property {boolean=} rethrowIfPossible true to rethrow errors when safe
|
||||
*/
|
||||
|
||||
class HookCodeFactory {
|
||||
constructor() {
|
||||
/** @type {CompileOptions | undefined} */
|
||||
this.options = undefined;
|
||||
/** @type {string[] | undefined} */
|
||||
this._args = undefined;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {CompileOptions} options compile options
|
||||
* @returns {EXPECTED_FUNCTION} the compiled hook function
|
||||
*/
|
||||
create(options) {
|
||||
this.init(options);
|
||||
/** @type {EXPECTED_FUNCTION} */
|
||||
let fn;
|
||||
switch (options.type) {
|
||||
case "sync":
|
||||
fn = new Function(
|
||||
this.args(),
|
||||
`"use strict";\n${this.header()}${this.contentWithInterceptors({
|
||||
onError: (err) => `throw ${err};\n`,
|
||||
onResult: (result) => `return ${result};\n`,
|
||||
onDone: () => "",
|
||||
resultReturns: true,
|
||||
rethrowIfPossible: true
|
||||
})}`
|
||||
);
|
||||
break;
|
||||
case "async":
|
||||
fn = new Function(
|
||||
this.args({
|
||||
after: "_callback"
|
||||
}),
|
||||
`"use strict";\n${this.header()}${this.contentWithInterceptors({
|
||||
onError: (err) => `_callback(${err});\n`,
|
||||
onResult: (result) => `_callback(null, ${result});\n`,
|
||||
onDone: () => "_callback();\n"
|
||||
})}`
|
||||
);
|
||||
break;
|
||||
case "promise": {
|
||||
let errorHelperUsed = false;
|
||||
const content = this.contentWithInterceptors({
|
||||
onError: (err) => {
|
||||
errorHelperUsed = true;
|
||||
return `_error(${err});\n`;
|
||||
},
|
||||
onResult: (result) => `_resolve(${result});\n`,
|
||||
onDone: () => "_resolve();\n"
|
||||
});
|
||||
let code = "";
|
||||
code += '"use strict";\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((function() { throw _err; })));\n";
|
||||
code += "else\n";
|
||||
code += "_reject(_err);\n";
|
||||
code += "};\n";
|
||||
}
|
||||
code += content;
|
||||
if (errorHelperUsed) {
|
||||
code += "_sync = false;\n";
|
||||
}
|
||||
code += "}));\n";
|
||||
fn = new Function(this.args(), code);
|
||||
break;
|
||||
}
|
||||
}
|
||||
this.deinit();
|
||||
return fn;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {Hook<EXPECTED_ANY, EXPECTED_ANY, EXPECTED_ANY>} instance the hook instance to attach the resolved tap function array to
|
||||
* @param {CompileOptions} options compile options containing the taps
|
||||
* @returns {void}
|
||||
*/
|
||||
setup(instance, options) {
|
||||
const { taps } = options;
|
||||
const { length } = taps;
|
||||
/** @type {EXPECTED_FUNCTION[]}} */
|
||||
const fns = Array.from({ length });
|
||||
for (let i = 0; i < length; i++) {
|
||||
fns[i] = taps[i].fn;
|
||||
}
|
||||
instance._x = fns;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {CompileOptions} options compile options
|
||||
* @returns {void}
|
||||
*/
|
||||
init(options) {
|
||||
this.options = options;
|
||||
// `_args` is only read (length / join / [0]) - never mutated - so we
|
||||
// can share the caller's array directly instead of paying for a copy
|
||||
// on every compile.
|
||||
this._args = /** @type {string[]} */ (
|
||||
/** @type {unknown} */ (options.args)
|
||||
);
|
||||
/** @type {string | undefined} */
|
||||
this._joinedArgs = undefined;
|
||||
}
|
||||
|
||||
/**
|
||||
* @returns {void}
|
||||
*/
|
||||
deinit() {
|
||||
this.options = undefined;
|
||||
this._args = undefined;
|
||||
this._joinedArgs = undefined;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {ContentOptions} options content generation options
|
||||
* @returns {string} generated code with interceptor calls wrapped around content
|
||||
*/
|
||||
contentWithInterceptors(options) {
|
||||
if (/** @type {CompileOptions} */ (this.options).interceptors.length > 0) {
|
||||
const { onError, onResult, onDone } = options;
|
||||
let code = "";
|
||||
const opts = /** @type {CompileOptions} */ (this.options);
|
||||
for (let i = 0; i < opts.interceptors.length; i++) {
|
||||
const interceptor = opts.interceptors[i];
|
||||
if (interceptor.call) {
|
||||
code += `${this.getInterceptor(i)}.call(${this.args({
|
||||
before: interceptor.context ? "_context" : undefined
|
||||
})});\n`;
|
||||
}
|
||||
}
|
||||
// `content` is intentionally not declared on the base class - it's
|
||||
// abstract and provided by every subclass. Skipping the runtime
|
||||
// stub keeps the prototype chain mononomorphic on the hot compile
|
||||
// path: subclass `content` is the only one V8 sees, no shadowing.
|
||||
// @ts-expect-error abstract method, provided by subclasses for performance reasons
|
||||
code += this.content(
|
||||
Object.assign(options, {
|
||||
onError:
|
||||
onError &&
|
||||
/** @type {OnErrorCallback} */
|
||||
(
|
||||
(err) => {
|
||||
let code = "";
|
||||
for (let i = 0; i < opts.interceptors.length; i++) {
|
||||
const interceptor = opts.interceptors[i];
|
||||
if (interceptor.error) {
|
||||
code += `${this.getInterceptor(i)}.error(${err});\n`;
|
||||
}
|
||||
}
|
||||
code += onError(err);
|
||||
return code;
|
||||
}
|
||||
),
|
||||
onResult:
|
||||
onResult &&
|
||||
/** @type {OnResultCallback} */
|
||||
(
|
||||
(result) => {
|
||||
let code = "";
|
||||
for (let i = 0; i < opts.interceptors.length; i++) {
|
||||
const interceptor = opts.interceptors[i];
|
||||
if (interceptor.result) {
|
||||
code += `${this.getInterceptor(i)}.result(${result});\n`;
|
||||
}
|
||||
}
|
||||
code += onResult(result);
|
||||
return code;
|
||||
}
|
||||
),
|
||||
onDone:
|
||||
onDone &&
|
||||
/** @type {OnDoneCallback} */
|
||||
(
|
||||
() => {
|
||||
let code = "";
|
||||
for (let i = 0; i < opts.interceptors.length; i++) {
|
||||
const interceptor = opts.interceptors[i];
|
||||
if (interceptor.done) {
|
||||
code += `${this.getInterceptor(i)}.done();\n`;
|
||||
}
|
||||
}
|
||||
code += onDone();
|
||||
return code;
|
||||
}
|
||||
)
|
||||
})
|
||||
);
|
||||
return code;
|
||||
}
|
||||
// @ts-expect-error abstract method, provided by subclasses for performance reasons
|
||||
return this.content(options);
|
||||
}
|
||||
|
||||
/**
|
||||
* @returns {string} generated header preamble shared across all call types
|
||||
*/
|
||||
header() {
|
||||
let code = "";
|
||||
code += this.needContext() ? "var _context = {};\n" : "var _context;\n";
|
||||
code += "var _x = this._x;\n";
|
||||
if (/** @type {CompileOptions} */ (this.options).interceptors.length > 0) {
|
||||
code += "var _taps = this.taps;\n";
|
||||
code += "var _interceptors = this.interceptors;\n";
|
||||
}
|
||||
return code;
|
||||
}
|
||||
|
||||
/**
|
||||
* @returns {boolean} true if any tap requested a `_context`
|
||||
*/
|
||||
needContext() {
|
||||
const { taps } = /** @type {CompileOptions} */ (this.options);
|
||||
for (let i = 0; i < taps.length; i++) {
|
||||
if (/** @type {{ context?: boolean }} */ (taps[i]).context) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {number} tapIndex index of the tap to call
|
||||
* @param {CallTapOptions} options content generation options
|
||||
* @returns {string} generated code that invokes the tap
|
||||
*/
|
||||
callTap(tapIndex, { onError, onResult, onDone, rethrowIfPossible }) {
|
||||
let code = "";
|
||||
let hasTapCached = false;
|
||||
const opts = /** @type {CompileOptions} */ (this.options);
|
||||
for (let i = 0; i < opts.interceptors.length; i++) {
|
||||
const interceptor = opts.interceptors[i];
|
||||
if (interceptor.tap) {
|
||||
if (!hasTapCached) {
|
||||
code += `var _tap${tapIndex} = ${this.getTap(tapIndex)};\n`;
|
||||
hasTapCached = true;
|
||||
}
|
||||
code += `${this.getInterceptor(i)}.tap(${
|
||||
interceptor.context ? "_context, " : ""
|
||||
}_tap${tapIndex});\n`;
|
||||
}
|
||||
}
|
||||
code += `var _fn${tapIndex} = ${this.getTapFn(tapIndex)};\n`;
|
||||
const tap =
|
||||
/** @type {FullTap & { context?: boolean }} */
|
||||
(opts.taps[tapIndex]);
|
||||
switch (tap.type) {
|
||||
case "sync":
|
||||
if (!rethrowIfPossible) {
|
||||
code += `var _hasError${tapIndex} = false;\n`;
|
||||
code += "try {\n";
|
||||
}
|
||||
if (onResult) {
|
||||
code += `var _result${tapIndex} = _fn${tapIndex}(${this.args({
|
||||
before: tap.context ? "_context" : undefined
|
||||
})});\n`;
|
||||
} else {
|
||||
code += `_fn${tapIndex}(${this.args({
|
||||
before: tap.context ? "_context" : undefined
|
||||
})});\n`;
|
||||
}
|
||||
if (!rethrowIfPossible) {
|
||||
code += "} catch(_err) {\n";
|
||||
code += `_hasError${tapIndex} = true;\n`;
|
||||
code += onError("_err");
|
||||
code += "}\n";
|
||||
code += `if(!_hasError${tapIndex}) {\n`;
|
||||
}
|
||||
if (onResult) {
|
||||
code += onResult(`_result${tapIndex}`);
|
||||
}
|
||||
if (onDone) {
|
||||
code += onDone();
|
||||
}
|
||||
if (!rethrowIfPossible) {
|
||||
code += "}\n";
|
||||
}
|
||||
break;
|
||||
case "async": {
|
||||
let cbCode = "";
|
||||
cbCode += onResult
|
||||
? `(function(_err${tapIndex}, _result${tapIndex}) {\n`
|
||||
: `(function(_err${tapIndex}) {\n`;
|
||||
cbCode += `if(_err${tapIndex}) {\n`;
|
||||
cbCode += onError(`_err${tapIndex}`);
|
||||
cbCode += "} else {\n";
|
||||
if (onResult) {
|
||||
cbCode += onResult(`_result${tapIndex}`);
|
||||
}
|
||||
if (onDone) {
|
||||
cbCode += onDone();
|
||||
}
|
||||
cbCode += "}\n";
|
||||
cbCode += "})";
|
||||
code += `_fn${tapIndex}(${this.args({
|
||||
before: tap.context ? "_context" : undefined,
|
||||
after: cbCode
|
||||
})});\n`;
|
||||
break;
|
||||
}
|
||||
case "promise":
|
||||
code += `var _hasResult${tapIndex} = false;\n`;
|
||||
code += `var _promise${tapIndex} = _fn${tapIndex}(${this.args({
|
||||
before: tap.context ? "_context" : undefined
|
||||
})});\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((function(_result${tapIndex}) {\n`;
|
||||
code += `_hasResult${tapIndex} = true;\n`;
|
||||
if (onResult) {
|
||||
code += onResult(`_result${tapIndex}`);
|
||||
}
|
||||
if (onDone) {
|
||||
code += onDone();
|
||||
}
|
||||
code += `}), function(_err${tapIndex}) {\n`;
|
||||
code += `if(_hasResult${tapIndex}) throw _err${tapIndex};\n`;
|
||||
code += onError(
|
||||
`!_err${tapIndex} ? new Error('Tap function (tapPromise) rejects "' + _err${tapIndex} + '" value') : _err${tapIndex}`
|
||||
);
|
||||
code += "});\n";
|
||||
break;
|
||||
}
|
||||
return code;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {CallTapsOptions} options series-call options
|
||||
* @returns {string} generated code calling all taps in series
|
||||
*/
|
||||
callTapsSeries({
|
||||
onError,
|
||||
onResult,
|
||||
resultReturns,
|
||||
onDone,
|
||||
doneReturns,
|
||||
rethrowIfPossible
|
||||
}) {
|
||||
const { taps } = /** @type {CompileOptions} */ (this.options);
|
||||
const tapsLength = taps.length;
|
||||
if (tapsLength === 0) return onDone();
|
||||
// Inlined findIndex to avoid the callback allocation.
|
||||
let firstAsync = -1;
|
||||
for (let i = 0; i < tapsLength; i++) {
|
||||
if (taps[i].type !== "sync") {
|
||||
firstAsync = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
const somethingReturns = resultReturns || doneReturns;
|
||||
// doneBreak doesn't depend on the loop variable - hoist to allocate once.
|
||||
/**
|
||||
* @param {boolean} skipDone true when need to skip done, otherwise false
|
||||
* @returns {string} code
|
||||
*/
|
||||
const doneBreak = (skipDone) => {
|
||||
if (skipDone) return "";
|
||||
return onDone();
|
||||
};
|
||||
let code = "";
|
||||
let current = onDone;
|
||||
let unrollCounter = 0;
|
||||
for (let j = tapsLength - 1; j >= 0; j--) {
|
||||
const i = j;
|
||||
const unroll =
|
||||
current !== onDone && (taps[i].type !== "sync" || unrollCounter++ > 20);
|
||||
if (unroll) {
|
||||
unrollCounter = 0;
|
||||
code += `function _next${i}() {\n`;
|
||||
code += current();
|
||||
code += "}\n";
|
||||
current = () => `${somethingReturns ? "return " : ""}_next${i}();\n`;
|
||||
}
|
||||
const done = current;
|
||||
const content = this.callTap(i, {
|
||||
onError: (error) => onError(i, error, done, doneBreak),
|
||||
onResult:
|
||||
onResult && ((result) => onResult(i, result, done, doneBreak)),
|
||||
onDone: !onResult ? done : undefined,
|
||||
rethrowIfPossible:
|
||||
rethrowIfPossible && (firstAsync < 0 || i < firstAsync)
|
||||
});
|
||||
current = () => content;
|
||||
}
|
||||
code += current();
|
||||
return code;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {CallTapsOptions} options looping-call options
|
||||
* @returns {string} generated code that calls taps in a loop
|
||||
*/
|
||||
callTapsLooping({ onError, onDone, rethrowIfPossible }) {
|
||||
const opts = /** @type {CompileOptions} */ (this.options);
|
||||
if (opts.taps.length === 0) return onDone();
|
||||
const syncOnly = opts.taps.every((t) => t.type === "sync");
|
||||
let code = "";
|
||||
if (!syncOnly) {
|
||||
code += "var _looper = (function() {\n";
|
||||
code += "var _loopAsync = false;\n";
|
||||
}
|
||||
code += "var _loop;\n";
|
||||
code += "do {\n";
|
||||
code += "_loop = false;\n";
|
||||
for (let i = 0; i < opts.interceptors.length; i++) {
|
||||
const interceptor = opts.interceptors[i];
|
||||
if (interceptor.loop) {
|
||||
code += `${this.getInterceptor(i)}.loop(${this.args({
|
||||
before: interceptor.context ? "_context" : undefined
|
||||
})});\n`;
|
||||
}
|
||||
}
|
||||
code += this.callTapsSeries({
|
||||
onError,
|
||||
onResult: (i, result, next, doneBreak) => {
|
||||
let code = "";
|
||||
code += `if(${result} !== undefined) {\n`;
|
||||
code += "_loop = true;\n";
|
||||
if (!syncOnly) code += "if(_loopAsync) _looper();\n";
|
||||
code += doneBreak(true);
|
||||
code += "} else {\n";
|
||||
code += next();
|
||||
code += "}\n";
|
||||
return code;
|
||||
},
|
||||
onDone:
|
||||
onDone &&
|
||||
(() => {
|
||||
let code = "";
|
||||
code += "if(!_loop) {\n";
|
||||
code += onDone();
|
||||
code += "}\n";
|
||||
return code;
|
||||
}),
|
||||
rethrowIfPossible: rethrowIfPossible && syncOnly
|
||||
});
|
||||
code += "} while(_loop);\n";
|
||||
if (!syncOnly) {
|
||||
code += "_loopAsync = true;\n";
|
||||
code += "});\n";
|
||||
code += "_looper();\n";
|
||||
}
|
||||
return code;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {CallTapsParallelOptions} options parallel-call options
|
||||
* @returns {string} generated code that calls all taps in parallel
|
||||
*/
|
||||
callTapsParallel({
|
||||
onError,
|
||||
onResult,
|
||||
onDone,
|
||||
rethrowIfPossible,
|
||||
onTap = (i, run) => run()
|
||||
}) {
|
||||
const { taps } = /** @type {CompileOptions} */ (this.options);
|
||||
const tapsLength = taps.length;
|
||||
if (tapsLength <= 1) {
|
||||
return this.callTapsSeries({
|
||||
onError,
|
||||
onResult,
|
||||
// @ts-expect-error is it a bug?
|
||||
onDone,
|
||||
rethrowIfPossible
|
||||
});
|
||||
}
|
||||
// done and doneBreak don't depend on the loop variable - hoist them
|
||||
// so they're allocated once per compile instead of once per tap.
|
||||
const done = () => {
|
||||
if (onDone) return "if(--_counter === 0) _done();\n";
|
||||
return "--_counter;";
|
||||
};
|
||||
/**
|
||||
* @param {boolean} skipDone true when need to skip done, otherwise false
|
||||
* @returns {string} code
|
||||
*/
|
||||
const doneBreak = (skipDone) => {
|
||||
if (skipDone || !onDone) return "_counter = 0;\n";
|
||||
return "_counter = 0;\n_done();\n";
|
||||
};
|
||||
let code = "";
|
||||
code += "do {\n";
|
||||
code += `var _counter = ${tapsLength};\n`;
|
||||
if (onDone) {
|
||||
code += "var _done = (function() {\n";
|
||||
code += onDone();
|
||||
code += "});\n";
|
||||
}
|
||||
for (let i = 0; i < tapsLength; i++) {
|
||||
code += "if(_counter <= 0) break;\n";
|
||||
code += onTap(
|
||||
i,
|
||||
() =>
|
||||
this.callTap(i, {
|
||||
onError: (error) => {
|
||||
let code = "";
|
||||
code += "if(_counter > 0) {\n";
|
||||
code += onError(i, error, done, doneBreak);
|
||||
code += "}\n";
|
||||
return code;
|
||||
},
|
||||
onResult:
|
||||
onResult &&
|
||||
((result) => {
|
||||
let code = "";
|
||||
code += "if(_counter > 0) {\n";
|
||||
code += onResult(i, result, done, doneBreak);
|
||||
code += "}\n";
|
||||
return code;
|
||||
}),
|
||||
onDone: !onResult ? () => done() : undefined,
|
||||
rethrowIfPossible
|
||||
}),
|
||||
done,
|
||||
doneBreak
|
||||
);
|
||||
}
|
||||
code += "} while(false);\n";
|
||||
return code;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {{ before?: string, after?: string }=} options optional arguments to prepend or append
|
||||
* @returns {string} comma-separated argument list as JS source
|
||||
*/
|
||||
args({ before, after } = {}) {
|
||||
// Hot during code generation. Join `_args` once and cache the result,
|
||||
// then build the customized variants via string concat instead of
|
||||
// allocating temporary `[before, ...allArgs]` / `[...allArgs, after]`
|
||||
// arrays and re-joining.
|
||||
let joined = this._joinedArgs;
|
||||
if (joined === undefined) {
|
||||
joined =
|
||||
/** @type {string[]} */ (this._args).length === 0
|
||||
? ""
|
||||
: /** @type {string[]} */ (this._args).join(", ");
|
||||
this._joinedArgs = joined;
|
||||
}
|
||||
if (!before && !after) return joined;
|
||||
if (joined.length === 0) {
|
||||
if (before && after) return `${before}, ${after}`;
|
||||
return /** @type {string} */ (before || after);
|
||||
}
|
||||
if (before && after) return `${before}, ${joined}, ${after}`;
|
||||
if (before) return `${before}, ${joined}`;
|
||||
return `${joined}, ${after}`;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {number} idx tap index
|
||||
* @returns {string} JS expression that resolves to the registered tap function at index `idx`
|
||||
*/
|
||||
getTapFn(idx) {
|
||||
return `_x[${idx}]`;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {number} idx tap index
|
||||
* @returns {string} JS expression that resolves to the tap descriptor at index `idx`
|
||||
*/
|
||||
getTap(idx) {
|
||||
return `_taps[${idx}]`;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {number} idx interceptor index
|
||||
* @returns {string} JS expression that resolves to the interceptor at index `idx`
|
||||
*/
|
||||
getInterceptor(idx) {
|
||||
return `_interceptors[${idx}]`;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = HookCodeFactory;
|
||||
+120
@@ -0,0 +1,120 @@
|
||||
/*
|
||||
MIT License http://www.opensource.org/licenses/mit-license.php
|
||||
Author Tobias Koppers @sokra
|
||||
*/
|
||||
"use strict";
|
||||
|
||||
const util = require("util");
|
||||
|
||||
/** @typedef {import("./Hook").EXPECTED_ANY} EXPECTED_ANY */
|
||||
|
||||
/**
|
||||
* @template H
|
||||
* @template [K=EXPECTED_ANY]
|
||||
* @typedef {(key: K) => H} HookFactory
|
||||
*/
|
||||
|
||||
/**
|
||||
* @template H
|
||||
* @template [K=EXPECTED_ANY]
|
||||
* @typedef {object} HookMapInterceptor
|
||||
* @property {((key: K, hook: H) => H)=} factory called when a hook is created for a new key
|
||||
*/
|
||||
|
||||
/**
|
||||
* @template H, K
|
||||
* @param {K} key key passed through unchanged
|
||||
* @param {H} hook hook returned unchanged
|
||||
* @returns {H} the hook unchanged
|
||||
*/
|
||||
const defaultFactory = (key, hook) => hook;
|
||||
|
||||
/**
|
||||
* @template H
|
||||
*/
|
||||
class HookMap {
|
||||
/**
|
||||
* @param {HookFactory<H>} factory factory creating new hooks for unknown keys
|
||||
* @param {string=} name name of the hook map
|
||||
*/
|
||||
constructor(factory, name = undefined) {
|
||||
/** @type {Map<EXPECTED_ANY, H>} */
|
||||
this._map = new Map();
|
||||
/** @type {string | undefined} */
|
||||
this.name = name;
|
||||
/** @type {HookFactory<H>} */
|
||||
this._factory = factory;
|
||||
/** @type {HookMapInterceptor<H>[]} */
|
||||
this._interceptors = [];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {EXPECTED_ANY} key the key to look up
|
||||
* @returns {H | undefined} the hook stored for `key`, if any
|
||||
*/
|
||||
get(key) {
|
||||
return this._map.get(key);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {EXPECTED_ANY} key the key to look up or create
|
||||
* @returns {H} the hook stored for `key`, creating one via the factory if missing
|
||||
*/
|
||||
for(key) {
|
||||
// Hot path: inline the map lookup to skip the `this.get(key)`
|
||||
// indirection. This gets hit on every hook access in consumers
|
||||
// like webpack.
|
||||
const map = this._map;
|
||||
const hook = map.get(key);
|
||||
if (hook !== undefined) {
|
||||
return hook;
|
||||
}
|
||||
let newHook = this._factory(key);
|
||||
const interceptors = this._interceptors;
|
||||
for (let i = 0; i < interceptors.length; i++) {
|
||||
newHook =
|
||||
/** @type {NonNullable<HookMapInterceptor<H>["factory"]>} */
|
||||
(interceptors[i].factory)(key, newHook);
|
||||
}
|
||||
map.set(key, newHook);
|
||||
return newHook;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {HookMapInterceptor<H>} interceptor the interceptor to register
|
||||
* @returns {void}
|
||||
*/
|
||||
intercept(interceptor) {
|
||||
this._interceptors.push(
|
||||
Object.assign(
|
||||
{
|
||||
factory: defaultFactory
|
||||
},
|
||||
interceptor
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
HookMap.prototype.tap =
|
||||
// @ts-expect-error deprecated
|
||||
util.deprecate(function tap(key, options, fn) {
|
||||
// @ts-expect-error deprecated
|
||||
return this.for(key).tap(options, fn);
|
||||
}, "HookMap#tap(key,…) is deprecated. Use HookMap#for(key).tap(…) instead.");
|
||||
|
||||
HookMap.prototype.tapAsync =
|
||||
// @ts-expect-error deprecated
|
||||
util.deprecate(function tapAsync(key, options, fn) {
|
||||
// @ts-expect-error deprecated
|
||||
return this.for(key).tapAsync(options, fn);
|
||||
}, "HookMap#tapAsync(key,…) is deprecated. Use HookMap#for(key).tapAsync(…) instead.");
|
||||
|
||||
HookMap.prototype.tapPromise =
|
||||
// @ts-expect-error deprecated
|
||||
util.deprecate(function tapPromise(key, options, fn) {
|
||||
// @ts-expect-error deprecated
|
||||
return this.for(key).tapPromise(options, fn);
|
||||
}, "HookMap#tapPromise(key,…) is deprecated. Use HookMap#for(key).tapPromise(…) instead.");
|
||||
|
||||
module.exports = HookMap;
|
||||
+137
@@ -0,0 +1,137 @@
|
||||
/*
|
||||
MIT License http://www.opensource.org/licenses/mit-license.php
|
||||
Author Tobias Koppers @sokra
|
||||
*/
|
||||
"use strict";
|
||||
|
||||
/** @typedef {import("./Hook").EXPECTED_ANY} EXPECTED_ANY */
|
||||
/** @typedef {import("./Hook").EXPECTED_FUNCTION} EXPECTED_FUNCTION */
|
||||
/** @typedef {import("./Hook").Tap} Tap */
|
||||
/** @typedef {import("./Hook").TapOptions} TapOptions */
|
||||
|
||||
/**
|
||||
* @template T
|
||||
* @typedef {import("./Hook").AsArray<T>} AsArray
|
||||
*/
|
||||
|
||||
/**
|
||||
* @template {EXPECTED_ANY[]} T
|
||||
* @template U
|
||||
* @typedef {import("./Hook").Append<T, U>} Append
|
||||
*/
|
||||
|
||||
/**
|
||||
* @template E, T
|
||||
* @typedef {import("./Hook").InnerCallback<E, T>} InnerCallback
|
||||
*/
|
||||
|
||||
/**
|
||||
* @template T
|
||||
* @template [R=void]
|
||||
* @template [AdditionalOptions=object]
|
||||
* @typedef {import("./Hook")<T, R, AdditionalOptions>} Hook
|
||||
*/
|
||||
|
||||
/**
|
||||
* @template T, R, AdditionalOptions
|
||||
* @typedef {import("./Hook").HookInterceptor<T, R, AdditionalOptions>} HookInterceptor
|
||||
*/
|
||||
|
||||
/**
|
||||
* @template {Hook<EXPECTED_ANY>} H
|
||||
*/
|
||||
class MultiHook {
|
||||
/**
|
||||
* @param {H[]} hooks the underlying hooks to multiplex tap calls across
|
||||
* @param {string=} name name of the multi hook
|
||||
*/
|
||||
constructor(hooks, name = undefined) {
|
||||
/** @type {H[]} */
|
||||
this.hooks = hooks;
|
||||
/** @type {string | undefined} */
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
/**
|
||||
* @template T, R
|
||||
* @param {string | Tap} options tap name or full tap options
|
||||
* @param {(...args: AsArray<T>) => R} fn the function to register
|
||||
* @returns {void}
|
||||
*/
|
||||
tap(options, fn) {
|
||||
const { hooks } = this;
|
||||
for (let i = 0; i < hooks.length; i++) {
|
||||
hooks[i].tap(options, fn);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @template T, R
|
||||
* @param {string | Tap} options tap name or full tap options
|
||||
* @param {(...args: Append<AsArray<T>, InnerCallback<Error, R>>) => void} fn the function to register
|
||||
* @returns {void}
|
||||
*/
|
||||
tapAsync(options, fn) {
|
||||
const { hooks } = this;
|
||||
for (let i = 0; i < hooks.length; i++) {
|
||||
hooks[i].tapAsync(options, fn);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @template T, R
|
||||
* @param {string | Tap} options tap name or full tap options
|
||||
* @param {(...args: AsArray<T>) => Promise<R>} fn the function to register
|
||||
* @returns {void}
|
||||
*/
|
||||
tapPromise(options, fn) {
|
||||
const { hooks } = this;
|
||||
for (let i = 0; i < hooks.length; i++) {
|
||||
hooks[i].tapPromise(options, /** @type {EXPECTED_ANY} */ (fn));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @returns {boolean} true if any of the underlying hooks reports usage
|
||||
*/
|
||||
isUsed() {
|
||||
const { hooks } = this;
|
||||
for (let i = 0; i < hooks.length; i++) {
|
||||
if (hooks[i].isUsed()) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {HookInterceptor<EXPECTED_ANY, EXPECTED_ANY, EXPECTED_ANY>} interceptor the interceptor to register
|
||||
* @returns {void}
|
||||
*/
|
||||
intercept(interceptor) {
|
||||
const { hooks } = this;
|
||||
for (let i = 0; i < hooks.length; i++) {
|
||||
hooks[i].intercept(interceptor);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {TapOptions} options the options to merge into each tap
|
||||
* @returns {MultiHook<H>} a new MultiHook wrapping each underlying hook with the options
|
||||
*/
|
||||
withOptions(options) {
|
||||
return /** @type {MultiHook<H>} */ (
|
||||
new MultiHook(
|
||||
this.hooks.map(
|
||||
(hook) =>
|
||||
/** @type {H} */ (
|
||||
/** @type {unknown} */ (hook.withOptions(options))
|
||||
)
|
||||
),
|
||||
this.name
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = MultiHook;
|
||||
+100
@@ -0,0 +1,100 @@
|
||||
/*
|
||||
MIT License http://www.opensource.org/licenses/mit-license.php
|
||||
Author Tobias Koppers @sokra
|
||||
*/
|
||||
"use strict";
|
||||
|
||||
const Hook = require("./Hook");
|
||||
const HookCodeFactory = require("./HookCodeFactory");
|
||||
|
||||
/** @typedef {import("./Hook").EXPECTED_ANY} EXPECTED_ANY */
|
||||
/** @typedef {import("./Hook").EXPECTED_FUNCTION} EXPECTED_FUNCTION */
|
||||
/** @typedef {import("./Hook").UnsetAdditionalOptions} UnsetAdditionalOptions */
|
||||
/** @typedef {import("./Hook").CompileOptions<EXPECTED_ANY, EXPECTED_ANY, EXPECTED_ANY>} CompileOptions */
|
||||
/** @typedef {import("./HookCodeFactory").ContentOptions} ContentOptions */
|
||||
|
||||
/**
|
||||
* @template T
|
||||
* @typedef {import("./Hook").AsArray<T>} AsArray
|
||||
*/
|
||||
/**
|
||||
* @template {EXPECTED_ANY[]} T
|
||||
* @typedef {import("./Hook").ArgumentNames<T>} ArgumentNames
|
||||
*/
|
||||
|
||||
/**
|
||||
* Mirror of the `SyncBailHook` class declared in `tapable.d.ts` for use as
|
||||
* the factory function's return type, so `new SyncBailHook(...)` resolves to
|
||||
* the specific subclass type instead of the generic `Hook`.
|
||||
* @template T
|
||||
* @template R
|
||||
* @template [AdditionalOptions=UnsetAdditionalOptions]
|
||||
* @typedef {import("../tapable").SyncBailHook<T, R, AdditionalOptions>} SyncBailHookType
|
||||
*/
|
||||
|
||||
class SyncBailHookCodeFactory extends HookCodeFactory {
|
||||
/**
|
||||
* @param {ContentOptions} options content generation options
|
||||
* @returns {string} generated body code
|
||||
*/
|
||||
content({ onError, onResult, resultReturns, onDone, rethrowIfPossible }) {
|
||||
return this.callTapsSeries({
|
||||
onError: (i, err) => onError(err),
|
||||
onResult: (i, result, next) =>
|
||||
`if(${result} !== undefined) {\n${onResult(
|
||||
result
|
||||
)};\n} else {\n${next()}}\n`,
|
||||
resultReturns,
|
||||
onDone,
|
||||
rethrowIfPossible
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
const factory = new SyncBailHookCodeFactory();
|
||||
|
||||
const TAP_ASYNC = () => {
|
||||
throw new Error("tapAsync is not supported on a SyncBailHook");
|
||||
};
|
||||
|
||||
const TAP_PROMISE = () => {
|
||||
throw new Error("tapPromise is not supported on a SyncBailHook");
|
||||
};
|
||||
|
||||
/**
|
||||
* @this {Hook<EXPECTED_ANY, EXPECTED_ANY, EXPECTED_ANY>}
|
||||
* @param {CompileOptions} options compile options
|
||||
* @returns {EXPECTED_FUNCTION} the compiled call function
|
||||
*/
|
||||
function COMPILE(options) {
|
||||
factory.setup(this, options);
|
||||
return factory.create(options);
|
||||
}
|
||||
|
||||
/**
|
||||
* @constructor
|
||||
* @template T
|
||||
* @template R
|
||||
* @template [AdditionalOptions=UnsetAdditionalOptions]
|
||||
* @param {ArgumentNames<AsArray<T>>=} args argument names of the hook
|
||||
* @param {string=} name name of the hook
|
||||
* @returns {SyncBailHookType<T, R, AdditionalOptions>} a new SyncBailHook instance
|
||||
*/
|
||||
function SyncBailHook(
|
||||
args = /** @type {ArgumentNames<AsArray<T>>} */ (/** @type {unknown} */ ([])),
|
||||
name = undefined
|
||||
) {
|
||||
const hook = new Hook(args, name);
|
||||
hook.constructor = SyncBailHook;
|
||||
hook.tapAsync = TAP_ASYNC;
|
||||
hook.tapPromise = TAP_PROMISE;
|
||||
hook.compile = COMPILE;
|
||||
return /** @type {SyncBailHookType<T, R, AdditionalOptions>} */ (
|
||||
/** @type {unknown} */ (hook)
|
||||
);
|
||||
}
|
||||
|
||||
/** @type {EXPECTED_ANY} */
|
||||
SyncBailHook.prototype = null;
|
||||
|
||||
module.exports = SyncBailHook;
|
||||
+95
@@ -0,0 +1,95 @@
|
||||
/*
|
||||
MIT License http://www.opensource.org/licenses/mit-license.php
|
||||
Author Tobias Koppers @sokra
|
||||
*/
|
||||
"use strict";
|
||||
|
||||
const Hook = require("./Hook");
|
||||
const HookCodeFactory = require("./HookCodeFactory");
|
||||
|
||||
/** @typedef {import("./Hook").EXPECTED_ANY} EXPECTED_ANY */
|
||||
/** @typedef {import("./Hook").EXPECTED_FUNCTION} EXPECTED_FUNCTION */
|
||||
/** @typedef {import("./Hook").UnsetAdditionalOptions} UnsetAdditionalOptions */
|
||||
/** @typedef {import("./Hook").CompileOptions<EXPECTED_ANY, EXPECTED_ANY, EXPECTED_ANY>} CompileOptions */
|
||||
/** @typedef {import("./HookCodeFactory").ContentOptions} ContentOptions */
|
||||
|
||||
/**
|
||||
* @template T
|
||||
* @typedef {import("./Hook").AsArray<T>} AsArray
|
||||
*/
|
||||
/**
|
||||
* @template {EXPECTED_ANY[]} T
|
||||
* @typedef {import("./Hook").ArgumentNames<T>} ArgumentNames
|
||||
*/
|
||||
|
||||
/**
|
||||
* Mirror of the `SyncHook` class declared in `tapable.d.ts` for use as the
|
||||
* factory function's return type, so `new SyncHook(...)` resolves to the
|
||||
* specific subclass type instead of the generic `Hook`.
|
||||
* @template T
|
||||
* @template [R=void]
|
||||
* @template [AdditionalOptions=UnsetAdditionalOptions]
|
||||
* @typedef {import("../tapable").SyncHook<T, R, AdditionalOptions>} SyncHookType
|
||||
*/
|
||||
|
||||
class SyncHookCodeFactory extends HookCodeFactory {
|
||||
/**
|
||||
* @param {ContentOptions} options content generation options
|
||||
* @returns {string} generated body code
|
||||
*/
|
||||
content({ onError, onDone, rethrowIfPossible }) {
|
||||
return this.callTapsSeries({
|
||||
onError: (i, err) => onError(err),
|
||||
onDone,
|
||||
rethrowIfPossible
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
const factory = new SyncHookCodeFactory();
|
||||
|
||||
const TAP_ASYNC = () => {
|
||||
throw new Error("tapAsync is not supported on a SyncHook");
|
||||
};
|
||||
|
||||
const TAP_PROMISE = () => {
|
||||
throw new Error("tapPromise is not supported on a SyncHook");
|
||||
};
|
||||
|
||||
/**
|
||||
* @this {Hook<EXPECTED_ANY, EXPECTED_ANY, EXPECTED_ANY>}
|
||||
* @param {CompileOptions} options compile options
|
||||
* @returns {EXPECTED_FUNCTION} the compiled call function
|
||||
*/
|
||||
function COMPILE(options) {
|
||||
factory.setup(this, options);
|
||||
return factory.create(options);
|
||||
}
|
||||
|
||||
/**
|
||||
* @constructor
|
||||
* @template T
|
||||
* @template [R=void]
|
||||
* @template [AdditionalOptions=UnsetAdditionalOptions]
|
||||
* @param {ArgumentNames<AsArray<T>>=} args argument names of the hook
|
||||
* @param {string=} name name of the hook
|
||||
* @returns {SyncHookType<T, R, AdditionalOptions>} a new SyncHook instance
|
||||
*/
|
||||
function SyncHook(
|
||||
args = /** @type {ArgumentNames<AsArray<T>>} */ (/** @type {unknown} */ ([])),
|
||||
name = undefined
|
||||
) {
|
||||
const hook = new Hook(args, name);
|
||||
hook.constructor = SyncHook;
|
||||
hook.tapAsync = TAP_ASYNC;
|
||||
hook.tapPromise = TAP_PROMISE;
|
||||
hook.compile = COMPILE;
|
||||
return /** @type {SyncHookType<T, R, AdditionalOptions>} */ (
|
||||
/** @type {unknown} */ (hook)
|
||||
);
|
||||
}
|
||||
|
||||
/** @type {EXPECTED_ANY} */
|
||||
SyncHook.prototype = null;
|
||||
|
||||
module.exports = SyncHook;
|
||||
+92
@@ -0,0 +1,92 @@
|
||||
/*
|
||||
MIT License http://www.opensource.org/licenses/mit-license.php
|
||||
Author Tobias Koppers @sokra
|
||||
*/
|
||||
"use strict";
|
||||
|
||||
const Hook = require("./Hook");
|
||||
const HookCodeFactory = require("./HookCodeFactory");
|
||||
|
||||
/** @typedef {import("./Hook").EXPECTED_ANY} EXPECTED_ANY */
|
||||
/** @typedef {import("./Hook").EXPECTED_FUNCTION} EXPECTED_FUNCTION */
|
||||
/** @typedef {import("./Hook").CompileOptions<EXPECTED_ANY, EXPECTED_ANY, EXPECTED_ANY>} CompileOptions */
|
||||
/** @typedef {import("./Hook").UnsetAdditionalOptions} UnsetAdditionalOptions */
|
||||
/** @typedef {import("./HookCodeFactory").ContentOptions} ContentOptions */
|
||||
/**
|
||||
* @template T
|
||||
* @typedef {import("./Hook").AsArray<T>} AsArray
|
||||
*/
|
||||
/**
|
||||
* @template {EXPECTED_ANY[]} T
|
||||
* @typedef {import("./Hook").ArgumentNames<T>} ArgumentNames
|
||||
*/
|
||||
|
||||
/**
|
||||
* Mirror of the `SyncLoopHook` class declared in `tapable.d.ts` for use as
|
||||
* the factory function's return type, so `new SyncLoopHook(...)` resolves
|
||||
* to the specific subclass type instead of the generic `Hook`.
|
||||
* @template T
|
||||
* @template [AdditionalOptions=UnsetAdditionalOptions]
|
||||
* @typedef {import("../tapable").SyncLoopHook<T, AdditionalOptions>} SyncLoopHookType
|
||||
*/
|
||||
|
||||
class SyncLoopHookCodeFactory extends HookCodeFactory {
|
||||
/**
|
||||
* @param {ContentOptions} options content generation options
|
||||
* @returns {string} generated body code
|
||||
*/
|
||||
content({ onError, onDone, rethrowIfPossible }) {
|
||||
return this.callTapsLooping({
|
||||
onError: (i, err) => onError(err),
|
||||
onDone,
|
||||
rethrowIfPossible
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
const factory = new SyncLoopHookCodeFactory();
|
||||
|
||||
const TAP_ASYNC = () => {
|
||||
throw new Error("tapAsync is not supported on a SyncLoopHook");
|
||||
};
|
||||
|
||||
const TAP_PROMISE = () => {
|
||||
throw new Error("tapPromise is not supported on a SyncLoopHook");
|
||||
};
|
||||
|
||||
/**
|
||||
* @this {Hook<EXPECTED_ANY, EXPECTED_ANY, EXPECTED_ANY>}
|
||||
* @param {CompileOptions} options compile options
|
||||
* @returns {EXPECTED_FUNCTION} the compiled call function
|
||||
*/
|
||||
function COMPILE(options) {
|
||||
factory.setup(this, options);
|
||||
return factory.create(options);
|
||||
}
|
||||
|
||||
/**
|
||||
* @constructor
|
||||
* @template T
|
||||
* @template [AdditionalOptions=UnsetAdditionalOptions]
|
||||
* @param {ArgumentNames<AsArray<T>>=} args argument names of the hook
|
||||
* @param {string=} name name of the hook
|
||||
* @returns {SyncLoopHookType<T, AdditionalOptions>} a new SyncLoopHook instance
|
||||
*/
|
||||
function SyncLoopHook(
|
||||
args = /** @type {ArgumentNames<AsArray<T>>} */ (/** @type {unknown} */ ([])),
|
||||
name = undefined
|
||||
) {
|
||||
const hook = new Hook(args, name);
|
||||
hook.constructor = SyncLoopHook;
|
||||
hook.tapAsync = TAP_ASYNC;
|
||||
hook.tapPromise = TAP_PROMISE;
|
||||
hook.compile = COMPILE;
|
||||
return /** @type {SyncLoopHookType<T, AdditionalOptions>} */ (
|
||||
/** @type {unknown} */ (hook)
|
||||
);
|
||||
}
|
||||
|
||||
/** @type {EXPECTED_ANY} */
|
||||
SyncLoopHook.prototype = null;
|
||||
|
||||
module.exports = SyncLoopHook;
|
||||
+106
@@ -0,0 +1,106 @@
|
||||
/*
|
||||
MIT License http://www.opensource.org/licenses/mit-license.php
|
||||
Author Tobias Koppers @sokra
|
||||
*/
|
||||
"use strict";
|
||||
|
||||
const Hook = require("./Hook");
|
||||
const HookCodeFactory = require("./HookCodeFactory");
|
||||
|
||||
/** @typedef {import("./Hook").EXPECTED_ANY} EXPECTED_ANY */
|
||||
/** @typedef {import("./Hook").EXPECTED_FUNCTION} EXPECTED_FUNCTION */
|
||||
/** @typedef {import("./Hook").CompileOptions<EXPECTED_ANY, EXPECTED_ANY, EXPECTED_ANY>} CompileOptions */
|
||||
/** @typedef {import("./Hook").UnsetAdditionalOptions} UnsetAdditionalOptions */
|
||||
/** @typedef {import("./HookCodeFactory").ContentOptions} ContentOptions */
|
||||
/**
|
||||
* @template T
|
||||
* @typedef {import("./Hook").AsArray<T>} AsArray
|
||||
*/
|
||||
/**
|
||||
* @template {EXPECTED_ANY[]} T
|
||||
* @typedef {import("./Hook").ArgumentNames<T>} ArgumentNames
|
||||
*/
|
||||
|
||||
/**
|
||||
* Mirror of the `SyncWaterfallHook` class declared in `tapable.d.ts` for use
|
||||
* as the factory function's return type, so `new SyncWaterfallHook(...)`
|
||||
* resolves to the specific subclass type instead of the generic `Hook`.
|
||||
* @template T
|
||||
* @template [R=AsArray<T>[0]]
|
||||
* @template [AdditionalOptions=UnsetAdditionalOptions]
|
||||
* @typedef {import("../tapable").SyncWaterfallHook<T, R, AdditionalOptions>} SyncWaterfallHookType
|
||||
*/
|
||||
|
||||
class SyncWaterfallHookCodeFactory extends HookCodeFactory {
|
||||
/**
|
||||
* @param {ContentOptions} options content generation options
|
||||
* @returns {string} generated body code
|
||||
*/
|
||||
content({ onError, onResult, resultReturns, rethrowIfPossible }) {
|
||||
return this.callTapsSeries({
|
||||
onError: (i, err) => onError(err),
|
||||
onResult: (i, result, next) => {
|
||||
let code = "";
|
||||
code += `if(${result} !== undefined) {\n`;
|
||||
code += `${/** @type {string[]} */ (this._args)[0]} = ${result};\n`;
|
||||
code += "}\n";
|
||||
code += next();
|
||||
return code;
|
||||
},
|
||||
onDone: () => onResult(/** @type {string[]} */ (this._args)[0]),
|
||||
doneReturns: resultReturns,
|
||||
rethrowIfPossible
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
const factory = new SyncWaterfallHookCodeFactory();
|
||||
|
||||
const TAP_ASYNC = () => {
|
||||
throw new Error("tapAsync is not supported on a SyncWaterfallHook");
|
||||
};
|
||||
|
||||
const TAP_PROMISE = () => {
|
||||
throw new Error("tapPromise is not supported on a SyncWaterfallHook");
|
||||
};
|
||||
|
||||
/**
|
||||
* @this {Hook<EXPECTED_ANY, EXPECTED_ANY, EXPECTED_ANY>}
|
||||
* @param {CompileOptions} options compile options
|
||||
* @returns {EXPECTED_FUNCTION} the compiled call function
|
||||
*/
|
||||
function COMPILE(options) {
|
||||
factory.setup(this, options);
|
||||
return factory.create(options);
|
||||
}
|
||||
|
||||
/**
|
||||
* @constructor
|
||||
* @template T
|
||||
* @template [R=AsArray<T>[0]]
|
||||
* @template [AdditionalOptions=UnsetAdditionalOptions]
|
||||
* @param {ArgumentNames<AsArray<T>>=} args argument names of the hook
|
||||
* @param {string=} name name of the hook
|
||||
* @returns {SyncWaterfallHookType<T, R, AdditionalOptions>} a new SyncWaterfallHook instance
|
||||
*/
|
||||
function SyncWaterfallHook(
|
||||
args = /** @type {ArgumentNames<AsArray<T>>} */ (/** @type {unknown} */ ([])),
|
||||
name = undefined
|
||||
) {
|
||||
if (args.length < 1) {
|
||||
throw new Error("Waterfall hooks must have at least one argument");
|
||||
}
|
||||
const hook = new Hook(args, name);
|
||||
hook.constructor = SyncWaterfallHook;
|
||||
hook.tapAsync = TAP_ASYNC;
|
||||
hook.tapPromise = TAP_PROMISE;
|
||||
hook.compile = COMPILE;
|
||||
return /** @type {SyncWaterfallHookType<T, R, AdditionalOptions>} */ (
|
||||
/** @type {unknown} */ (hook)
|
||||
);
|
||||
}
|
||||
|
||||
/** @type {EXPECTED_ANY} */
|
||||
SyncWaterfallHook.prototype = null;
|
||||
|
||||
module.exports = SyncWaterfallHook;
|
||||
+19
@@ -0,0 +1,19 @@
|
||||
/*
|
||||
MIT License http://www.opensource.org/licenses/mit-license.php
|
||||
Author Tobias Koppers @sokra
|
||||
*/
|
||||
"use strict";
|
||||
|
||||
module.exports.AsyncParallelBailHook = require("./AsyncParallelBailHook");
|
||||
module.exports.AsyncParallelHook = require("./AsyncParallelHook");
|
||||
module.exports.AsyncSeriesBailHook = require("./AsyncSeriesBailHook");
|
||||
module.exports.AsyncSeriesHook = require("./AsyncSeriesHook");
|
||||
module.exports.AsyncSeriesLoopHook = require("./AsyncSeriesLoopHook");
|
||||
module.exports.AsyncSeriesWaterfallHook = require("./AsyncSeriesWaterfallHook");
|
||||
module.exports.HookMap = require("./HookMap");
|
||||
module.exports.MultiHook = require("./MultiHook");
|
||||
module.exports.SyncBailHook = require("./SyncBailHook");
|
||||
module.exports.SyncHook = require("./SyncHook");
|
||||
module.exports.SyncLoopHook = require("./SyncLoopHook");
|
||||
module.exports.SyncWaterfallHook = require("./SyncWaterfallHook");
|
||||
module.exports.__esModule = true;
|
||||
+28
@@ -0,0 +1,28 @@
|
||||
/*
|
||||
MIT License http://www.opensource.org/licenses/mit-license.php
|
||||
Author Tobias Koppers @sokra
|
||||
*/
|
||||
"use strict";
|
||||
|
||||
/** @typedef {import("./Hook").EXPECTED_FUNCTION} EXPECTED_FUNCTION */
|
||||
|
||||
/**
|
||||
* Browser shim for `util.deprecate`. Logs the deprecation message once on the
|
||||
* first call, then forwards arguments to `fn`.
|
||||
* @param {EXPECTED_FUNCTION} fn the function to wrap
|
||||
* @param {string} msg the deprecation message
|
||||
* @returns {EXPECTED_FUNCTION} the wrapped function
|
||||
*/
|
||||
module.exports.deprecate = (fn, msg) => {
|
||||
let once = true;
|
||||
return function deprecate() {
|
||||
if (once) {
|
||||
// eslint-disable-next-line no-console
|
||||
console.warn(`DeprecationWarning: ${msg}`);
|
||||
once = false;
|
||||
}
|
||||
// @ts-expect-error expected
|
||||
// eslint-disable-next-line prefer-rest-params
|
||||
return fn.apply(this, arguments);
|
||||
};
|
||||
};
|
||||
Reference in New Issue
Block a user