<!-- mobian-agent-page publisher="dailydev" canonical="https://daily.dev/posts/build-a-vite-plugin-to-catch-broken-svg-assets-before-production-ps89inq3t" -->

---
title: Build a Vite Plugin to Catch Broken SVG Assets Before...
description: A step-by-step tutorial shows how to build a custom Vite plugin that scans a project's SVG assets, enforces rules like requiring a viewBox, banning script and...
canonical: https://daily.dev/posts/build-a-vite-plugin-to-catch-broken-svg-assets-before-production-ps89inq3t
twitter:card: summary_large_image
twitter:site: @dailydotdev
og:type: website
og:site_name: daily.dev
og:title: Build a Vite Plugin to Catch Broken SVG Assets Before Production | daily.dev
og:description: A step-by-step tutorial shows how to build a custom Vite plugin that scans a project's SVG assets, enforces rules like requiring a viewBox, banning script and...
og:url: https://daily.dev/posts/build-a-vite-plugin-to-catch-broken-svg-assets-before-production-ps89inq3t
og:image: https://api.daily.dev/og/posts/PS89iNq3T.png
og:image:alt: Build a Vite Plugin to Catch Broken SVG Assets Before Production
og:image:width: 1200
og:image:height: 630
og:locale: en
---

> ## Documentation Index
> Fetch the complete documentation index at: https://daily.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Build a Vite Plugin to Catch Broken SVG Assets Before Production

**[SitePoint](https://daily.dev/sources/sitepoint)** · 17 min read · 0 upvotes · 0 comments

## Summary

A step-by-step tutorial shows how to build a custom Vite plugin that scans a project's SVG assets, enforces rules like requiring a viewBox, banning script and foreignObject elements, and staying under a size budget, then blocks builds and warns during development when assets violate those rules. It covers structuring the plugin, hooking into buildStart and configureServer, making it configurable, extracting a reusable validation engine, and wiring the same checks into CI, framing the approach as turning tribal knowledge about asset requirements into an executable build contract.

## Full article

daily.dev links to this article rather than hosting it. Read it at the original source: <https://www.sitepoint.com/build-a-vite-plugin-to-catch-broken-svg-assets-before-production>

## Questions this post answers

### How do I write a Vite plugin that fails the build when an SVG file is missing a viewBox attribute?

Use the buildStart hook to read and scan SVG files with a regex check for viewBox, collect any missing-attribute errors into a list, then call this.error(message) inside buildStart to abort the build with a formatted error listing the offending file and reason. This turns a documentation convention into an enforced build contract rather than a manual review step.

_Developers wiring asset checks into their build pipeline can find similar Vite plugin patterns on daily.dev._

### How can I get instant feedback in the Vite dev server when a watched asset file changes and fails validation?

Use the configureServer plugin hook to access server.watcher, add the target directory to it, and listen for the change event; when an SVG file changes, run the validation function and log formatted errors to the terminal without needing a rebuild. This gives feedback the moment a file is saved instead of waiting for a production build to fail.

_daily.dev surfaces build-tooling techniques like this for developers refining their dev feedback loops._

### What is a reasonable file-size budget to enforce for brand or icon SVG assets in a frontend project?

A commonly cited starting point is 100KB for brand logos and around 30-40KB for smaller icon sets, checked via Node's fs.stat and compared against a configurable maxSizeKB option. Exceeding the budget triggers a build error stating the actual size versus the allowed maximum, catching accidental bloat from unoptimized design-tool exports.

_Teams setting asset budgets can track build-tooling approaches like this on daily.dev when choosing enforcement strategies._

## Similar posts on daily.dev

- [Build a Vite plugin from scratch](https://daily.dev/posts/build-a-vite-plugin-from-scratch-ghvwrwdxi) · Flavio Copes · 7 upvotes · 0 comments
- [Embeddable Widgets with Vite, React, Tailwind 4 & Web Components](https://daily.dev/posts/embeddable-widgets-with-vite-react-tailwind-4-web-components-kmten04bk) · Viget · 8 upvotes · 1 comments
- [The woes of sanitizing SVGs](https://daily.dev/posts/the-woes-of-sanitizing-svgs-pwfqj7jvs) · Hacker News · 1 upvotes · 0 comments
- [Novel clickjacking attack relies on CSS and SVG](https://daily.dev/posts/novel-clickjacking-attack-relies-on-css-and-svg-609qdgjok) · The Register · 1 upvotes · 0 comments
- [How I \(Almost\) Got Hit by a Fake Recruiter Malware Scam — and How Claude Helped Me Catch It](https://daily.dev/posts/how-i-almost-got-hit-by-a-fake-recruiter-malware-scam-and-how-claude-helped-me-catch-it-2iteehrpf) · Medium · 0 upvotes · 0 comments

---

Tags: [#javascript](https://daily.dev/tags/javascript), [#devtools](https://daily.dev/tags/devtools), [#svg](https://daily.dev/tags/svg), [#vite](https://daily.dev/tags/vite)

[View this post on daily.dev](https://daily.dev/posts/build-a-vite-plugin-to-catch-broken-svg-assets-before-production-ps89inq3t)

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://daily.dev/#organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180},"sameAs":["https://twitter.com/dailydotdev","https://github.com/dailydotdev","https://www.linkedin.com/company/daily-dev-ltd"]},{"@type":"WebSite","@id":"https://daily.dev/#website","url":"https://daily.dev","name":"daily.dev","publisher":{"@id":"https://daily.dev/#organization"},"potentialAction":{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https://daily.dev/search?q={search_term_string}"},"query-input":"required name=search_term_string"}}]}
{"@context":"https://schema.org","@type":"TechArticle","headline":"Build a Vite Plugin to Catch Broken SVG Assets Before Production","url":"https://daily.dev/posts/build-a-vite-plugin-to-catch-broken-svg-assets-before-production-ps89inq3t","mainEntityOfPage":{"@type":"WebPage","@id":"https://daily.dev/posts/build-a-vite-plugin-to-catch-broken-svg-assets-before-production-ps89inq3t"},"datePublished":"2026-08-25T15:02:55.581Z","dateModified":"2026-09-14T07:55:55.992Z","description":"A step-by-step tutorial shows how to build a custom Vite plugin that scans a project's SVG assets, enforces rules like requiring a viewBox, banning script and...","image":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/9c89e13cb6f5eb586bcf4642ba1d7c25?_a=AQAEuop","thumbnailUrl":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/9c89e13cb6f5eb586bcf4642ba1d7c25?_a=AQAEuop","isAccessibleForFree":true,"articleSection":"SitePoint","inLanguage":"en","publisher":{"@type":"Organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180}},"author":{"@type":"Organization","name":"SitePoint","logo":"https://media.daily.dev/image/upload/t_logo,f_auto/v1/logos/sitepoint","url":"https://daily.dev/sources/sitepoint"},"commentCount":0,"discussionUrl":"https://daily.dev/posts/build-a-vite-plugin-to-catch-broken-svg-assets-before-production-ps89inq3t","interactionStatistic":[{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":0},{"@type":"InteractionCounter","interactionType":{"@type":"CommentAction"},"userInteractionCount":0}],"keywords":"javascript,devtools,svg,vite","timeRequired":"PT17M"}
{"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https://daily.dev"},{"@type":"ListItem","position":2,"name":"SitePoint","item":"https://daily.dev/sources/sitepoint"},{"@type":"ListItem","position":3,"name":"Build a Vite Plugin to Catch Broken SVG Assets Before Production"}]}
{"@context":"https://schema.org","@type":"FAQPage","@id":"https://daily.dev/posts/build-a-vite-plugin-to-catch-broken-svg-assets-before-production-ps89inq3t#faq","mainEntity":[{"@type":"Question","name":"How do I write a Vite plugin that fails the build when an SVG file is missing a viewBox attribute?","acceptedAnswer":{"@type":"Answer","text":"Use the buildStart hook to read and scan SVG files with a regex check for viewBox, collect any missing-attribute errors into a list, then call this.error(message) inside buildStart to abort the build with a formatted error listing the offending file and reason. This turns a documentation convention into an enforced build contract rather than a manual review step. Developers wiring asset checks into their build pipeline can find similar Vite plugin patterns on daily.dev."}},{"@type":"Question","name":"How can I get instant feedback in the Vite dev server when a watched asset file changes and fails validation?","acceptedAnswer":{"@type":"Answer","text":"Use the configureServer plugin hook to access server.watcher, add the target directory to it, and listen for the change event; when an SVG file changes, run the validation function and log formatted errors to the terminal without needing a rebuild. This gives feedback the moment a file is saved instead of waiting for a production build to fail. daily.dev surfaces build-tooling techniques like this for developers refining their dev feedback loops."}},{"@type":"Question","name":"What is a reasonable file-size budget to enforce for brand or icon SVG assets in a frontend project?","acceptedAnswer":{"@type":"Answer","text":"A commonly cited starting point is 100KB for brand logos and around 30-40KB for smaller icon sets, checked via Node's fs.stat and compared against a configurable maxSizeKB option. Exceeding the budget triggers a build error stating the actual size versus the allowed maximum, catching accidental bloat from unoptimized design-tool exports. Teams setting asset budgets can track build-tooling approaches like this on daily.dev when choosing enforcement strategies."}}]}
```

