InversifyJS icon indicating copy to clipboard operation
InversifyJS copied to clipboard

Minification issue : Missing required @inject or @multiInject annotation in: argument 0 in class e

Open elsasslegend opened this issue 5 years ago • 5 comments

Error Missing required @inject or @multiInject annotation in: argument 0 in class e after class name has been mangled by a minification plugin (such as TerserPlugin).

Current Behavior

Issue is located in the function :

getFunctionName(v: any): string {
    if (v.name) {
        return v.name;
    } else {
        const name = v.toString();
        const match = name.match(/^function\s*([^\s(]+)/);
        return match ? match[1] : `Anonymous function: ${name}`;
    }
}

This function relies on the constructor/class name which is mangled during minification (using Angular TerserPlugin).

Possible Solution

Couldn't the serviceIdentifier be used as key ? Or maybe pass into @injectable decorator a string to refer to the class once minified ?

Steps to Reproduce (for bugs)

  1. bind a service using bind( ... string ...).to(... object ....)
  2. minify the app using webpack or angualr (with terser plugin)
  3. use Container.get(bindingName). An error is throw.

Is there any workaround to avoid this (other that disabling class name mangle, which should not be seen as a solution..) .

elsasslegend avatar Dec 04 '20 14:12 elsasslegend

~~I have the exact same issue and i've bean banging my head against the wall for hours, because my code worked in development, but crashed on startup in production~~

EDIT: after research, it seems my issue was not related to inversify.js

lkarmelo avatar Dec 31 '20 10:12 lkarmelo

Unfortunately I didn't find any proper solution. This seems to be related to minification, probably when using TerserPlugin shipped with angular. For some (long to explain) reasons, we also have to use inversify on client side (in conjunction with angular dependency injection). The only thing that worked for me, but that is absolutely ugly, is to always use property injection. They are not impacted by the minification process. If you find some other workarounds please let me know. It seems that inversify lib is not maintained anymore, even while being massively used by Frameworks Like NestJs.

elsasslegend avatar Dec 31 '20 13:12 elsasslegend

~I have the exact same issue and i've bean banging my head against the wall for hours, because my code worked in development, but crashed on startup in production~

EDIT: after research, it seems my issue was not related to inversify.js

What was it for you ?

elsasslegend avatar Apr 12 '21 13:04 elsasslegend

@djflex68 It was just my misunderstanding of how terser plugin works. I had statements in my code like this:

/*#__PURE__*/decorate(injectable(), SomeService);

which were, erased in production, leaving SomeService undecorated.

lkarmelo avatar Apr 12 '21 13:04 lkarmelo

the SomeService is the class going to export or extends/implements class/interface?

alanyong91 avatar Oct 20 '21 05:10 alanyong91