Skip to content

Commit

Permalink
Compatibility with rollup v1.0
Browse files Browse the repository at this point in the history
* Contains several under the hood changes such as removal of deprecated options and new API hooks.
* Tests reworked to compare results against rollup itself. Should detect breaking changes much easier.
  • Loading branch information
AgronKabashi authored Jan 5, 2019
1 parent 59b3d70 commit c35f229
Show file tree
Hide file tree
Showing 33 changed files with 2,135 additions and 936 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
node_modules
.DS_STORE
.DS_STORE
.nyc_output
1 change: 1 addition & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
test
rollup.config.js
ISSUE_TEMPLATE.md
.nyc_output
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog

## Version 3.0.0
*2019-01-01*
* Compatibility with rollup v1.0. Contains several under the hood changes such as removal of deprecated options and new API hooks.
* Tests reworked to compare results against rollup itself. Should detect breaking changes much easier.

## Version 2.2.0
*2018-11-20*
* Added support for new hooks, `renderStart` and `renderError`.
Expand Down
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ export default {
]),

conditional(!isProduction, [
filesize(),
watch()
filesize()
])
]
})
Expand Down Expand Up @@ -62,7 +61,7 @@ export default {
...
plugins: [
conditional(false, [ // false here will prevent any of the plugins' life cycle hooks from being executed
// This plugin however will create a http(s)-server immediately and outside any of the life cycle hooks
// rollup-plugin-serve will however instantiate a http(s)-server immediately and outside any of the life cycle hooks
// resulting in the http-server running even though we don't want it to.
serve()
])
Expand Down
103 changes: 59 additions & 44 deletions build/rollup-plugin-conditional.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,79 +2,93 @@

const ensureArray = input => Array.isArray(input) ? input : [input]; // TODO: Issue #4 - Reduce complexity

const byMethod = methodNames => plugin => ensureArray(methodNames).some(methodName => plugin.hasOwnProperty(methodName)); // TODO: Issue #4 - Reduce complexity
const byMethod = names => plugin => ensureArray(names).some(name => plugin.hasOwnProperty(name)); // TODO: Issue #4 - Reduce complexity

const codeTransformSequencer = (plugin, methodNames) =>
const codeTransformSequencer = (plugin, hookNames) =>
(previousResult = "") => {
const input = typeof previousResult === "object" ? previousResult.code : previousResult;
const methodName = ensureArray(methodNames).find(methodName => plugin.hasOwnProperty(methodName)); // TODO: Issue #4 - Reduce complexity
return plugin[methodName](input) || previousResult;
const hookName = ensureArray(hookNames).find(hookName => plugin.hasOwnProperty(hookName)); // TODO: Issue #4 - Reduce complexity
return plugin[hookName](input) || previousResult;
};

const stringConcatSequencer = (plugin, methodName) =>
const stringConcatSequencer = (plugin, hookName, separator = "\n") =>
async (previousResult = "") => {
let result = await Promise.resolve(plugin[methodName]());
result = result && `\n${result}` || "";
return `${previousResult}${result}`;
const result = await Promise.resolve(plugin[hookName]());
return result ? `${previousResult}${previousResult && separator}${result}` : previousResult;
};

const doubleLineStringConcatSequencer = (...args) => stringConcatSequencer(...args, "\n\n");

/**
* Run the specified plugins in sequence, passing the output to the next plugin in the queue
* Execute the plugins in sequence, passing the output of the current to the next plugin in the queue
* @param {Array<{}>} plugins Collection of rollup plugins
* @param {string} methodName Name of plugin API method to filter by and execute
* @param {Function} callback Callback method for handling the result of each plugin.methodName call
* @param {string|Array<string>} hookName Name(s) of plugin API method(s) to filter by and execute
* @param {Function} callback Callback method for handling the result of each plugin.hookName call
* @returns {Function} A reduced function that returns the final result of all the plugins' results
*/
const promisifiedSequence = (plugins, methodName, callback) =>
const promisifiedSequence = (plugins, hookName, callback) =>
(...args) => {
const [firstPlugin, ...restPlugins] = plugins.filter(byMethod(methodName));
const hookNames = ensureArray(hookName); // TODO: Issue #4 - Reduce complexity
const [firstPlugin, ...restPlugins] = plugins.filter(byMethod(hookNames));

if (!restPlugins.length && !firstPlugin) {
return null;
}

const startingValue = Promise.resolve(callback(firstPlugin, methodName)(...args));
const startingValue = Promise.resolve(callback(firstPlugin, hookNames)(...args));

return restPlugins.reduce((result, plugin) =>
result.then(callback(plugin, methodName)), startingValue);
result.then(callback(plugin, hookNames)), startingValue);
};

/**
* Run the specified plugins in sequence, passing the output to the next plugin in the queue
* @param {Array<{}>} plugins Collection of rollup plugins
* @param {string} methodName Name of plugin API method to filter by and execute
* @param {string} hookName Name of plugin API method to filter by and execute
* @returns {Function} A reduced function that returns the final result of all the plugins' results
*/
const sequence = (plugins, methodName) =>
const sequence = (plugins, hookName) =>
(...args) => {
const [firstPlugin, ...restPlugins] = plugins.filter(byMethod(methodName));
const [firstPlugin, ...restPlugins] = plugins.filter(byMethod(hookName));

if (!restPlugins.length && !firstPlugin) {
return null;
}

const startingValue = firstPlugin[methodName](...args);
const startingValue = firstPlugin[hookName](...args);

return restPlugins.reduce((result, plugin) => plugin[methodName](result), startingValue);
return restPlugins.reduce((result, plugin) => plugin[hookName](result), startingValue);
};

/**
* Find and return the result of the last plugin in the collection that returns a truthy value
* Find and return the result of the first plugin with specified hookName that returns a truthy value
* @param {Array<{}>} plugins Collection of rollup plugins
* @param {string} methodName Name of plugin API method to filter by and execute
* @param {string} hookName Plugin hook to execute
* @returns {Function} Reduced function that returns the truthy result of the last plugin in the colleciton
*/
const once = (plugins, methodName) =>
const firstAvailable = (plugins, hookName) =>
(...args) => plugins
.filter(byMethod(methodName))
.reduceRight((output, plugin) => output || plugin[methodName](...args), null);
.filter(byMethod(hookName))
.reduce((output, plugin) => output || plugin[hookName](...args), null);

const all = (plugins, methodName) =>
(...args) => Promise.all([
plugins
.filter(byMethod(methodName))
.map(plugin => Promise.resolve(plugin[methodName](...args)))
]);
/**
* Execute all hooks for the plugins with the specified hookName
* @param {Array<{}>} plugins Collection of rollup plugins
* @param {string} hookName Plugin hook to execute
* @returns {Function} Reduced function that returns the truthy result of the last plugin in the colleciton
*/
const all = (plugins, hookName) =>
(...args) => {
const hookNames = ensureArray(hookName); // TODO: Issue #4 - Reduce complexity
return Promise.all([
plugins
.filter(byMethod(hookNames))
.map(plugin => {
const hookName = hookNames.find(hookName => plugin.hasOwnProperty(hookName));
return Promise.resolve(plugin[hookName](...args));
})
]);
};

function conditional (condition, plugins) {
if (!condition) {
Expand All @@ -83,26 +97,27 @@ function conditional (condition, plugins) {

plugins = typeof plugins === "function" ? plugins() : plugins; // eslint-disable-line no-param-reassign
if (!Array.isArray(plugins) || !plugins.length) {
return {};
return;
}

return {
buildStart: all(plugins, "buildStart"),
name: "rollup-plugin-conditional",
banner: promisifiedSequence(plugins, "banner", stringConcatSequencer),
buildEnd: all(plugins, "buildEnd"),
generateBundle: all(plugins, "generateBundle"),
load: once(plugins, "load"),
resolveId: once(plugins, "resolveId"),
buildStart: all(plugins, "buildStart"),
footer: promisifiedSequence(plugins, "footer", stringConcatSequencer),
generateBundle: all(plugins, ["generateBundle", "ongenerate", "onwrite"]),
intro: promisifiedSequence(plugins, "intro", doubleLineStringConcatSequencer),
load: firstAvailable(plugins, "load"),
options: sequence(plugins, "options"),
transform: promisifiedSequence(plugins, "transform", codeTransformSequencer),
outro: promisifiedSequence(plugins, "outro", doubleLineStringConcatSequencer),
renderChunk: promisifiedSequence(plugins, ["renderChunk", "transformChunk", "transformBundle"], codeTransformSequencer), // TODO: Issue #4 - Reduce complexity
renderStart: all(plugins, "renderStart"),
renderError: all(plugins, "renderError"),
ongenerate: all(plugins, "ongenerate"), // TODO: Issue #4 - Reduce complexity
onwrite: all(plugins, "onwrite"), // TODO: Issue #4 - Reduce complexity
intro: promisifiedSequence(plugins, "intro", stringConcatSequencer),
outro: promisifiedSequence(plugins, "outro", stringConcatSequencer),
banner: promisifiedSequence(plugins, "banner", stringConcatSequencer),
footer: promisifiedSequence(plugins, "footer", stringConcatSequencer)
renderStart: all(plugins, "renderStart"),
resolveId: firstAvailable(plugins, "resolveId"),
resolveDynamicImport: firstAvailable(plugins, "resolveDynamicImport"),
transform: promisifiedSequence(plugins, "transform", codeTransformSequencer),
watchChange: sequence(plugins, "watchChange")
};
}

Expand Down
Loading

0 comments on commit c35f229

Please sign in to comment.