Optional chaining failing with `"target": "esnext"` in `tsconfig` -- Rollup doesn't understand this syntax yet
See original GitHub issueWhat happens and why it is wrong
https://github.com/brandon-leapyear/rollup-optional-chaining-repro
Copied from README:
yarn yarn rollup -c
Uncomment the
"target": "esnext"
line intsconfig.json
to show the error: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)
Seems to be a
rollup-plugin-typescript2
bug because with"target": "esnext"
uncommented,yarn tsc --noEmit
still works (e.g. Typescript parses it fine).
Environment
Shouldn’t be relevant
Versions
- typescript: latest
- rollup: latest
- rollup-plugin-typescript2: latest
rollup.config.js
import typescript from 'rollup-plugin-typescript2'
export default {
input: 'index.ts',
output: [
{
file: 'dist/index.js',
format: 'es',
},
],
plugins: [
typescript({
typescript: require('typescript'),
})
],
}
tsconfig.json
{
"compilerOptions": {
"allowSyntheticDefaultImports": true,
"jsx": "react",
// Uncomment to break:
// "target": "esnext",
}
}
package.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"
}
}
plugin output with verbosity 3
Issue Analytics
- State:
- Created 4 years ago
- Reactions:2
- Comments:8 (5 by maintainers)
Top Results From Across the Web
Optional Chaining Causes Build to Fail · Issue #205 - GitHub
I've done some investigation and I think that Rollup/acorn is having trouble parsing the new optional chaining syntax. If you set target: ...
Read more >How to get optional chaining working in TypeScript?
The problem is you are targeting esnext this will tell the compiler to output all language features as is without any transpilation.
Read more >babel/plugin-transform-typescript
This plugin adds support for the types syntax used by the TypeScript programming ... supports certain JavaScript proposals such as optional chaining (...
Read more >TSConfig Reference - Docs on every TSConfig option
The value of extends is a string which contains a path to another configuration file to inherit from. The path may use Node.js...
Read more >TypeScript rules for Bazel - bazelbuild/rules_nodejs
A unique name for this target. deps. Optional list of labels. Default: []. Additional tsconfig.json files referenced via extends. src.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@dagda1 this is not a bug – this is typescript generating correct code (for
esnext
target
) that rollup can’t quite consume. You can add babel plugin into the chain if you must haveesnext
.Typescript transpiles
a?.b
to(_a = a) === null || _a === void 0 ? void 0 : _a.b
whenes2015
(default) is used.It transpiles
a?.b
toa?.b
whenesnext
is used. I guess this is a native JS feature in esnext,The error you see is rollup failing to parse this. (not sure if they are supposed to support it or not).
You can verify by running
tsc
, getting it to emit index.js and then using js file as rollup input. You should get the same error.