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

Commit 7fa725b

Browse files
guybedfordlukastaegert
authored andcommitted
dataToNamedExports helper
1 parent 0bb4653 commit 7fa725b

File tree

6 files changed

+70
-2
lines changed

6 files changed

+70
-2
lines changed

README.md

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

106+
### dataToNamedExports
107+
108+
Helper for treeshakable data imports
109+
110+
```js
111+
import { dataToNamedExports } from 'rollup-pluginutils';
112+
113+
const esModuleSource = dataToNamedExports({
114+
custom: 'data',
115+
to: ['treeshake']
116+
});
117+
/*
118+
Outputs the string ES module source:
119+
export const custom = 'data';
120+
export const to = ['treeshake'];
121+
export default { custom, to };
122+
*/
123+
```
124+
106125

107126
## License
108127

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/dataToNamedExports.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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 ( obj ) {
6+
let output = '';
7+
let defaultExports = '\n';
8+
const usedLegalKeys = Object.create( null );
9+
let index = 0;
10+
const keys = Object.keys( obj );
11+
for ( let i = 0; i < keys.length; i++ ) {
12+
const key = keys[i];
13+
const legalKey = makeLegalIdentifier( key );
14+
output += `export const ${ legalKey } = ${tosource( obj[key] )};\n`;
15+
while ( usedLegalKeys[legalKey + (index || '')] )
16+
index++;
17+
index = 0;
18+
usedLegalKeys[legalKey + ( index || '' )] = true;
19+
if ( defaultExports.length > 1 )
20+
defaultExports += ',\n';
21+
if ( key === legalKey )
22+
defaultExports += ` ${ key }`;
23+
else
24+
defaultExports += ` ${ `'${ key }'` }: ${ legalKey }`;
25+
}
26+
if ( defaultExports.length > 3 )
27+
defaultExports += '\n';
28+
output += `export default {${ defaultExports }};`;
29+
return output;
30+
};

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 dataToNamedExports } from './dataToNamedExports';

test/test.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -442,4 +442,21 @@ describe( 'rollup-pluginutils', function () {
442442
assert.equal( makeLegalIdentifier( 'arguments' ), '_arguments' );
443443
});
444444
});
445+
446+
describe( 'dataToNamedExports', function () {
447+
var dataToNamedExports = utils.dataToNamedExports;
448+
449+
it( 'outputs treeshakable data', function () {
450+
assert.equal( dataToNamedExports({ some: 'data', another: 'data' }), `export const some = "data";\nexport const another = "data";\nexport default {\n some,\n another\n};` );
451+
});
452+
453+
it( 'handles illegal identifiers', function () {
454+
assert.equal( dataToNamedExports({ '1': 'data', 'default': 'data' }), `export const _1 = "data";\nexport const _default = "data";\nexport default {\n '1': _1,\n 'default': _default\n};` );
455+
});
456+
457+
it( 'supports non-JSON data', function () {
458+
const date = new Date();
459+
assert.equal( dataToNamedExports({ inf: Infinity, date }), `export const inf = Infinity;\nexport const date = new Date(${date.getTime()});\nexport default {\n inf,\n date\n};` );
460+
});
461+
});
445462
});

0 commit comments

Comments
 (0)