Development-only code
Development-only code lets a component tell its author about a mistake without shipping the message to production, like a button with no accessible name, a prop out of range or a slot left empty. In Unframework you write if (isDev) { … }, and each framework gets its own development check, which its production build folds to false and drops. Think of a design system that warns in every app that uses it while it's developed, and adds nothing to any of them once it's built.
This page walks you through writing it. We'll look at:
- A setup check: a warning from the props, where the setup runs.
- A client check: a warning from the rendered DOM, once mounted.
- What the compiler checks: where
isDevmay go. - What your app does: the one thing a production server sets.
A setup check
Import isDev from @uxfront/unframework, and write if (isDev) { … } at the top of the setup. The block runs once where the setup runs, and reads what the setup reads, like the props, plus console:
import { isDev } from "@uxfront/unframework";
// A button that shows only an icon, so its `label` is its accessible name. A development build
// warns its author when it has none, and a production build drops the check (ADR-0117).
export default function IconButton({ label }: { label?: string }) {
if (isDev) {
if (!label) console.warn("IconButton has no accessible name: pass it a `label`.");
}
return (
<button type="button" aria-label={label}>
<svg viewBox="0 0 16 16" width="16" height="16" aria-hidden="true">
<path d="M8 1l2 5h5l-4 3 2 6-5-4-5 4 2-6-4-3h5z" />
</svg>
</button>
);
}
React, Vue and Svelte test process.env.NODE_ENV, which the app's bundler replaces. Solid and Qwik import their own isDev, which their production build exports as false. Angular tests the global ngDevMode, and Astro tests import.meta.env.DEV, which Vite replaces. Each framework runs the block where it runs the setup: React in the body, Vue in <script setup>, Svelte in its script, Solid and Qwik in the body, Angular in ngOnInit and Astro in the frontmatter. Svelte, Solid and Qwik read the props there untracked, so the block subscribes to nothing:
import type { ComponentPropsWithRef } from "react";
export type IconButtonAttributes = Omit<ComponentPropsWithRef<"button">, "label" | "children">;
export default function IconButton({
label,
className,
style,
...attributes
}: { label?: string } & IconButtonAttributes) {
if (process.env.NODE_ENV !== "production") {
if (!label) console.warn("IconButton has no accessible name: pass it a `label`.");
}
return (
<button type="button" aria-label={label} className={className} style={style} {...attributes}>
<svg viewBox="0 0 16 16" width="16" height="16" aria-hidden="true">
<path d="M8 1l2 5h5l-4 3 2 6-5-4-5 4 2-6-4-3h5z" />
</svg>
</button>
);
}
React runs its body on every render, so its check runs on each one, and more than once under StrictMode, as React's own warnings do. The block writes nothing the component holds, so a development render and a production one show the same thing.
A client check
Some checks need the rendered DOM, like the text a parent filled in. Write if (isDev) in client code, a lifecycle hook, a handler or a watcher's callback, and it runs there:
import { isDev, onMounted, useTemplateRef } from "@uxfront/unframework";
// A link whose content its parent fills. Only the rendered DOM can say whether it holds a name,
// so a development build checks it once mounted, in client code (ADR-0117).
export default function IconLink({ href, label }: { href: string; label?: string }) {
const link = useTemplateRef<HTMLAnchorElement>();
onMounted(() => {
if (isDev) {
const name = link.value?.getAttribute("aria-label") ?? link.value?.textContent?.trim();
if (!name) console.warn("IconLink has no accessible name: give it a `label` or some text.");
}
});
return (
<a ref={link} href={href} aria-label={label}>
<svg viewBox="0 0 16 16" width="16" height="16" aria-hidden="true">
<path d="M2 8h10M8 4l4 4-4 4" />
</svg>
</a>
);
}
Astro runs no client code, so a hook's check never runs there, and its output leaves the hook out (UF4001).
What the compiler checks
isDev is read in one form: as the whole test of an if with no else. Anything else is UF2040, since a development build and a production one could then render differently. That covers its value, a negation, an else, a template, a getter, a composable and a directive's hook, and a template, a getter or an initial value that calls a function which tests it.
A component that reads isDev declares nothing named process or ngDevMode, a prop, a constant or a parameter: the check React, Vue, Svelte and Angular write reads that global, which the name would hide (UF2003).
Inside a setup check, the setup's own rules hold. It may declare its own const and let, loop and branch, and call console, but it writes no state, emits no event, calls no setup function, reads no browser global but console, never returns, and reads only what the setup declares before it (UF2023). It may throw, to fail loudly in development where a production build renders on.
What your app does
A production build needs nothing more: every framework's build replaces or resolves its check. A React, Vue or Svelte server reads process.env.NODE_ENV as it runs, as React and Vue themselves do, so start a production server with NODE_ENV=production, or its render warns too. A package built with unframework build --package keeps each check as written, so the app's build decides.
The decision behind this page is ADR-0117.
Async setup
An async component awaits its data at the top of its setup, and the server renders what it loaded on every framework, then hydrates it in place.
Targets
What each of the eight targets writes, the capability matrix of where frameworks differ, how each target takes events, and how Angular hosts a component.