typedi icon indicating copy to clipboard operation
typedi copied to clipboard

question: <How to use it with esbuild>

Open pokers opened this issue 3 years ago • 5 comments

Hi,

I'm trying to use typedi on my project which is written by typescript and build it by using esbuild. After build bundle using esbuild, there no source code I used @Service to inject. Is there any options or way to use it? Here are my config option and version.

esbuild : 0.14.41 typescript : 4.6.4 typedi : 0.10.0

tsconfig.js

{
  "compilerOptions": {
    "target": "es6",                                  /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */
    "lib": ["es6"],                                        /* Specify a set of bundled library declaration files that describe the target runtime environment. */
    "experimentalDecorators": true,                   /* Enable experimental support for TC39 stage 2 draft decorators. */
    "emitDecoratorMetadata": true,                    /* Emit design-type metadata for decorated declarations in source files. */
    "module": "commonjs",                                /* Specify what module code is generated. */
    "moduleResolution": "node",                       /* Specify how TypeScript looks up a file from a given module specifier. */
    "declaration": true,                              /* Generate .d.ts files from TypeScript and JavaScript files in your project. */
    "emitDeclarationOnly": true,                      /* Only output d.ts files and not JavaScript files. */
    "outDir": "./dist",                                   /* Specify an output folder for all emitted files. */
    "esModuleInterop": true,                             /* Emit additional JavaScript to ease support for importing CommonJS modules. 
    "forceConsistentCasingInFileNames": true,            /* Ensure that casing is correct in imports. */
    "strict": true,                                      /* Enable all strict type-checking options. */
    "strictPropertyInitialization": false,             /* Check for class properties that are declared but not set in the constructor. */
    "skipLibCheck": true                                 /* Skip type checking all .d.ts files. */
  }
}

esbuild

esbuild
  .build({
    entryPoints,
    bundle: true,
    outdir: path.join(__dirname, outDir),
    outbase: functionsDir, 
    platform: 'node',
    sourcemap: 'inline',
});

The problem:

pokers avatar Jun 09 '22 07:06 pokers

typedi 0.8.0, typescript 4.7.4 + esbuild works ok for me on a project.

There are many breaking changes in 0.9.0 - https://github.com/typestack/typedi/blob/develop/CHANGELOG.md#090---20210110

ash2048 avatar Jun 30 '22 10:06 ash2048

And this is why we've switched most of our project to alternative DI libraries.

semiautomatixfu avatar Aug 16 '22 01:08 semiautomatixfu

And this is why we've switched most of our project to alternative DI libraries.

What are those alternatives? I'm looking for a good DI library for JS/TS, but most seem to be unmaintained at this point.

marko-hologram avatar Feb 25 '23 23:02 marko-hologram

And this is why we've switched most of our project to alternative DI libraries.

What are those alternatives? I'm looking for a good DI library for JS/TS, but most seem to be unmaintained at this point.

Well, after being a bit miffed about TypeDI, I ended up making a fork: https://github.com/freshgum-bubbles/typedi. I'd say it's pretty feature complete. The API is still technically in beta stage, but it's just a matter of ironing out the interface and adding more features on top of Typestack's TypeDI. It's feature-complete, of course, and I've written up a documentation site for it too: https://typedi.js.org/

freshgum-bubbles avatar Aug 04 '23 20:08 freshgum-bubbles

esbuild states that it doesn't support Decorator Metadata: https://esbuild.github.io/content-types/#no-type-system so I'm not sure how you're getting it to work with that? do you have a transformation step beforehand?

Tried removing it and relying on the new stage 3 decorators, available in TS 5.0: https://devblogs.microsoft.com/typescript/announcing-typescript-5-0/#decorators but I get warnings that I can't use @Inject within a constructor:

constructor(
  @Inject(APP_CONFIG) private appConfig: AppConfig,
) {}

I'm guessing to use stage 3 I'd need to refactor that to look like this?

constructor() {
  this.appConfig = Container.get(APP_CONFIG);
}

When I look at the spec for stage 3 decorators: https://github.com/tc39/proposal-decorators#access-and-metadata-sidechanneling

It looks like this sort of thing would be supported but I'm not sure if TypeDI supports that? or if stage 3 are supported at all?

export class MyService {
  @Inject(APP_CONFIG) private appConfig: AppConfig;

  constructor() {...}
}

Was hoping to use esbuild to speed up build times but constantly running into new walls and the information is so sparse on the subject of modules, decorators etc. This is the frustrating thing when relying on specs that can change... we're stuck in the transition period where I don't really know what's happening, what's working, what's supported or how.

It would be great if typedi could add a notice explaining if it depends on stage 2

intellix avatar Aug 26 '23 11:08 intellix