lgrignon / jsweet-gradle-plugin

Brings the power of JSweet to Gradle
Apache License 2.0
17 stars 15 forks source link

Generated namespace causing error in frontend #29

Closed Skillzore closed 3 years ago

Skillzore commented 3 years ago

After transpiling my java code into typescript and hooking it into my frontend, I get the following error:

Failed to compile.

./src/jsweet/typescript/com/myapp/pocjsweet/validate.ts
SyntaxError: ~/git/poc-jsweet/frontend/src/jsweet/typescript/com/myapp/pocjsweet/validate.ts: Namespace not marked type-only declare. Non-declarative namespaces are only supported experimentally in Babel. To enable and review caveats see: https://babeljs.io/docs/en/babel-plugin-transform-typescript
  1 | /* Generated from Java with JSweet 3.0.0 - http://www.jsweet.org */
> 2 | namespace com.myapp.pocjsweet {
    |           ^^^
  3 |     export class validate {
  4 |         /*private*/ value: string;
  5 | 

Is this caused by the babel version included with create-react-app? I am not using namespace at all in the frontend at the moment.

lgrignon commented 3 years ago

I would recommend you to target es6 and module mode es6 for a better integration with recent TS app :)

Skillzore commented 3 years ago

You mean with create-react-app or jsweet?

Skillzore commented 3 years ago

This seems to be Babel not being supportive of full typescript namespaces. https://babeljs.io/docs/en/babel-plugin-transform-typescript#impartial-namespace-support

Since using Babel is a must (whether it be with create-react-app or not), jsweet might not work out for us then. Unless we can somehow get jsweet not to use namespaces.

Skillzore commented 3 years ago

Also, namespaces aren't recommended to be used anymore. Even though the rumor about them being deprecated is false. https://github.com/microsoft/TypeScript/issues/30994

Skillzore commented 3 years ago

Even typescript themselves recommend using modules over namespaces: https://www.typescriptlang.org/docs/handbook/namespaces-and-modules.html

So why is jsweet generating a namespace from my java package and not a module?

lgrignon commented 3 years ago

There are multiple module mode, that's what I was talking about. Please specify module mode es2015$ https://github.com/lgrignon/jsweet-maven-plugin#basic-configuration

I recommend you to read some of the documentation. All of this is already explained.

lgrignon commented 3 years ago

config part of the readme: https://github.com/lgrignon/jsweet-gradle-plugin options of the maven plugin: https://github.com/lgrignon/jsweet-maven-plugin#basic-configuration some of the language specs: https://github.com/cincheo/jsweet/blob/master/doc/jsweet-language-specifications.md

lgrignon commented 3 years ago

and of course the examples :)

lgrignon commented 3 years ago

please tell me if module option fixed your problem, and close this issue if so

Skillzore commented 3 years ago

Yeah, I started to realize that you might have meant a jsweet setting. running module 'es2015' resolved the issue at hand but got another error. The generated file is missing a default export which results in an error:

Failed to compile.

./src/App.tsx
Attempted import error: './jsweet/typescript/com/myapp/pocjsweet/validate' does not contain a default export (imported as 'validate').

How do I get jsweet to generate a default export? I can't find a plugin option that does this.

lgrignon commented 3 years ago

It should generate exports for your classes. Cannot you import the generated class identifier? You should

Skillzore commented 3 years ago

Yeah, sorry, just had to import { validate } instead and it works! Thanks a lot!