npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@mnrendra/mixexports

v2.2.1

Published

A function to mix CommonJS exports.

Downloads

31

Readme

@mnrendra/mixexports

A function to mix CommonJS exports.

Use cases

1. Mix named exports with default export.

transforming:

exports.named1 = 'named1';
exports.named2 = 'named2';
exports.default = () => 'main';

to:

exports.named1 = 'named1';
exports.named2 = 'named2';
exports.default = () => 'main';
module.exports = exports.default;
Object.defineProperties(module.exports, {
  __esModule: { value: exports.__esModule },
  named1: { value: exports.named1 },
  named2: { value: exports.named2 },
  default: { value: exports.default }
});

This allows the consumer to import or require the module in the following ways:

import main, { named1 } from 'module'

console.log(main()) // will print: 'main'
console.log(named1) // will print: 'named1'
console.log(main.named2) // will print: 'named2'

or:

const main = require('module')
const { named1 } = require('module')

console.log(main()) // will print: 'main'
console.log(named1) // will print: 'named1'
console.log(main.named2) // will print: 'named2'

2. Export all named exports using module.exports.

transforming:

exports.named1 = 'named1';
exports.named2 = 'named2';

to:

exports.named1 = 'named1';
exports.named2 = 'named2';
module.exports = {};
exports.default = module.exports;
Object.defineProperties(module.exports, {
  __esModule: { value: exports.__esModule },
  named1: { value: exports.named1, enumerable: true },
  named2: { value: exports.named2, enumerable: true },
  default: { value: exports.default }
});

This allows the consumer to import or require the module in the following ways:

import main, { named1 } from 'module'

console.log(named1) // will print: 'named1'
console.log(main.named2) // will print: 'named2'

or:

const main = require('module')
const { named1 } = require('module')

console.log(named1) // will print: 'named1'
console.log(main.named2) // will print: 'named2'

3. Keep external live bindings.

transforming:

let amount = 0;
const increaseAmount = () => {
  amount = amount + 1;
};

Object.defineProperty(exports, "amount", { get () { return amount; }, enumerable: true });
exports.increaseAmount = increaseAmount;

to:

let amount = 0;
const increaseAmount = () => {
  amount = amount + 1;
};
Object.defineProperty(exports, "amount", { get () { return amount; } });
exports.increaseAmount = increaseAmount;
module.exports = {};
exports.default = module.exports;
Object.defineProperties(exports, {
  __esModule: { value: exports.__esModule },
  amount: { get () { return amount; }, enumerable: true },
  increaseAmount: { value: exports.increaseAmount, enumerable: true },
  default: { value: exports.default }
});

This allows the consumer to import or require the module in the following ways:

import main, { increaseAmount } from 'module'

console.log(main.amount) // will print: 0
increaseAmount()
console.log(main.amount) // will print: 1

or:

const main = require('module')
const { increaseAmount } = require('module')

console.log(main.amount) // will print: 0
increaseAmount()
console.log(main.amount) // will print: 1

Note: This function uses Object.defineProperties to make all the named exports non-enumerable. Therefore, when the consumer logs the default value with console.log, all the named exports will be hidden (unless the module has no default export) but can still be accessed via destructuring with import or require.

Install

npm i @mnrendra/mixexports

Usage

Using ES Modules:

import { readFileSync, writeFileSync } from 'node:fs'
import mixexports from '@mnrendra/mixexports'

const source = readFileSync('./source.js', { encoding: 'utf8' })

const result = mixexports(source) // mix to pretty format
const minify = mixexports(source, { minify: true }) // mix to minify format

writeFileSync('./result.js', result)
writeFileSync('./minify.js', minify)

Using CommonJS:

const { readFileSync, writeFileSync } = require('node:fs')
const mixexports = require('@mnrendra/mixexports')

const source = readFileSync('./source.js', { encoding: 'utf8' })

const result = mixexports(source) // mix to pretty format
const minify = mixexports(source, { minify: true }) // mix to minify format

writeFileSync('./result.js', result)
writeFileSync('./minify.js', minify)

Options

  • minify type: boolean default: false To produce the minified or pretty format.

  • defineEsModule type: boolean|undefined default: undefined To specify whether to define exports.__esModule.

Types

import type {
  Options // The options interface.
} from '@mnrendra/mixexports'

License

MIT

Author

@mnrendra