Skip to content
This repository has been archived by the owner on Aug 4, 2021. It is now read-only.

dataToNamedExports helper #29

Merged
merged 7 commits into from
May 7, 2018
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
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,30 @@ makeLegalIdentifier( 'foo-bar' ); // 'foo_bar'
makeLegalIdentifier( 'typeof' ); // '_typeof'
```

### dataToEsm

Helper for treeshakable data imports

```js
import { dataToEsm } from 'rollup-pluginutils';

const esModuleSource = dataToEsm({
custom: 'data',
to: ['treeshake']
}, options = {
compact: false,
indent: '\t',
preferConst: false,
objectShorthand: false
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

});
/*
Outputs the string ES module source:
export const custom = 'data';
export const to = ['treeshake'];
export default { custom, to };
*/
```


## License

Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
},
"dependencies": {
"estree-walker": "^0.3.0",
"micromatch": "^2.3.11"
"micromatch": "^2.3.11",
"tosource": "^1.0.0"
},
"repository": "rollup/rollup-pluginutils",
"keywords": [
Expand Down
2 changes: 1 addition & 1 deletion rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import buble from 'rollup-plugin-buble';
export default {
entry: 'src/index.js',
plugins: [ buble() ],
external: [ 'path', 'estree-walker', 'micromatch' ],
external: [ 'path', 'estree-walker', 'micromatch', 'tosource' ],

targets: [
{
Expand Down
27 changes: 27 additions & 0 deletions src/dataToEsm.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import makeLegalIdentifier from './makeLegalIdentifier';
import tosource from 'tosource';

// convert data object into separate named exports (and default)
export default function dataToNamedExports ( data, options = {} ) {
const t = options.compact ? '' : 'indent' in options ? options.indent : '\t';
const _ = options.compact ? '' : ' ';
const n = options.compact ? '' : '\n';
const declarationType = options.preferConst ? 'const' : 'var';

let namedExportCode = '';
const defaultExportRows = [];
const dataKeys = Object.keys( data );
for (let i = 0; i < dataKeys.length; i++) {
const key = dataKeys[i];
if (key === makeLegalIdentifier( key )) {
if ( options.objectShorthand )
defaultExportRows.push(key);
else
defaultExportRows.push( `${ key }:${ _ }${ key }` );
namedExportCode += `export ${declarationType} ${key}${ _ }=${ _ }${ tosource( data[key], null, options.compact ? false : t ) };${ n }`;
} else {
defaultExportRows.push( `${ JSON.stringify(key) }: ${ tosource( data[key], null, options.compact ? false : t )}` );
}
}
return namedExportCode + `export default${ _ }{${ n }${ t }${ defaultExportRows.join(`,${ n }${ t }`) }${ n }};${ n }`;
};
1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ export { default as addExtension } from './addExtension';
export { default as attachScopes } from './attachScopes';
export { default as createFilter } from './createFilter';
export { default as makeLegalIdentifier } from './makeLegalIdentifier';
export { default as dataToEsm } from './dataToEsm';
22 changes: 22 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -442,4 +442,26 @@ describe( 'rollup-pluginutils', function () {
assert.equal( makeLegalIdentifier( 'arguments' ), '_arguments' );
});
});

describe( 'dataToEsm', function () {
var dataToEsm = utils.dataToEsm;

it( 'outputs treeshakable data', function () {
assert.equal( dataToEsm( { some: 'data', another: 'data' } ), 'export var some = "data";\nexport var another = "data";\nexport default {\n\tsome: some,\n\tanother: another\n};\n' );
});

it( 'handles illegal identifiers, object shorthand, preferConst', function () {
assert.equal( dataToEsm( { '1': 'data', 'default': 'data' }, { objectShorthand: true, preferConst: true } ), 'export default {\n\t"1": "data",\n\t"default": "data"\n};\n' );
});

it( 'supports non-JSON data', function () {
const date = new Date();
assert.equal( dataToEsm( { inf: Infinity, date: date } ), 'export var inf = Infinity;\nexport var date = new Date(' + date.getTime() + ');\nexport default {\n\tinf: inf,\n\tdate: date\n};\n' );
});

it( 'supports a compact argument', function () {
assert.equal( dataToEsm( { some: 'data', another: 'data' }, { compact: true, objectShorthand: true } ), 'export var some="data";export var another="data";export default{some,another};' );
assert.equal( dataToEsm( { some: { deep: { object: 'definition', here: 'here' } }, another: 'data' }, { compact: true, objectShorthand: false } ), 'export var some={deep:{object:"definition",here:"here"}};export var another="data";export default{some:some,another:another};' );
});
});
});