Files
windmill/frontend/vite.config.js
T
AlexRV12 f7d1cf2df1 keep zod's english messages in production builds (#12273)
* fix: keep zod's english messages in production builds

* refactor: keep zod's own locale setup in the bundle
2026-11-06 16:47:59 +01:00

585 lines
20 KiB
JavaScript

import { sveltekit } from '@sveltejs/kit/vite'
import { existsSync, readFileSync } from 'fs'
import { fileURLToPath } from 'url'
import { searchForWorkspaceRoot } from 'vite'
import mkcert from 'vite-plugin-mkcert'
const file = fileURLToPath(new URL('package.json', import.meta.url))
const json = readFileSync(file, 'utf8')
// Only the version is exposed to the client (see `define` below). Defining the
// whole parsed package.json would inline it — scripts, dependency lists, ... —
// into every chunk that reads `__pkg__.version`.
const { version } = JSON.parse(json)
// The postinstall downloads the pinned UI Builder artifact into static/ui_builder,
// which SvelteKit serves at /ui_builder. Serve that directly; only proxy to a
// live UI Builder dev server on :4000 when the bundle is absent (mirrors the
// backend's static-vs-:4000 fallback). Delete static/ui_builder to develop the
// builder against :4000.
const uiBuilderStaticPresent = existsSync(
fileURLToPath(new URL('static/ui_builder/app-preview.html', import.meta.url))
)
/**
* Vite's preload helper (`__vitePreload`) checks every dependency of a dynamic import against
* every `<link>` in the document before adding its own: with relative paths it walks
* `document.getElementsByTagName('link')` reading each `href`. A route node here preloads
* hundreds of chunks, so loading Home made that walk quadratic, ~40ms of a warm reload.
*
* The walk only has to catch links the helper did not add itself (its own are deduped by its
* `seen` map), which are the ones already in the document when it first runs: those are
* snapshotted once into a set. Patched in `renderChunk`, before the chunk's hash is taken, so
* a cached copy of the unpatched helper is never served under the same name. Fails the build
* when the helper no longer has the expected shape, so a Vite upgrade cannot silently bring
* the walk back.
*/
function preloadWithoutLinkScan() {
// Matches Vite's source and its minified form alike.
const scan =
/for\s*\(\s*let\s+(\w+)\s*=\s*(\w+)\.length\s*-\s*1\s*;\s*\1\s*>=\s*0\s*;\s*\1--\s*\)\s*\{\s*(?:const|let)\s+(\w+)\s*=\s*\2\[\1\]\s*;?\s*if\s*\(\s*\3\.href\s*===\s*(\w+)\s*&&\s*\(\s*!(\w+)\s*\|\|\s*\3\.rel\s*===\s*(["'`])stylesheet\6\s*\)\s*\)\s*return\s*;?\s*\}/
const present = `function __wmLinkPresent(links, href, css) {
const s = (__wmLinkPresent.s ??= (() => {
const all = new Set(), sheets = new Set()
for (const l of links) {
all.add(l.href)
if (l.rel === 'stylesheet') sheets.add(l.href)
}
return { all, sheets }
})())
return (css ? s.sheets : s.all).has(href)
}
`
// The server pass has no preload helper to patch.
let ssr = false
let patched = 0
return {
name: 'wm-preload-without-link-scan',
apply: 'build',
configResolved(config) {
ssr = !!config.build.ssr
},
renderChunk(code, chunk) {
if (ssr || !code.includes('vite:preloadError')) return null
if (!scan.test(code)) {
this.error(
`wm-preload-without-link-scan: ${chunk.fileName} holds Vite's preload helper, but not the link scan this plugin replaces. Check what the new helper does before dropping the plugin.`
)
}
patched++
return {
code: present + code.replace(scan, 'if (__wmLinkPresent($2, $4, $5)) return;'),
map: null
}
},
generateBundle() {
if (!ssr && patched === 0) {
this.error("wm-preload-without-link-scan: no chunk holds Vite's preload helper")
}
}
}
}
/**
* Fail the build if the emitted chunks import each other in a cycle.
*
* A chunk's imports all evaluate before its own body, so in a cycle one member runs
* while another has initialized nothing: its `const`/`class` exports read as
* `undefined` and the app dies on load with "X is not a constructor" behind
* SvelteKit's default 500 page. The module graph being acyclic is not enough —
* chunk *grouping* alone can create one. See docs/frontend-import-cycles.md.
*/
function assertAcyclicChunks() {
return {
name: 'wm-assert-acyclic-chunks',
generateBundle(_options, bundle) {
const imports = new Map()
for (const [file, chunk] of Object.entries(bundle)) {
if (chunk.type === 'chunk') imports.set(file, chunk.imports ?? [])
}
const state = new Map() // file -> 'visiting' | 'done'
const describe = (file) => {
const chunk = bundle[file]
const ids = chunk.moduleIds ?? Object.keys(chunk.modules ?? {})
const own = ids.filter((id) => id.includes('/src/')).map((id) => id.split('/src/')[1])
const shown = own.slice(0, 6).join(', ')
return ` ${file}\n ${shown}${own.length > 6 ? `, +${own.length - 6} more` : ''}`
}
for (const root of imports.keys()) {
if (state.get(root)) continue
const stack = [[root, 0]]
const path = [root]
state.set(root, 'visiting')
while (stack.length) {
const frame = stack[stack.length - 1]
const deps = imports.get(frame[0]) ?? []
if (frame[1] >= deps.length) {
state.set(path.pop(), 'done')
stack.pop()
continue
}
const dep = deps[frame[1]++]
if (state.get(dep) === 'visiting') {
const cycle = path.slice(path.indexOf(dep)).concat(dep)
this.error(
`Cyclic chunk imports — this ships a runtime crash, see docs/frontend-import-cycles.md:\n` +
cycle.map(describe).join('\n ->\n')
)
}
if (!state.has(dep) && imports.has(dep)) {
state.set(dep, 'visiting')
path.push(dep)
stack.push([dep, 0])
}
}
}
}
}
}
/**
* Fail the build if a chunk statically reachable from one containing `entry` contains a module
* whose id includes one of `forbidden`. Only static imports count: a dynamic import is the
* fix, not the problem.
*/
function assertStaticClosureExcludes(ctx, bundle, entry, forbidden, hint) {
const chunks = Object.entries(bundle).filter(([, c]) => c.type === 'chunk')
const idsOf = (c) => c.moduleIds ?? Object.keys(c.modules ?? {})
const starts = chunks
.filter(([, c]) => idsOf(c).some((id) => id.includes(entry)))
.map(([file]) => file)
if (!starts.length) ctx.error(`No chunk contains ${entry}; update ${hint}`)
const seen = new Set(starts)
const queue = [...starts]
while (queue.length) {
const chunk = bundle[queue.shift()]
if (!chunk) continue
const hit = idsOf(chunk).find((id) => forbidden.some((f) => id.includes(f)))
if (hit) ctx.error(`${entry} statically loads ${hit}; import it lazily (see ${hint})`)
for (const dep of chunk.imports ?? []) {
if (seen.has(dep)) continue
seen.add(dep)
queue.push(dep)
}
}
}
const isClientBuild = (bundle) =>
Object.keys(bundle).some((file) => file.startsWith('_app/immutable/'))
/**
* Fail the build if a public app URL statically loads more than a raw app needs.
*
* These pages also serve raw apps, which only need a small shell around their bundle's
* iframe, and the page shows nothing until its static imports have loaded. One static
* import of AppPreview (or of anything reaching monaco) makes every public app page
* preload ~650 chunks; one of the generated client, `$lib/stores` or `$lib/utils` drags
* in most of the app shell. The root layout wraps these pages, so it must not import
* app.css either. See loadAppPreview.ts and publicAppApi.ts.
*/
function assertLeanPublicAppRoutes() {
// Route directories, so +page.js counts as well as +page.svelte.
const routes = ['/src/routes/public/[workspace]/[...secret]/', '/src/routes/a/[...path]/']
const forbidden = [
'/src/lib/components/apps/editor/AppPreview.svelte',
'/node_modules/monaco-editor/',
'/src/lib/assets/app.css',
'/src/lib/gen/services.gen.ts',
'/src/lib/stores.ts',
'/src/lib/utils.ts',
'/src/lib/components/Login.svelte',
'/node_modules/@melt-ui/'
]
return {
name: 'wm-assert-lean-public-app-routes',
generateBundle(_options, bundle) {
if (!isClientBuild(bundle)) return
for (const route of [...routes, '/src/routes/+layout.svelte']) {
assertStaticClosureExcludes(this, bundle, route, forbidden, 'publicAppApi.ts')
}
}
}
}
/**
* Fail the build if the app shell or the home page statically loads monaco.
*
* The layouts below wrap every page, so what they import statically is downloaded before
* any page renders, and the home page is where most sessions land; monaco is several MB.
* Editors, drawers and modals that reach it are dynamic imports mounted on first use (see
* the `(logged)` layout).
*/
function assertMonacoFreeAppShell() {
const entries = [
'/src/routes/+layout.svelte',
'/src/routes/(root)/+layout.svelte',
'/src/routes/(root)/(logged)/+layout.svelte',
'/src/routes/(root)/(logged)/+page.svelte'
]
const forbidden = ['/node_modules/monaco-editor/', '/node_modules/@codingame/monaco-vscode-']
return {
name: 'wm-assert-monaco-free-app-shell',
generateBundle(_options, bundle) {
if (!isClientBuild(bundle)) return
for (const entry of entries) {
assertStaticClosureExcludes(this, bundle, entry, forbidden, 'the (logged) layout')
}
}
}
}
/**
* Fail the build if the app shell or the home page statically loads the AI chat engine or the
* provider SDKs.
*
* Everything those entries import statically loads and evaluates before the first page renders,
* and the engine alone is over a megabyte. The shell reaches it only from a click: through
* `sessionRuntimeRegistry` and `aiClientWorkspace`, which stand in for the modules that would pull
* it in.
*/
function assertAiEngineFreeAppShell() {
const entries = [
'/src/routes/+layout.svelte',
'/src/routes/(root)/+layout.svelte',
'/src/routes/(root)/(logged)/+layout.svelte',
'/src/routes/(root)/(logged)/+page.svelte'
]
const forbidden = [
'/src/lib/components/copilot/chat/AIChatManager.svelte.ts',
'/node_modules/openai/',
'/node_modules/@anthropic-ai/sdk/'
]
return {
name: 'wm-assert-ai-engine-free-app-shell',
generateBundle(_options, bundle) {
if (!isClientBuild(bundle)) return
for (const entry of entries) {
assertStaticClosureExcludes(this, bundle, entry, forbidden, 'sessionRuntimeRegistry.svelte.ts')
}
}
}
}
/**
* zod's `sideEffects: false` lets the bundler drop the module whose top-level config(en()) installs
* its English messages, which turns every default message into "Invalid input" in production builds
* only. The entry module needs the flag too, or the bundler never walks from it into that module.
* zod 4.5 registers the locale itself, so this can go on that upgrade.
*/
function keepZodLocale() {
return {
name: 'wm-keep-zod-locale',
transform(code, id) {
if (/\/zod\/(index|v4\/classic\/external)\.js$/.test(id)) {
return { code, moduleSideEffects: true }
}
}
}
}
/**
* Fail the build if zod's English messages were tree-shaken out of the client bundle (see
* keepZodLocale). Only a production bundle can lose them, so a dev server or a unit test passes
* either way.
*/
function assertZodMessagesKept() {
// The server pass leaves zod external, so none of its chunks hold the messages.
// isClientBuild can't skip that pass: it also writes under _app/immutable/, so every build would fail.
let ssr = false
return {
name: 'wm-assert-zod-messages-kept',
configResolved(config) {
ssr = !!config.build.ssr
},
generateBundle(_options, bundle) {
if (ssr) return
const kept = Object.values(bundle).some(
(c) => c.type === 'chunk' && c.code.includes('Too big: expected')
)
if (!kept) {
this.error(
'No chunk holds zod\'s English messages, so every zod error would read "Invalid input". See keepZodLocale.'
)
}
}
}
}
const remoteUrl =
process.env.REMOTE ??
(process.env.BACKEND_PORT
? `http://localhost:${process.env.BACKEND_PORT}`
: 'https://app.windmill.dev/')
// Browsers scope cookies by host, not port, so dev servers sharing a host (one per
// worktree) would share one `token` and each login would sign the others out. The session
// is stored under a name tied to its backend and forwarded as `token`, the name the backend
// reads. ISOLATE_DEV_AUTH=0 keeps the shared `token` for tools reading it across instances.
const isolateDevAuth = process.env.ISOLATE_DEV_AUTH !== '0'
const authCookie = `token_${new URL(remoteUrl).host.replace(/\W/g, '_')}`
// Isolated cookies are host-only: `Domain=localhost` is rejected when the dev server is
// reached as 127.0.0.1 or over the network.
const cookieDomain = isolateDevAuth ? '' : 'localhost'
// The readable hint the backend mirrors from the session cookie (session_hint.rs), renamed
// with it so that one worktree's login does not stand for another's session.
const sessionHintCookie = isolateDevAuth ? `${authCookie}_hint` : 'wm_session'
function isolateAuthCookie(proxy) {
if (!isolateDevAuth) return
proxy.on('proxyReq', (proxyReq, req) => {
const kept = []
let session
for (const cookie of (req.headers.cookie ?? '').split(/;\s*/)) {
if (cookie.startsWith(`${authCookie}=`)) session = cookie.slice(authCookie.length + 1)
else if (cookie && !cookie.startsWith('token=')) kept.push(cookie)
}
if (session !== undefined) kept.push(`token=${session}`)
if (kept.length) proxyReq.setHeader('cookie', kept.join('; '))
else proxyReq.removeHeader('cookie')
})
proxy.on('proxyRes', (proxyRes) => {
const setCookie = proxyRes.headers['set-cookie']
if (setCookie) {
proxyRes.headers['set-cookie'] = setCookie.map((c) =>
c.replace(/^token=/, `${authCookie}=`).replace(/^wm_session=/, `${sessionHintCookie}=`)
)
}
})
}
// Cross-origin isolation headers, scoped to mirror the production predicate —
// see `needs_cross_origin_isolation` in backend/windmill-api/src/static_assets.rs
// for why only opted-in public apps get them.
// `enforce: 'pre'` so these headers are set before SvelteKit's sirv static
// handler serves `static/` files and ends the response without calling next().
function needsCrossOriginIsolation(url) {
const [path, query = ''] = url.split('?')
return (
(path.startsWith('/public/') || path.startsWith('/a/')) &&
new URLSearchParams(query).has('wm_coep')
)
}
// Normalized like the static server resolves it, so `//ui_builder/...` or an
// encoded name can't reach the file without the header.
function isPreviewShell(url) {
let path = url.split('?')[0]
try {
path = decodeURIComponent(path)
} catch {}
path = path.replace(/\/+/g, '/')
return path === '/ui_builder/app-preview.html'
}
let plugin = {
name: 'configure-response-headers',
enforce: 'pre',
configureServer: (server) => {
server.middlewares.use((req, res, next) => {
if (needsCrossOriginIsolation(req.url ?? '')) {
res.setHeader('Cross-Origin-Opener-Policy', 'same-origin')
res.setHeader('Cross-Origin-Embedder-Policy', 'require-corp')
}
// Mirrors `static_assets.rs`. Set before the static handler and the :4000
// proxy fallback alike, so both paths serve the shell sandboxed.
if (isPreviewShell(req.url ?? '')) {
res.setHeader(
'Content-Security-Policy',
'sandbox allow-scripts allow-forms allow-popups allow-popups-to-escape-sandbox allow-downloads allow-modals allow-top-navigation'
)
}
// CORP on everything so dev assets stay loadable as subresources of
// isolated documents on other dev origins (e.g. 127.0.0.1 vs localhost).
res.setHeader('Cross-Origin-Resource-Policy', 'cross-origin')
next()
})
}
}
/** @type {import('vite').UserConfig} */
const config = {
server: {
https: process.env.HTTPS === 'true',
allowedHosts: [
'localhost',
'127.0.0.1',
'0.0.0.0',
'rubendev.wimill.xyz',
'windmill.xyz',
'app.windmill.xyz',
'public.windmill.xyz'
],
port: parseInt(process.env.FRONTEND_PORT) || 3000,
// Transform the app shell's module graph at startup rather than on the first page
// load, which otherwise sits on the loading screen for ~15s (Tailwind's full content
// scan for app.css alone takes several seconds).
warmup: {
clientFiles: [
'./src/lib/assets/app.css',
'./src/routes/+layout.svelte',
'./src/routes/(root)/+layout.svelte',
'./src/routes/(root)/(logged)/+layout.svelte',
'./src/routes/(root)/(logged)/+page.svelte'
]
},
cors: { origin: '*' },
// `windmill-chat` (svelte.config.js alias) lives outside the frontend root.
fs: {
allow: [
searchForWorkspaceRoot(process.cwd()),
fileURLToPath(new URL('../chat-sdk', import.meta.url))
]
},
proxy: {
'^/\\.well-known/.*': {
target: remoteUrl,
changeOrigin: true,
cookieDomainRewrite: cookieDomain,
configure: isolateAuthCookie
},
'^/api/w/[^/]+/s3_proxy/.*': {
target: remoteUrl,
changeOrigin: false, // Important for signature to be correct
cookieDomainRewrite: cookieDomain,
configure: (proxy, options) => {
isolateAuthCookie(proxy)
proxy.on('proxyReq', (proxyReq, req, res) => {
// Prevent collapsing slashes during URL normalization
const originalPath = req.url
proxyReq.path = originalPath
})
}
},
'^/api/.*': {
target: remoteUrl,
changeOrigin: true,
cookieDomainRewrite: cookieDomain,
configure: isolateAuthCookie
},
'^/ws/.*': {
target: process.env.REMOTE_LSP ?? process.env.REMOTE_EXTRA ?? 'https://app.windmill.dev',
changeOrigin: true,
ws: true
},
'^/ws_mp/.*': {
target: process.env.REMOTE_MP ?? process.env.REMOTE_EXTRA ?? 'https://app.windmill.dev',
changeOrigin: true,
ws: true
},
'^/ws_debug/.*': {
target: process.env.REMOTE_DEBUG ?? process.env.REMOTE_EXTRA ?? 'https://app.windmill.dev',
changeOrigin: true,
ws: true
},
...(uiBuilderStaticPresent
? {}
: {
'^/ui_builder/.*': {
target: 'http://localhost:4000',
changeOrigin: true,
headers: {
'Cross-Origin-Resource-Policy': 'cross-origin'
}
}
})
}
},
preview: { port: 3001 },
plugins: [
// Only the dev proxy renames the hint: a build reads the name the backend sets.
{
name: 'wm-session-hint-cookie',
config: (_, { command }) => ({
define: {
__WM_SESSION_HINT__: JSON.stringify(
command === 'serve' ? sessionHintCookie : 'wm_session'
)
}
})
},
sveltekit(),
...(process.env.HTTPS === 'true' ? [mkcert()] : []),
plugin,
assertAcyclicChunks(),
preloadWithoutLinkScan(),
assertLeanPublicAppRoutes(),
assertMonacoFreeAppShell(),
assertAiEngineFreeAppShell(),
keepZodLocale(),
assertZodMessagesKept()
],
define: { '__pkg__.version': JSON.stringify(version) },
optimizeDeps: {
include: ['highlight.js', 'highlight.js/lib/core', 'monaco-vim'],
exclude: [
'@codingame/monaco-vscode-standalone-typescript-language-features',
'@codingame/monaco-vscode-standalone-languages',
'windmill-client'
]
},
worker: { format: 'es' },
resolve: {
alias: {
path: 'path-browserify',
'monaco-editor/esm/vs/editor/contrib/hover/browser/hover':
'monaco-editor/esm/vs/editor/contrib/hover/browser/hoverContribution'
},
dedupe: ['vscode', 'monaco-editor']
},
assetsInclude: ['**/*.wasm'],
build: {
rollupOptions: {
output: {
// src/lib/gen is a self-contained generated client and a leaf of the module
// graph. Left alone the bundler splits it — index.ts in one chunk, the
// 12k-line schemas.gen.ts in another beside app code that imports back —
// which makes the *chunk* graph cyclic where the module graph is not, and
// modules then evaluate against uninitialized bindings. See
// docs/frontend-import-cycles.md.
// The client's config is split out for the public app routes, which set the
// credential without loading the client. It imports nothing at runtime, so
// its chunk cannot be part of a cycle.
advancedChunks: {
groups: [
{
name: 'gen-config',
test: /[\\/]src[\\/]lib[\\/]gen[\\/]core[\\/]OpenAPI\.ts$/,
priority: 1
},
{ name: 'gen', test: /[\\/]src[\\/]lib[\\/]gen[\\/]/ }
]
}
}
}
},
test: {
expect: { requireAssertions: true },
projects: [
{
extends: './vite.config.js',
test: {
name: 'server',
environment: 'node',
include: ['src/**/*.{test,spec}.{js,ts}'],
exclude: ['src/**/*.svelte.{test,spec}.{js,ts}', 'src/**/*.dom.{test,spec}.{js,ts}'],
setupFiles: ['src/lib/test-setup.ts']
}
},
{
// `*.dom.test.ts` — for the pure DOM utilities (snapshot serialization,
// replay sanitization) whose contracts can only be asserted against a
// real document.
extends: './vite.config.js',
test: {
name: 'dom',
environment: 'jsdom',
include: ['src/**/*.dom.{test,spec}.{js,ts}']
}
}
]
}
}
export default config