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

vite-plugin-conditional-imports

v0.1.3

Published

Strip conditional imports (only, path patterns, or custom predicate) in production builds and warn on leftover references

Downloads

35

Readme

vite-plugin-conditional-imports

npm version

Vite plugin to strip imports conditionally.

Typical use case:

import debug from './debug' with { only: 'dev' }

if (import.meta.env.DEV) {
  debug()
}

This plugin can be configured to strip the ./debug import when building for production. Then we rely on Rollup's tree shaking to remove the dead code during a production build.

If we forget to guard dev code, the plugin notices missing references in the chunk output and fails the build.

You can also perform stripping based on the import path itself.

Usage

Strip based on import with metadata:

import { defineConfig } from 'vite'
import { conditionalImports } from 'vite-plugin-conditional-imports'

export default defineConfig({
  plugins: [
    conditionalImports(ctx => {
      // Strip in production when import has `with { only: 'dev' }`
      return ctx.config.mode === 'production' && ctx.withObject?.only === 'dev'
    }),
  ],
})

Strip based on path:

export default defineConfig({
  plugins: [
    conditionalImports(async ctx => {
      // Strip in production when import paths contains `/dev/`
      return (
        ctx.config.mode === 'production' &&
        (await ctx.resolvedTarget).includes('/dev/')
      )
    }),
  ],
})

Context object

| Property | Description | | ---------------- | --------------------------------------------------------------------------------------------------------------------------------------------------- | | target | Import specifier as written in source (e.g. ./dev-only.js, @lib/custom). | | resolvedTarget | Promise<string>, resolved path relative to Vite's config.root. Lazy: resolution runs only when you access it (e.g. await ctx.resolvedTarget). | | source | Path of the file that contains the import, relative to config.root. | | withObject | Import attributes from with { ... } (e.g. { only: 'dev' }). | | config | Resolved Vite config (config.mode, config.build.ssr, etc.). | | env | Vite config env (env.mode, env.ssrBuild, etc.). |

Extra options

You can pass an options object after the callback functon with the following properties:

| Option | Type | Default | Description | | ---------------- | --------- | ------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | | autoSourceMaps | boolean | true | When true, the plugin enables Vite sourcemaps (if not set) and uses them to resolve error locations to the original file. When false, it does not touch the config, and if source maps are not already enabled, it falls back to reporting all files where a given import was stripped, which is less useful, but builds faster. | | applyInServe | boolean | false | Set to true to also use the plugin with vite serve. |