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

Support creating index with default export

Open bmish opened this issue 3 years ago • 9 comments

Currently, this plugin only supports creating an index with named exports. I need an index file with a default-exported object.

Example:

import foo from './foo.js';
import bar from './bar.js';
import _3_x_recommended from './3-x-recommended.js'; // note unsafe variable name needs to be tweaked

export default { 
  foo, 
  bar, 
  '3-x-recommended': _3_x_recommended
};

I see this functionality is in a forked version of this tool: https://github.com/tjjfvi/create-index-better#modes

// @create-index {"mode":"default{}"}

import foo from './foo.js'; import bar from './bar.js'; export default { foo, bar };

Has it been considered for the original create-index? @tjjfvi would you consider upstreaming this functionality (and presumably any other improvements from the fork)?

bmish avatar Dec 29 '21 23:12 bmish

Open to it. As long as it is behind a flag, I see no harm.

gajus avatar Jan 03 '22 18:01 gajus

I definitely support any improvements from create-index-better being upstreamed. I haven’t the bandwidth to do that atm, but I’m happy to interface with someone who does if that’s helpful.

tjjfvi avatar Jan 04 '22 02:01 tjjfvi

I need an index file with a default-exported object.

@bmish why?

ChocolateLoverRaj avatar Mar 11 '22 18:03 ChocolateLoverRaj

Example of the index file I need to generate:

  • https://github.com/ember-template-lint/ember-template-lint/blob/master/lib/config/index.js
  • https://github.com/ember-template-lint/ember-template-lint/blob/master/lib/rules/index.js

bmish avatar Mar 11 '22 18:03 bmish

@bmish But why can't you just export named and use import * as obj?

ChocolateLoverRaj avatar Mar 11 '22 18:03 ChocolateLoverRaj

@ChocolateLoverRaj I'm unclear what you mean by that. Can you show an example?

bmish avatar Mar 11 '22 18:03 bmish

@bmish https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import#import_an_entire_modules_contents

ChocolateLoverRaj avatar Mar 11 '22 20:03 ChocolateLoverRaj

@ChocolateLoverRaj I'm not sure how that helps me. I need to re-export every file in a directory.

bmish avatar Mar 12 '22 15:03 bmish

@bmish In the file you can do this:

// letters.js
export const a = 0
export const b = 1
export const c = 2

Instead of default export. Then In the file you want to import the first file with you can do this:

import * as letters from './letters.js'

letters.a // 0
letters.b // 1
letters.c // 2

ChocolateLoverRaj avatar Mar 12 '22 17:03 ChocolateLoverRaj