Skip to content

Commit

Permalink
feat: Export defineConfig defines the auxiliary function of the confi…
Browse files Browse the repository at this point in the history
…guration (#4127)

* feat: Export defineConfig defines the auxiliary function of the configuration

* docs: Add some documentation for `defineConfig`

* docs: Set `Config Intellisense` as the fourth-level heading

Refer to: https://github.com/rollup/rollup/pull/4127/files/5d68a031143358ff84f6f91676af09061ed93a65
  • Loading branch information
rxliuli committed Jun 6, 2021
1 parent 9f69fe3 commit d985955
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 0 deletions.
27 changes: 27 additions & 0 deletions docs/01-command-line-reference.md
Expand Up @@ -200,6 +200,33 @@ export default commandLineArgs => {
}
```

#### Config Intellisense

Since Rollup ships with TypeScript typings, you can leverage your IDE's intellisense with jsdoc type hints:

```javascript
// rollup.config.js
/**
* @type {import('rollup').RollupOptions}
*/
const config = {
// ...
}

export default config
```

Alternatively you can use the helper which should provide intellisense without the need for jsdoc annotations:defineConfig

```javascript
// rollup.config.js
import { defineConfig } from 'rollup'

export default defineConfig({
// ...
})
```

### Differences to the JavaScript API

While config files provide an easy way to configure Rollup, they also limit how Rollup can be invoked and where configuration is taken from. Especially if you are rebundling Rollup in another build tool or want to integrate it into an advanced build process, it may be better to directly invoke Rollup programmatically from your scripts.
Expand Down
10 changes: 10 additions & 0 deletions src/rollup/rollup.ts
Expand Up @@ -20,6 +20,7 @@ import {
OutputOptions,
Plugin,
RollupBuild,
RollupOptions,
RollupOutput,
RollupWatcher
} from './types';
Expand Down Expand Up @@ -282,3 +283,12 @@ function writeOutputFile(

return Promise.all([writeFile(fileName, source), writeSourceMapPromise]);
}

/**
* Auxiliary function for defining rollup configuration
* 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 {
return options;
}
3 changes: 3 additions & 0 deletions src/rollup/types.d.ts
Expand Up @@ -883,3 +883,6 @@ interface AcornNode {
start: number;
type: string;
}

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

0 comments on commit d985955

Please sign in to comment.