Proposal: export ** from './FooBar'

# Cyril Auburtin (6 years ago)

Sometimes, (particularly in React codebases) you have to write many lines like:

export { default as FooBar } from './FooBar';

It could be automated of course

It could also be turned into

export * from './FooBar';

only if FooBar is also a named export in that file, but usually files with one export just use a default export


This proposed syntax:

export ** from './FooBar';

Would do:

export * from './FooBar';
export { default as FooBar } from './FooBar';

the filename is used as default export name.

If the filename isn't a valid JS identifier, like 'foo-bar' for example, it would throw

# Mike Samuel (6 years ago)

On Fri, Mar 1, 2019 at 1:35 AM Cyril Auburtin <cyril.auburtin at gmail.com>

wrote:

Sometimes, (particularly in React codebases) you have to write many lines like:

export { default as FooBar } from './FooBar';

It could be automated of course

It could also be tuned into

export * from './FooBar';

only if FooBar is also a named export in that file, but usually files with one export just use a default export


This proposed syntax:

export ** from './FooBar';

Would do:

export * from './FooBar';
export { default as FooBar } from './FooBar';

the filename is used as default export name.

If the filename isn't a valid JS identifier, like 'foo-bar' for example, it would throw

ES doesn't impose many constraints on the string content of a module specifier but in practice it maps fairly directly to a URL per html.spec.whatwg.org/multipage/webappapis.html#hostgetimportmetaproperties so filename is probably broadly available.

Would this use the filename from the module specifier or from the URL (assuming import.meta.url)? IIRC, only the module specifier is available within ES currently. The translation between specifiers and URLs (if that happens) is up to a host callback (HostResolveImportedModule) and does not surface inside the ES engine. That import.meta.url exists is up to another host callback so assuming it's existence might require constraining host implementations of HostGetMetaProperties(?).

What would this do when import.meta.url/specifier has a query part, or that look like a non-hierarchical URL? export ** from "path?foo=bar"; export ** from "?foo=bar"; export ** from "data:application/javascript+module,export default null;"

Does anyone know if existing CDNs use the path component for something other than the path contributed by the uploader and expect the original filename in the query, like host.cdn/path/to/tarball?p=path/within/tarball.js ?

# Jordan Harband (6 years ago)

tc39/ecma262#1174 adds export * as someName from 'path'; tc39/proposal-export-default-from is the proposal that you're looking for.

# Guy Bedford (6 years ago)

export ** has been suggested before, and it does seem like it would be a useful proposal to me.