Skip to content

Commit 47e9192

Browse files
rrdelaneydevongovett
authored andcommitted
Add Reason asset type (#342)
* 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
1 parent 9f56e42 commit 47e9192

File tree

7 files changed

+66
-0
lines changed

7 files changed

+66
-0
lines changed

β€Žpackage.jsonβ€Ž

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
"babylon": "^6.17.4",
1515
"babylon-walk": "^1.0.2",
1616
"browser-resolve": "^1.11.2",
17+
"bsb-js": "^1.0.1",
1718
"chalk": "^2.1.0",
1819
"chokidar": "^1.7.0",
1920
"commander": "^2.11.0",

β€Žsrc/Parser.jsβ€Ž

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ class Parser {
1212
this.registerExtension('es6', './assets/JSAsset');
1313
this.registerExtension('jsm', './assets/JSAsset');
1414
this.registerExtension('mjs', './assets/JSAsset');
15+
this.registerExtension('ml', './assets/ReasonAsset');
16+
this.registerExtension('re', './assets/ReasonAsset');
1517
this.registerExtension('ts', './assets/TypeScriptAsset');
1618
this.registerExtension('tsx', './assets/TypeScriptAsset');
1719
this.registerExtension('coffee', './assets/CoffeeScriptAsset');

β€Žsrc/assets/ReasonAsset.jsβ€Ž

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
const bsb = require('bsb-js');
2+
const fs = require('fs');
3+
const JSAsset = require('./JSAsset');
4+
const promisify = require('../utils/promisify');
5+
const readFile = promisify(fs.readFile);
6+
7+
class ReasonAsset extends JSAsset {
8+
async parse(code) {
9+
// This runs BuckleScript - the Reason to JS compiler.
10+
// Other Asset types use `localRequire` but the `bsb-js` package already
11+
// does that internally. This should also take care of error handling in
12+
// the Reason compilation process.
13+
if (process.env.NODE_ENV !== 'test') {
14+
await bsb.runBuild();
15+
}
16+
17+
// This is a simplified use-case for Reason - it only loads the recommended
18+
// BuckleScript configuration to simplify the file processing.
19+
const outputFile = this.name.replace(/\.(re|ml)$/, '.bs.js');
20+
const outputContent = await readFile(outputFile);
21+
this.contents = outputContent.toString();
22+
23+
// After loading the compiled JS source, use the normal JS behavior.
24+
return await super.parse(this.contents);
25+
}
26+
}
27+
28+
module.exports = ReasonAsset;
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
var local = require('./local.re');
2+
3+
module.exports = function () {
4+
return local.a + local.b;
5+
};

β€Žtest/integration/reason/local.bs.jsβ€Ž

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
let a = 1;
2+
3+
let b = 2;

β€Žtest/reason.jsβ€Ž

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
const assert = require('assert');
2+
const fs = require('fs');
3+
const {bundle, run, assertBundleTree} = require('./utils');
4+
5+
describe('reason', function() {
6+
it('should produce a bundle', async function() {
7+
let b = await bundle(__dirname + '/integration/reason/index.js');
8+
9+
assert.equal(b.assets.size, 2);
10+
assert.equal(b.childBundles.size, 0);
11+
12+
let output = run(b);
13+
assert.equal(typeof output, 'function');
14+
assert.equal(output(), 3);
15+
});
16+
});

0 commit comments

Comments
Β (0)