1.4.0 • Published 4 years ago

rollup-plugin-export-default v1.4.0

Weekly downloads
104
License
MIT
Repository
github
Last release
4 years ago

Export all as default

This is a Rollup plugin which will export all named exports as default if no default export is specified. This makes it easy to do import pkg from 'pkg' rather than import * as pkg from 'pkg', while still allowing named imports like import { myNamedExport } from 'pkg'.

Example

Consider the following source, which provides 3 named exports:

const namedExport1 = 42;
const namedExport2 = 1.6180339887498948482;

export const inlineNamedExport = {
  foo: 'bar',
  fizz: 'buzz',
};

export {
  namedExport1,
  namedExport2,
};

Build with Rollup, and you can see that in the absence of a manually specified default export, all named exports are provided as default:

import testDefaultExport from '../build/testDefaultExport.mjs';
import {
  inlineNamedExport,
  namedExport1,
  namedExport2,
} from '../build/testDefaultExport.mjs';

console.log(testDefaultExport);
console.log({
  inlineNamedExport,
  namedExport1,
  namedExport2,
});

Logs to the console:

{
  inlineNamedExport: { foo: 'bar', fizz: 'buzz' },
  namedExport1: 42,
  namedExport2: 1.618033988749895
}
{
  inlineNamedExport: { foo: 'bar', fizz: 'buzz' },
  namedExport1: 42,
  namedExport2: 1.618033988749895
}

This is done by adding an export default { ... } statement following the generated export { ... } statement, i.e.:

// generated by Rollup for output.format = 'esm'
export { namedExport1, namedExport2, namedExport3 };

// added by this plugin:
export default { namedExport1, namedExport2, namedExport3 };

It is effectively the same as doing:

export const namedExport1 = ...;
export const namedExport2 = ...;
export const namedExport3 = ...;

export default {
  namedExport1,
  namedExport2,
  namedExport3
};