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

@typhonjs-build-test/rollup-plugin-pkg-imports

v0.5.0

Published

Provides Rollup plugins that resolve import specifiers defined in `package.json` that link other NPM packages.

Downloads

469

Readme

@typhonjs-build-test/rollup-plugin-pkg-imports

NPM Code Style License API Docs Discord Twitch

Provides three Rollup plugins that resolve import specifiers defined in package.json imports that link other NPM packages.

  • importsLocal - Resolves import specifiers as local packages w/ fully qualified sub-path exports. In this case import specifier should exactly match an actual local main package name / export.

  • importsExternal - Resolves NPM packages from import specifiers substituting the fully qualified name in addition to adding a regular expression to the Rollup external configuration.

  • importsResolve - Resolves NPM package paths to the associated import specifier.

API documentation

Overview

These plugins are useful for library authors and general developers for a variety of use cases. importsExternal in particular is helpful when developing packages that have peer dependencies that are not directly bundled into the library package or dependencies between sub-path exports. The plugins are similar to @rollup/plugin-node-resolve and function as a resolution source to resolve internal imports from package.json.

importsExternal automatically constructs regular expressions added to the Rollup external configuration array in addition to resolving against the value provided for each activated imports entry. You may use globs in defining the imports entries allowing targeting of external peer dependency packages that have sub-path exports.

importsLocal automatically constructs regular expressions added to the Rollup external configuration array for all values that have a local path starting with ./. Any usage of the imports entries will be resolved dropping the leading # and should match the actual exports of the package. importsLocal works best for Typescript oriented packages in terms of generating types.

By default, for importsExternal and importsResolve all imports entries that refer to a local path starting with ./ are ignored. importsLocal will register keys that have values that start with ./ signifying a local path.

You may use importsExternal and importsResolve together, but usage of importsLocal should be exclusive and not paired with the former plugins without careful consideration as they serve different purposes.


Examples importsLocal:

Example #1 package.json imports entry:

{
  "name": "my-package",
  "imports": {
    "#my-package": "./src/index.ts",
    "#my-package/sub": "./src/sub/index.ts"
  },
  "exports": {
    ".": {
      "types": "./dist/index.d.ts",
      "import": "./dist/index.js"
    },
    "./sub": {
      "types": "/dist/sub/index.d.ts",
      "import": "/dist/sub/index.js"
    }
  }
}

Above the main package #my-package refers to the source code of the main export and #my-package/sub refers to the source code of the sub-path export my-package/sub. When developing a local package split across independent sub-path exports you are able to reference the local packages with the import specifier and importsLocal will replace it by dropping the leading # character. When bundling each import specifier will be marked as external and not included in the respective bundles of the main or sub-path exports.

Examples importsExternal:

Example #1 package.json imports entry:

{
  "imports": {
    "#shortname/*": "@my-org-name/a-long-package-name/*"
  },
  "peerDependencies": {
    "@my-org-name/a-long-package-name": ">=1.0.0"
  }
}

Above the #shortname/* is a shortened import key for @my-org-name/a-long-package-name. You can abbreviate the keys however you like. This allows the use of import { thing } from '#shortname/thing'; rather than the fully qualified name: import { thing } from '@my-org-name/a-long-package-name/thing';.

Variation on Example #1:

When developing a package with sub-path exports where there are cross-linked dependencies between various sub-path exports you can map an import specifier to the local package. When independently bundling each sub-path export importsExternal will automatically handle creating the appropriate Rollup external configuration to exclude bundling cross-linked sub-path exports together.


Example Rollup configuration object

// Both of the following packages are optional.
import resolve             from '@rollup/plugin-node-resolve';
import { generateDTS }     from '@typhonjs-build-test/esm-d-ts';

import { importsExternal } from '@typhonjs-build-test/rollup-plugin-pkg-imports';

const rollupConfig = {
   input: 'src/index.js',
   output: {
      file: 'dist/index.js',
      format: 'esm'
   },
   plugins: [
      importsExternal(),
      resolve(),  // Use `importsExternal` before `@rollup/plugin-node-resolve`.
      generateDTS.plugin() // Optional use of `@typhonjs-build-test/esm-d-ts`.
   ]
}

By default the closest package.json from the Rollup configuration input source is automatically loaded and all imports are processed as external.

You may provide a configuration object to importsExternal with the following entries:

| Attribute | Type | Description | |-----------|------|------------------------------------------| |importsKeys| string[] | Only target the provided imports keys. | | packageObj| object | A specific package.json object to use. |


Usage inside a ESM JS source file (./src/index.js):

import { something } from '#shortname/sub-path';

#shortname/sub-path is connected to @my-external-package/a-long-package-name/sub-path and is not included in the bundled output.


Example importsResolve:

Example #2 package.json imports entry:

{
  "imports": {
    "#shortname/*": "@my-org-name/a-long-package-name/*"
  }
}

Above the #shortname/* is a shortened import key for @my-org-name/a-long-package-name. You can abbreviate the keys however you like. This allows the use of import { thing } from '#shortname/thing'; rather than the fully qualified name: import { thing } from '@my-org-name/a-long-package-name/thing';.

Essentially, importsResolve is a convenience mechanism when using Rollup to automatically resolve import specifiers. The referenced packages are included in the bundle generated without having to manually configure @rollup/plugin-replace or @rollup/plugin-alias.

Additionally, you may provide a string array exportConditions in the plugin options to resolve specific export conditions. The default is ['node', 'import', 'default'].

For instance if you are using Vite 4.2+ import specifiers are automatically resolved in production / Rollup builds. This plugin functions in a similar manner, but handy for direct Rollup builds.


Synergies

If you are working on ES Modules / modern Javascript and document your code with JSDoc a great optional Rollup plugin and tool to use is @typhonjs-build-test/esm-d-ts. The Rollup plugin for esm-d-ts automatically creates bundled TS declarations from ESM source and is aware of rollup-plugin-pkg-imports allowing the generated TS declarations to include the correct mapped packages.

Roadmap / TODO

  • Evaluate any concerns for mono-repo use cases.
  • Create a comprehensive testsuite; rest assured though as these plugins are used in production environments.