Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generating js output of the ts file #244

Closed
alirezamirian opened this issue Jul 12, 2018 · 5 comments
Closed

Generating js output of the ts file #244

alirezamirian opened this issue Jul 12, 2018 · 5 comments

Comments

@alirezamirian
Copy link

We are using this plugin to maintain a generated npm package for types and use that in frontend via npm.
The problem is that Enums don't work at runtime, and are undefined. We use <mapEnum>asEnum</mapEnum> and <nonConstEnums>true</nonConstEnums> and also
<outputFileType>implementationFile</outputFileType>.
When I put the js transpiled version of the generated types beside it, it works fine but without that, enums are undefined in runtime.

@vojtechhabarta
Copy link
Owner

I am not sure what you exactly mean. When I use following settings in test:

settings.outputFileType = TypeScriptFileType.implementationFile;
settings.outputKind = TypeScriptOutputKind.module;
settings.mapEnum = EnumMapping.asEnum;
settings.nonConstEnums = true;

generated enum looks like this:

export enum Direction {
    North = "North",
    East = "East",
    South = "South",
    West = "West",
}

When you are generating implementation file (*.ts) you need to compile it somewhere to *.js file.

When I compiled this TypeScript code I got following JavaScript code:

var Direction;
(function (Direction) {
    Direction["North"] = "North";
    Direction["East"] = "East";
    Direction["South"] = "South";
    Direction["West"] = "West";
})(Direction = exports.Direction || (exports.Direction = {}));

So when you import this file then Direction should be usable.

Is this what you expect?

@alirezamirian
Copy link
Author

alirezamirian commented Jul 12, 2018 via email

@vojtechhabarta
Copy link
Owner

This is really disadvantage of generating .ts files instead of .d.ts file, it needs to be compiled.

I don't have any plans to generate JavaScript code because that's really responsibility of TypeScript compiler.

It is possible to generate package.json file using generateNpmPackageJson parameter. And if you want to invoke TypeScript compiler from Maven you can use frontend-maven-plugin which is able to download nodejs and run it.

@alirezamirian
Copy link
Author

I see. Though I'm intentionally generating .ts files to be able to use enums at runtime.
One solution (other than compiling to js) for my situation would be to configure typescript to include that particular generated package from node_modules, but it seems there is an open issue in emitting compiled code from node_modules.

@vojtechhabarta
Copy link
Owner

Closing the issue (as part of cleanup after release).
Hopefully you solved the problem to your needs. We can reopen the issue if needed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants