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

normalize-exports

v0.0.7

Published

Normalizes the exports field of package.json

Downloads

3

Readme

normalize-exports

Normalizes the "exports" field of package.json.

Install

$ npm install normalize-exports

Usage

See src/normalize.test.ts for examples.

import { normalizeExports } from 'normalize-exports';

const exportsField = {
  '.': {
    import: './dist/esm/index.js',
    default: './dist/index.js',
  },
  './feature': {
    require: './dist/cjs/feature.js',
    default: './dist/feature.js',
  },
  './anotherFeature': [
    {
      worker: './dist/node/anotherFeature.js',
    },
    {
      browser: './dist/umd/anotherFeature.js',
    },
  ],
  './yetAnotherFeature': {
    node: {
      deno: './dist/deno/yetAnotherFeature.js',
      worker: './dist/node/yetAnotherFeature.js',
    },
  },
};

normalizeExports(exportsField);
/* =>
{
  '.': './dist/esm/index.js',
  './feature': './dist/cjs/feature.js'
}
*/

normalizeExports(exportsField, { conditions: ['import', 'require', 'browser'] });
/* =>
{
  '.': './dist/esm/index.js',
  './feature': './dist/cjs/feature.js',
  './anotherFeature': './dist/umd/anotherFeature.js'
}
*/

normalizeExports(exportsField, { conditions: ['import', 'node', 'worker', 'default'] });
/* =>
{
  '.': './dist/esm/index.js',
  './feature': './dist/feature.js',
  './anotherFeature': './dist/node/anotherFeature.js',
  './yetAnotherFeature': './dist/node/yetAnotherFeature.js'
}
*/

API Specification

normalizeExports(exports, options?)

Returns object

Normalizes the "exports" field of package.json and returns a flattened object after matching nested conditions and resolving wildcard patterns.

Empty object will be returned if none of the conditions match.

May throw an error if:

  • "exports" is undefined or empty.
  • "exports" is not well-formed.
  • A subpath target is invalid.

exports

Type: object
Required: true

The "exports" field of package.json.

options.conditions

Type: string[]
Required: false

Export conditions that should be matched. Following conditions are supported by default: ['node-addons', 'node', 'import', 'require', 'default'] as per https://nodejs.org/api/packages.html#conditional-exports. Providing a non-empty array will override the default conditions.

The order specified here does not matter. Conditions are always matched based on "exports" map's key order.

For example,

normalizeExports({
  worker: './dist/node/index.js',
  production: './dist/prod/index.js',
});

/* =>
{}
*/

normalizeExports({
  worker: './dist/node/index.js',
  default: './dist/index.js',
});

/* =>
{
  '.': './dist/index.js'
}
*/

normalizeExports(
  {
    worker: './dist/node/index.js',
    production: './dist/prod/index.js',
  },
  { conditions: ['worker', 'production'] }
);

/* =>
{
  '.': './dist/node/index.js'
}
*/

normalizeExports(
  {
    worker: './dist/node/index.js',
    production: './dist/prod/index.js',
  },
  { conditions: ['production'] }
);

/* =>
{
  '.': './dist/prod/index.js'
}
*/

options.cwd

Type: string
Required: false

Current working directory to resolve subpaths containing wildcard patterns.

For example,

normalizeExports({
  './features/*.js': './dist/features/*.js',
});

/* =>
{
  './features/*.js': './dist/features/*.js'
}
*/

normalizeExports(
  {
    './features/*.js': './dist/features/*.js',
  },
  {
    cwd: '/Volumes/test',
  }
);

/* =>
{
  './features/nested-feature/index': './dist/features/nested-feature/index.js',
  './features/someFeature': './dist/features/someFeature.js',
}
*/

License

MIT