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.
The plugin rewrites react and turns on automatic JSX for every module in the config. Adding it to an existing React app’s config replaces that app’s React.
Options
interface LoomPreviewOptions { entry?: string; targets?: string | string[] | true; title?: string; html?: boolean; shims?: Record<string, string>; assets?: boolean;}| Option | Default | Effect |
|---|---|---|
entry | auto-detected | Client 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. |
html | true | false 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. |
assets | true | false 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:
| Name | enforce | apply | Job |
|---|---|---|---|
loom-preview:rbxts-syntax | pre | both | Rewrites roblox-ts-only syntax in your source. See source transforms. |
loom-preview:roblox-promise | pre | both | Gives 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-preview | pre | both | Resolution and the config hook. |
loom-preview:tsconfig-baseurl | — | both | Resolves bare specifiers against your tsconfig baseUrl when normal resolution finds nothing. |
loom-preview:serve-globals | pre | serve | Injects the Roblox globals script into <head>. |
loom:asset-proxy | — | serve | Answers rbxassetid:// lookups on the dev server. |
loom:asset-bundle | — | build | Unless assets: false: downloads rbxassetid:// images into the output. |
loom-preview:gallery-targets | — | serve | Only with targets: serves and watches the virtual:loom-targets import map. |
loom-preview:index-html | pre | both | Unless 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)] } },}| Key | Why |
|---|---|
esbuild.jsx: "automatic" | roblox-ts source never imports React, so the classic transform would throw React is not defined. |
optimizeDeps.exclude | The 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.include | An 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.allow | Lets 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.
| Pattern | Resolves to |
|---|---|
Every shims entry | The module you named. Exact match, ahead of everything below, so a shim can replace any built-in. |
@rbxts/ui-labs, @rbxts/ripple, @rbxts/react-ripple | Loom’s built-in adapters. Exact matches: @rbxts/ui-labs/controls and @rbxts/ui-labs-extra are not captured. |
@rbxts/react-roblox | The preview client: createRoot, createPortal, setPreviewTheme. Exact, so an unadapted subpath reaches the entrypoint error instead of landing on createRoot. |
@rbxts/services | The service singletons. |
@rbxts/react/jsx-runtime | React’s JSX runtime (absolute path). |
@rbxts/react/jsx-dev-runtime | React’s dev JSX runtime (absolute path). |
@rbxts/react | The compatibility facade: loom’s one React instance, plus the Roblox-only surface (ReactComponent, Event / Change / Tag, None) and bindings. |
react/jsx-runtime | Same absolute path as above. |
react/jsx-dev-runtime | Same absolute path as above. |
react | One absolute React path, resolved from the preview package. |
react-reconciler, react-reconciler/constants, @loom-dev/react, and the loom packages it imports | Absolute 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 paths | The 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 noindex.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 fromvirtual: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.tssrc/client/main.client.tsx src/client/main.client.tssrc/main.tsx src/main.ts src/index.tsx src/client.tsxIf 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:
- Source-first redirect. Unless the specifier starts with
@rbxts/, loom looks upnode_modules/<pkg>/package.jsonfrom the importer. If its"main"ends in.luaor.luau, loom resolvessrc/index.ts(x)instead. This works even whenout/doesn’t exist.@rbxts/*goes through the alias table instead. - Normal resolution. If it throws, for example because
"main"points at a missing file, the specifier counts as unresolved. - Post-resolution fallback. If the result is a
.luauor.luafile, loom retries the nearest package’ssrc/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 source | Becomes |
|---|---|
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
| Specifier | Contents |
|---|---|
@loom-dev/preview | createRoot, LoomReactRoot, loomPreview, LoomPreviewOptions. |
@loom-dev/preview/vite | loomPreview, 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/client | createRoot, createPortal, setPreviewTheme, PreviewTheme, LoomReactRoot, RootOptions, act, createBlockingRoot, createLegacyRoot, version, and a default export. |
@loom-dev/preview/globals | Side effect only: installs the globals and declares their ambient types. |
@loom-dev/preview/services | The service singletons @rbxts/services is aliased to. |
@loom-dev/preview/gallery | Node-side gallery pieces: findLoomTargets, normalizeTargetsPatterns, globToRegExp, parseGalleryParams, parseBackgroundColor, toViteFsUrl, the codegen helpers, and the path constants. loom-dev builds on these. |