Internals
This document explains, at a high level, what the plugin does internally and why.
Overview
The default export is a Vite plugin that:
Enhances dev: proxies Appframe endpoints, logs in and refreshes the session, serves the real article HTML with your local entry injected, and caches i18n strings.
Enhances build: shapes Rollup output to Appframe’s expected file locations and names, builds a manifest and sourcemaps, and optionally treats React as external.
It also exports two helpers: addAppframeBuildConfig(config) and createDevMiddleware(server).
Dev server flow
Files: src/index.ts, src/devServer.ts, src/proxy.ts, src/localization.ts
Login and session refresh (
proxy.ts)On dev startup, the plugin reads
appframe.proxy.hostname(orappframe.deploy.hostname) frompackage.jsonusingimportJson.It logs in with
APPFRAME_LOGINandAPPFRAME_PWD(required). The HTTPS request capturesAppframeWebAuthandAppframeWebSessioncookies and caches them per-host.A timer renews the login every 5 minutes to minimize 403 errors due to expired sessions.
Proxying routes (
index.ts,devServer.ts)The plugin builds a regex list of routes to proxy to Appframe, e.g.
^/api/.*,^/file/.*,^/lib/.*, plus anyappframe.proxy.routes.Vite’s native proxy is configured so requests to these paths forward to
https://<hostname>.For each proxied request, the plugin attaches the cached auth cookies and sets
Originto the remote hostname (Appframe rejects local origins).
Serving article HTML (
devServer.ts)To keep dev close to prod, the plugin fetches the article HTML from the Appframe server and then transforms it:
Removes production JS/CSS (article assets) and adds your local module entry (
/src/index.tsx→/src/index.ts→/src/index.js).Removes Bugsnag and Web Vitals scripts, and skips service worker registration.
Injects a script that merges locally cached i18n strings into
af.article.i18n.Rewrites absolute links that don’t point to the article root to include the server hostname.
The raw HTML is cached at
node_modules/.appframe/index.htmlfor up to 3 hours (unless the request hasCache-Control: no-cache).The final HTML passes through
vite.transformIndexHtmlso HMR and module graph behave normally.
i18n localization cache (
localization.ts)Dev can trigger many small localization requests over HTTP/1.1, which slows startup. The plugin adds a middleware for
/api/user/localize/new/<article>that forwards the request with cookies and caches the response.The cache is stored at
node_modules/.appframe/localizeCache.json(writes are debounced) and injected into the page during article HTML transform to warm the client cache.
Build configuration
File: src/build.ts
The helper
addAppframeBuildConfigis applied automatically inbuildmode by the plugin, but is also exported for custom usage.Key behaviors:
Enables
build.manifestandbuild.sourcemapby default.Determines the app entry by checking
/src/index.tsx, then/src/index.ts, then/src/index.js.Writes output to Appframe file paths using the article ID:
JS:
file/article/script/<ARTICLE_ID>/main.[hash].min.js(and chunk variants)CSS:
file/article/style/<ARTICLE_ID>/[name].[hash].min.css
Adds Rollup visualizer to write
dist/stats.html.When
appframe.build.externals !== false, usesvite-plugin-externalsto treatreactandreact-domas externals mapped toReactandReactDOM(and sets Rollupoutput.globalsaccordingly).
Miscellaneous
Module resolution: Adds alias
{ find: "~/", replacement: "/src/" }so~/...maps to projectsrc.Logging: Login progress uses
chalkand interactive terminal updates when supported.JSON import helper:
importJsonreads JSON off disk because Node ESM JSON import is not used here.
Last updated
Was this helpful?