简体   繁体   English

汇总配置文件中的汇总是否支持 typescript?

[英]Does rollup support typescript in rollup config file?

It seems we can use typescript to write rollup config file.看来我们可以使用 typescript 来编写汇总配置文件。 Say, I can create a file named rollup.config.ts , with content:比如说,我可以创建一个名为rollup.config.ts的文件,内容如下:

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 .如果我将 rollup 调用为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我尝试将 ts-node 与

For the meantime, while it is not yet supported, JSDoc might be come useful to type check the rollup configuration.同时,虽然它还不受支持,但 JSDoc 可能有助于对汇总配置进行类型检查。 (It only works on editor that support JSDoc. eg VSCode). (它只适用于支持 JSDoc 的编辑器。例如 VSCode)。

/** @type {import('rollup').RollupOptions} */
const options = {
  ...
};

export default options;

You can create your rollup configuration in TypeScript您可以在 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 .您需要将rollup.config.ts添加到rollup.config.ts中包含的文件中。

{
    "include": ["src/**/*", "rollup.config.ts"]
}

You can create a separate rollup.config.js that looks like this:您可以创建一个单独的rollup.config.js ,如下所示:

require('ts-node').register({
  compilerOptions: {
    module: 'CommonJS'
  },
  // and other tsconfig.json options as you like
});

module.exports = require('./rollup.config.ts');

You'll need to npm install --save-dev ts-node of course.你当然需要npm install --save-dev ts-node Then run npx rollup -c and you're off to the races.然后运行npx rollup -c就可以开始比赛了。

For the fastest compilation, go with this:要获得最快的编译,请使用以下命令:

npm install sucrase -D

And in your rollup.config.js , do this:在您的rollup.config.js ,执行以下操作:

require('sucrase/register')
module.exports = require('./rollup.config.ts')

https://github.com/alangpierce/sucrase https://github.com/alangpierce/sucrase

使用Rollup v2.52.0 ,您可以将--configPlugin选项指定为:

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

it is actually possible, just import defineConfig from rollup and this will give your great IDE suggestions, for example:实际上是可能的,只需从rollup导入defineConfig ,这将为您提供很好的IDE建议,例如:

//rollup.config.js

import { defineConfig } from 'rollup';

const rollupConfig = defineConfig({
  input: 'src/index.tsx',
  output: [
    {
      file: 'dist/bundle.cjs.js',
      format: 'cjs',
    },
  ],
});

export default rollupConfig;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM