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

add typescript config support #3835

Merged
merged 17 commits into from Jun 16, 2021
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
10 changes: 8 additions & 2 deletions cli/run/commandPlugins.ts
Expand Up @@ -13,7 +13,13 @@ export function addCommandPluginsToInputOptions(
if (command.waitForBundleInput === true) {
inputOptions.plugins!.push(waitForInputPlugin());
}
const commandPlugin = command.plugin;
addPluginsFromCommandOption(command.plugin, inputOptions);
}

export function addPluginsFromCommandOption(
commandPlugin: unknown,
inputOptions: InputOptions
): void {
if (commandPlugin) {
const plugins = Array.isArray(commandPlugin) ? commandPlugin : [commandPlugin];
for (const plugin of plugins) {
Expand All @@ -30,7 +36,7 @@ export function addCommandPluginsToInputOptions(
}
}

function loadAndRegisterPlugin(inputOptions: InputOptions, pluginText: string) {
function loadAndRegisterPlugin(inputOptions: InputOptions, pluginText: string): void {
let plugin: any = null;
let pluginArg: any = undefined;
if (pluginText[0] === '{') {
Expand Down
2 changes: 1 addition & 1 deletion cli/run/getConfigPath.ts
Expand Up @@ -33,7 +33,7 @@ export function getConfigPath(commandConfig: string | true): string {

function findConfigFileNameInCwd(): string {
const filesInWorkingDir = new Set(readdirSync(process.cwd()));
for (const extension of ['mjs', 'cjs']) {
for (const extension of ['mjs', 'cjs', 'ts']) {
const fileName = `${DEFAULT_CONFIG_BASE}.${extension}`;
if (filesInWorkingDir.has(fileName)) return fileName;
}
Expand Down
24 changes: 15 additions & 9 deletions cli/run/loadConfigFile.ts
Expand Up @@ -10,7 +10,7 @@ import { GenericConfigObject } from '../../src/utils/options/options';
import relativeId from '../../src/utils/relativeId';
import { stderr } from '../logging';
import batchWarnings, { BatchWarnings } from './batchWarnings';
import { addCommandPluginsToInputOptions } from './commandPlugins';
import { addCommandPluginsToInputOptions, addPluginsFromCommandOption } from './commandPlugins';

function supportsNativeESM() {
return Number(/^v(\d+)/.exec(process.version)![1]) >= 13;
Expand Down Expand Up @@ -41,15 +41,18 @@ export default async function loadAndParseConfigFile(

async function loadConfigFile(
fileName: string,
commandOptions: any
commandOptions: Record<string, unknown>
): Promise<GenericConfigObject[]> {
const extension = path.extname(fileName);

const configFileExport =
extension === '.mjs' && supportsNativeESM()
? (await import(pathToFileURL(fileName).href)).default
commandOptions.configPlugin ||
!(extension === '.cjs' || (extension === '.mjs' && supportsNativeESM()))
? await getDefaultFromTranspiledConfigFile(fileName, commandOptions)
: extension === '.cjs'
? getDefaultFromCjs(require(fileName))
: await getDefaultFromTranspiledConfigFile(fileName, commandOptions.silent);
: (await import(pathToFileURL(fileName).href)).default;

return getConfigList(configFileExport, commandOptions);
}

Expand All @@ -59,17 +62,20 @@ function getDefaultFromCjs(namespace: GenericConfigObject) {

async function getDefaultFromTranspiledConfigFile(
fileName: string,
silent: boolean
commandOptions: Record<string, unknown>
): Promise<unknown> {
const warnings = batchWarnings();
const bundle = await rollup.rollup({
const inputOptions = {
external: (id: string) =>
(id[0] !== '.' && !path.isAbsolute(id)) || id.slice(-5, id.length) === '.json',
input: fileName,
onwarn: warnings.add,
plugins: [],
treeshake: false
});
if (!silent && warnings.count > 0) {
};
addPluginsFromCommandOption(commandOptions.configPlugin, inputOptions);
const bundle = await rollup.rollup(inputOptions);
if (!commandOptions.silent && warnings.count > 0) {
stderr(bold(`loaded ${relativeId(fileName)} with warnings`));
warnings.flush();
}
Expand Down
18 changes: 18 additions & 0 deletions docs/01-command-line-reference.md
Expand Up @@ -22,6 +22,12 @@ Typically, it is called `rollup.config.js` and sits in the root directory of you

If you want to write your config as a CommonJS module using `require` and `module.exports`, you should change the file extension to `.cjs`, which will prevent Rollup from trying to transpile the file. Furthermore if you are on Node 13+, changing the file extension to `.mjs` will also prevent Rollup from transpiling it but import the file as an ES module instead. See [using untranspiled config files](guide/en/#using-untranspiled-config-files) for more details and why you might want to do this.

You can also use other languages for your configuration files like TypeScript. To do that, install a corresponding Rollup plugin like `@rollup/plugin-typescript` and use the [`--configPlugin`](guide/en/#--configplugin-plugin) option:

```
rollup --config rollup.config.ts --configPlugin typescript
```

Config files support the options listed below. Consult the [big list of options](guide/en/#big-list-of-options) for details on each option:

```javascript
Expand Down Expand Up @@ -227,6 +233,8 @@ export default defineConfig({
})
```

See also the [`--configPlugin`](guide/en/#--configplugin-plugin) for how to write your config in TypeScript.

### 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 Expand Up @@ -410,6 +418,16 @@ By default, plugin functions be called with no argument to create the plugin. Yo
rollup -i input.js -f es -p 'terser={output: {beautify: true, indent_level: 2}}'
```

#### `--configPlugin <plugin>`

Allows to specify Rollup plugins to transpile or otherwise control the parsing of your configuration file. The main benefit is that it allows you to use non-JavaScript configuration files. For instance the following will allow you to write your configuration in TypeScript, provided you have `@rollup/plugin-typescript` installed:

```
rollup --config rollup.config.ts --configPlugin @rollup/plugin-typescript
```

It supports the same syntax as the [`--plugin`](guide/en/#-p-plugin---plugin-plugin) option i.e. you can spcify the option multiple times, you can omit the `@rollup/plugin-` prefix and just write `typescript` and you can specify plugin options via `={...}`.

#### `-v`/`--version`

Print the installed version number.
Expand Down