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

conditional-export

v1.0.6

Published

find node package entry or path in exports

Downloads

10

Readme

NPM version

Find entry or path in package.json exports or imports.

https://nodejs.org/docs/latest-v12.x/api/packages.html#packages_exports

Usage

Apis usage description.

Debugging platform: https://imtaotao.github.io/conditional-export/

findEntryInExports

import { findEntryInExports } from 'conditional-export';

const exports = {
  '.': {
    require: './index.cjs',
    development: './index.development.js',
    default: './index.js',
  },
}

findEntryInExports(exports); // ./index.cjs
findEntryInExports(exports, ['development']); // ./index.development.js
findEntryInExports(exports, ['production']); // ./index.js

findPathInExports

import { findPathInExports } from 'conditional-export';

const exports = {
  './lib/*': {
    require: './src/*.cjs',
    development: './src/*.development.js',
    default: './src/*.js',
  },
}

findPathInExports('./lib/index', exports); // ./src/index.cjs
findPathInExports('./lib/index', exports, ['development']); // ./src/index.development.js
findPathInExports('./lib/index', exports, ['production']); // ./src/index.js

Multiple conditions, default conditions is ['require'].

You can specify multiple conditions.

import { findPathInExports } from 'conditional-export';

const exports = {
  './a': {
    node: {
      import: './feature-node.mjs',
      require: './feature-node.cjs',
    },
    default: './feature.default.mjs',
  }
};

findPathInExports('./a', exports); // './feature.default.mjs'
findPathInExports('./a', exports, ['node', 'require']); // './feature-node.cjs'

findPathInImports

import { findPathInImports } from 'conditional-export';

const imports = {
  '#timezones/': './data/timezones/',
  '#timezones/utc': './data/timezones/utc/index.mjs',
  '#external-feature': 'external-pkg/feature',
  '#moment/': './'
}

findPathInImports('#timezones/utc', imports); // ./data/timezones/utc/index.mjs
findPathInImports('#external-feature', imports); // external-pkg/feature
findPathInImports('#timezones/utc/index.mjs', imports); // ./data/timezones/utc/index.mjs
findPathInImports('#moment/data/timezones/utc/index.mjs', imports); // ./data/timezones/utc/index.mjs
findPathInImports('#timezones/utc/', imports); // ./data/timezones/utc/
findPathInImports('#unknown', imports); // null
findPathInImports('#moment', imports); // null

findPkgData

import { findPkgData } from 'conditional-export';

const exports = {
  './': './src/util/',
  './timezones/': './data/timezones/',
  './timezones/utc': './data/timezones/utc/index.mjs',
}

const data = findPkgData('@vue/core/timezones/pdt.mjs', exports);
// {
//   name: '@vue/core',
//   version: '',
//   path: './data/timezones/pdt.mjs',
//   resolve: '@vue/core/data/timezones/pdt.mjs',
//   raw: '@vue/core/timezones/pdt.mjs',
// }

parseModuleId

import { parseModuleId } from 'conditional-export';

parseModuleId('vue')
// {
//   name: 'vue',
//   path: '',
//   version: '',
//   raw: 'vue',
// }

parseModuleId('vue/');
// {
//   name: 'vue',
//   path: './',
//   version: '',
//   raw: 'vue/',
// }

parseModuleId('@vue/[email protected]/a.js');
// {
//   name: '@vue/core',
//   path: './a.js',
//   version: 'v1.0.0',
//   raw: '@vue/[email protected]/a.js',
// }

Extended usage

When you want to convert to absolute path, you can handle it like this.

import { findPkgData } from 'conditional-export';

const data = findPkgData('vue/src/index.js', exports, ['require', ...]);

if (data.path !== null) {
  const resolvePath = isNodeEnv
    ? path.resolve(pkgDir, data.path) // NodeJs
    : new URL(data.path, pkgDir).href; // Browser
} else {
  // If the module doesn't exist, you should throw an error.
  throw new Error(`Module '${data.raw}' Not Found`);
}

CDN

<!DOCTYPE html>
<html lang='en'>
<body>
  <script src='https://unpkg.com/conditional-export/dist/entry.umd.js'></script>
  <script>
    const {
      findPkgData,
      findPathInImports,
      findPathInExports,
      findEntryInExports,
      parseModuleId,
    } = NodePackageExports;
    
    // ...
  </script>
</body>
</html>