Proposal: re-export declaration merging?

For example:

// some-lib/index.js
export * as w from './compiler.js';
export * as w from './methods.js';
export * as w from './module.js';
export * as w from './nodes.js';
export * as w from './utils.js';

and the consumer can:

import {w} from 'some-lib/index.js'

Benefit: w is auto-importable in IDEs in this manner.

Otherwise, with only this:

// some-lib/index.js
export * from './compiler.js';
export * from './methods.js';
export * from './module.js';
export * from './nodes.js';
export * from './utils.js';

the consumer can do this:

import * as w from 'some-lib/index.js'

but they cannot type w and have their IDE suggest importing w from some-lib.

With small libs, being able to auto-import the lib's index is more useful than tree-shaking (exporting everyting under a variable makes it more difficult to tree shake).

You can accomplish this with an intermediate file:

// some-lib/all-exports.js
export * from './compiler.js';
export * from './methods.js';
export * from './module.js';
export * from './nodes.js';
export * from './utils.js';
// some-lib/index.js
export * as w from './all-exports.js';

It's a little awkward, but I think not so bad or common that it warrants introducing a magic merging behavior on what would otherwise be an identifier conflict.

3 Likes

You could even do this with a single file, though that's probably far more awkward:

// some-lib/index.js
export * from './compiler.js';
export * from './methods.js';
export * from './module.js';
export * from './nodes.js';
export * from './utils.js';

import * as w from './index.js'; // self-import
export { w }

But I agree, "namespace object merging" should not be necessary.

I'd rather see IDEs offer an auto-import when you type someLib. You still can choose to rename that local variable to w later. I don't like modules that export namespace objects when named exports would have been sufficient.

All bundlers I've seen handle namespace objects just fine, as long as you only access individual properties on them (instead of treating them as objects and, say, enumerate their properties or something).

2 Likes

That just blew my mind

3 Likes