@mnrendra/rollup-plugin-mixexport
TypeScript icon, indicating that this package has built-in type declarations

1.0.0 • Public • Published

@mnrendra/rollup-plugin-mixexport

🍣 A Rollup plugin to mix the named and default exports together.
So, the consumers of your bundle will not have to use chunk.default to access their default export.

Example:

Your source code might be like this:

export const named () => {
  console.log('named')
}

export default () => {
  console.log('default')
}

Then, your consumer could consume by like this:

import index, { named } from 'your-module-name'
index() // will print: 'default'
named() // will print: 'named'

Or, could consume by like this:

const index = require('your-module-name')
const { named } = require('your-module-name')
index() // will print: 'default'
named() // will print: 'named'

Why?

Because by default, Rollup will not mix the named and default exports together.
So, Rollup will automatically add .default for every default export that is mixed with the named exports.

Requirements

This plugin requires:
LTS Node version (v14.0.0+),
Rollup (v4.12.0+),
✅ ESBuild plugin (v6.1.1+)

Install

npm i -D rollup-plugin-esbuild @mnrendra/rollup-plugin-mixexport

Usage

Using rollup.config.mjs:

import esbuild from 'rollup-plugin-esbuild' // 'rollup-plugin-esbuild' is required
import mixexport from '@mnrendra/rollup-plugin-mixexport'

export default [
  {
    external: (id) => !/^[./]/.test(id),
    input: 'your_input_file.(js|cjs|mjs|jsx|ts|cts|mts|tsx)',
    output: [
      {
        file: 'dist/your_output_file.js',
        format: 'cjs',
        sourcemap: true
      },
      {
        file: 'dist/your_output_file.mjs',
        format: 'es',
        sourcemap: true
      }
    ],
    plugins: [
      esbuild({ minify: true }), // <-- need to execute `esbuild` immediately before `mixexport`
      mixexport() // <-- execute `mixexport` immediately after `esbuild`
    ],
    onwarn ({ code }) {
      if (code === 'MIXED_EXPORTS') return false // to disable Rollup's 'MIXED_EXPORTS' warning log
    }
  }
]

Using rollup.config.js:

const esbuild = require('rollup-plugin-esbuild') // 'rollup-plugin-esbuild' is required
const mixexport = require('@mnrendra/rollup-plugin-mixexport')

module.export = [
  {
    external: (id) => !/^[./]/.test(id),
    input: 'your_input_file.(js|cjs|mjs|jsx|ts|cts|mts|tsx)',
    output: [
      {
        file: 'dist/your_output_file.js',
        format: 'cjs',
        sourcemap: true
      },
      {
        file: 'dist/your_output_file.mjs',
        format: 'es',
        sourcemap: true
      }
    ],
    plugins: [
      esbuild.default({ minify: true }),
      mixexport()
    ],
    onwarn ({ code }) {
      if (code === 'MIXED_EXPORTS') return false // to disable Rollup's 'MIXED_EXPORTS' warning log
    }
  }
]

License

MIT

Author

@mnrendra

Package Sidebar

Install

npm i @mnrendra/rollup-plugin-mixexport

Weekly Downloads

1

Version

1.0.0

License

MIT

Unpacked Size

26.1 kB

Total Files

9

Last publish

Collaborators

  • mnrendra