Skip to content

Commit

Permalink
Initial CoffeeScript support (#118)
Browse files Browse the repository at this point in the history
* Initial CoffeeScript support

* Minor

* Simplify CoffeeScriptAsset
  • Loading branch information
shawwn authored and devongovett committed Dec 11, 2017
1 parent 87350f4 commit 2d680c0
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 1 deletion.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ coverage
dist
lib
!test/**/node_modules
.vscode/
.vscode/
.idea/
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
"devDependencies": {
"babel-cli": "^6.26.0",
"babel-preset-env": "^1.6.1",
"coffeescript": "^2.0.3",
"cross-env": "^5.1.1",
"husky": "^0.14.3",
"less": "^2.7.2",
Expand Down
1 change: 1 addition & 0 deletions src/Parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class Parser {
this.registerExtension('mjs', './assets/JSAsset');
this.registerExtension('ts', './assets/TypeScriptAsset');
this.registerExtension('tsx', './assets/TypeScriptAsset');
this.registerExtension('coffee', './assets/CoffeeScriptAsset');
this.registerExtension('json', './assets/JSONAsset');
this.registerExtension('yaml', './assets/YAMLAsset');
this.registerExtension('yml', './assets/YAMLAsset');
Expand Down
16 changes: 16 additions & 0 deletions src/assets/CoffeeScriptAsset.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const JSAsset = require('./JSAsset');
const config = require('../utils/config');
const localRequire = require('../utils/localRequire');

class CoffeeScriptAsset extends JSAsset {
async parse(code) {
// require coffeescript, installed locally in the app
let coffee = localRequire('coffeescript', this.name);

// Transpile Module using CoffeeScript and parse result as ast format through babylon
this.contents = coffee.compile(code, {});
return await super.parse(this.contents);
}
}

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

module.exports = function () {
return local.a + local.b.c;
};
4 changes: 4 additions & 0 deletions test/integration/coffee/local.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports =
a: 1
b:
c: 2
14 changes: 14 additions & 0 deletions test/javascript.js
Original file line number Diff line number Diff line change
Expand Up @@ -234,4 +234,18 @@ describe('javascript', function() {
assert.equal(typeof output, 'function');
assert.equal(output(), 3);
});

it('should support requiring CoffeeScript files', async function () {
let b = await bundle(__dirname + '/integration/coffee/index.js');

assertBundleTree(b, {
name: 'index.js',
assets: ['index.js', 'local.coffee'],
childBundles: []
});

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

0 comments on commit 2d680c0

Please sign in to comment.