File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff 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' ) ;
Original file line number Diff line number Diff line change 11const Asset = require ( '../Asset' ) ;
2+ const path = require ( 'path' ) ;
3+ const json5 = require ( 'json5' ) ;
24const { minify} = require ( 'uglify-es' ) ;
35
46class 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 ) ;
Original file line number Diff line number Diff line change 1+ var local = require ( './local.json5' ) ;
2+
3+ module . exports = function ( ) {
4+ return local . a + local . b ;
5+ } ;
Original file line number Diff line number Diff line change 1+ /*
2+ * comment
3+ */
4+ {
5+ a : 1 , // comment
6+ b : 2 ,
7+ }
8+ /* end */
Original file line number Diff line number Diff line change 1+ /*
2+ * comment
3+ */
4+ {
5+ "test" : "test" , // comment
6+ }
7+ /* end */
Original file line number Diff line number Diff 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} ) ;
You can’t perform that action at this time.
0 commit comments