Staticview Vite Plugin
@staticview/vite-plugin builds the staticview web components of a Vite project from their .ts source, with the HTML and CSS that each one inlines.
Installation
npm install @staticview/vite-pluginUsage
import staticviewVitePlugin from "@staticview/vite-plugin";import { defineConfig } from "vite";
export default defineConfig({ plugins: [staticviewVitePlugin({ include: ["src/components/**/*.ts"] })],});Editing a component file or a file that it inlines with $inline reloads the whole page.
Production mode
The plugin builds in production mode when Vite does, as in vite build, and forceProduction overrides that. Production mode makes minify default to true, and a PostCSS config gets "production" as its env.
PostCSS
postcssPlugins run on the CSS that the components inline. With loadPostcssConfig, the plugins of the project's PostCSS config run before them, and a config plugin that postcssPlugins already holds is skipped with a warning.
A config that exports a function gets env and minify in its context, where minify is the value of minifyCSS. Restart the dev server after you edit the config, since the plugin loads it once.
Server-side rendering
Without iife, a component throws when it is imported during server-side rendering, since there is no HTMLElement on the server. Set iife to make the import do nothing there, or import the component only on the client.
Component files with imports or exports
For a component file that imports or exports a value, or reads import.meta, the build turns off iife, minifyCssClasses, and minifyCssVariables and logs a warning. Type-only imports and exports do not count.
Renamed custom properties
minifyCssVariables renames the custom properties only in the component CSS and JS. Give a custom property an ignored prefix, like --sv-, when the page or a style attribute in the component HTML sets it.
Options
| Option | Default | Description |
|---|---|---|
include | required | Globs of the component .ts files to build. A matched file without a component fails the build. |
exclude | [] | Globs of the files to leave out. |
forceProduction | Vite's mode | Overrides production mode. |
sourceMap | false | Maps the output to the .ts source in dev. |
loadPostcssConfig | false | Runs the project's PostCSS config. |
postcssPlugins | [] | PostCSS plugins for the component CSS. |
iife | false | Makes a server import do nothing. |
minify | true in production | Default of the other minify options. |
minifyCSS | minify | Minifies the CSS. |
minifyHTML | minify | Minifies the HTML and its SVG. |
minifyJS | minify | Minifies the JavaScript. |
minifyCssClasses | minify | Gives CSS classes short names. |
minifyCssClassesIgnorePrefix | ["_"] | Prefixes of classes that keep their names. |
minifyCssVariables | minify | Gives custom properties short names. |
minifyCssVariablesIgnorePrefix | ["sv-", "_"] | Prefixes of properties that keep their names. |
disabledTrims | [] | Trims to skip. |
Trims
A trim is a small output saving that the minifiers do not make, and it runs when its minifier runs. Name a trim in disabledTrims to turn it off, like when it breaks your code.
These trims run with minifyCSS:
minifyCustomPropertyValuesdrops the spaces in custom property values and shortens their numbers.removeMathOperatorSpacesdrops the spaces around*and/.useBooleanMediaFeatureswrites media features like(forced-colors: active)as(forced-colors).useShortTransparentColorwritestransparentas#0000in color properties.
These trims run with minifyJS:
useGlobalParseFunctionsreplacesNumber.parseFloatandNumber.parseIntwith the global functions.useNumbersForDomBooleanswrites1and0fortrueandfalsewhere the DOM reads a boolean.removeUnreachableSwitchDefaultremoves adefault:that only throws, in theswitchofattributeChangedCallbackor withsatisfies never.replaceStringSwitchWithIfChainreplaces aswitchon a variable whose cases are all strings with anifchain.inlineEventFactoriesmoves a static_eventsfactory that has one caller into that call.privatizeUnderscoreMembersturns class members named with one leading underscore into private members.inlinePrivateFieldAliasesreplaces a local that holds a private field with the field.inlineSingleUseArrowFieldsmoves an arrow in a private field into the one call that passes it.inlineSingleUseLocalsmoves the value of a local that code reads once into that read.moveConstructorIntoFieldmoves the constructor body of a component into a last private field.removeFalsyFieldInitializersremoves thefalseornullvalue of a private field when no code can tell it fromundefined.hoistStaticPrivateFieldsmoves the static private and underscore fields of a class into variables above it, wheniifeis on or the file imports or exports a value.hoistStaticPrivateMethodsdoes the same for static methods, which turn into arrow functions.defineReflectedPropertiesInLoopreplaces the getter and setter pairs that only reflect an attribute with one loop that defines them.declareConstantsWithLetdeclares eachconstthat no code writes to aslet.
These trims run with minifyHTML:
useCommasInSvgNumberswrites the numbers of SVGviewBoxanddwith commas.omitImpliedEndTagsremoves the end tags and self-closing slashes that the HTML parser implies.