Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does rollup support typescript in rollup config file?

It seems we can use typescript to write rollup config file. Say, I can create a file named rollup.config.ts, with content:

import typescript from 'rollup-plugin-typescript2';

export default {
  input: 'main.ts',
  plugins: [typescript()],
  output: {
    file: 'bundle.js',
    format: 'cjs',
  },
  external: ['lodash']
}

It's working if I invoke rollup as rollup -c rollup.config.ts.

But if I use some typings in it:

import typescript from 'rollup-plugin-typescript2';
import {RollupFileOptions} from "rollup";

const config: RollupFileOptions = {
  input: 'main.ts',
  plugins: [typescript()],
  output: {
    file: 'bundle.js',
    format: 'cjs',
  },
  external: ['lodash']
}

export default config;

It will report errors like:

$ rollup -c rollup.config.ts
[!] Error: Unexpected token
rollup.config.ts (4:12)
2: import {RollupFileOptions} from "rollup";
3: 
4: const config: RollupFileOptions = {
                 ^

Is it possible to make it work? I tried to use ts-node with

like image 772
Freewind Avatar asked Feb 15 '19 14:02

Freewind


People also ask

Does rollup support typescript?

This plugin requires an LTS Node version (v14. 0.0+) and Rollup v2. 14.0+. This plugin also requires at least TypeScript 3.7.

How do I create a D TS file in rollup?

ts rollup generation, you simply need to set dtsRollup. enabled to true in your api-extractor. json config file. By default, the rollup file will be written to "<projectFolder>/dist/<unscopedPackageName>.

Does rollup use Tsconfig?

The @rollup/plugin-typescript will load any compilerOptions from the tsconfig. json file by default.

Is rollup deprecated?

This package has been deprecated and is no longer maintained. Please use @rollup/plugin-inject.


2 Answers

You can create your rollup configuration in TypeScript

import { RollupOptions } from "rollup";

const bundle: RollupOptions = {
//...
}
export default bundle

and use it with

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

You will need to add rollup.config.ts to the included files in your tsconfig.json.

{
    "include": ["src/**/*", "rollup.config.ts"]
}
like image 101
Ffloriel Avatar answered Sep 17 '22 12:09

Ffloriel


With Rollup v2.52.0, you can specify a --configPlugin option as:

rollup --config rollup.config.ts --configPlugin typescript
like image 34
Sid Vishnoi Avatar answered Sep 17 '22 12:09

Sid Vishnoi