TypeStrong / ts-loader

TypeScript loader for webpack
https://johnnyreilly.com/ts-loader-goes-webpack-5
MIT License
3.46k stars 431 forks source link

['babel-loader', 'ts-loader'] i get Syntax Error: SyntaxError with ?. #1289

Open 1021683053 opened 3 years ago

1021683053 commented 3 years ago

Expected Behaviour

I use ['babel-loader', 'ts-loader'] in ts-loader result will to es6 but typescript compile something is missing.

Actual Behaviour

image

image

JonWallsten commented 3 years ago

@1021683053: Can you create a small reproducible repo?

chaosmirage commented 3 years ago

I am getting same issue.

webpack.config.js

...
module: {
    rules: [
      {
        test: /\.(js|jsx)$/,
        use: 'babel-loader',
        exclude: /node_modules/
      },
      {
        test: /\.(ts|tsx)$/,
        use: 'ts-loader',
        exclude: /node_modules/
      },
   }
}
...

and according to this answer https://github.com/webpack/webpack/issues/10227#issuecomment-787945709, it is webpack 4 error.

But I don't understand: if I added test: js|jsx|ts|tsx to babel-loader I will not get this error.

webpack.config.js

...
module: {
    rules: [
      {
        test: /\.(js|jsx|ts|tsx)$/,
        use: 'babel-loader',
        exclude: /node_modules/
      },
      {
        test: /\.(ts|tsx)$/,
        use: 'ts-loader',
        exclude: /node_modules/
      },
   }
}
...

Why does it work?

nyngwang commented 2 years ago

@chaosmirage Did you resolve it? Apparently, it should mean that some typescript transpile functions are only available through babel-loader. So it is indeed a bug of ts-loader.

johnnyreilly commented 2 years ago

I've happily used ts-loader with ?. successfully. I wonder if this might be a tsconfig.json option issue. I seem to recall I had to set various properties first; I forget which I'm afraid

chaosmirage commented 2 years ago

@nyngwang Thank you for the hypothetical explanation!

Actually, I don't remember how fixed it, maybe changing tsconfig from

"target": "ES2020",                       /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */
"module": "ES2020",                       /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */
"lib": ["ES2020", "dom"],                 /* Specify library files to be included in the compilation. */

to

"target": "ES2019",                       /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */
"module": "ES2015",                       /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */
"lib": ["ES2019", "dom"],                 /* Specify library files to be included in the compilation. */

was helped