class-transformer icon indicating copy to clipboard operation
class-transformer copied to clipboard

question: @Type() is not working anymore after compilation with rollup

Open MickL opened this issue 4 years ago • 7 comments

Description

I do have a shared project that contains interfaces and dto's:

export class MySubDto {
  @IsString()
  name: string;
}

export class MyClassDto {
  @IsString()
  id: string;

  @Type(() => MySubDto)
  @ValidateNested()
  child: MySubDto;
}

Which works fine if I use plainToClass() in the same project:

MyClassDto { id: '1', child: MySubDto { name: 'cat' } }

But after I compiled the project with Rollup, import MyClassDto and use plainToClass() the @Type does not have any effect anymore:

MyClassDto { id: '1', child: { name: 'cat' } }

I wonder if you could give me any hint why this could be the case. For everything else (class validator) it works fine. Maybe this is a bug? Or what could it be?

I do compile to umd and es5, not sure which one my app (node.js) picks up.

Version: 0.4.0

I have created a minimal reproduction example: https://github.com/MickL/class-transformer-type

MickL avatar Feb 23 '21 18:02 MickL

There are some notes in the console while compiling, not sure if they are related to the issuse:

(!) `this` has been rewritten to `undefined`
https://rollupjs.org/guide/en/#error-this-is-undefined
../node_modules/class-validator/esm5/validation/Validator.js
1: var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
                    ^
2:     function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3:     return new (P || (P = Promise))(function (resolve, reject) {
...and 3 other occurrences
../node_modules/class-transformer/esm5/ClassTransformer.js
1: var __assign = (this && this.__assign) || function () {
                   ^
2:     __assign = Object.assign || function(t) {
3:         for (var s, i = 1, n = arguments.length; i < n; i++) {
...and 1 other occurrence
../node_modules/class-validator/esm5/decorator/common/ValidateNested.js
1: var __assign = (this && this.__assign) || function () {
                   ^
2:     __assign = Object.assign || function(t) {
3:         for (var s, i = 1, n = arguments.length; i < n; i++) {
...and 1 other occurrence

MickL avatar Feb 23 '21 18:02 MickL

@NoNameProvided May you take a look at this? :)

MickL avatar Mar 01 '21 17:03 MickL

The compiler most probably strips the type information. Make sure the built version contains the type metadata attached to the classes and properties. It should look something like this: __metadata("design:type", <class-type-here>)

NoNameProvided avatar Mar 02 '21 10:03 NoNameProvided

@NoNameProvided I see this in the generated built files:

ES5: https://github.com/MickL/class-transformer-type/blob/e61dbf6161033c5dce321b23984c9193aa49234d/my-lib/dist/my-lib.es5.js#L710 UMD: https://github.com/MickL/class-transformer-type/blob/e61dbf6161033c5dce321b23984c9193aa49234d/my-lib/dist/my-lib.umd.js#L718

Is the issue related to the message of rollup about "this"? May you have time to take a look at my minimal reproduction repo? https://github.com/MickL/class-transformer-type

MickL avatar Mar 02 '21 11:03 MickL

@NoNameProvided Would be very glad if you could take a look at the repo. Is the issue related to the warning about "this"?

MickL avatar Mar 22 '21 11:03 MickL

I am not super familiar with Rollup, so I don't know. I may take a look at this on the weekend but not promising a thing, as there is a lot of issue to look at.

NoNameProvided avatar Mar 23 '21 16:03 NoNameProvided

I had a similar issue. I added class-transformer as a dependency in package.json of my shared-types package and it worked :D

azertyalex avatar Apr 21 '23 14:04 azertyalex