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

resolve-with-prefix

v4.0.1

Published

Resolve modules with a predefined prefix.

Downloads

107

Readme

resolve-with-prefix

npm Linux Build Status Windows Build Status Code Coverage

Resolve configuration files with a predefined prefix.

About

Add a predefined prefix to find resolvable modules. This is meant to be used with configuration files.

For example, @babel/env --> @babel/preset-env.

Installation

npm install --save resolve-with-prefix

Usage

import {
	resolveWithPrefix,
	resolveWithPrefixSync,
	createResolver,
	createResolverSync,
} from 'resolve-with-prefix';

const presetOptions = {
	prefix: 'babel-preset',
	org: '@babel',
	orgPrefix: 'preset',
};

const pluginOptions = {
	prefix: 'babel-plugin',
	org: '@babel',
	orgPrefix: 'plugin',
};

const resolvePreset = createResolver(presetOptions);
const resolvePresetSync = createResolverSync(presetOptions);
const resolvePlugin = createResolver(pluginOptions);
const resolvePluginSync = createResolverSync(pluginOptions);

// resolve @babel/preset-env, @babel/env
await resolveWithPrefix('@babel/env', presetOptions);
resolveWithPrefixSync('@babel/env', presetOptions);
await resolvePreset('@babel/env');
resolvePresetSync('@babel/env');

// resolve babel-plugin-transform-object-rest-spread, transform-object-rest-spread
await resolveWithPrefix('transform-object-rest-spread', pluginOptions);
resolveWithPrefixSync('transform-object-rest-spread', pluginOptions);
await resolvePlugin('transform-object-rest-spread');
resolvePluginSync('transform-object-rest-spread');

Options

import { resolveWithPrefix, createResolver } from 'resolve-with-prefix';

const options = {
	/**
	 * Prefix to add to packageId
	 *
	 * Optional
	 *
	 * Accepts a single or an array of prefixes
	 */
	prefix: 'example-prefix',

	/**
	 * NPM Scope of organization to override prefix
	 *
	 * Optional
	 */
	org: '@example',

	/**
	 * Org prefixes
	 *
	 * Optional
	 *
	 * Accepts a single or an array of prefixes
	 */
	orgPrefix: [
		'prefix',
		'example-prefix',
	],

	/**
	 * Only allow prefixed module resolution.
	 * Explicit modules can be required by pre-pending "module:"
	 * For example, module:local-module
	 *
	 * Optional
	 *
	 * Default: true
	 */
	strict: false,
};

const resolve = createResolver(options);

/**
 * Matches the first matched packages
 * example-prefix-one , one
 */
await resolveWithPrefix('one', options);
await resolve('one');

/**
 * Matches the first matched packages
 * @other/example-prefix-one , @other/one
 */
await resolveWithPrefix('@other/one', options);
await resolve('@other/one');

/**
 * Matches the first matched packages
 * @example/prefix-one , @example/example-prefix-one , @example/one
 */
await resolveWithPrefix('@example/one', options);
await resolve('@example/one');

/**
 * Use the dirname option to specify where to search for node_modules
 *
 * Default is process.cwd()
 */
await resolveWithPrefix('one', { ...options, dirname: __dirname });
await resolve('one', { dirname: __dirname });

/**
 * Explicitly resolve a module
 *
 * See configuration option: strict
 */
await resolveWithPrefix('module:local-module', options);
await resolve('module:local-module');

/**
 * Absolute and relative paths allowed
 */
await resolve('/path/to/module');
await resolve('./path/to/module');

Thanks To

This package was created with the great work / lessons learned from: