Rollup-plugin-typescript2: Optional chaining failing with "target": "esnext" in tsconfig

Created on 15 Nov 2019  ·  7Comments  ·  Source: ezolenko/rollup-plugin-typescript2

What happens and why it is wrong

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

Copied from README:

yarn rollup -c

Uncomment the "target": "esnext" line in tsconfig.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

plugin-debug.txt

All 7 comments

Typescript transpiles a?.b to (_a = a) === null || _a === void 0 ? void 0 : _a.b when es2015 (default) is used.

It transpiles a?.b to a?.b when esnext 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.

I don't think that's the case. After uncommenting "target": "esnext" and running yarn tsc index.ts, index.js contains:

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

(yarn rollup -c still fails)

Hm, doesn't happen for me, I get

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

Check if your typescript version is what you think it is (maybe yarn launches global version?):

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

I have the same problem with target "esnext" and optional chaining. Is there a fix yet? Everything compiles with tsc

@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 have esnext.

@ezolenko is this still the case that the extra babel plugins are required for rollup and optional chaining?

As far as I know yes, you can try with latest rollup version and see if it works (if something is fixed it would be on rollup side or in one of its dependencies).

Was this page helpful?
0 / 5 - 0 ratings