这是indexloc提供的服务,不要输入任何密码
Skip to content

fix: #893 #896

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

Closed
wants to merge 2 commits into from
Closed

fix: #893 #896

wants to merge 2 commits into from

Conversation

jerrywu001
Copy link

Fixes #893

  • src\index.ts (line 102)
+ // set tsconfig path from esbuildOptions function raw
+ if (!options.tsconfig && options.esbuildOptions) {
+   const regex = /(\w+)\.tsconfig\s*=\s*['"]([^'"]*)['"]/;
+   const match = options.esbuildOptions.toString().match(regex);
+   if (match) {
+     options.tsconfig = match[2];
+   }
+ }

const tsconfig = loadTsConfig(process.cwd(), options.tsconfig)
  • src\plugins\es5.ts (line 12)
    esbuildOptions(options) {
      if (options.target === 'es5') {
-       options.target = 'es2020'
+      // options.target = 'es2020'
        enabled = true
      }
    },

After the two, the test can pass (But it conflict with the other 2 test cases !!!!!!)

test('set target by esbuildOptions', async () => {
  await expect(
    run(
      getTestName(),
      {
        'input.ts': `const foo = 'foo';export default foo;`,
        'tsup.config.ts': `export default {
    esbuildOptions(options) {
      options.tsconfig = 'custom.tsconfig.json';
    },
  }`,
        'custom.tsconfig.json': `{
    "compilerOptions": {
      "target": "es5",
      "module": "esnext"
    }
  }`
      },
      { flags: [ '--format',  'cjs' ] }
    )
  ).rejects.toThrowError(
    `Transforming const to the configured target environment ("es5") is not supported yet`
  )
})

These got failed!!!!!! (line 849). because I deleted the options.target = 'es2020'

This is a problem. @egoist

test('es5 target', async () => {
  const { output, outFiles } = await run(
    getTestName(),
    {
      'input.ts': `
    export class Foo {
      hi (): void {
        let a = () => 'foo'

        console.log(a())
      }
    }
    `,
    },
    {
      flags: ['--target', 'es5'],
    }
  )
  expect(output).toMatch(/createClass/)
  expect(outFiles).toEqual(['input.js'])
})

@codesandbox
Copy link

codesandbox bot commented Apr 28, 2023

CodeSandbox logoCodeSandbox logo  Open in CodeSandbox Web Editor | VS Code | VS Code Insiders

@vercel
Copy link

vercel bot commented Apr 28, 2023

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
tsup ✅ Ready (Inspect) Visit Preview 💬 Add feedback Apr 28, 2023 10:13am

@jerrywu001
Copy link
Author

There is a logical contradiction, see my specific description above. @egoist

@ComLock
Copy link

ComLock commented Apr 28, 2023

I may not understand the inner workings, but what I need to continue to work is:

https://tsup.egoist.dev/#es5-support

You can use --target es5 to compile the code down to es5, in this target your code will be transpiled by esbuild to es2020 first, and then transpiled to es5 by [SWC](https://swc.rs/).

So I suspect esbuild works with es2020 and then swc turns it back to es5 afterwards.

@ComLock
Copy link

ComLock commented Apr 28, 2023

When I set target: "es5" in the tsconfig.json I get an error, but when I set target: "es5" in the tsup.config.ts, it works as https://tsup.egoist.dev/#es5-support

So I suspect esbuild reads it's target from the tsconfig.json file.

@ComLock
Copy link

ComLock commented Apr 28, 2023

So I think this Pull Request will break ES5 support (https://tsup.egoist.dev/#es5-support)

@jerrywu001
Copy link
Author

jerrywu001 commented Apr 28, 2023

So I think this Pull Request will break ES5 support (https://tsup.egoist.dev/#es5-support)

Of course, So, esbuild.build should set the target from --tsconfig path? @ComLock

Can you improve this pull request, and commit your update. @ComLock

@ComLock
Copy link

ComLock commented Apr 28, 2023

Ideally setting target only once, in one place would be nice. But the most important thing for me is the ES5 support (https://tsup.egoist.dev/#es5-support)

esbuild isn't able to do that yet, which is why swc is used.

@ComLock
Copy link

ComLock commented Apr 28, 2023

I can try, but it might be Greek to me :)

@jerrywu001
Copy link
Author

I can try, but it might be Greek to me :)

There seems to be no solution that is compatible with both scenarios.

@ComLock
Copy link

ComLock commented Apr 28, 2023

It seems esbuild looks into the tsconfig.json and throws errors before tsup looks into the tsconfig.custom.json

@ComLock
Copy link

ComLock commented Apr 28, 2023

So esbuild is probably used by bundle-require (in loadTsupConfig in load.ts) to transpile the tsup.config.ts file.

@ComLock
Copy link

ComLock commented Apr 28, 2023

So there are at least two "environments".

  1. The environment tsup runs in (loads modules in)
  2. The target environment

By default the tsconfig.json file is used by everyone, my code editor program too :)

It's possible to use a custom tsconfig.json file with bundle-require

@jerrywu001
Copy link
Author

jerrywu001 commented Apr 28, 2023

So there are at least two "environments".

  1. The environment tsup runs in (loads modules in)
  2. The target environment

By default the tsconfig.json file is used by everyone, my code editor program too :)

It's possible to use a custom tsconfig.json file with bundle-require

So, I add an attribute to distinguish them.
image

I finished the job.
image
@ComLock @egoist

Tips:

  • --target=es5 and tsconfig.compilerOptions.target: "es5" can not use at the same time.
  • can only use --target es5 to compile the code down to es5.

@@ -7,9 +7,8 @@ export const es5 = (): Plugin => {
return {
name: 'es5-target',

esbuildOptions(options) {
if (options.target === 'es5') {
options.target = 'es2020'
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This still worries me. I think I will get an error from esbuild about it not supporting es5, rather than esbuild working with es2020 and then swc transpiling it back to es5?

Copy link
Author

@jerrywu001 jerrywu001 Apr 28, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know what you worry about. See here, the usage:

  • --target es5/tsup.target=es5 and tsconfig.compilerOptions.target: "es5" can not use at the same time.
  • can only use --target es5/tsup.target=es5 to compile the code down to es5.

@ComLock

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just tested your branch and it breaks ES5 Support as I expected

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was a pleasure to work with you. @ComLock

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Neither target on cmdline, in tsup.config.json nor tsconfig.custom.json works. All gives es5 not supported by esbuild errors.

Copy link
Author

@jerrywu001 jerrywu001 Apr 28, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

all the best, after git pull, you should run npm run build, then run npm run test-only . @ComLock

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In order to avoid the error I have to set esbuildOptions target: 'es2020' in addition to 'es5' (cmdline or tsup.config or tsconfig.custom)

Copy link
Author

@jerrywu001 jerrywu001 Apr 28, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no need to do this change @ComLock

it breaks the case
image

just this, it can pass all the cases
image

@@ -7,9 +7,8 @@ export const es5 = (): Plugin => {
return {
name: 'es5-target',

esbuildOptions(options) {
if (options.target === 'es5') {
options.target = 'es2020'
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just tested your branch and it breaks ES5 Support as I expected

@ComLock
Copy link

ComLock commented Apr 28, 2023

Everything works without this patch, I closed the issue #893 because it was all confusion. See the closing comment on it.

@ComLock
Copy link

ComLock commented Apr 28, 2023

Good learning journey though :)

@jerrywu001 jerrywu001 closed this Apr 28, 2023
@ComLock
Copy link

ComLock commented Apr 28, 2023

Sidenote: egoist/bundle-require#38

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

Successfully merging this pull request may close these issues.

Custom tsconfig isn't working
2 participants