Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make options hook async #3660

Merged
merged 1 commit into from Jul 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/05-plugin-development.md
Expand Up @@ -124,7 +124,7 @@ You can use [`this.getModuleInfo`](guide/en/#thisgetmoduleinfomoduleid-string--m

#### `options`
Type: `(options: InputOptions) => InputOptions | null`<br>
Kind: `sync, sequential`<br>
Kind: `async, sequential`<br>
Previous Hook: This is the first hook of the build phase.<br>
Next Hook: [`buildStart`](guide/en/#buildstart)

Expand Down
15 changes: 9 additions & 6 deletions src/rollup/rollup.ts
Expand Up @@ -32,7 +32,7 @@ export async function rollupInternal(
rawInputOptions: GenericConfigObject,
watcher: RollupWatcher | null
): Promise<RollupBuild> {
const { options: inputOptions, unsetOptions: unsetInputOptions } = getInputOptions(
const { options: inputOptions, unsetOptions: unsetInputOptions } = await getInputOptions(
rawInputOptions,
watcher !== null
);
Expand Down Expand Up @@ -89,28 +89,31 @@ export async function rollupInternal(
return result;
}

function getInputOptions(
async function getInputOptions(
rawInputOptions: GenericConfigObject,
watchMode: boolean
): { options: NormalizedInputOptions; unsetOptions: Set<string> } {
): Promise<{ options: NormalizedInputOptions; unsetOptions: Set<string> }> {
if (!rawInputOptions) {
throw new Error('You must supply an options object to rollup');
}
const rawPlugins = ensureArray(rawInputOptions.plugins) as Plugin[];
const { options, unsetOptions } = normalizeInputOptions(
rawPlugins.reduce(applyOptionHook(watchMode), rawInputOptions)
await rawPlugins.reduce(applyOptionHook(watchMode), Promise.resolve(rawInputOptions))
);
normalizePlugins(options.plugins, ANONYMOUS_PLUGIN_PREFIX);
return { options, unsetOptions };
}

function applyOptionHook(watchMode: boolean) {
return (inputOptions: GenericConfigObject, plugin: Plugin): GenericConfigObject => {
return async (
inputOptions: Promise<GenericConfigObject>,
plugin: Plugin
): Promise<GenericConfigObject> => {
if (plugin.options)
return (
(plugin.options.call(
{ meta: { rollupVersion, watchMode } },
inputOptions
await inputOptions
) as GenericConfigObject) || inputOptions
);

Expand Down
33 changes: 33 additions & 0 deletions test/function/samples/options-async-hook/_config.js
@@ -0,0 +1,33 @@
const assert = require('assert');
const { promises: fs } = require('fs');

module.exports = {
description: 'resolves promises between sequential options hooks',
options: {
input: 'super-unused',
treeshake: false,
plugins: [
{
name: 'test-plugin-1',
async options(options) {
assert.deepStrictEqual(JSON.parse(JSON.stringify(options)), {
input: 'super-unused',
plugins: [{ name: 'test-plugin-1' }, { name: 'test-plugin-2' }],
strictDeprecations: true,
treeshake: false
});
return Object.assign({}, options, {
input: (await fs.readFile('file.txt', 'utf8')).trim()
});
}
},
{
name: 'test-plugin-2',
options(options) {
assert.strictEqual(options.input, 'unused');
return Object.assign({}, options, { input: 'used' });
}
}
]
}
};
1 change: 1 addition & 0 deletions test/function/samples/options-async-hook/file.txt
@@ -0,0 +1 @@
unused
1 change: 1 addition & 0 deletions test/function/samples/options-async-hook/unused.js
@@ -0,0 +1 @@
throw new Error('Not executed');
1 change: 1 addition & 0 deletions test/function/samples/options-async-hook/used.js
@@ -0,0 +1 @@
export default 42;