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 解析得很好)。
应该不相关
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",
}
}
{
"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"
}
}
当使用es2015
(默认)时,Typescript 将a?.b
为(_a = a) === null || _a === void 0 ? void 0 : _a.b
。
当使用esnext
时,它将a?.b
为a?.b
。 我猜这是 esnext 中的原生 JS 特性,
您看到的错误是 rollup 无法解析它。 (不确定他们是否应该支持它)。
您可以通过运行tsc
,让它发出 index.js,然后使用 js 文件作为汇总输入。 你应该得到同样的错误。
我不认为是这样。 取消注释"target": "esnext"
并运行yarn tsc index.ts
, index.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 插件来进行汇总和可选链接?
据我所知是的,您可以尝试使用最新的汇总版本,看看它是否有效(如果修复了某些内容,它将在汇总端或其依赖项之一中)。