Skip to content

Commit

Permalink
Merge pull request #240 from PepsRyuu/plugin-object-hooks
Browse files Browse the repository at this point in the history
Support for Plugin Object Hooks
  • Loading branch information
PepsRyuu authored Sep 24, 2022
2 parents 4b1a301 + 8fc1fef commit 3a014cc
Show file tree
Hide file tree
Showing 3 changed files with 708 additions and 18 deletions.
77 changes: 60 additions & 17 deletions lib/impl/PluginLifecycle.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,18 @@ const RollupConfigContainer = require('./RollupConfigContainer');
* @return {Promise<any>}
*/
async function _callAsyncHook (plugin, hook, args) {
if (typeof plugin.execute[hook] === 'string') {
return plugin.execute[hook];
let handler = plugin.execute[hook];

if (typeof handler === 'string') {
return handler;
}

if (typeof handler === 'object') {
handler = handler.handler;
}

if (plugin.execute[hook]) {
let hr = plugin.execute[hook].apply(plugin.context, args);
if (handler) {
let hr = handler.apply(plugin.context, args);

if (hr instanceof Promise) {
return (await plugin.error.wrapAsync(hr));
Expand All @@ -36,11 +42,29 @@ async function _callAsyncHook (plugin, hook, args) {
* @return {any}
*/
function _callSyncHook (plugin, hook, args) {
if (plugin.execute[hook]) {
return plugin.execute[hook].apply(plugin.context, args);
let handler = plugin.execute[hook];

if (typeof handler === 'object') {
handler = handler.handler;
}

if (handler) {
return handler.apply(plugin.context, args);
}
}

function _getSortedPlugins(plugins, hook) {
plugins = plugins.slice(0);

return plugins.filter(p => {
return typeof p.execute[hook] === 'object' && p.execute[hook].order === 'pre';
}).concat(plugins.filter(p => {
return typeof p.execute[hook] === 'function' || typeof p.execute[hook] === 'string' || (typeof p.execute[hook] === 'object' && !p.execute[hook].order);
})).concat(plugins.filter(p => {
return typeof p.execute[hook] === 'object' && p.execute[hook].order === 'post';
}));
}

/**
* @param {PluginContainer} container
* @param {string} hook
Expand All @@ -50,8 +74,10 @@ function _callSyncHook (plugin, hook, args) {
async function callAsyncFirstHook (container, hook, args) {
// hook may return a promise.
// waits for hook to return value other than null or undefined.
for (let i = 0; i < container.__plugins.length; i++) {
let hr = await _callAsyncHook(container.__plugins[i], hook, args);
let plugins = _getSortedPlugins(container.__plugins, hook);

for (let i = 0; i < plugins.length; i++) {
let hr = await _callAsyncHook(plugins[i], hook, args);

if (hr !== null && hr !== undefined) {
return hr;
Expand All @@ -70,11 +96,12 @@ async function callAsyncFirstHook (container, hook, args) {
async function callAsyncSequentialHook (container, hook, toArgs, fromResult, start) {
// hook may return a promise.
// all plugins that implement this hook will run, passing data onwards
let plugins = _getSortedPlugins(container.__plugins, hook);
let output = start;

for (let i = 0; i < container.__plugins.length; i++) {
for (let i = 0; i < plugins.length; i++) {
let args = toArgs(output);
let hr = await _callAsyncHook(container.__plugins[i], hook, args);
let hr = await _callAsyncHook(plugins[i], hook, args);

if (hr !== null && hr !== undefined) {
output = fromResult(hr, output);
Expand All @@ -95,12 +122,25 @@ async function callAsyncParallelHook (container, hook, args) {
// all hooks are executed at the same time without waiting
// will wait for all hooks to complete before returning
let hookResults = [];
let plugins = _getSortedPlugins(container.__plugins, hook);
let previous = [];

for (let i = 0; i < plugins.length; i++) {
if (typeof plugins[i].execute[hook] === 'object' && plugins[i].execute[hook].sequential) {
let values = await Promise.all(previous);
hookResults.push(...values);
previous = [];
let v = await _callAsyncHook(plugins[i], hook, args);
hookResults.push(v);
continue;
}

for (let i = 0; i < container.__plugins.length; i++) {
hookResults.push(_callAsyncHook(container.__plugins[i], hook, args));
previous.push(_callAsyncHook(plugins[i], hook, args));
}

return Promise.all(hookResults);
let values = await Promise.all(previous);
hookResults.push(...values);
return hookResults;
}

/**
Expand All @@ -110,9 +150,11 @@ async function callAsyncParallelHook (container, hook, args) {
* @return {any}
*/
function callSyncFirstHook (container, hook, args) {
let plugins = _getSortedPlugins(container.__plugins, hook);

// waits for hook to return value other than null of undefined
for (let i = 0; i < container.__plugins.length; i++) {
let hr = _callSyncHook(container.__plugins[i], hook, args);
for (let i = 0; i < plugins.length; i++) {
let hr = _callSyncHook(plugins[i], hook, args);

if (hr !== null && hr !== undefined) {
return hr;
Expand All @@ -128,10 +170,11 @@ function callSyncFirstHook (container, hook, args) {
*/
function callSyncSequentialHook (container, hook, args) {
// all plugins that implement this hook will run, passing data onwards
let plugins = _getSortedPlugins(container.__plugins, hook);
let output = args[0];

for (let i = 0; i < container.__plugins.length; i++) {
let hr = _callSyncHook(container.__plugins[i], hook, [output]);
for (let i = 0; i < plugins.length; i++) {
let hr = _callSyncHook(plugins[i], hook, [output]);
if (hr !== null && hr !== undefined) {
output = hr;
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,6 @@
"mocha-istanbul-ui": "^0.4.1",
"proxyquire": "^2.0.1",
"requirejs": "^2.3.6",
"rollup": "^2.77.0"
"rollup": "^2.79.1"
}
}
Loading

0 comments on commit 3a014cc

Please sign in to comment.