How to get entire export object in Ceylon JavaScript

34 views
Skip to first unread message

Lucono

unread,
Dec 24, 2016, 9:03:48 AM12/24/16
to ceylon-users
Hi,

When I have a JavaScript module that exports data in the following way:

/* my-module.js */

var data = {
  one
: 1,
  two
: 2,
  three
: 3
};

module.exports = data;

In Ceylon JS, how do I import and get a handle on the entire "data" object exported as the module? In plain JS I would have something like:

var data = require('my-module');

But how would I do this in Ceylon JS?

Thanks.




Enrique Zamudio

unread,
Dec 25, 2016, 11:15:32 AM12/25/16
to ceylon-users
Actually, if your module is in commonJS format, you'd need to do

var data=require('my-module').data;

To get that object.

In Ceylon, you need to import the JS module in your module descriptor and then import the object in your source code. Then second part is easy:

import my.module { data }

shared void run() {
dynamic {
print(data.one);
}
}

And in your module descriptor:

import npm:"my-module" "version";

If your module is not in npm, just something in your NODE_PATH or require.js path, then it's just easier to directly require it in your code:

shared void run() {
dynamic {
dynamic data=require("my-module");
print(data.one);
}
}

This gives me an idea of having dynamic imports...

Lucono

unread,
Dec 25, 2016, 6:40:30 PM12/25/16
to ceylon-users
Hi Enrique,

I think your suggestion would work if the module was exported in this way:

/* my-module.js */
var data = { ... };
exports
.data = data;

But instead, the case I'm referring to assigns to the exports object:

/* my-module.js*/var data = { ... };
module.exports = data;

How would I get a handle on "data" in this case?

Thanks.

Enrique Zamudio

unread,
Dec 26, 2016, 10:46:54 AM12/26/16
to ceylon-users
Then the only way is to call require directly as in my second example.
Reply all
Reply to author
Forward
0 new messages