Home:ALL Converter>Does rollup support typescript in rollup config file?

Does rollup support typescript in rollup config file?

Ask Time:2019-02-15T22:29:50         Author:Freewind

Json Formatter

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

Author:Freewind,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/54711437/does-rollup-support-typescript-in-rollup-config-file
Rannie Aguilar Peralta :

For the meantime, while it is not yet supported, JSDoc might be come useful to type check the rollup configuration. (It only works on editor that support JSDoc. e.g. VSCode).\n\n/** @type {import('rollup').RollupOptions} */\nconst options = {\n ...\n};\n\nexport default options;\n",
2019-08-02T17:07:03
Ffloriel :

You can create your rollup configuration in TypeScript\nimport { RollupOptions } from "rollup";\n\nconst bundle: RollupOptions = {\n//...\n}\nexport default bundle\n\nand use it with\nrollup --config rollup.config.ts --configPlugin @rollup/plugin-typescript\n\nYou will need to add rollup.config.ts to the included files in your tsconfig.json.\n{\n "include": ["src/**/*", "rollup.config.ts"]\n}\n",
2022-02-18T14:34:28
yy