Minification issue : Missing required @inject or @multiInject annotation in: argument 0 in class e
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)
- bind a service using bind( ... string ...).to(... object ....)
- minify the app using webpack or angualr (with terser plugin)
- 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..) .
~~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
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.
~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 ?
@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.
the SomeService is the class going to export or extends/implements class/interface?