Loomreference

Vite plugin

Everything `loomPreview()` contributes — options, plugins, aliases, merged config and the generated page.

import { loomPreview } from "@loom-dev/preview/vite";
loomPreview(options?: LoomPreviewOptions): Plugin[]

It returns an array of plugins. Put the array in plugins as one entry; Vite flattens nested arrays.

Options

interface LoomPreviewOptions {
entry?: string;
targets?: string | string[] | true;
title?: string;
html?: boolean;
shims?: Record<string, string>;
assets?: boolean;
}
OptionDefaultEffect
entryauto-detectedClient entry, root-relative (/src/boot.tsx) or relative to the project root. A leading ./ is stripped, and a missing leading slash is added. See entry detection.
targets—Gallery mode. Normalized like the CLI’s --targets: true is **/*.loom.tsx, a value containing * is used verbatim, anything else is a directory expanded to <value>/**/*.loom.tsx.
title"loom preview", or "loom gallery" with targets<title> of the generated page.
htmltruefalse removes the HTML plugin: no generated page, no entry detection, no Rollup input.
shims{}Redirects for roblox-ts packages loom can’t run: { "<specifier>": "<module>" }. Exact match only, and applied before every alias below, so a shim can override the built-ins. See Package compatibility.
assetstruefalse stops a build from downloading its rbxassetid:// images, both from the emitted output and from the prerender pass that mounts each target. Use it for a build that must stay off the network. The dev server is unaffected. See rbxassetid:// in a static build.

The plugins

In the order loomPreview() returns them:

NameenforceapplyJob
loom-preview:rbxts-syntaxprebothRewrites roblox-ts-only syntax in your source. See source transforms.
loom-preview:roblox-promiseprebothGives a source file that uses Promise, and doesn’t declare or import one, the roblox-ts Promise (Promise.new, andThen, Promise.delay, …) instead of the native one. async / await still work.
loom-previewprebothResolution and the config hook.
loom-preview:tsconfig-baseurl—bothResolves bare specifiers against your tsconfig baseUrl when normal resolution finds nothing.
loom-preview:serve-globalspreserveInjects the Roblox globals script into <head>.
loom:asset-proxy—serveAnswers rbxassetid:// lookups on the dev server.
loom:asset-bundle—buildUnless assets: false: downloads rbxassetid:// images into the output.
loom-preview:gallery-targets—serveOnly with targets: serves and watches the virtual:loom-targets import map.
loom-preview:index-htmlprebothUnless html: false: the generated page.

The resolver needs enforce: "pre". Vite’s own vite:resolve runs before normal plugins, so a normal-phase resolveId would never see bare specifiers whose main points at Luau.

Config contributions

The config hook returns this object. Vite deep-merges it with your config, and your values win.

{
esbuild: { jsx: "automatic", jsxImportSource: "@rbxts/react" },
optimizeDeps: {
include: [
"react", "react/jsx-runtime", "react/jsx-dev-runtime",
// …plus, when loom is installed under node_modules:
"@loom-dev/react", "react-reconciler", "react-reconciler/constants",
],
exclude: [
"@loom-dev/preview", "@loom-dev/vide", "@loom-dev/runtime",
"@loom-dev/renderer", "@loom-dev/scene", "@loom-dev/layout",
// …plus "@loom-dev/react" when loom resolves to a source checkout.
],
},
resolve: { alias: [ /* see below */ ] },
server: { fs: { allow: [<loom package roots>, searchForWorkspaceRoot(projectRoot)] } },
}
KeyWhy
esbuild.jsx: "automatic"roblox-ts source never imports React, so the classic transform would throw React is not defined.
optimizeDeps.excludeThe loom packages own the WASM engine. Pre-bundling would break their new URL(...wasm) references, and several hold module state that must exist once.
optimizeDeps.includeAn installed @loom-dev/react imports the CommonJS react-reconciler. Unbundled, it fails with does not provide an export named 'DefaultEventPriority'. Each included id is also aliased to an absolute path, because Vite resolves optimizeDeps from your project root.
server.fs.allowLets the dev server read loom’s own files and the WASM binary.

optimizeDeps and server.fs apply to the dev server only. The aliases and esbuild settings apply to both serve and build.

Alias table

Order matters. More specific patterns come first.

PatternResolves to
Every shims entryThe module you named. Exact match, ahead of everything below, so a shim can replace any built-in.
@rbxts/ui-labs, @rbxts/ripple, @rbxts/react-rippleLoom’s built-in adapters. Exact matches: @rbxts/ui-labs/controls and @rbxts/ui-labs-extra are not captured.
@rbxts/react-robloxThe preview client: createRoot, createPortal, setPreviewTheme. Exact, so an unadapted subpath reaches the entrypoint error instead of landing on createRoot.
@rbxts/servicesThe service singletons.
@rbxts/react/jsx-runtimeReact’s JSX runtime (absolute path).
@rbxts/react/jsx-dev-runtimeReact’s dev JSX runtime (absolute path).
@rbxts/reactThe compatibility facade: loom’s one React instance, plus the Roblox-only surface (ReactComponent, Event / Change / Tag, None) and bindings.
react/jsx-runtimeSame absolute path as above.
react/jsx-dev-runtimeSame absolute path as above.
reactOne absolute React path, resolved from the preview package.
react-reconciler, react-reconciler/constants, @loom-dev/react, and the loom packages it importsAbsolute paths. Added only for an installed loom, so the pre-bundled adapter can find them from node_modules/.vite/deps.
@rbxts/vide@loom-dev/vide.
Your tsconfig pathsThe paths you declared. See TypeScript setup.

Every built-in target is an absolute path inside the loom packages, so the aliases work however loom is installed. Loom uses absolute aliases rather than resolve.dedupe because dedupe would resolve React from your project root, which may hold a different major. The aliased @rbxts/react and the reconciler must share one React.

The generated page

Unless html: false, the plugin owns the entry document.

Serve. A middleware answers / and /index.html with the generated document, run through transformIndexHtml so Vite’s own injections still apply. It respects base, so a gallery mounted under /loom-preview/ answers there.

  • Without targets, it runs after Vite’s own middlewares, and only when the project root has no index.html.
  • With targets, it runs before them and always wins. Its module script loads the gallery shell through a /@fs/ URL, and the shell reads the target map from virtual:loom-targets.

Build. The same document becomes a virtual <root>/index.html and the Rollup input, so vite build works with no HTML file. An explicit build.rollupOptions.input is left alone. In gallery mode the page imports the globals, then a target map of relative import()s (one chunk per target), then the shell.

With no entry option, the first path that exists wins:

src/main.client.tsx src/main.client.ts
src/client/main.client.tsx src/client/main.client.ts
src/main.tsx src/main.ts src/index.tsx src/client.tsx

If none exists, and there is no index.html and no targets, you get an error naming the candidates when the page is first requested or the build starts.

Globals injection

Under serve, the plugin registers virtual:loom-globals and injects it into <head> as a script tag:

<script type="module" src="/@id/__x00__virtual:loom-globals"></script>

It works whether index.html is a real file or generated. The virtual module imports the globals installer by absolute path, so it resolves even from a project that can’t see @loom-dev/preview.

Under build, the HTML plugin prepends import ".../globals.ts" to each of the page’s entry modules instead. ESM evaluates imports in order, so the globals install before anything else runs.

Resolution

resolveId handles bare specifiers only. Anything starting with ., / or \0, or containing :, falls through untouched.

For a bare specifier from a real importer:

  1. Source-first redirect. Unless the specifier starts with @rbxts/, loom looks up node_modules/<pkg>/package.json from the importer. If its "main" ends in .lua or .luau, loom resolves src/index.ts(x) instead. This works even when out/ doesn’t exist. @rbxts/* goes through the alias table instead.
  2. Normal resolution. If it throws, for example because "main" points at a missing file, the specifier counts as unresolved.
  3. Post-resolution fallback. If the result is a .luau or .lua file, loom retries the nearest package’s src/index.ts(x).

A package that only ships Luau fails with an error naming it and showing the shims entry to add. Results are cached per specifier.

Source transforms

loom-preview:rbxts-syntax rewrites your .ts / .tsx files before esbuild sees them. Files under node_modules and loom’s own sources are skipped. It runs in both serve and build.

roblox-ts sourceBecomes
import X = require("m")import * as X from "m"
.size(), .isEmpty()Calls that work on arrays, strings, Map and Set alike. See Luau globals.
.get(), .set(), .has(), .delete()A receiver’s own method if it has one. Otherwise the object is read as a Luau table, so a plain object cast to Map works.
.match()Luau’s string.match on a string with a pattern. A RegExp argument, or a receiver with its own match, keeps the JS method.

The import rewrite only matches at the start of a line, so const x = require(...) and commented-out forms are left alone. Every rewrite is idempotent.

Other exports

SpecifierContents
@loom-dev/previewcreateRoot, LoomReactRoot, loomPreview, LoomPreviewOptions.
@loom-dev/preview/viteloomPreview, LoomPreviewOptions, findEntry / ENTRY_CANDIDATES (the entry lookup, for pre-flighting it yourself), and the helpers the plugin is built from: shimAliases, resolveShimTarget, tsconfigAliases, tsconfigBaseUrl, parseTsconfigJson, injectRobloxPromise.
@loom-dev/preview/clientcreateRoot, createPortal, setPreviewTheme, PreviewTheme, LoomReactRoot, RootOptions, act, createBlockingRoot, createLegacyRoot, version, and a default export.
@loom-dev/preview/globalsSide effect only: installs the globals and declares their ambient types.
@loom-dev/preview/servicesThe service singletons @rbxts/services is aliased to.
@loom-dev/preview/galleryNode-side gallery pieces: findLoomTargets, normalizeTargetsPatterns, globToRegExp, parseGalleryParams, parseBackgroundColor, toViteFsUrl, the codegen helpers, and the path constants. loom-dev builds on these.