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

Usage without Reflect #180

Open
Djaler opened this issue Oct 2, 2021 · 6 comments
Open

Usage without Reflect #180

Djaler opened this issue Oct 2, 2021 · 6 comments

Comments

@Djaler
Copy link

Djaler commented Oct 2, 2021

Is your feature request related to a problem? Please describe.
I need to install reflect-metadata even when if emitDecoratorMetadata not used.

Description
In some cases emitDecoratorMetadata can't work. For example, when using esbuild as typescript compiler (which is used in popular Vite).
Tsyringe can work in such environment, it just will not interfere injection token (one need to provide token to @inject manually), but I still need to install and import reflect-metadata because of tsyringe requires a reflect polyfill. Please add 'import "reflect-metadata"' to the top of your entry point.

Alternate solutions
I understand why this check is needed in most cases, but maybe you can add a way to tell tsyringe that I don't need a Reflect?

@roblebel
Copy link

+1 We have the exact same need. A warning log could be sufficient instead of throwing an error, since it's not true that tsyringe require a reflect polyfill.

@shrink
Copy link

shrink commented Nov 30, 2021

I've had success switching from reflect-metadata to @abraham/reflection which is a drop-in replacement and works with esbuild and vite.

// main.ts
-import "reflect-metadata";
+import "@abraham/reflection";

// Your code here...

@Djaler
Copy link
Author

Djaler commented Nov 30, 2021

@shrink the only plus here is that @abraham/reflsection is much smaller. But it still not really work with esbuild (because esbuild doesn't produce a code which will call Reflect API

@dummdidumm
Copy link

I had the same problem when converting a project from a Rollup-based solution to SvelteKit, which uses Vite, which uses ESBuild. I needed to provide the import although it's not needed when using the explicit constructor(@inject(OtherClass) otherClass: OtherClass) {..} method.

@dh94
Copy link

dh94 commented Mar 25, 2023

I have found a way to make it work in vite 4+.
I've used this package: @anatine/esbuild-decorators
and added it to the optimizeBuild step.
From my understanding it shouldn't have worked after build (because from the docs vite does not use esbuild when building for prod) , but it does work!

https://stackblitz.com/edit/vitejs-vite-hzkjj3?file=vite.config.ts

@ghost
Copy link

ghost commented Jan 24, 2024

Here's a system for using TSyringe without decorators at all, by simply making the same calls to Reflect.decorate() and Reflect.metadata() which would normally be compiled by the decorators - https://gist.github.com/waynebloss/28fd81a00eec98a67007bec40473a71b

It's a work in progress and being used only internally, not packaged in any way. Some inspiration was taken from the source code of VS Code.

It also allows for nice autocomplete for dependencies which were registered by name and a shorthand for doing injectWithTransform, injectAll, injectAllWithTransform, e.g.

registerClass(MyClass, [
  "dep1",                            // Inject dep1 by id.
  ["dep2"],                          // ...array of dep2 by id.
  ["dep3", getYesNo, ...yesNoArgs],  // ...dep3 transformed to bool.
  [Dep4, [getId, ...getIdArgs]],     // ...dep4 array transformed to string.
  YadaProvider,                      // ...exact type.
]);

image

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

No branches or pull requests

7 participants