Files
advanced-camera-card/scripts/svg-path-plugin.js
T
Dermot Duffy 80ec92e3b2 refactor: Replace Rollup with Vite (#2656)
This is a high risk change: every byte the card ships is emitted by a
different bundler, minified by a different minifier, and every
stylesheet is compiled by a different sass. The intent is that the
card's behaviour is unchanged.

**Significant development win**: Build time drops from 24s to 1.1s 🎉 

Also adds a test suite ('dist') that runs against the built bundle.
2026-08-04 15:00:13 -07:00

29 lines
799 B
JavaScript

import { readFileSync } from 'node:fs';
/**
* Vite plugin: importing an SVG yields `{ path, viewBox }` extracted at build
* time, the shape a Home Assistant custom iconset serves. The SVG must be a
* single-path icon.
*
* @type {() => import('vite').Plugin}
*/
export const svgPath = () => ({
name: 'svg-path',
// Run before Vite's builtin asset handling.
enforce: 'pre',
load(id) {
if (!id.endsWith('.svg')) {
return null;
}
const source = readFileSync(id, 'utf-8');
const path = / d="([^"]*)"/.exec(source)?.[1];
const viewBox = /viewBox="([^"]*)"/.exec(source)?.[1];
if (!path || !viewBox) {
throw new Error(`Imported SVG must have a path and viewBox: ${id}`);
}
return `export default ${JSON.stringify({ path, viewBox })};`;
},
});