Skip to content

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

Terminal window
npm install @staticview/vite-plugin

Usage

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:

  • minifyCustomPropertyValues drops the spaces in custom property values and shortens their numbers.
  • removeMathOperatorSpaces drops the spaces around * and /.
  • useBooleanMediaFeatures writes media features like (forced-colors: active) as (forced-colors).
  • useShortTransparentColor writes transparent as #0000 in color properties.

These trims run with minifyJS:

  • useGlobalParseFunctions replaces Number.parseFloat and Number.parseInt with the global functions.
  • useNumbersForDomBooleans writes 1 and 0 for true and false where the DOM reads a boolean.
  • removeUnreachableSwitchDefault removes a default: that only throws, in the switch of attributeChangedCallback or with satisfies never.
  • replaceStringSwitchWithIfChain replaces a switch on a variable whose cases are all strings with an if chain.
  • inlineEventFactories moves a static _events factory that has one caller into that call.
  • privatizeUnderscoreMembers turns class members named with one leading underscore into private members.
  • inlinePrivateFieldAliases replaces a local that holds a private field with the field.
  • inlineSingleUseArrowFields moves an arrow in a private field into the one call that passes it.
  • inlineSingleUseLocals moves the value of a local that code reads once into that read.
  • moveConstructorIntoField moves the constructor body of a component into a last private field.
  • removeFalsyFieldInitializers removes the false or null value of a private field when no code can tell it from undefined.
  • hoistStaticPrivateFields moves the static private and underscore fields of a class into variables above it, when iife is on or the file imports or exports a value.
  • hoistStaticPrivateMethods does the same for static methods, which turn into arrow functions.
  • defineReflectedPropertiesInLoop replaces the getter and setter pairs that only reflect an attribute with one loop that defines them.
  • declareConstantsWithLet declares each const that no code writes to as let.

These trims run with minifyHTML:

  • useCommasInSvgNumbers writes the numbers of SVG viewBox and d with commas.
  • omitImpliedEndTags removes the end tags and self-closing slashes that the HTML parser implies.