From 28983e068ffbb6ff8b319eb467543858e508261c Mon Sep 17 00:00:00 2001 From: Lukas Taegert Date: Mon, 21 May 2018 19:10:11 +0200 Subject: [PATCH 1/4] 2.2.1 --- package-lock.json | 2 +- package.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/package-lock.json b/package-lock.json index fec57f4..a9bfb31 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "rollup-pluginutils", - "version": "2.2.0", + "version": "2.2.1", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index 4b9c83b..e9e5135 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "rollup-pluginutils", "description": "Functionality commonly needed by Rollup plugins", - "version": "2.2.0", + "version": "2.2.1", "main": "dist/pluginutils.cjs.js", "module": "dist/pluginutils.es.js", "jsnext:main": "dist/pluginutils.es.js", From 796bb63373af3d7e2b8ea204fff66a68ce205b20 Mon Sep 17 00:00:00 2001 From: Guy Bedford Date: Tue, 15 May 2018 13:14:53 +0200 Subject: [PATCH 2/4] support a namedExports false option --- README.md | 3 ++- src/dataToEsm.js | 3 +++ test/test.js | 5 +++++ 3 files changed, 10 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 7d0bfa2..206f4be 100644 --- a/README.md +++ b/README.md @@ -117,7 +117,8 @@ const esModuleSource = dataToEsm({ compact: false, indent: '\t', preferConst: false, - objectShorthand: false + objectShorthand: false, + namedExports: true }); /* Outputs the string ES module source: diff --git a/src/dataToEsm.js b/src/dataToEsm.js index b026e24..f8e3a31 100644 --- a/src/dataToEsm.js +++ b/src/dataToEsm.js @@ -46,6 +46,9 @@ export default function dataToNamedExports (data, options = {}) { const _ = options.compact ? '' : ' '; const n = options.compact ? '' : '\n'; const declarationType = options.preferConst ? 'const' : 'var'; + + if (options.namedExports === false) + return `export default${ _ }${ serialize( data, options.compact ? null : t, '' ) };`; let namedExportCode = ''; const defaultExportRows = []; diff --git a/test/test.js b/test/test.js index 48b27a1..2985eaa 100644 --- a/test/test.js +++ b/test/test.js @@ -512,5 +512,10 @@ describe( 'rollup-pluginutils', function () { it ( 'supports null serialize', function () { assert.equal( dataToEsm( { null: null } ), 'export default {\n\t"null": null\n};\n' ); }); + + it ( 'supports default only', function () { + var arr = ['a', 'b']; + assert.equal( dataToEsm( { arr: arr }, { namedExports: false } ), 'export default {\n\tarr: [\n\t\t"a",\n\t\t"b"\n\t]\n};' ); + }); }); }); From 1a584e4c19406bbff9dde899b4c8f80b35781c13 Mon Sep 17 00:00:00 2001 From: Guy Bedford Date: Tue, 15 May 2018 13:18:30 +0200 Subject: [PATCH 3/4] also provide default-only for non-objects --- src/dataToEsm.js | 2 +- test/test.js | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/src/dataToEsm.js b/src/dataToEsm.js index f8e3a31..7d83f3a 100644 --- a/src/dataToEsm.js +++ b/src/dataToEsm.js @@ -47,7 +47,7 @@ export default function dataToNamedExports (data, options = {}) { const n = options.compact ? '' : '\n'; const declarationType = options.preferConst ? 'const' : 'var'; - if (options.namedExports === false) + if (options.namedExports === false || typeof data !== 'object' || Array.isArray(data)) return `export default${ _ }${ serialize( data, options.compact ? null : t, '' ) };`; let namedExportCode = ''; diff --git a/test/test.js b/test/test.js index 2985eaa..0620383 100644 --- a/test/test.js +++ b/test/test.js @@ -517,5 +517,10 @@ describe( 'rollup-pluginutils', function () { var arr = ['a', 'b']; assert.equal( dataToEsm( { arr: arr }, { namedExports: false } ), 'export default {\n\tarr: [\n\t\t"a",\n\t\t"b"\n\t]\n};' ); }); + + it ( 'default only for non-objects', function () { + var arr = ['a', 'b']; + assert.equal( dataToEsm( arr ), 'export default [\n\t"a",\n\t"b"\n];' ); + }); }); }); From eea7d7e2c9b7095415c0b0cc42623f4c781053e8 Mon Sep 17 00:00:00 2001 From: Guy Bedford Date: Mon, 21 May 2018 18:44:35 +0200 Subject: [PATCH 4/4] include null data case --- src/dataToEsm.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/dataToEsm.js b/src/dataToEsm.js index 7d83f3a..79347ff 100644 --- a/src/dataToEsm.js +++ b/src/dataToEsm.js @@ -47,7 +47,7 @@ export default function dataToNamedExports (data, options = {}) { const n = options.compact ? '' : '\n'; const declarationType = options.preferConst ? 'const' : 'var'; - if (options.namedExports === false || typeof data !== 'object' || Array.isArray(data)) + if (options.namedExports === false || typeof data !== 'object' || Array.isArray(data) || data === null) return `export default${ _ }${ serialize( data, options.compact ? null : t, '' ) };`; let namedExportCode = '';