diff --git a/README.md b/README.md index 425b87c..21a278a 100644 --- a/README.md +++ b/README.md @@ -47,7 +47,13 @@ export default { // specify indentation for the generated default export — // defaults to '\t' - indent: ' ' + indent: ' ', + + // ignores indent and generates the smallest code + compact: true, // Default: false + + // generate a named export for every property of the JSON object + namedExports: true // Default: true }) ] }; diff --git a/src/index.js b/src/index.js index 8b28ccb..bb30ef0 100644 --- a/src/index.js +++ b/src/index.js @@ -17,7 +17,12 @@ export default function json (options = {}) { } return { - code: dataToEsm(data, {preferConst: options.preferConst, indent}), + code: dataToEsm(data, { + preferConst: options.preferConst, + compact: options.compact, + namedExports: options.namedExports, + indent + }), map: {mappings: ''} }; } diff --git a/test/samples/form/compact.js b/test/samples/form/compact.js new file mode 100644 index 0000000..18e43bd --- /dev/null +++ b/test/samples/form/compact.js @@ -0,0 +1 @@ +export var validKey=true;export var nested={subKey:"ok"};export var array=[1,"2"];export default{validKey:validKey,"invalid-key": 1,nested:nested,array:array,"function": "not used","null": null}; diff --git a/test/samples/form/namedExports.js b/test/samples/form/namedExports.js new file mode 100644 index 0000000..7e6bee0 --- /dev/null +++ b/test/samples/form/namedExports.js @@ -0,0 +1,13 @@ +export default { + validKey: true, + "invalid-key": 1, + nested: { + subKey: "ok" + }, + array: [ + 1, + "2" + ], + "function": "not used", + "null": null +}; diff --git a/test/test.js b/test/test.js index b4854ce..8c71cde 100644 --- a/test/test.js +++ b/test/test.js @@ -138,6 +138,20 @@ describe('rollup-plugin-json', () => { read('samples/form/customIndent.js') ); }); + + it('generates correct code with compact=true', () => { + assert.deepEqual( + json({ compact: true }).transform(read('samples/form/input.json'), 'input.json').code + '\n', + read('samples/form/compact.js') + ); + }); + + it('generates correct code with namedExports=false', () => { + assert.deepEqual( + json({ namedExports: false }).transform(read('samples/form/input.json'), 'input.json').code + '\n', + read('samples/form/namedExports.js') + ); + }); }); function read (file) {