Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unexpected token error at code with optional chaining when using Typescript #10227

Closed
inoyakaigor opened this issue Jan 9, 2020 · 19 comments · Fixed by #11221
Closed

Unexpected token error at code with optional chaining when using Typescript #10227

inoyakaigor opened this issue Jan 9, 2020 · 19 comments · Fixed by #11221
Projects

Comments

@inoyakaigor
Copy link

inoyakaigor commented Jan 9, 2020

UPD for everyone who will find this issue. For fix this error acornjs/acorn#891 and acornjs/acorn#890 must be merged.
UPD2 we all awaiting while estree/estree/pull/204 will be merged

UPD3 fix for optional chaining landed into acorn 7.3.0 but webpack@4 uses acorn 6.x. so fix of this issue will be in webpack@5: #10227 (comment)

Temporary fixes

For Babel

Add @babel/plugin-proposal-optional-chaining and @babel/plugin-proposal-nullish-coalescing-operator plugins in your config

For Typescript

Do not upgrade to version 3.8 or set compiler option target to es2019

Yarn/NPM level

See the #10227 (comment)


Do you want to request a feature or report a bug?
I don't know is it a bug or feature

What is the current behavior?

Get error "Unecpected token"

What is the expected behavior?

Build bundle successfull

Description
When I set in tsconfig.json target: "ESNext" TS keeps my code with optional chaining (and nullish coalescing) as is. But I've got an error

Module parse failed: Unexpected token (2:10)
File was processed with these loaders:
 * ./node_modules/ts-loader/index.js
You may need an additional loader to handle the result of these loaders.
| var a = { b: 2 };
> var c = a?.b;

I made a minimum reproduce repo inoyakaigor/webpack-typescript-optional-chaining

How I can avoid this error?

@alexander-akait
Copy link
Member

It looks like you just deleted our lovely crafted issue template. It was there for good reasons. Please help us solving your issue by answering the questions asked in this template. I'm closing this. Please either update the issue with the template and reopen, or open a new issue.

@inoyakaigor
Copy link
Author

@evilebottnawi Closing the issue because I removed the template looks like excessive bureaucracy.

However, I updated the description.

@mc0
Copy link

mc0 commented Jan 16, 2020

@evilebottnawi looks like @inoyakaigor updated the description and also provided a reproduction repo. I think we're hitting this same issue with optional chaining.

@ianschmitz
Copy link

For others that stumble on this issue, it looks like we're waiting on acornjs/acorn#891 and acornjs/acorn#890 before we stop transpiling this syntax via babel/tsc.

@ospfranco
Copy link

it looks like the mentioned issues on acorn have been merged, any news?

@yangmingshan
Copy link

@evilebottnawi Can we reopen this issue? Since this is a unresolved problem, it will help people find answers and let people track the progress.

@inoyakaigor
Copy link
Author

inoyakaigor commented Jun 11, 2020

Good news everyone! Acorn 7.3.0 with optional chaining support just released 2 hours ago! https://github.com/acornjs/acorn/blob/master/acorn/CHANGELOG.md

@ianschmitz
Copy link

Unfortunately webpack 4 uses acorn 6.x - not sure if we'll get this before webpack 5. Fingers crossed!

@alexander-akait
Copy link
Member

Yes, it will be part of webpack@5

@OnurGvnc
Copy link

OnurGvnc commented Jul 3, 2020

as a temporary solution;
https://github.com/OnurGvnc/acorn-with-stage3

package.json:

{
  ...
  "resolutions": {
    "acorn": "npm:acorn-with-stage3"
  },
  "dependencies": {

SarjuHansaliya added a commit to pangolindex/components that referenced this issue Dec 17, 2022
this change fixes optional chaining issue which was fixed in acord@v8 and that is fixed in webpack 5

reference link: webpack/webpack#10227 (comment)
SarjuHansaliya added a commit to pangolindex/components that referenced this issue Dec 17, 2022
this change fixes optional chaining issue which was fixed in acord@v8 and that is fixed in webpack 5

reference link: webpack/webpack#10227 (comment)
@jcready
Copy link

jcready commented Feb 24, 2023

FYI to others that are considering using the acorn-with-stage3 solution #10227 (comment) just be aware that it breaks the built-in Split Chunks: Dynamic Imports. Presumably because the AST generated by acorn v7 (which acorn-with-stage3 uses) is different enough that webpack v4 cannot find the import() nodes to perform the splitting.

ling1726 added a commit to ling1726/fluentui-contrib that referenced this issue Jul 6, 2023
Sets The compilation target of the repo to 2019 for webpack 4 compat webpack/webpack#10227
which does not support optional chaining without extra babel loader
transpilation.
azmy60 added a commit to beekeeper-studio/beekeeper-studio that referenced this issue Feb 1, 2024
webpack did not recognize optional chaining token (?.)

webpack/webpack#10227 (comment)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects