使用@rollup/plugin-babel和Typescript进行Rollup:Expected ',',got ':'

dgenwo3n  于 2023-11-15  发布在  Babel
关注(0)|答案(2)|浏览(652)

我在Rollup 4中遇到了这个问题:
[!] RollupError:Expected ',',got ':'(注意你需要插件来导入不是JavaScript的文件)src/index.ts(48:19)
我的Babel配置似乎工作正常,我可以直接运行Babel并获得transplied输出:

npx babel src/index.ts // Run perfectly fine

字符串
我的babel.config.json

{
  "presets": [
    [
      "@babel/preset-env",
      {
        "targets": "defaults"
      }
    ],
    "@babel/preset-typescript"
  ]
}


现在来看看我的rollup.config.mjs。我在考虑加载外部babel.config.json时的一些问题,所以我只是将Babel配置复制到babel()插件中:

import babel from '@rollup/plugin-babel';
import resolve from '@rollup/plugin-node-resolve';

export default {
  input: 'src/index.ts',
  output: {
    file: 'dist/index.esm.js',
    format: 'esm',
  },
  plugins: [
    resolve(),
    babel({
      babelrc: false,
      presets: [
        [
          '@babel/preset-env',
          {
            targets: 'defaults',
          },
        ],
        '@babel/preset-typescript',
      ],
    }),
  ],
};


这是我的tsconfig.json

{
  "compilerOptions": {
    "target": "ESNext",
    "declaration": true,
    "declarationDir": "dist/",
    "isolatedModules": true,
    "moduleResolution": "bundler",
    "lib": ["esnext", "DOM"]
  }
}

bksxznpy

bksxznpy1#

解决方案是将.ts添加到extensions配置中。没有它,Babel不知道TypeScript文件也应该被处理:

extensions: ['.js', '.jsx', '.es6', '.es', '.mjs', '.cjs', '.ts']

字符串

w6lpcovy

w6lpcovy2#

你需要一个typescript编译器,比如bunesbuild或者除了babel之外的任何东西:

import babel from '@rollup/plugin-babel';
import resolve from '@rollup/plugin-node-resolve';
import typescript from 'rollup-plugin-typescript2';

export default {
  input: 'src/index.ts',
  output: {
    file: 'dist/index.esm.js',
    format: 'esm',
  },
  plugins: [
    resolve(),
    typescript(),
    babel({
      babelrc: false,
      presets: [
        [
          '@babel/preset-env',
          {
            targets: 'defaults',
          },
        ],
        '@babel/preset-typescript',
      ],
    }),
  ],
};

字符串

相关问题