plugins: Optional Chaining Causes Build to Fail

  • Rollup Plugin Name: @rollup/plugin-typescript
  • Rollup Plugin Version: ^3.0.0
  • Rollup Version: ^1.31.0
  • Operating System (or Browser): Mac OS
  • Node Version: v12.3.1

How Do We Reproduce?

git clone git@github.com:bennypowers/rollup-plugin-typescript-optional-chaining-repro.git
cd rollup-plugin-typescript-optional-chaining-repro
npm i
npx rollup -c

Expected Behavior

Compiles

Actual Behavior

Fails with

index.ts → stdout...
[!] Error: Unexpected token (Note that you need plugins to import files that are not JavaScript)
index.ts (2:3)
1: const a = { b: { c: undefined } };
2: a?.b?.c;
      ^
Error: Unexpected token (Note that you need plugins to import files that are not JavaScript)
    at error (/Users/bennyp/Documents/rt-repro/node_modules/rollup/dist/shared/node-entry.js:5400:30)
    at Module.error (/Users/bennyp/Documents/rt-repro/node_modules/rollup/dist/shared/node-entry.js:9820:16)
    at tryParse (/Users/bennyp/Documents/rt-repro/node_modules/rollup/dist/shared/node-entry.js:9713:23)
    at Module.setSource (/Users/bennyp/Documents/rt-repro/node_modules/rollup/dist/shared/node-entry.js:10076:33)
    at /Users/bennyp/Documents/rt-repro/node_modules/rollup/dist/shared/node-entry.js:12362:20
    at async Promise.all (index 0)
    at async Promise.all (index 0)
    at async Promise.all (index 0)

I now present the relevant contents of said repro repo

package.json

{
  "devDependencies": {
    "@rollup/plugin-typescript": "^3.0.0",
    "rollup": "^1.31.0",
    "typescript": "^3.7.5"
  }
}

rollup.config.js

import typescript from '@rollup/plugin-typescript';

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

index.ts

const a = { b: { c: undefined } };
a?.b?.c;

tsconfig.json

{
  "include": ["index.ts"],
  "compilerOptions": {
    "experimentalDecorators": true,
    "target": "esnext",
    "module": "esnext"
  }
}

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 7
  • Comments: 16 (7 by maintainers)

Commits related to this issue

Most upvoted comments

Fixed in version 4.0.0 of the typescript plugin

@etroynov it is a problem in rollup, not in typescript @NotWoods it should be possible to use optional chaining without other compilers

i found the solution, in tsconfig i change:

target esnext -> es2019

For some reason optional chaining does not work with 2020, esnext targets.

@numfin please combine multiple successive replies so we keep the notifications down.

I’ve done some investigation and I think that Rollup/acorn is having trouble parsing the new optional chaining syntax. If you set target: 'es2020' (so that Typescript compiles the syntax with a helper) the issue goes away.

Yes, that would be great!