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

question: <How to use it with esbuild> #783

Open
pokers opened this issue Jun 9, 2022 · 5 comments
Open

question: <How to use it with esbuild> #783

pokers opened this issue Jun 9, 2022 · 5 comments
Labels
type: question Questions about the usage of the library.

Comments

@pokers
Copy link

pokers commented Jun 9, 2022

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 pokers added the type: question Questions about the usage of the library. label Jun 9, 2022
@pokers pokers changed the title question: <How to use it whit esbuild> question: <How to use it with esbuild> Jun 9, 2022
@ash2048
Copy link

ash2048 commented Jun 30, 2022

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

@semiautomatixfu
Copy link

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

@marko-hologram
Copy link

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.

@freshgum-bubbles
Copy link

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/

@intellix
Copy link

intellix commented Aug 26, 2023

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: question Questions about the usage of the library.
Development

No branches or pull requests

6 participants