Synopsis
sixportify is a browserify transform that enables you to write CommonJS module exports as if you were using ES6 (also known as harmony).
Install
Node.js
With NPM
npm install sixportify
From source
git clone https://github.com/pluma/sixportify.gitcd sixportifynpm install
Basic usage example
somelib.js
var config = x: 4; { return configx + y;}
index.js
var somelib = ;console; // 5somelibconfigx = 2;console; // 3
Usage
var browserify = sixportify = b = ; b;b;b;
es6ify
Usage example with somelib.js
const BAR = "Hello World!"; { console; }
index.js
var Foo = ;; // "Hello World!"
Usage
var browserify = siportify = es6ify = b = ; b; // should always come before es6ifyb;b;b;b;
Caveats
The implementation is incredibly naïve.
If your export statements are not at the beginning of the line (optionally indented with any whitespace character), sixportify
won't find them:
// This won't work.var foo = 'bar'; var baz = 'qux'; // This won't work either.var foo = 'bar'; // Nor will this.var foo = 'bar' qux = 'baz'; // `qux` will not be exported! // This is fine, though:var foo = 'bar': 'qux';
While sixportify
works just fine with variable declarations,
keep in mind that re-assignment may have unintended consequences. E.g.
var foo = 'bar';foo = 'qux';
In this case the value that will actually be exported as exports.foo
will be "qux"
, not "bar"
.
You should therefore treat exported var
declarations as constants.
Likewise, the following will not work as intended:
// in somelib.jsvar foo = 'bar'; { console; // still refers to the local var} // in index.jsvar somelib = ;somelibfoo = 'world'; // re-assigns the exported varsomelib; // "Hello bar!"
ES6/harmony, let, const, generators and classes
If you want to use sixportify
with ES6-style classes, you can do that:
// in somelib.es6 { console; } // in index.es6var Foo = Foo;var foo = ;foo; // "sup"
Generators (function*
) as well as variables declared with let
or const
are fully supported too (even though the latter two shouldn't really be exportable according to harmony -- if the runtime supports it, sixportify
won't judge you).
This means you can use sixportify to preprocess your ES6-style exports for es6ify.
Keep in mind that sixportify
does not understand decomposition, so the following will not work:
// BROKEN!var obj = 'foo': 'bar';var foo = obj;// ALSO BROKEN!var arr = 'hello';var qux = arr;
Unlicense
This is free and unencumbered public domain software. For more information, see http://unlicense.org/ or the accompanying UNLICENSE file.