Skip to content

Commit

Permalink
feat: add a default export
Browse files Browse the repository at this point in the history
This adds a default export object containing all named exports.

Eg.:
```js
import _king from './king.js';
export const king = _king;

import _queen from './queen';
export const queen = _queen;

export default {
  king,
  queen
};
```
  • Loading branch information
laggingreflex committed Nov 28, 2016
1 parent be0e6fd commit 240c7e8
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 11 deletions.
13 changes: 12 additions & 1 deletion src/utilities/createIndexCode.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,20 @@ const buildExportBlock = (files) => {
let importBlock;

importBlock = _.map(files, (fileName) => {
return 'export ' + safeVariableName(fileName) + ' from \'./' + fileName + '\';';
const moduleName = _.camelCase(safeVariableName(fileName));

return 'import _' + moduleName + ' from \'./' + fileName + '\';\n' +
'export const ' + moduleName + ' = _' + moduleName + ';\n';
});

importBlock.push('export default {');
importBlock.push.apply(importBlock, _.map(files, (fileName, idx) => {
const moduleName = _.camelCase(safeVariableName(fileName));

return ' ' + moduleName + (idx + 1 < files.length ? ',' : '');
}), '');
importBlock.push('};');

importBlock = importBlock.join('\n');

return importBlock;
Expand Down
38 changes: 32 additions & 6 deletions test/createIndexCode.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,12 @@ describe('createIndexCode()', () => {
expect(indexCode).to.equal(codeExample(`
'create index';
export foo from './foo';
import _foo from './foo';
export const foo = _foo;
export default {
foo
};
`));
});
it('describes multiple children', () => {
Expand All @@ -29,8 +34,16 @@ export foo from './foo';
expect(indexCode).to.equal(codeExample(`
'create index';
export bar from './bar';
export foo from './foo';
import _bar from './bar';
export const bar = _bar;
import _foo from './foo';
export const foo = _foo;
export default {
bar,
foo
};
`));
});
context('file with extension', () => {
Expand All @@ -40,7 +53,12 @@ export foo from './foo';
expect(indexCode).to.equal(codeExample(`
'create index';
export foo from './foo.js';
import _foo from './foo.js';
export const foo = _foo;
export default {
foo
};
`));
});
});
Expand All @@ -51,8 +69,16 @@ export foo from './foo.js';
expect(indexCode).to.equal(codeExample(`
'create index';
export bar from './bar';
export foo from './foo';
import _bar from './bar';
export const bar = _bar;
import _foo from './foo';
export const foo = _foo;
export default {
bar,
foo
};
`));
});
});
Expand Down
12 changes: 10 additions & 2 deletions test/fixtures/write-index/mixed/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
'create index';

export bar from './bar';
export foo from './foo.js';
import _bar from './bar';
export const bar = _bar;

import _foo from './foo.js';
export const foo = _foo;

export default {
bar,
foo
};

12 changes: 10 additions & 2 deletions test/writeIndex.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,16 @@ describe('writeIndex()', () => {
expect(indexCode).to.equal(codeExample(`
'create index';
export bar from './bar';
export foo from './foo.js';
import _bar from './bar';
export const bar = _bar;
import _foo from './foo.js';
export const foo = _foo;
export default {
bar,
foo
};
`));
});
});

0 comments on commit 240c7e8

Please sign in to comment.