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 🙏

© 2026 – Pkg Stats / Ryan Hefner

importmap-vite-plugin

v0.1.0

Published

Vite plugin to generate import map JSON virtual module for sharing dependencies without duplication

Readme

importmap-vite-plugin

A Vite plugin that generates an import map JSON virtual module you can use in your app. This allows you to share dependencies without duplication - your existing app modules (like React) won't be duplicated when using external libraries.

Installation

npm install importmap-vite-plugin
# or
pnpm add importmap-vite-plugin
# or
yarn add importmap-vite-plugin

Usage

Add the plugin to your vite.config.js:

import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'
import { importMapPlugin } from 'importmap-vite-plugin'

export default defineConfig({
  plugins: [
    react(),
    importMapPlugin({
      imports: {
        // Map to local modules (these will be bundled)
        'react': './src/import-map/react',
        'react-dom': './src/import-map/react-dom',
        'react/jsx-runtime': './src/import-map/react/jsx-runtime',
        
        // Map to external URLs (these remain external)
        'framer-motion': 'https://esm.sh/framer-motion?external=react',
        '@motionone/dom': 'https://esm.sh/@motionone/dom?external=react',
        'framer': 'https://esm.sh/unframer@latest/esm/framer.js?external=react',
      }
    })
  ],
})

Setting up Local Module Re-exports

When using CommonJS modules (like React from node_modules), you need to create re-export files to ensure named imports work correctly. CommonJS modules only have a default export, so modules using named imports like import { useState } from 'react' would fail without this wrapper.

Create these files in your src/import-map/ directory:

src/import-map/react.js

import React from 'react'

// Re-export all React exports to support both named and default imports
export const {
    isValidElement,
    createElement,
    Fragment,
    useState,
    useEffect,
    useRef,
    useMemo,
    useCallback,
    useContext,
    Children,
    cloneElement,
    createContext,
    forwardRef,
    lazy,
    memo,
    Profiler,
    PureComponent,
    Suspense,
    Component,
    startTransition,
    useDebugValue,
    useDeferredValue,
    useId,
    useImperativeHandle,
    useInsertionEffect,
    useLayoutEffect,
    useReducer,
    useSyncExternalStore,
    useTransition,
    createRef,
    cache,
    useOptimistic,
    StrictMode,
    captureOwnerStack,
    useActionState,
    use,
} = React

export default React

// Export the URL for the import map
export const url = import.meta.url

src/import-map/react-dom.js

import ReactDOM from 'react-dom'

export const {
    createPortal,
    flushSync,
    hydrate,
    render,
    unmountComponentAtNode,
    unstable_batchedUpdates,
    unstable_renderSubtreeIntoContainer,
    version,
} = ReactDOM

export default ReactDOM
export const url = import.meta.url

src/import-map/react/jsx-runtime.js

export { jsx, jsxs, Fragment } from 'react/jsx-runtime'

Using the Import Map

The plugin creates a virtual module that you can import in your application:

import importMap from 'virtual:importmap'

// Use the import map in your application
console.log(importMap)
// {
//   imports: {
//     'react': 'http://localhost:5173/src/import-map/react',
//     'react-dom': 'http://localhost:5173/src/import-map/react-dom',
//     'framer-motion': 'https://esm.sh/framer-motion?external=react',
//     ...
//   }
// }

Complete Example - Mounting the Import Map

Here's a complete example of how to mount the import map in your app's entry point:

import React from 'react'
import ReactDOM from 'react-dom/client'
import { App } from './app'
import importMap from 'virtual:importmap'

function setupImportMap() {
    const mapScript = document.createElement('script')
    mapScript.type = 'importmap'
    mapScript.textContent = JSON.stringify(importMap, null, 2)
    console.log(mapScript.textContent)
    document.head.append(mapScript)
}
setupImportMap()

ReactDOM.createRoot(document.getElementById('root')!).render(<App />)

TypeScript Support

To make the virtual:importmap module type-safe, add this declaration file to your project:

src/vite-env.d.ts

/// <reference types="vite/client" />

declare module 'virtual:importmap' {
  const importMap: {
    imports: Record<string, string>
  }
  export default importMap
}

Features

  • No Duplication: Uses your app's existing modules instead of bundling duplicates
  • Dev & Build Support: Works in both development and production builds
  • Local & External: Supports both local file paths and external URLs
  • Type Safe: Includes TypeScript definitions

How It Works

The plugin:

  1. Creates a virtual module virtual:importmap that you can import
  2. In development, uses Vite's ?url imports for local modules
  3. In production builds, emits chunks for local modules and provides their URLs
  4. External URLs are passed through as-is

License

ISC