BAM Weblog

Roy - node.js Modules

Brian McKenna — 2012-01-22

This week I was at 2012 in Ballarat. I talked about altJS (languages that compile to JavaScript). The video is up on YouTube.

During my off-time, I worked on Roy’s modules branch and made it capable of importing and exporting node.js modules. The branch adds an “export” macro:

let obj = {x: 1, y: 2, t: "test"}
export obj

This will assign the identifier to the “exports” global specified by CommonJS Modules/1.0:

var obj = {
    "x": 1,
    "y": 2,
    "t": "test"
exports["obj"] = obj;

My work on separate compilation causes a “.roym” file to be written during compilation:

obj: {x: Number, y: Number, t: String}

On the other side, there’s an “import” macro:

import "./structural"

let f o = o.x + o.y
f structural.obj

Which loads the “.roym” file and assigns the types under the module name and compiles to node.js compatible requires:

var structural = require("./structural");
var f = function(o) {
    return o.x + o.y;

That’s it. Type-safe node.js modules. Awesome.

I have a few things to continue with:

  • Add flags to change macros to target browser globals and AMD modules
  • Fix writing all top-level type information to the “.roym” file; only write exported values
Please enable JavaScript to view the comments powered by Disqus.