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

Overload defineConfig to accept a RollupOptionsFunction parameter #4728

Merged
merged 3 commits into from Dec 1, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
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 with defineConfigFn()', async () => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor nit: The two test descriptions no longer fit.

const { options, warnings } = await loadConfigFile(
path.resolve(__dirname, 'samples/defineconfigfn-as-esm/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 with defineConfigFn()', async () => {
const { options, warnings } = await loadConfigFile(
path.resolve(__dirname, 'samples/defineconfigfn-as-cjs/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'
}
}))