Webpack tree shaking - removes export called Entity

217 views Asked by At

I stumbled across a very strange name issue. I'm trying to use a TypeScript library (TypeORM) in an Ionic App which uses webpack to build and compile the app.

TypeORM has decorators for Entities called Entity, AbstractEntity and others. When I import and use the Entity decorator is does not get marked as used by webpack. When I switch to the AbstractEntity it is marked as used. Every other decorator gets marked as used.

This Typescript class

import { Entity, Column, PrimaryGeneratedColumn } from 'typeorm';

@Entity()
export class User {
   @PrimaryGeneratedColumn()
   user_id_internal: number;

   @Column({unique: true})
   user_id: string;
   @Column()
   first_name: string;
   @Column()
   last_name: string;
}

Is translated into

var User = (function () {
   function User() {
   }
   return User;
}());
__decorate([
   Object(__WEBPACK_IMPORTED_MODULE_0_typeorm__["f" /* PrimaryGeneratedColumn */])(),
   __metadata("design:type", Number)
], User.prototype, "user_id_internal", void 0);
__decorate([
    Object(__WEBPACK_IMPORTED_MODULE_0_typeorm__["a" /* Column */])({ unique: true }),
    __metadata("design:type", String)
], User.prototype, "user_id", void 0);
__decorate([
    Object(__WEBPACK_IMPORTED_MODULE_0_typeorm__["a" /* Column */])(),
    __metadata("design:type", String)
], User.prototype, "first_name", void 0);
__decorate([
    Object(__WEBPACK_IMPORTED_MODULE_0_typeorm__["a" /* Column */])(),
    __metadata("design:type", String)
], User.prototype, "last_name", void 0);
User = __decorate([
    Object(__WEBPACK_IMPORTED_MODULE_0_typeorm__["b" /* Entity */])()
], User);

but Entity is marked as unused

// index.ts imports
/* unused harmony namespace reexport */
/* harmony import */ var __WEBPACK_IMPORTED_MODULE_31__decorator_entities_Entity__ = __webpack_require__(382);

// Entity.ts exports
/* harmony import */ var __WEBPACK_IMPORTED_MODULE_0__index__ = __webpack_require__(4);
/* unused harmony export Entity */

but when I change Entity to AbstractEntity gets marked as used

// index.ts
/* harmony import */ var __WEBPACK_IMPORTED_MODULE_32__decorator_entities_AbstractEntity__ = __webpack_require__(555);
/* harmony namespace reexport (by used) */ __webpack_require__.d(__webpack_exports__, "a", function() { return __WEBPACK_IMPORTED_MODULE_32__decorator_entities_AbstractEntity__["a"]; });

// AbstractEntity.ts
/* harmony export (immutable) */ __webpack_exports__["a"] = AbstractEntity;
/* harmony import */ var __WEBPACK_IMPORTED_MODULE_0__index__ = __webpack_require__(4);

Any ideas as to why?

1

There are 1 answers

0
Daniel Kurz On BEST ANSWER

It turns out, that Entity was exported twice in the main typescript file. The compiler doesn't complain about it but webpack seems to have a problem with it. Removing the second export fixed the problem.