jvandemo / generator-angular2-library

Yeoman generator to create an Angular library
MIT License
752 stars 122 forks source link

Enums - cannot read property #265

Closed kkozlowski89 closed 6 years ago

kkozlowski89 commented 6 years ago

Hi,

I have defined enums like that:

File: enum.generated.ts

export module own{
    export enum FirstEnum {
         Test1 = 400,
         Test2 = 401
    }

  export enum SecondEnum{
         Test1 = 400,
         Test2 = 401
    }
}

so in other files I can use it like that:

import { own } from 'path/to/file/enums.generated';

I use this approach for few angular application and everything works correctly. But when I started doing my private npm package it doesn't work. When I started playground I got error messages like that: Cannot read property 'Test1' of undefined

I used it for example:

import { own } from 'path/to/file/enums.generated';

export class SampleComponent(){
     constructor(){  
            let value = own.SecondEnum.Test1
     }
}

in my module I have ofcourse:

export { own } from './enums.generated';

I tried also

export * from './enums.generated';

Any ideas? Thanks a lot!

kkozlowski89 commented 6 years ago

It seems there are no these enums of index.js file, but why?

kkozlowski89 commented 6 years ago

Ok I have my enums on index.js file In gulpfile.js I set treeshake: false

but still I cannot use it I wanted.

Anyone can help me?

caroso1222 commented 6 years ago

Does this happen only in Playground? Have you tried npm link and testing in a clean angular project? Also, try building the library and running npm pack inside dist, grab the tar.gz and run npm i path/to/tar.gz in your angular app, and see if you still got trouble.

I'd just like to isolate the problem to the playground, the build artifact, or the source code itself.

kkozlowski89 commented 6 years ago

Yes I tried in clean angular project. I resolved this issue - I made some workaround.

Few words of explanation: I generated my ts files via typelite library (c# library) Ts code is correct but js generated during tsc compilation not.

Generated js was:

var crtenum;
(function (crtenum) {
    /** @enum {number} */
    var FirstEnum = {
        Test1 : 400,
        Test2 : 401
    };
    FirstEnum [FirstEnum .Test1] = "Test1";
    FirstEnum [FirstEnum .Test2] = "Test2";
})(crtenum || (crtenum = {}));

But should be:

var crtenum;
(function (crtenum) {
    /** @enum {number} */
    crtenum.FirstEnum = {
        Test1 : 400,
        Test2 : 401
    };
    crtenum.FirstEnum [FirstEnum .Test1] = "Test1";
    crtenum.FirstEnum [FirstEnum .Test2] = "Test2";
})(crtenum || (crtenum = {}));

And I generated this code also in c# and later in gulpfile I added step to insert this code to index.js in correct place.

Everything works but I spent a lot of hours doing it.

Thanks, issue can be close.

jvandemo commented 6 years ago

@kkozlowski89 — Thank you for sharing your findings 👍 @caroso1222 — Thank you for the follow-up 👍