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

Using vite 4.4.0 or 4.4.1 breaks Typescript decorator support in some projects #13736

Closed
7 tasks done
hybridherbst opened this issue Jul 6, 2023 · 23 comments · Fixed by #13805
Closed
7 tasks done

Using vite 4.4.0 or 4.4.1 breaks Typescript decorator support in some projects #13736

hybridherbst opened this issue Jul 6, 2023 · 23 comments · Fixed by #13805
Labels
p3-minor-bug An edge case that only affects very specific usage (priority) regression The issue only appears after a new release

Comments

@hybridherbst
Copy link

Describe the bug

I'm a bit at a loss at what's going on - in a project I have using vite 4.4.0 or 4.4.1 breaks decorators in TypeScript, but going back to 4.3.9 fixes the issue.

All I'm getting on 4.4.0 is SyntaxError: Invalid character: '@' (on some code on our end that uses decorators), while in 4.3.9 everything works.

What is extra weird is that I can't reproduce the issue on StackBlitz even with the same setup / configs in place. I've included my tests so far nonetheless, as I can 100% reproduce the issue locally by switching between vite 4.4.0 and 4.3.9 as dependency.

Discord discussion: https://discord.com/channels/804011606160703521/804011606160703524/1126556878676168784

Reproduction

Tried to repro here but no luck so far: https://stackblitz.com/edit/vitejs-vite-ojnmgf?file=myPackage%2FmyCode.ts

Steps to reproduce

No response

System Info

System:
    OS: macOS 13.4.1
    CPU: (12) arm64 Apple M2 Max
    Memory: 221.48 MB / 32.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 18.16.1 - /usr/local/bin/node
    npm: 9.5.1 - /usr/local/bin/npm
  Browsers:
    Chrome: 114.0.5735.198
    Safari: 16.5.1
  npmPackages:
    @vitejs/plugin-basic-ssl: ^1.0.1 => 1.0.1 
    vite: 4.4.1 => 4.4.1

Used Package Manager

npm

Logs

Click to expand!
SyntaxError: Invalid character: '@'

Validations

@stackblitz
Copy link

stackblitz bot commented Jul 6, 2023

Fix this issue in StackBlitz Codeflow Start a new pull request in StackBlitz Codeflow.

@hybridherbst
Copy link
Author

I've read through the breaking changes in esbuild (https://github.com/evanw/esbuild/blob/main/CHANGELOG.md#0180:~:text=Prefer%20.js%20over%20.ts%20within%20node_modules%20(%233019)) and can just assume that something there affects this case...

@royair
Copy link

royair commented Jul 6, 2023

me too

@bhvngt
Copy link

bhvngt commented Jul 7, 2023

Same here. Here's the reproduction - https://codesandbox.io/p/sandbox/typescript-playground-export-forked-x2lj66?file=%2Fpackage.json%3A6%2C19;

Steps to reproduce

  • open terminal and run yarn test. It throws following error
    image

If you change the resolution of vite to 4.3.9 and then run yarn install && yarn test on the console, error goes away and test passes.
image

@Asvarox
Copy link

Asvarox commented Jul 7, 2023

Specifying "experimentalDecorators": true in tsconfig.json (compilerOptions) helps

@Levix
Copy link

Levix commented Jul 7, 2023

Specifying "experimentalDecorators": true in tsconfig.json (compilerOptions) helps

No solution to the problem.

@bluwy bluwy added p3-minor-bug An edge case that only affects very specific usage (priority) regression The issue only appears after a new release and removed pending triage labels Jul 7, 2023
@bluwy
Copy link
Member

bluwy commented Jul 7, 2023

Likely related to #13525, maybe we need to set experimentalDecorators: true by default for now.

@bhvngt
Copy link

bhvngt commented Jul 7, 2023

starting esbuild 0.18.5 ECMA decorators have been supported. So this may require a bump in the supported esbuild version for vite. In the meantime, we can live with experimentalDecorators: true

@Crone1331
Copy link

experimentalDecorators: true did not solve the problem

@hybridherbst
Copy link
Author

We also had experimentalDecorators: true already on (since we're using decorators) and the issue happens with that.

@XinChou16
Copy link

We also had experimentalDecorators: true already on (since we're using decorators) and the issue happens with that.

same as you, we already set it in tsconfig.json, still throw err

@bluwy
Copy link
Member

bluwy commented Jul 10, 2023

Another way to make sure if experimentalDecorators is really set is with this in vite.config.js:

export default {
  esbuild: {
    tsconfigRaw: {
      compilerOptions: {
        experimentalDecorators: true,
      },
   },
  },
}

It could be that your tsconfig isn't include-ing the files that use decorators.

@marwie
Copy link

marwie commented Jul 10, 2023

Same issue here, tsconfig is correct.

I also tried with the suggestion above adding it to vite.config.js without luck

@marwie
Copy link

marwie commented Jul 10, 2023

The error doesnt happen when the package comes from npm - but it does reproduce with the package installed via a local file path

@CollinHerber
Copy link

Can confirm this same issue in Aurelia projects. Enabling experimental decorators via tsconfig does not fix the problem.

@hazzo
Copy link

hazzo commented Jul 11, 2023

Not working here either.

[ERROR] Parameter decorators only work when experimental decorators are enabled

Already got "experimentalDecorators": true. Using inversifyjs decorators

@XinChou16

This comment was marked as duplicate.

@bluwy
Copy link
Member

bluwy commented Jul 12, 2023

Can anyone share a repro? There's only one (#13736 (comment)) and that worked when I set "experimentalDecorators": true. I'm thinking we can add that by default, but I'm confused why it doesn't work for some others.

@CollinHerber
Copy link

Can anyone share a repro? There's only one (#13736 (comment)) and that worked when I set "experimentalDecorators": true. I'm thinking we can add that by default, but I'm confused why it doesn't work for some others.

https://stackblitz.com/edit/au2-conventions-qvwece?file=tsconfig.json

@marwie
Copy link

marwie commented Jul 12, 2023

I just had a case where decorators silently failed for fields (no invocation of the decorator function!)
but the error started to showup when I added a method decorator. After downgrading to vite <= 4.3.9 the error disappeared and it worked as expected.

Took me a while to find this since I didnt get any errors

@bluwy
Copy link
Member

bluwy commented Jul 13, 2023

stackblitz.com/edit/au2-conventions-qvwece?file=tsconfig.json

@CollinHerber Thanks! I was able to track down another spot to fix, will handle that in the PR too. As a workaround, you can set this in your vite.config.js:

  optimizeDeps: {
    esbuildOptions: {
      tsconfigRaw: {
        compilerOptions: {
          experimentalDecorators: true
        }
      }
    }
  }

@bourquep
Copy link

bourquep commented Jul 13, 2023

In my case, Vite 4.4.x fails with the following declaration (but Vite 3.3.9 succeeds):

@themedComponent
@trackWebPage<StudyoAnalyticsPage>('Preparing')
@inject(
  StudyoWebServiceKeys.viewModelFactory,
  StudyoWebServiceKeys.localizationService,
  StudyoWebServiceKeys.reactRouterRouteService
)
@observer
export class PreparingSchoolScreen extends React.Component<PreparingSchoolScreenProps> {

The error is:

CleanShot 2023-07-13 at 06 12 43@2x

I do have experimentalDecorators set to true in my tsconfig.

Update

Setting experimentalDecorators to true in vite.config.js as described above solved it for me!

Update 2

Instead of setting experimentalDecorators in vite.config.js, I simply configured it to use my tsconfig.json (which already sets experimentalDecorators:

  optimizeDeps: {
    esbuildOptions: {
      tsconfig: 'tsconfig.json'
    }
  },

And it works.

@ChrisBellew
Copy link

ChrisBellew commented Jul 24, 2023

As a workaround I found that if you're using Nuxt 3 you can give some tsconfig to esbuild which is used internally inside nitro.

export default defineNuxtConfig({
  nitro: {
    esbuild: {
      options: {
        target: "esnext",
        tsconfigRaw: {
          compilerOptions: {
            experimentalDecorators: true,
            emitDecoratorMetadata: true,
          },
        },
      },
    },
  },
});

@github-actions github-actions bot locked and limited conversation to collaborators Aug 8, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
p3-minor-bug An edge case that only affects very specific usage (priority) regression The issue only appears after a new release
Projects
None yet
Development

Successfully merging a pull request may close this issue.