coffee-script icon indicating copy to clipboard operation
coffee-script copied to clipboard

[Iced3] ES2015 modules support

Open laurentpayot opened this issue 9 years ago • 20 comments

Hello there, a little question.

Since version 1.11 CoffeeScript supports ES2015 import and export syntax by producing an import or export statement in the resulting output. We have to add a layer of transpiler like babel or target ES6 to make it work, just like Iced3. But in Iced3, with babel, when I use the import syntax I get error: reserved word 'import'.

Is the IcedCoffeeScript codebase moving away from CoffeeScript on this point ?

laurentpayot avatar Nov 15 '16 15:11 laurentpayot

I've just noticed https://github.com/maxtaco/coffee-script/pull/194 but it doesn't seem to work…

laurentpayot avatar Nov 15 '16 15:11 laurentpayot

I'll try to cut a release today. We just haven't released in a bit. Thanks!

maxtaco avatar Nov 15 '16 15:11 maxtaco

Ah yes it has not been released yet and I have issues building it (Error: Cannot find module 'iced-runtime-3'). Thanks @maxtaco!

laurentpayot avatar Nov 15 '16 15:11 laurentpayot

Ok, should be live in v111.1.1

That's a lot of 1s!

Big thanks to @zapu who did all of the work.

maxtaco avatar Nov 15 '16 15:11 maxtaco

ES2015 imports are now working when using promises. In order to use use async/defer, I have to install the iced-runtime-3 module. But once this module installed ES2015 imports are not working anymore.

@maxtaco Is a new release also needed for iced-runtime-3?

laurentpayot avatar Nov 15 '16 16:11 laurentpayot

I didn't think so. I wonder what is up.

On Tuesday, November 15, 2016, Laurent Payot [email protected] wrote:

ES2015 imports are now working when using promises. In order to use use async/defer, I have to install the iced-runtime-3 module. But once this module installed ES2015 imports are not working anymore.

@maxtaco https://github.com/maxtaco Is a new release also needed for iced-runtime-3?

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/maxtaco/coffee-script/issues/198#issuecomment-260690849, or mute the thread https://github.com/notifications/unsubscribe-auth/AA05__2SAvupqM8_Tgft5-WMOSghQ5NVks5q-d3TgaJpZM4Kyo5f .

maxtaco avatar Nov 15 '16 16:11 maxtaco

Weird, maybe installing iced-runtime-3 overwrites iced-coffee-script with a different version?

zapu avatar Nov 15 '16 16:11 zapu

It really shouldn't. I am st a conference today so might not get a chance to look right away.

On Tuesday, November 15, 2016, Michał Zochniak [email protected] wrote:

Weird, maybe installing iced-runtime-3 overwrites iced-coffee-script with a different version?

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/maxtaco/coffee-script/issues/198#issuecomment-260691497, or mute the thread https://github.com/notifications/unsubscribe-auth/AA05_8vsoasmmwiYbKOcVtBG_5-OA_Vfks5q-d5TgaJpZM4Kyo5f .

maxtaco avatar Nov 15 '16 16:11 maxtaco

By the way I'm using iced3 -c script.coffee && babel-node script.js for my tests...

laurentpayot avatar Nov 15 '16 16:11 laurentpayot

Hm, but won't iced3 -c compile just the script.coffee, so anything that script.js tries to import might not be compiled?

zapu avatar Nov 15 '16 16:11 zapu

I'm importing "standard" js (Firebase modules). And I'm also struggling to get any result from Firebase createUser() (that returns a promise) with the await/defer syntax. Not to mention catching errors…

laurentpayot avatar Nov 15 '16 17:11 laurentpayot

async/defer is "just" a syntactic sugar for callback based asynchronous functions. So in theory you could try to do something like await promise_obj.then defer result but I'm not sure how this would turn out in your code. It might make more sense to just use the promise normally and pass anonymous functions as arguments to then.

zapu avatar Nov 15 '16 17:11 zapu

I will try to experiment with iced3 -c and babel-node once I get off work, in few hours.

zapu avatar Nov 15 '16 17:11 zapu

Thank you so much @zapu, it really helped! Now I'm using something like await promise_obj.then(defer result).catch((error) -> console.log(error.message)) in a synchronous-like way of coding. Amazing 😃

laurentpayot avatar Nov 15 '16 17:11 laurentpayot

I still have the ES2015 import issue with await/defer, I get error: import statements must be at top-level scope, if it helps you… Thanks again for your time!

laurentpayot avatar Nov 15 '16 18:11 laurentpayot

If you have top-level await/defer, it has to wrap your entire file in a function, which probably causes this error. Iced should probably handle this better, I overlooked the fact that this may be an issue. Is this the case here?

zapu avatar Nov 15 '16 18:11 zapu

Spot on. Yes after wrapping my await/defer in a function the imports issues are gone.

But now if using ES2015 imports when I call my function I get:

   __iced_passed_deferral = iced.findDeferral(arguments);
                           ^

ReferenceError: iced is not defined

laurentpayot avatar Nov 15 '16 18:11 laurentpayot

What are your compile flags right now?

Can you try something like iced3 -b -c -I node script.coffee?

zapu avatar Nov 15 '16 18:11 zapu

I tried and it worked! The -I node option was missing. That's strange because the node mode is supposed to be the default, isn't it?

laurentpayot avatar Nov 15 '16 18:11 laurentpayot

Yes, but I just remembered that ES6 modules in CoffeeScript forces the bare option, because otherwise the whole code is wrapped in a top-level function anyways. So by doing ES6 import/export, it has to "undo" that behavior. I think there was even a discussion about this in CoffeeScript repo on GitHub. In our case, forcing bare also makes it not include the require('iced-runtime') in the code.

Thank you for going through all of this, I think there are few places that we can make improvements in Iced.

zapu avatar Nov 15 '16 18:11 zapu