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

Commit 804f659

Browse files
committed
Merge branch 'dataToNamedExports'
2 parents 0bb4653 + 5f40964 commit 804f659

File tree

7 files changed

+78
-2
lines changed

7 files changed

+78
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
## 2.1.0
44
*unreleased*
5+
* Add `dataToEsm` helper to create named exports from objects ([#29](https://github.com/rollup/rollup-pluginutils/issues/29))
56
* Support literal keys in object patterns ([#27](https://github.com/rollup/rollup-pluginutils/issues/27))
67

78
## 2.0.1

README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,30 @@ makeLegalIdentifier( 'foo-bar' ); // 'foo_bar'
103103
makeLegalIdentifier( 'typeof' ); // '_typeof'
104104
```
105105

106+
### dataToEsm
107+
108+
Helper for treeshakable data imports
109+
110+
```js
111+
import { dataToEsm } from 'rollup-pluginutils';
112+
113+
const esModuleSource = dataToEsm({
114+
custom: 'data',
115+
to: ['treeshake']
116+
}, options = {
117+
compact: false,
118+
indent: '\t',
119+
preferConst: false,
120+
objectShorthand: false
121+
});
122+
/*
123+
Outputs the string ES module source:
124+
export const custom = 'data';
125+
export const to = ['treeshake'];
126+
export default { custom, to };
127+
*/
128+
```
129+
106130

107131
## License
108132

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@
2424
},
2525
"dependencies": {
2626
"estree-walker": "^0.3.0",
27-
"micromatch": "^2.3.11"
27+
"micromatch": "^2.3.11",
28+
"tosource": "^1.0.0"
2829
},
2930
"repository": "rollup/rollup-pluginutils",
3031
"keywords": [

rollup.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import buble from 'rollup-plugin-buble';
44
export default {
55
entry: 'src/index.js',
66
plugins: [ buble() ],
7-
external: [ 'path', 'estree-walker', 'micromatch' ],
7+
external: [ 'path', 'estree-walker', 'micromatch', 'tosource' ],
88

99
targets: [
1010
{

src/dataToEsm.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import makeLegalIdentifier from './makeLegalIdentifier';
2+
import tosource from 'tosource';
3+
4+
// convert data object into separate named exports (and default)
5+
export default function dataToNamedExports ( data, options = {} ) {
6+
const t = options.compact ? '' : 'indent' in options ? options.indent : '\t';
7+
const _ = options.compact ? '' : ' ';
8+
const n = options.compact ? '' : '\n';
9+
const declarationType = options.preferConst ? 'const' : 'var';
10+
11+
let namedExportCode = '';
12+
const defaultExportRows = [];
13+
const dataKeys = Object.keys( data );
14+
for (let i = 0; i < dataKeys.length; i++) {
15+
const key = dataKeys[i];
16+
if (key === makeLegalIdentifier( key )) {
17+
if ( options.objectShorthand )
18+
defaultExportRows.push(key);
19+
else
20+
defaultExportRows.push( `${ key }:${ _ }${ key }` );
21+
namedExportCode += `export ${declarationType} ${key}${ _ }=${ _ }${ tosource( data[key], null, options.compact ? false : t ) };${ n }`;
22+
} else {
23+
defaultExportRows.push( `${ JSON.stringify(key) }: ${ tosource( data[key], null, options.compact ? false : t )}` );
24+
}
25+
}
26+
return namedExportCode + `export default${ _ }{${ n }${ t }${ defaultExportRows.join(`,${ n }${ t }`) }${ n }};${ n }`;
27+
};

src/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ export { default as addExtension } from './addExtension';
22
export { default as attachScopes } from './attachScopes';
33
export { default as createFilter } from './createFilter';
44
export { default as makeLegalIdentifier } from './makeLegalIdentifier';
5+
export { default as dataToEsm } from './dataToEsm';

test/test.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -442,4 +442,26 @@ describe( 'rollup-pluginutils', function () {
442442
assert.equal( makeLegalIdentifier( 'arguments' ), '_arguments' );
443443
});
444444
});
445+
446+
describe( 'dataToEsm', function () {
447+
var dataToEsm = utils.dataToEsm;
448+
449+
it( 'outputs treeshakable data', function () {
450+
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' );
451+
});
452+
453+
it( 'handles illegal identifiers, object shorthand, preferConst', function () {
454+
assert.equal( dataToEsm( { '1': 'data', 'default': 'data' }, { objectShorthand: true, preferConst: true } ), 'export default {\n\t"1": "data",\n\t"default": "data"\n};\n' );
455+
});
456+
457+
it( 'supports non-JSON data', function () {
458+
const date = new Date();
459+
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' );
460+
});
461+
462+
it( 'supports a compact argument', function () {
463+
assert.equal( dataToEsm( { some: 'data', another: 'data' }, { compact: true, objectShorthand: true } ), 'export var some="data";export var another="data";export default{some,another};' );
464+
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};' );
465+
});
466+
});
445467
});

0 commit comments

Comments
 (0)