create-index icon indicating copy to clipboard operation
create-index copied to clipboard

How to create index of named exports?

Open deadivan opened this issue 9 years ago • 5 comments
trafficstars

I use webpack and babel as my es6 transpiler in my dev.create-index act correct when all siblings are files. It breaks when export subdir.

For example

├── king.js
└── queen
    ├── bar.js
    └── foo.js

create-index can correctly build index.js in dir queen

export bar from './bar.js';
export foo from './foo.js';

but in the root dir ,index.js

export queen from './queen'

just export undefined.

I found that

export * as queen from './queen';

worked.

So I forked your code and change the dir export style and committed.

https://github.com/deadivan/create-index

Please tell me if I doing something wrong.

deadivan avatar Jun 10 '16 04:06 deadivan

create-index is used to create index only of files that export default. Your code (export * as queen from './queen';) creates an object mapping all named exports. Adding this behaviour to create-index would make index unpredictable (now you know that index is created only for export default value).

I don't think this belongs in the core, but I will leave the issue open in case anyone else has a similar use case and can propose an implementation.

gajus avatar Jun 15 '16 21:06 gajus

Thanks for reply. Ignoring what i changed,any idea how to export js file under dir queen using create-index? Or my babel setting cause 'undefined' error ? Or we just don't have a proper strategy to generate index recursively?

deadivan avatar Jun 17 '16 03:06 deadivan

Same question. I'd like to have a simple plain file with all components from nested dirs.

viteksafronov avatar Aug 07 '17 09:08 viteksafronov

I have the same issue. The index.js generated for a directory exports the default export of each file, but not not in itself export a default export. In this case, how can a parent directory re-export the default export of the child directory?

I updated it to work in this situation here although I was getting errors when using this syntax:

export * as queen from './queen';

and instead use this syntax:

import * as queen from './queen';
export { queen };

SharpSeeEr avatar Mar 06 '19 19:03 SharpSeeEr

So I forked your code and change the dir export style and committed.

https://github.com/deadivan/create-index

@deadivan Can you enable issues in your fork?

ChocolateLoverRaj avatar Mar 11 '22 18:03 ChocolateLoverRaj