Skip to content

Commit 9310641

Browse files
aicestdevongovett
authored andcommitted
Add JSON5 support (#695)
1 parent 5a37322 commit 9310641

6 files changed

Lines changed: 58 additions & 2 deletions

File tree

src/Parser.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ class Parser {
1818
this.registerExtension('tsx', './assets/TypeScriptAsset');
1919
this.registerExtension('coffee', './assets/CoffeeScriptAsset');
2020
this.registerExtension('json', './assets/JSONAsset');
21+
this.registerExtension('json5', './assets/JSONAsset');
2122
this.registerExtension('yaml', './assets/YAMLAsset');
2223
this.registerExtension('yml', './assets/YAMLAsset');
2324
this.registerExtension('gql', './assets/GraphqlAsset');

src/assets/JSONAsset.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
const Asset = require('../Asset');
2+
const path = require('path');
3+
const json5 = require('json5');
24
const {minify} = require('uglify-es');
35

46
class JSONAsset extends Asset {
@@ -7,8 +9,14 @@ class JSONAsset extends Asset {
79
this.type = 'js';
810
}
911

12+
parse(code) {
13+
return path.extname(this.name) === '.json5' ? json5.parse(code) : null;
14+
}
15+
1016
generate() {
11-
let code = `module.exports = ${this.contents};`;
17+
let code = `module.exports = ${
18+
this.ast ? JSON.stringify(this.ast, null, 2) : this.contents
19+
};`;
1220

1321
if (this.options.minify) {
1422
let minified = minify(code);

test/integration/json5/index.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
var local = require('./local.json5');
2+
3+
module.exports = function () {
4+
return local.a + local.b;
5+
};

test/integration/json5/local.json5

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/*
2+
* comment
3+
*/
4+
{
5+
a: 1, // comment
6+
b: 2,
7+
}
8+
/* end */
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/*
2+
* comment
3+
*/
4+
{
5+
"test": "test", // comment
6+
}
7+
/* end */

test/javascript.js

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,24 @@ describe('javascript', function() {
211211
assert.equal(output(), 3);
212212
});
213213

214+
it('should support requiring JSON5 files', async function() {
215+
let b = await bundle(__dirname + '/integration/json5/index.js');
216+
217+
assertBundleTree(b, {
218+
name: 'index.js',
219+
assets: ['index.js', 'local.json5'],
220+
childBundles: [
221+
{
222+
type: 'map'
223+
}
224+
]
225+
});
226+
227+
let output = run(b);
228+
assert.equal(typeof output, 'function');
229+
assert.equal(output(), 3);
230+
});
231+
214232
it('should support importing a URL to a raw asset', async function() {
215233
let b = await bundle(__dirname + '/integration/import-raw/index.js');
216234

@@ -480,6 +498,15 @@ describe('javascript', function() {
480498
});
481499

482500
let json = fs.readFileSync(__dirname + '/dist/index.js', 'utf8');
483-
assert(json.includes('test:"test"'));
501+
assert(json.includes('{test:"test"}'));
502+
});
503+
504+
it('should minify JSON5 files', async function() {
505+
await bundle(__dirname + '/integration/uglify-json5/index.json5', {
506+
production: true
507+
});
508+
509+
let json = fs.readFileSync(__dirname + '/dist/index.js', 'utf8');
510+
assert(json.includes('{test:"test"}'));
484511
});
485512
});

0 commit comments

Comments
 (0)