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

Compiler error using @key decorator #98

Closed
alariej opened this issue Feb 21, 2019 · 21 comments
Closed

Compiler error using @key decorator #98

alariej opened this issue Feb 21, 2019 · 21 comments

Comments

@alariej
Copy link

alariej commented Feb 21, 2019

I have tested the simpler subscription examples successfully, and now I am trying autosubscriptions using the @key decorator, such as in your example:

@autoSubscribe
    getTodosForUser(@key username: string) {
        return this._todosByUser[username];
    }

Unfortunately, I get a compiler error:

Error: Module parse failed: Unexpected character '@' (1:20609)
You may need an appropriate loader to handle this file type.

It seems like Babel has a problem with the @key decorator being used within a function, and not with other decorators like @autosubscribe and @AutoSubscribeStore.

I've checked my babel.config.js and everything seems OK, using:

  const plugins = [
    ['@babel/plugin-proposal-decorators', { legacy: true }],
    ["@babel/plugin-proposal-class-properties", { "loose": true}]
  ];

Did someone else come across this problem? Any recommendation on how to solve this?

@deregtd
Copy link
Collaborator

deregtd commented Feb 21, 2019

In theory, once the typescript outputs the javascript that babel will be processing, the decorators should be completely gone. Are you trying to use the decorators straight inside javascript?

@alariej
Copy link
Author

alariej commented Feb 21, 2019

No, I use it in my typescript code. As I wrote, it is odd, since all decorators other than the @key create no problem at all.

@deregtd
Copy link
Collaborator

deregtd commented Feb 21, 2019

It appears that using decorators straight in babel has different support than Typescript, and reading the comments, they're actually a little mad that Typescript went ahead and implemented function parameter decorators:
babel/proposals#13 (more context at babel/babel#1301)

So I think you have to use Typescript to process the decorators, not babel, at this time.

@alariej
Copy link
Author

alariej commented Feb 22, 2019

I see, thanks for info. You mean, I should remove the Babel decorator plugin in babel.config.js? Do I need a Typescript plugin then? Any additional info you can point me to?

@deregtd
Copy link
Collaborator

deregtd commented Feb 22, 2019

I'm not even sure what the right terminology is, but in most of our apps, we only use babel later in the build pipeline. Typescript compiles the decorators down into non-decorated ES5 or ES6 (depending on the project), and then babel runs some other cleanup on it from there. If you look at this TodoList sample app from the ReactXP repo, it shows using resub with a typescript app and a simplified version of the build pipeline that we use in our private projects:

https://github.com/Microsoft/reactxp/tree/master/samples/TodoList

@thegalah
Copy link

Any update on this or on the best way to resolve the issue? None of the examples seem to use the @key function parameter decorator

@deregtd
Copy link
Collaborator

deregtd commented May 20, 2019

Several of the unit tests use @key. You can start with one of those build systems.

@BiggA94
Copy link
Contributor

BiggA94 commented Jun 26, 2019

UPDATE:
Sorry, I used the key function incorrectly.
Updated for the working approach :)

I don't know your actual code, but I have the same problem.
So my aproach was, that i call the key function manually.
Something like

class SubStore extends StoreBase {
@autoSubscribe
    getTodosForUser(username: string) {
        return this._todosByUser[username];
    }
}

key(SubStore .prototype, 'getTodosForUser', 0);

should work.

@deregtd
Copy link
Collaborator

deregtd commented Jun 26, 2019

Very strange. We have @key working lots of places in our codebase:

    @autoSubscribe
    isLoading(@key username: string): boolean {
    ...
        "resub": "^1.2.0",
        "typescript": "3.5.1",

@thegalah
Copy link

thegalah commented Jun 30, 2019

Yeah I wasn't able to get @key working. My build config is the same as the create react-xp app from https://github.com/a-tarasyuk/create-rx-app. @BiggA94 's method seems to work though

@deregtd
Copy link
Collaborator

deregtd commented Jul 1, 2019

@a-tarasyuk Have you tried using @key in any of your samples? I've never actually tried building with the create-rx-app since I always just copy an older project of mine and keep evolving my private stash of garbage. :)

@a-tarasyuk
Copy link
Contributor

a-tarasyuk commented Jul 4, 2019

@deregtd no, I have not tried. I suppose that the problem with the @babel/plugin-proposal-decorators which doesn't handle parameter decorators., I think there are babel plugins which can be used to add this feature., if we are talking about create-rx-app - it doesn't include all possible babel plugins :).

@berickson1
Copy link
Collaborator

I just ran into this problem with a personal project - using React Native with babel transpiling typescript. As @a-tarasyuk mentioned above, you need an additional plugin to add babel support.

I had success with babel-plugin-parameter-decorator

My plugin list looks like this:

  plugins: [
    ["@babel/plugin-proposal-decorators", { legacy: true }],
    "babel-plugin-parameter-decorator"
  ],

berickson1 added a commit that referenced this issue Jul 12, 2019
@berickson1
Copy link
Collaborator

I just put out a PR to add a note about babel to our README, this should help unblock consumers using babel. @thegalah, can you try this out?

@thegalah
Copy link

thegalah commented Jul 12, 2019

I tried adding babel-plugin-parameter-decorator and I get:

Module build failed (from ./node_modules/babel-loader/lib/index.js):
TypeError: Cannot read property 'name' of undefined

@berickson1
Copy link
Collaborator

Opened a bug on babel-plugin-parameter-decorator
WarnerHooh/babel-plugin-parameter-decorator#5

@WarnerHooh
Copy link

WarnerHooh commented Jul 19, 2019

Hi all,

Nice to hear this plugin babel-plugin-parameter-decorator helps. And thanks your feedback @berickson1 .
I have published a new version(1.0.8) already. This should fix the problem.
Please feel free to let me know if you still have the problem.

deregtd pushed a commit that referenced this issue Jul 19, 2019
* Update Readme to address #98

* Update README.md
@berickson1
Copy link
Collaborator

Thanks @WarnerHooh - this solves my issues. @thegalah - can you bump package version to 1.0.8 and try again?

@thegalah
Copy link

@berickson1 I've bumped the package version to 1.0.8 and I now get:

ERROR in ./src/Services/Data/Stores/UserScheduleStore.ts
Module build failed (from ./node_modules/babel-loader/lib/index.js):
TypeError: Cannot read property 'forEach' of undefined
    at node_modules\babel-plugin-parameter-decorator\lib\index.js:84:60

@WarnerHooh
Copy link

Hi @thegalah,

Thanks for the feedback, I had a quick fix in 1.0.9. Hope all good if you try with this version.

@thegalah
Copy link

This worked for me! Thanks for the great work!

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

7 participants