Skip to content

Commit

Permalink
Add .toml asset support (#742)
Browse files Browse the repository at this point in the history
* Add `.toml` asset support

* Leverage `utils/serializeObject` in TOMLAsset
  • Loading branch information
brandon93s authored and devongovett committed Feb 5, 2018
1 parent 58f8900 commit 55b9640
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/Parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class Parser {
this.registerExtension('json5', './assets/JSONAsset');
this.registerExtension('yaml', './assets/YAMLAsset');
this.registerExtension('yml', './assets/YAMLAsset');
this.registerExtension('toml', './assets/TOMLAsset');
this.registerExtension('gql', './assets/GraphqlAsset');
this.registerExtension('graphql', './assets/GraphqlAsset');

Expand Down
22 changes: 22 additions & 0 deletions src/assets/TOMLAsset.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const Asset = require('../Asset');
const toml = require('toml');
const serializeObject = require('../utils/serializeObject');

class TOMLAsset extends Asset {
constructor(name, pkg, options) {
super(name, pkg, options);
this.type = 'js';
}

parse(code) {
return toml.parse(code);
}

generate() {
return {
js: serializeObject(this.ast, this.options.minify)
};
}
}

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

module.exports = function () {
return local.a + local.b.c;
};
4 changes: 4 additions & 0 deletions test/integration/toml/local.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
a = 1

[b]
c = 2
27 changes: 27 additions & 0 deletions test/javascript.js
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,24 @@ describe('javascript', function() {
assert.equal(output(), 3);
});

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

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

let output = run(b);
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');

Expand Down Expand Up @@ -576,4 +594,13 @@ describe('javascript', function() {
let json = fs.readFileSync(__dirname + '/dist/index.js', 'utf8');
assert(json.includes('{a:1,b:{c:2}}'));
});

it('should minify TOML for production', async function() {
await bundle(__dirname + '/integration/toml/index.js', {
production: true
});

let json = fs.readFileSync(__dirname + '/dist/index.js', 'utf8');
assert(json.includes('{a:1,b:{c:2}}'));
});
});

0 comments on commit 55b9640

Please sign in to comment.