Skip to content

Commit

Permalink
Add JSON5 support (#695)
Browse files Browse the repository at this point in the history
  • Loading branch information
aicest authored and devongovett committed Jan 30, 2018
1 parent 5a37322 commit 9310641
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/Parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class Parser {
this.registerExtension('tsx', './assets/TypeScriptAsset');
this.registerExtension('coffee', './assets/CoffeeScriptAsset');
this.registerExtension('json', './assets/JSONAsset');
this.registerExtension('json5', './assets/JSONAsset');
this.registerExtension('yaml', './assets/YAMLAsset');
this.registerExtension('yml', './assets/YAMLAsset');
this.registerExtension('gql', './assets/GraphqlAsset');
Expand Down
10 changes: 9 additions & 1 deletion src/assets/JSONAsset.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
const Asset = require('../Asset');
const path = require('path');
const json5 = require('json5');
const {minify} = require('uglify-es');

class JSONAsset extends Asset {
Expand All @@ -7,8 +9,14 @@ class JSONAsset extends Asset {
this.type = 'js';
}

parse(code) {
return path.extname(this.name) === '.json5' ? json5.parse(code) : null;
}

generate() {
let code = `module.exports = ${this.contents};`;
let code = `module.exports = ${
this.ast ? JSON.stringify(this.ast, null, 2) : this.contents
};`;

if (this.options.minify) {
let minified = minify(code);
Expand Down
5 changes: 5 additions & 0 deletions test/integration/json5/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
var local = require('./local.json5');

module.exports = function () {
return local.a + local.b;
};
8 changes: 8 additions & 0 deletions test/integration/json5/local.json5
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/*
* comment
*/
{
a: 1, // comment
b: 2,
}
/* end */
7 changes: 7 additions & 0 deletions test/integration/uglify-json5/index.json5
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/*
* comment
*/
{
"test": "test", // comment
}
/* end */
29 changes: 28 additions & 1 deletion test/javascript.js
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,24 @@ describe('javascript', function() {
assert.equal(output(), 3);
});

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

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

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

it('should support importing a URL to a raw asset', async function() {
let b = await bundle(__dirname + '/integration/import-raw/index.js');

Expand Down Expand Up @@ -480,6 +498,15 @@ describe('javascript', function() {
});

let json = fs.readFileSync(__dirname + '/dist/index.js', 'utf8');
assert(json.includes('test:"test"'));
assert(json.includes('{test:"test"}'));
});

it('should minify JSON5 files', async function() {
await bundle(__dirname + '/integration/uglify-json5/index.json5', {
production: true
});

let json = fs.readFileSync(__dirname + '/dist/index.js', 'utf8');
assert(json.includes('{test:"test"}'));
});
});

0 comments on commit 9310641

Please sign in to comment.