typedi icon indicating copy to clipboard operation
typedi copied to clipboard

question: Using Typedi with Vite - Returning ContainerInstance2

Open lucastanger opened this issue 3 years ago • 3 comments

I was trying to... We are using an Nx monorepo for our projects. As Nx included Vite in its most recent release, we wanted to try it out in one of our react projects. The brain of our applications is a library that injects services via typedi, which worked without any problems.

The problem: Since we changed to Vite, typedi is not injecting the services anymore. One thing i noticed was that instead of returning the Service as expected it returned an Instance of ContainerInstance2. I could not find any explanation why this suddenly occurs?

lucastanger avatar Dec 12 '22 13:12 lucastanger

I have no experience with vite but the container instance is injected as the last parameter always, so it seems like your service is not recognized for some reason. Could you please setup a repo where we could test?

attilaorosz avatar Dec 12 '22 14:12 attilaorosz

I ran into a similar problem, it is because esbuild (it is used under the hood in vite) intentionally does not support emitDecoratorMetadata (https://github.com/evanw/esbuild/issues/257), this causes decorators to not work.

I was helped by the solution https://github.com/anatine/esbuildnx/tree/main/packages/esbuild-decorators

JoCat avatar Mar 16 '23 22:03 JoCat

This works for me.

import { esbuildDecorators } from '@anatine/esbuild-decorators';

export default defineConfig(() => {
  return {
    ...
    optimizeDeps: {
      esbuildOptions: {
        plugins: [
          esbuildDecorators({
            tsconfig: 'tsconfig.app.json',
          })
        ],
      }
    }
  };
});


asika32764 avatar Oct 08 '23 19:10 asika32764