Skip to content

Commit

Permalink
Add Reason asset type (#342)
Browse files Browse the repository at this point in the history
* Add bsb-js dependency

* Add ReasonAsset type

* Actually add the Reason asset type

* Add OCaml file type, add integration test, remote unused imports

* use promisify for reading files

* Fix integration tests
  • Loading branch information
rrdelaney authored and devongovett committed Dec 21, 2017
1 parent 9f56e42 commit 47e9192
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 0 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"babylon": "^6.17.4",
"babylon-walk": "^1.0.2",
"browser-resolve": "^1.11.2",
"bsb-js": "^1.0.1",
"chalk": "^2.1.0",
"chokidar": "^1.7.0",
"commander": "^2.11.0",
Expand Down
2 changes: 2 additions & 0 deletions src/Parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ class Parser {
this.registerExtension('es6', './assets/JSAsset');
this.registerExtension('jsm', './assets/JSAsset');
this.registerExtension('mjs', './assets/JSAsset');
this.registerExtension('ml', './assets/ReasonAsset');
this.registerExtension('re', './assets/ReasonAsset');
this.registerExtension('ts', './assets/TypeScriptAsset');
this.registerExtension('tsx', './assets/TypeScriptAsset');
this.registerExtension('coffee', './assets/CoffeeScriptAsset');
Expand Down
28 changes: 28 additions & 0 deletions src/assets/ReasonAsset.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
const bsb = require('bsb-js');
const fs = require('fs');
const JSAsset = require('./JSAsset');
const promisify = require('../utils/promisify');
const readFile = promisify(fs.readFile);

class ReasonAsset extends JSAsset {
async parse(code) {
// This runs BuckleScript - the Reason to JS compiler.
// Other Asset types use `localRequire` but the `bsb-js` package already
// does that internally. This should also take care of error handling in
// the Reason compilation process.
if (process.env.NODE_ENV !== 'test') {
await bsb.runBuild();
}

// This is a simplified use-case for Reason - it only loads the recommended
// BuckleScript configuration to simplify the file processing.
const outputFile = this.name.replace(/\.(re|ml)$/, '.bs.js');
const outputContent = await readFile(outputFile);
this.contents = outputContent.toString();

// After loading the compiled JS source, use the normal JS behavior.
return await super.parse(this.contents);
}
}

module.exports = ReasonAsset;
5 changes: 5 additions & 0 deletions test/integration/reason/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
var local = require('./local.re');

module.exports = function () {
return local.a + local.b;
};
11 changes: 11 additions & 0 deletions test/integration/reason/local.bs.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions test/integration/reason/local.re
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
let a = 1;

let b = 2;
16 changes: 16 additions & 0 deletions test/reason.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const assert = require('assert');
const fs = require('fs');
const {bundle, run, assertBundleTree} = require('./utils');

describe('reason', function() {
it('should produce a bundle', async function() {
let b = await bundle(__dirname + '/integration/reason/index.js');

assert.equal(b.assets.size, 2);
assert.equal(b.childBundles.size, 0);

let output = run(b);
assert.equal(typeof output, 'function');
assert.equal(output(), 3);
});
});

0 comments on commit 47e9192

Please sign in to comment.