Rollup-plugin-typescript2: 可选链接失败,“目标”:tsconfig 中的“esnext”

创建于 2019-11-15  ·  7评论  ·  资料来源: ezolenko/rollup-plugin-typescript2

会发生什么以及为什么会出错

https://github.com/brandon-leapyear/rollup-optional-chaining-repro

从自述文件复制:

yarn rollup -c

取消对"target": "esnext"符合tsconfig.json显示错误:

index.ts: (3:57)
[!] Error: Unexpected token (Note that you need plugins to import files that are not JavaScript)
index.ts (3:57)
1: import React from 'react'
2: 
3: const a: { b?: string } = {}
                               ^
4: 
5: export default () => <span>{a?.b}</span>
Error: Unexpected token (Note that you need plugins to import files that are not JavaScript)
    at error (/Users/bchinn/Desktop/rollup-example/node_modules/rollup/dist/rollup.js:5351:30)
    at Module.error (/Users/bchinn/Desktop/rollup-example/node_modules/rollup/dist/rollup.js:9643:9)
    at tryParse (/Users/bchinn/Desktop/rollup-example/node_modules/rollup/dist/rollup.js:9552:16)
    at Module.setSource (/Users/bchinn/Desktop/rollup-example/node_modules/rollup/dist/rollup.js:9868:33)
    at /Users/bchinn/Desktop/rollup-example/node_modules/rollup/dist/rollup.js:12148:20
    at async Promise.all (index 0)
    at async Promise.all (index 0)
    at async Promise.all (index 0)

似乎是rollup-plugin-typescript2错误,因为"target": "esnext"
未注释, yarn tsc --noEmit仍然有效(例如,Typescript 解析得很好)。

环境

应该不相关

版本

  • 打字稿:最新
  • 汇总:最新
  • rollup-plugin-typescript2:最新

汇总配置文件

import typescript from 'rollup-plugin-typescript2'

export default {
  input: 'index.ts',
  output: [
    {
      file: 'dist/index.js',
      format: 'es',
    },
  ],
  plugins: [
    typescript({
      typescript: require('typescript'),
    })
  ],
}

配置文件

{
  "compilerOptions": {
    "allowSyntheticDefaultImports": true,
    "jsx": "react",

    // Uncomment to break:
    // "target": "esnext",
  }
}

包.json

{
    "name": "rollup-optional-chaining-repro",
    "version": "0.0.1",
    "private": true,
    "devDependencies": {
        "rollup": "^1.27.0",
        "rollup-plugin-typescript2": "^0.25.2"
    },
    "dependencies": {
        "typescript": "^3.7.2"
    }
}

详细程度为 3 的插件输出

插件调试.txt

所有7条评论

当使用es2015 (默认)时,Typescript 将a?.b(_a = a) === null || _a === void 0 ? void 0 : _a.b

当使用esnext时,它将a?.ba?.b 。 我猜这是 esnext 中的原生 JS 特性,

您看到的错误是 rollup 无法解析它。 (不确定他们是否应该支持它)。

您可以通过运行tsc ,让它发出 index.js,然后使用 js 文件作为汇总输入。 你应该得到同样的错误。

我不认为是这样。 取消注释"target": "esnext"并运行yarn tsc index.tsindex.js包含:

var _a;
var a = {};
console.log((_a = a) === null || _a === void 0 ? void 0 : _a.b);

yarn rollup -c仍然失败)

嗯,不会发生在我身上,我明白了

const a = {};
console.log(a?.b);

检查您的打字稿版本是否与您认为的一样(也许 yarn 推出了全球版本?):

.\node_modules\.bin\tsc --version
Version 3.7.2

我对目标“esnext”和可选链接有同样的问题。 有修复吗? 一切都用tsc编译

@dagda1这不是一个错误——这是打字稿生成正确的代码(对于 esnext 目标),汇总不能完全消耗。 如果你必须有 esnext,你可以将 babel 插件添加到链中。

@ezolenko是否仍然需要额外的 babel 插件来进行汇总和可选链接?

据我所知是的,您可以尝试使用最新的汇总版本,看看它是否有效(如果修复了某些内容,它将在汇总端或其依赖项之一中)。

此页面是否有帮助?
0 / 5 - 0 等级