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 🙏

© 2025 – Pkg Stats / Ryan Hefner

vite-config-editor

v0.0.1

Published

Fluent, idempotent edits to vite.config.ts via ts-morph

Readme

vite-config-editor

npm package Build Status Downloads Issues Code Coverage Commitizen Friendly Semantic Release

Fluent, idempotent edits to vite.config.ts via ts-morph

Install

npm install --save-dev vite-config-editor

Quick Start

// update-vite-config.js
import { ViteConfigEditor } from 'vite-config-editor';

const editor = ViteConfigEditor.open('./vite.config.ts');

// Ensure defineConfig wrapper
editor.ensureDefineConfigWrap();

// Add plugins
editor.ensurePluginCall({
  from: '@vitejs/plugin-react',
  callee: 'react',
  importKind: 'default',
  args: { jsx: true },
});

// Set properties with type-safe methods
editor.setString('base', '/app/');
editor.setNumber('server.port', 5173);
editor.setBoolean('build.sourcemap', true);

// Save with optional formatting
await editor.save({ format: true });

Run the script:

node update-vite-config.js

Features

  • Idempotent operations: Run operations multiple times without creating duplicates
  • Smart import resolution: Automatically handles import aliases and conflicts
  • Advanced plugin management: Add, remove, and configure Vite plugins with full control
  • Type-safe property setters: Dedicated methods for strings, numbers, booleans, and objects
  • Configurable formatting: Optional code formatting and import organization
  • Clear error messages: Actionable errors with file context and suggestions
  • TypeScript AST: Powered by ts-morph for reliable code transformations
  • Multiple config shapes: Supports export default defineConfig({...}), aliased defineConfig, and plain objects

API Documentation

Import Management

ensureDefaultImport(from: string, desiredLocal: string): string

Returns the actual local identifier used, avoiding duplicates when imports already exist.

const id = editor.ensureDefaultImport('@vitejs/plugin-react', 'customReact');
// Returns 'react' if already imported as 'react', otherwise 'customReact'

ensureNamedImport(from: string, name: string, alias?: string): string

editor.ensureNamedImport('vite', 'defineConfig', 'dc');
// import { defineConfig as dc } from 'vite'

Plugin Management

ensurePluginCall(spec: PluginSpec): ViteConfigEditor

Intelligently adds plugins, detecting existing ones even with different import names.

editor.ensurePluginCall({
  from: '@vitejs/plugin-vue',
  callee: 'vue',
  importKind: 'default',
  args: { jsx: true },
});

removePluginCall(pluginName: string): ViteConfigEditor

editor.removePluginCall('react');

addToPluginArrayArg(pluginName: string, argPath: string, value: string, options?)

Supports nested paths and insertion anchoring:

// Add to nested array
editor.addToPluginArrayArg('laravel', 'build.input', 'app.js');

// Insert after specific item
editor.addToPluginArrayArg('laravel', 'input', 'app.js', {
  after: 'app.css',
});

Property Management

Type-safe setters

editor.setString('base', '/app/');
editor.setBoolean('build.sourcemap', true);
editor.setNumber('server.port', 5173);
editor.mergeObject('resolve', { alias: { '@': '/src' } });

Array management

editor.addUniqueStringToArray('resolve.extensions', '.ts');

Saving

await editor.save({
  format: true, // Format code (default: false)
  organizeImports: true, // Organize imports (default: false)
  preserveEOL: true, // Preserve line endings (default: true)
});

Advanced Usage

Import Alias Handling

The library correctly handles existing imports with different names:

// Config has: import myReact from '@vitejs/plugin-react'
editor.ensurePluginCall({
  from: '@vitejs/plugin-react',
  callee: 'react',
  importKind: 'default',
});
// Uses existing 'myReact', doesn't add duplicate import

DefineConfig Aliasing

Detects and preserves defineConfig aliases:

// Config has: import { defineConfig as dc } from 'vite'
//            export default dc({ ... })
editor.ensureDefineConfigWrap(); // Correctly uses 'dc'

Import Consolidation

editor.consolidateImports();
// Before: import { a } from 'vite'; import { b } from 'vite';
// After: import { a, b } from 'vite';

Error Handling

Clear, actionable errors with context:

try {
  ViteConfigEditor.open('./missing.ts');
} catch (error) {
  // Cannot find config file at: ./missing.ts
  // Working directory: /current/path
  // Suggested path: /current/path/missing.ts
}

Type Definitions

type PluginSpec = {
  from: string; // Module to import from
  callee: string; // Function to call
  argsText?: string; // Raw arguments (default: "()")
  importKind?: 'default' | 'named';
  importAlias?: string; // Desired import alias
  args?: any; // Arguments object
};

type FormatOptions = {
  format?: boolean; // Format file
  organizeImports?: boolean; // Organize imports
  preserveEOL?: boolean; // Preserve line endings
};