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

target in tsconfig doesn't affect esbuild's runtime library #2628

Closed
mengxinssfd opened this issue Oct 24, 2022 · 4 comments
Closed

target in tsconfig doesn't affect esbuild's runtime library #2628

mengxinssfd opened this issue Oct 24, 2022 · 4 comments
Labels

Comments

@mengxinssfd
Copy link

I found that after building the code, there is an extra __spreadValues function, which is written inside var prop in b ||= {}

var __spreadValues = (a, b) => {
  for (var prop in b ||= {})
    if (__hasOwnProp.call(b, prop))
      __defNormalProp(a, prop, b[prop]);
  if (__getOwnPropSymbols)
    for (var prop of __getOwnPropSymbols(b)) {
      if (__propIsEnum.call(b, prop))
        __defNormalProp(a, prop, b[prop]);
    }
  return a;
};

It's using the ECMAScript 2021 - (ES12) feature, when I set the target to a version before ES12, it doesn't work

@evanw
Copy link
Owner

evanw commented Oct 24, 2022

Can you provide a self-contained code sample and esbuild configuration that will reproduce the problem?

@mengxinssfd
Copy link
Author

mengxinssfd commented Oct 25, 2022

Can you provide a self-contained code sample and esbuild configuration that will reproduce the problem?

You can look at this repository https://github.com/mengxinssfd/esbuild-isuue--2628.

Sorry, what I said earlier was not correct. esbuild.build({target:'ES2017'}) is useful.

It is accurate to use the target inside tsconfig.json without specifying target:'ES version'.

@nettybun
Copy link

@evanw I ran into this just now, the issue is that ||= is used despite the tsconfig target asking for ES6. Here's a repro:

$ echo 'var a = { a:1 }; export default { b:2, ...a }' | npx esbuild --loader=ts --tsconfig-raw='{"compilerOptions": { "target": "es6" }}'

var __defProp = Object.defineProperty;
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __propIsEnum = Object.prototype.propertyIsEnumerable;
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
var __spreadValues = (a2, b) => {
  for (var prop in b ||= {})
    if (__hasOwnProp.call(b, prop))
      __defNormalProp(a2, prop, b[prop]);
  if (__getOwnPropSymbols)
    for (var prop of __getOwnPropSymbols(b)) {
      if (__propIsEnum.call(b, prop))
        __defNormalProp(a2, prop, b[prop]);
    }
  return a2;
};
var a = { a: 1 };
export default __spreadValues({ b: 2 }, a);

Here's the docs that says tsconfig target will be respected: https://esbuild.github.io/content-types/#tsconfig-json

Using --target=es6 will correctly transform ||=

$ echo 'var a = { a:1 }; export default { b:2, ...a }' | npx esbuild --loader=ts --tsconfig-raw='{"compilerOptions": { "target": "es6" }}' --target=es6

...
for (var prop in b || (b = {}))
...

@evanw evanw added the tsconfig label Dec 4, 2022
makenowjust added a commit to makenowjust-labs/recheck that referenced this issue Jan 16, 2023
@evanw evanw changed the title Lowering the target version cannot change __spreadValues target in tsconfig doesn't affect esbuild's runtime library Mar 24, 2023
@evanw evanw closed this as completed in 9be7875 May 17, 2023
@evanw
Copy link
Owner

evanw commented May 17, 2023

FYI I'm changing this so target in tsconfig.json doesn't affect any file, including esbuild's runtime library. You'll now have to use esbuild's --target=es2021 setting for this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants