Skip to content

Commit

Permalink
Fix support for async functions as config (#3472)
Browse files Browse the repository at this point in the history
* Support async functions as config

* Retrigger tests
  • Loading branch information
lukastaegert committed Mar 30, 2020
1 parent 9fa326f commit 943995d
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 10 deletions.
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',
},
});

0 comments on commit 943995d

Please sign in to comment.