Skip to content

Commit

Permalink
Overload defineConfig to accept a RollupOptionsFunction parameter (#4728
Browse files Browse the repository at this point in the history
)

* Add defineConfigFunction auxiliary function

* Overload defineConfig a 3rd time instead.

* Update tests descriptions
  • Loading branch information
Septh committed Dec 1, 2022
1 parent 4b1a954 commit f44c933
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/rollup/rollup.ts
Expand Up @@ -29,6 +29,7 @@ import type {
Plugin,
RollupBuild,
RollupOptions,
RollupOptionsFunction,
RollupOutput,
RollupWatcher
} from './types';
Expand Down Expand Up @@ -275,6 +276,8 @@ async function writeOutputFile(
* Mainly to facilitate IDE code prompts, after all, export default does not prompt, even if you add @type annotations, it is not accurate
* @param options
*/
export function defineConfig<T extends RollupOptions | RollupOptions[]>(options: T): T {
export function defineConfig<T extends RollupOptions | RollupOptions[] | RollupOptionsFunction>(
options: T
): T {
return options;
}
5 changes: 5 additions & 0 deletions src/rollup/types.d.ts
Expand Up @@ -927,3 +927,8 @@ interface AcornNode {

export function defineConfig(options: RollupOptions): RollupOptions;
export function defineConfig(options: RollupOptions[]): RollupOptions[];
export function defineConfig(optionsFunction: RollupOptionsFunction): RollupOptionsFunction;

export type RollupOptionsFunction = (
commandLineArguments: Record<string, any>
) => MaybePromise<RollupOptions | RollupOptions[]>;
18 changes: 18 additions & 0 deletions test/load-config-file/index.js
Expand Up @@ -41,6 +41,15 @@ describe('loadConfigFile', () => {
assert.deepStrictEqual(JSON.parse(JSON.stringify(options)), defaultConfigs);
});

it('loads an ESM config file exporting a config as a function with defineConfig()', async () => {
const { options, warnings } = await loadConfigFile(
path.resolve(__dirname, 'samples/esm-defineconfig-as-fn/rollup.config.mjs')
);
assert.strictEqual(warnings.count, 0);
// Remove undefined values and functions before checking
assert.deepStrictEqual(JSON.parse(JSON.stringify(options)), defaultConfigs);
});

it('loads a CommonJS config file', async () => {
const { options, warnings } = await loadConfigFile(
path.resolve(__dirname, 'samples/cjs-via-pkg/rollup.config.js')
Expand All @@ -59,6 +68,15 @@ describe('loadConfigFile', () => {
assert.deepStrictEqual(JSON.parse(JSON.stringify(options)), defaultConfigs);
});

it('loads a CommonJS config file exporting a config as a function with defineConfig()', async () => {
const { options, warnings } = await loadConfigFile(
path.resolve(__dirname, 'samples/cjs-defineconfig-as-fn/rollup.config.cjs')
);
assert.strictEqual(warnings.count, 0);
// Remove undefined values and functions before checking
assert.deepStrictEqual(JSON.parse(JSON.stringify(options)), defaultConfigs);
});

it('throws a helpful error when loading an ES module that should actually be CommonJS', async () => {
let caughtError;
try {
Expand Down
@@ -0,0 +1,10 @@
// @ts-check
const { defineConfig } = require('../../../../dist/shared/rollup')

module.exports = defineConfig(args => ({
input: 'my-input',
output: {
file: 'my-file',
format: 'es'
}
}))
@@ -0,0 +1,10 @@
// @ts-check
import { defineConfig } from '../../../../dist/es/rollup.js'

export default defineConfig(args => ({
input: 'my-input',
output: {
file: 'my-file',
format: 'es'
}
}))

0 comments on commit f44c933

Please sign in to comment.