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

Fix support for async functions as config #3472

Merged
merged 2 commits into from Mar 30, 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
21 changes: 11 additions & 10 deletions cli/run/loadConfigFile.ts
Expand Up @@ -22,7 +22,7 @@ interface NodeModuleWithCompile extends NodeModule {
export default async function loadAndParseConfigFile(
fileName: string,
commandOptions: any = {}
): Promise<{options: MergedRollupOptions[], warnings: BatchWarnings}> {
): Promise<{ options: MergedRollupOptions[]; warnings: BatchWarnings }> {
const configs = await loadConfigFile(fileName, commandOptions);
const warnings = batchWarnings();
try {
Expand All @@ -31,7 +31,7 @@ export default async function loadAndParseConfigFile(
addCommandPluginsToInputOptions(options, commandOptions);
return options;
});
return {options: normalizedConfigs, warnings};
return { options: normalizedConfigs, warnings };
} catch (err) {
warnings.flush();
throw err;
Expand All @@ -43,11 +43,12 @@ async function loadConfigFile(
commandOptions: any
): Promise<GenericConfigObject[]> {
const extension = path.extname(fileName);
const configFileExport = await (extension === '.mjs' && supportsNativeESM()
? (await import(pathToFileURL(fileName).href)).default
: extension === '.cjs'
const configFileExport =
extension === '.mjs' && supportsNativeESM()
? (await import(pathToFileURL(fileName).href)).default
: extension === '.cjs'
? getDefaultFromCjs(require(fileName))
: getDefaultFromTranspiledConfigFile(fileName, commandOptions.silent));
: await getDefaultFromTranspiledConfigFile(fileName, commandOptions.silent);
return getConfigList(configFileExport, commandOptions);
}

Expand Down Expand Up @@ -109,10 +110,10 @@ async function loadConfigFromBundledFile(fileName: string, bundledCode: string)
}
}

function getConfigList(configFileExport: any, commandOptions: any) {
const defaultExport = configFileExport.default || configFileExport;
const config =
typeof defaultExport === 'function' ? defaultExport(commandOptions) : defaultExport;
async function getConfigList(configFileExport: any, commandOptions: any) {
const config = await (typeof configFileExport === 'function'
? configFileExport(commandOptions)
: configFileExport);
if (Object.keys(config).length === 0) {
return error({
code: 'MISSING_CONFIG',
Expand Down
4 changes: 4 additions & 0 deletions test/cli/samples/config-async-function/_config.js
@@ -0,0 +1,4 @@
module.exports = {
description: 'supports using an async function as config',
command: 'rollup -c',
};
5 changes: 5 additions & 0 deletions test/cli/samples/config-async-function/_expected.js
@@ -0,0 +1,5 @@
'use strict';

var main = 42;

module.exports = main;
1 change: 1 addition & 0 deletions test/cli/samples/config-async-function/main.js
@@ -0,0 +1 @@
export default 42;
6 changes: 6 additions & 0 deletions test/cli/samples/config-async-function/rollup.config.js
@@ -0,0 +1,6 @@
export default async () => ({
input: 'main.js',
output: {
format: 'cjs',
},
});