NodeJS: How to add a "global" require directive?

96 προβολές
Παράβλεψη και μετάβαση στο πρώτο μη αναγνωσμένο μήνυμα

Cambiata

μη αναγνωσμένη,
12 Ιουλ 2014, 2:59:18 μ.μ.12/7/14
ως haxe...@googlegroups.com
I'm trying to get React.hx going on server side, and it's works fine except for one annoying thing:
The component classes extend React, and therefore a require("react") directive is needed "outside" the class definitions.

This is what the compiler generates, wich causes an "ReferenceError: React is not defined" error:

(function () { "use strict";
   
...
   
Content.__super__ = React;
   
...

This is what is needed:

(function () { "use strict";
   
var React = require("react"); // <- This line has to be added!
   
...
   
Content.__super__ = React;
   
...

Right now I'm running a post-build script that adds this missing line, but how can this (or equivalent) be accomplished by haxe code?
(Please excuse my nodenoobishness..!)

/ Jonas



Juraj Kirchheim

μη αναγνωσμένη,
12 Ιουλ 2014, 3:27:05 μ.μ.12/7/14
ως haxe...@googlegroups.com
With 3.1.3 this works:

@:native('require("react")')
class React {}

Please note that every time you refer to the value `React` it will be
required, which is not too bad thanks to the module cache, but should
not happen in performance critical sections of your app.

With the nightly you can do this instead:

@:jsRequire("react")
class React {}

Alternatively something like this would also work:

package react;

extern class React {
//...
#if nodejs
static inline function __init__():Void {
untyped (react.React = require("react"));
}
#end
}

Best,
Juraj

clemos

μη αναγνωσμένη,
13 Ιουλ 2014, 4:50:38 π.μ.13/7/14
ως haxe...@googlegroups.com

Hi Juraj,

I don't think the latter would work, as __init__  functions are injected after class definitions. The module won't be required yet when the child class is build.
I believe @jsRequire is the way to go.

Best,
Clément

--
To post to this group haxe...@googlegroups.com
http://groups.google.com/group/haxelang?hl=en
---
You received this message because you are subscribed to the Google Groups "Haxe" group.
For more options, visit https://groups.google.com/d/optout.

Juraj Kirchheim

μη αναγνωσμένη,
13 Ιουλ 2014, 5:30:26 π.μ.13/7/14
ως haxe...@googlegroups.com
Ah, yes, I forgot it's about subclassing. Yes, then only the two first
ones will work.

Cambiata

μη αναγνωσμένη,
13 Ιουλ 2014, 5:46:23 π.μ.13/7/14
ως haxe...@googlegroups.com
Thank you, Juraj and Clemos!

The @:native('require("react")') approach sort of solves it, but it gives lots of these. Not that pretty, and not performance ideal either, as I understand it.
Maybe I'll stick with my post-compile rewrite, if there's no other way to add code to the root scope outside the classes..?





clemos

μη αναγνωσμένη,
13 Ιουλ 2014, 6:15:45 π.μ.13/7/14
ως haxe...@googlegroups.com
Have you tried @:jsRequire ?

Otherwise, my haxe-js-kit project features an alternative way : 
It's not fully documented, but it uses a mix of __init__ and @:native that produces :
@:native('( react.React || require("react") )')
and
static function __init__(){ react.React = require('react'); }
so it's both extendable and probably more optimal than just 'require'.
It also makes it possible to easily / automatically export dependencies in a 'package.json' file for distribution...

But then again, @:jsRequire does just what you want in a more concise / less hackish way.

Best,
Clément


Juraj Kirchheim

μη αναγνωσμένη,
13 Ιουλ 2014, 6:58:11 π.μ.13/7/14
ως haxe...@googlegroups.com
On Sun, Jul 13, 2014 at 12:15 PM, clemos <cl3...@gmail.com> wrote:
> Have you tried @:jsRequire ?

That's not in the latest stable Haxe version. If that's not a
requirement, then sure, this is the best option by far ;)

Cambiata

μη αναγνωσμένη,
13 Ιουλ 2014, 12:33:52 μ.μ.13/7/14
ως haxe...@googlegroups.com
I'm still on 3.1.3. Will try @:jsRequire when I have time for 3.2...
Thanks for the haxe-js-kit tip, btw!


Franco Ponticelli

μη αναγνωσμένη,
13 Ιουλ 2014, 1:12:56 μ.μ.13/7/14
ως haxe...@googlegroups.com
Until the new release is out you can get the best of both worlds by doing:

@:native('react.React || (react.React = require("react"))')

no?


On Sun, Jul 13, 2014 at 10:33 AM, Cambiata <jona...@gmail.com> wrote:
I'm still on 3.1.3. Will try @:jsRequire when I have time for 3.2...
Thanks for the haxe-js-kit tip, btw!


Απάντηση σε όλους
Απάντηση στον συντάκτη
Προώθηση
0 νέα μηνύματα