From 9310641bcd11f891657fb02f2e3acd641153a99b Mon Sep 17 00:00:00 2001 From: Fai Date: Tue, 30 Jan 2018 12:28:05 +0800 Subject: [PATCH] Add JSON5 support (#695) --- src/Parser.js | 1 + src/assets/JSONAsset.js | 10 +++++++- test/integration/json5/index.js | 5 ++++ test/integration/json5/local.json5 | 8 +++++++ test/integration/uglify-json5/index.json5 | 7 ++++++ test/javascript.js | 29 ++++++++++++++++++++++- 6 files changed, 58 insertions(+), 2 deletions(-) create mode 100644 test/integration/json5/index.js create mode 100644 test/integration/json5/local.json5 create mode 100644 test/integration/uglify-json5/index.json5 diff --git a/src/Parser.js b/src/Parser.js index 132df847e6e..c3f73268826 100644 --- a/src/Parser.js +++ b/src/Parser.js @@ -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'); diff --git a/src/assets/JSONAsset.js b/src/assets/JSONAsset.js index 662c56e3822..4d81925845f 100644 --- a/src/assets/JSONAsset.js +++ b/src/assets/JSONAsset.js @@ -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 { @@ -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); diff --git a/test/integration/json5/index.js b/test/integration/json5/index.js new file mode 100644 index 00000000000..b27ad1bc6ba --- /dev/null +++ b/test/integration/json5/index.js @@ -0,0 +1,5 @@ +var local = require('./local.json5'); + +module.exports = function () { + return local.a + local.b; +}; diff --git a/test/integration/json5/local.json5 b/test/integration/json5/local.json5 new file mode 100644 index 00000000000..248a6a50911 --- /dev/null +++ b/test/integration/json5/local.json5 @@ -0,0 +1,8 @@ +/* + * comment + */ +{ + a: 1, // comment + b: 2, +} +/* end */ diff --git a/test/integration/uglify-json5/index.json5 b/test/integration/uglify-json5/index.json5 new file mode 100644 index 00000000000..236bfc65859 --- /dev/null +++ b/test/integration/uglify-json5/index.json5 @@ -0,0 +1,7 @@ +/* + * comment + */ +{ + "test": "test", // comment +} +/* end */ diff --git a/test/javascript.js b/test/javascript.js index bef6a61a85f..42314d8d82b 100644 --- a/test/javascript.js +++ b/test/javascript.js @@ -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'); @@ -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"}')); }); });