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.
@customElement
, @injectableElement
, and customElements.define
patternsmm-
, pl-
)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
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/],
}),
],
});
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 |
Each directory entry supports:
path
: The directory path to scanwhitelist
: Optional array of regex patterns for files to includeblacklist
: Optional array of regex patterns for files to excludeBuild Start: The plugin scans specified directories for component definitions using these patterns:
@customElement('tag-name')
@injectableElement('tag-name')
customElements.define('tag-name', ...)
File Processing: When processing files, the plugin:
</mm-button>
)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...
componentAutoImporter({
directories: [{ path: './src/components' }],
prefixes: [/my-/],
loadWhitelist: [/\.(ts|js)$/],
})
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$/],
})
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$/],
})
The plugin recognizes these component definition patterns:
import { customElement } from 'lit/decorators.js';
@customElement('my-button')
export class MyButton extends LitElement {
// component implementation
}
import { injectableElement } from '@arcmantle/lit-utilities';
@injectableElement('my-dialog')
export class MyDialog extends LitElement {
// component implementation
}
customElements.define('my-input', class extends HTMLElement {
// component implementation
});
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!
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)$/],
})
Apache-2.0
This package is part of the Arcmantle Weave monorepo. Contributions are welcome!