Skip to content
This repository was archived by the owner on Aug 4, 2021. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
3 changes: 3 additions & 0 deletions src/dataToEsm.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 || typeof data !== 'object' || Array.isArray(data) || data === null)
return `export default${ _ }${ serialize( data, options.compact ? null : t, '' ) };`;

let namedExportCode = '';
const defaultExportRows = [];
Expand Down
10 changes: 10 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -512,5 +512,15 @@ 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};' );
});

it ( 'default only for non-objects', function () {
var arr = ['a', 'b'];
assert.equal( dataToEsm( arr ), 'export default [\n\t"a",\n\t"b"\n];' );
});
});
});