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

@arcmantle/vite-plugin-ce-auto-import

v1.0.4

Published

Automatically imports required web components.

Readme

@arcmantle/vite-plugin-ce-auto-import

A Vite plugin that automatically imports required web components based on their usage in your code. This plugin scans your source files for custom element tags and automatically injects the necessary import statements, eliminating the need to manually import each web component.

Features

  • 🔍 Automatic Detection: Scans files for custom element usage and automatically imports required components
  • 🏷️ Multiple Tag Patterns: Supports @customElement, @injectableElement, and customElements.define patterns
  • 📁 Directory-based: Configurable directories to scan for component definitions
  • 🎯 Prefix Filtering: Filter components by tag name prefixes (e.g., mm-, pl-)
  • Smart Caching: Built-in caching system for improved performance
  • 🎛️ Flexible Configuration: Whitelist/blacklist patterns for both component scanning and file processing

Installation

npm install @arcmantle/vite-plugin-ce-auto-import
# or
pnpm add @arcmantle/vite-plugin-ce-auto-import
# or
yarn add @arcmantle/vite-plugin-ce-auto-import

Usage

Add the plugin to your vite.config.ts:

import { defineConfig } from 'vite';
import { componentAutoImporter } from '@arcmantle/vite-plugin-ce-auto-import';

export default defineConfig({
  plugins: [
    componentAutoImporter({
      directories: [{ path: './src/components' }],
      prefixes: [/mm-/],
      loadWhitelist: [/\.ts$/],
      loadBlacklist: [/\.demo/],
    }),
  ],
});

Configuration

AutoImportPluginProps

| Property | Type | Description | |----------|------|-------------| | directories | Array<{ path: string; whitelist?: RegExp[]; blacklist?: RegExp[]; }> | Directories to scan for component definitions | | prefixes | RegExp[] | Array of regex patterns to match tag name prefixes | | loadWhitelist | RegExp[] | Files that should be processed by the plugin | | loadBlacklist | RegExp[] | Files that should be excluded from processing | | cache | Map<string, string> | Optional custom cache for tag-to-file mappings |

Directory Configuration

Each directory entry supports:

  • path: The directory path to scan
  • whitelist: Optional array of regex patterns for files to include
  • blacklist: Optional array of regex patterns for files to exclude

How It Works

  1. Build Start: The plugin scans specified directories for component definitions using these patterns:

    • @customElement('tag-name')
    • @injectableElement('tag-name')
    • customElements.define('tag-name', ...)
  2. File Processing: When processing files, the plugin:

    • Checks if the file matches whitelist/blacklist criteria
    • Scans for closing tags matching configured prefixes (e.g., </mm-button>)
    • Looks up component import paths from the cache
    • Injects import statements at the top of the file
  3. Import Injection: Generated imports look like:

    /* Component imports injected from: @arcmantle/vite-plugin-ce-auto-import */
    import './components/button/button.component.js';
    import './components/dialog/dialog.component.js';
    /*  */
    
    // Your original code here...

Examples

Basic Configuration

componentAutoImporter({
  directories: [{ path: './src/components' }],
  prefixes: [/my-/],
  loadWhitelist: [/\.(ts|js)$/],
})

Advanced Configuration

componentAutoImporter({
  directories: [
    {
      path: './src/components',
      whitelist: [/\.component\.ts$/],
      blacklist: [/\.test\.ts$/]
    },
    {
      path: './src/widgets',
      whitelist: [/\.widget\.ts$/]
    }
  ],
  prefixes: [/mm-/, /widget-/],
  loadWhitelist: [/\.ts$/],
  loadBlacklist: [/\.demo\.ts$/, /\.test\.ts$/],
})

Multiple Prefixes

componentAutoImporter({
  directories: [{ path: './src' }],
  prefixes: [
    /^app-/,      // matches app-header, app-footer
    /^ui-/,       // matches ui-button, ui-input
    /^layout-/,   // matches layout-grid, layout-flex
  ],
  loadWhitelist: [/\.ts$/],
})

Component Definition Patterns

The plugin recognizes these component definition patterns:

@customElement Decorator

import { customElement } from 'lit/decorators.js';

@customElement('my-button')
export class MyButton extends LitElement {
  // component implementation
}

@injectableElement Decorator

import { injectableElement } from '@arcmantle/lit-utilities';

@injectableElement('my-dialog')
export class MyDialog extends LitElement {
  // component implementation
}

customElements.define

customElements.define('my-input', class extends HTMLElement {
  // component implementation
});

Use Cases

Component Libraries

Perfect for component libraries where you want automatic imports:

// Before: Manual imports required
import './components/button.js';
import './components/input.js';
import './components/dialog.js';

// Your template using the components
const template = html`
  <my-button>Click me</my-button>
  <my-input placeholder="Enter text"></my-input>
  <my-dialog>Modal content</my-dialog>
`;

// After: Automatic imports based on usage
const template = html`
  <my-button>Click me</my-button>
  <my-input placeholder="Enter text"></my-input>
  <my-dialog>Modal content</my-dialog>
`;
// Imports are automatically injected!

Large Applications

For applications with many custom elements across multiple directories:

componentAutoImporter({
  directories: [
    { path: './src/components' },      // UI components
    { path: './src/widgets' },         // Complex widgets
    { path: './src/pages' },          // Page-level components
  ],
  prefixes: [/app-/, /ui-/, /page-/],
  loadWhitelist: [/\.(ts|js)$/],
  loadBlacklist: [/\.test\.(ts|js)$/, /\.stories\.(ts|js)$/],
})

Performance

  • Caching: The plugin uses intelligent caching to avoid re-scanning unchanged files
  • Selective Processing: Only processes files matching whitelist criteria
  • Build-time: All scanning happens at build time, no runtime overhead

Requirements

  • Node.js >= 22
  • Vite >= 7.0.0

Dependencies

  • globby: Fast glob matching for directory scanning
  • oxc-walker: High-performance JavaScript/TypeScript AST walker
  • magic-string: Efficient string manipulation with source maps

License

Apache-2.0

Contributing

This package is part of the Arcmantle Weave monorepo. Contributions are welcome!