histoire
Version:
A fast story book powered by Vite
149 lines (148 loc) • 5.59 kB
JavaScript
import { lookup as lookupMime } from 'mrmime';
import { APP_PATH, getHistoireStyleFiles } from '../alias.js';
import { getDevClientPreloads } from '../server/client-ready.js';
import { RESOLVED_PREVIEW_RUNTIME_ID } from '../virtual/index.js';
import { renderVitestRunnerBootstrap } from '../virtual/vitest-runner-bootstrap.js';
import { renderAppShellAppearance } from './app-shell-appearance.js';
import { ENTRY_RELOAD_SCRIPT, renderModuleEntry, UNHANDLED_MODULE_FAILURE_SCRIPT } from './entry-reload.js';
import { versionModuleFile } from './module-version.js';
/** Early Histoire routes inherit Vite headers without replacing caller-owned response policy. */
export function applyDevResponseHeaders(server, res) {
for (const [name, value] of Object.entries(server.config.server?.headers ?? {})) {
if (value !== undefined && res.getHeader?.(name) === undefined)
res.setHeader(name, value);
}
}
/**
* Suffix of the app bundles served in local Histoire development.
*/
function getBundleSuffix() {
return process.env.HISTOIRE_DEV ? '-dev' : '';
}
/**
* Head and entry script of the sandbox document.
*
* Built Histoire styles load as `<link>` stylesheets: with versioned URLs the
* browser caches and reuses them across sandbox documents, instead of every
* iframe evaluating ~270KB of CSS wrapped in JS modules and injecting it
* through the HMR client. The entry script is then the preview runtime itself.
* Local Histoire development keeps the CSS imports of its `-dev` bundle, whose
* source styles hot-update.
* @param version Maps an absolute file path to its versioned module path.
*/
export function renderSandboxEntry(version) {
if (process.env.HISTOIRE_DEV) {
return {
head: '',
script: renderModuleEntry(`/@fs/${APP_PATH}/bundle-sandbox${getBundleSuffix()}.js`),
};
}
const { style, bundledStyle } = getHistoireStyleFiles();
return {
head: [style, bundledStyle].map(file => `<link rel="stylesheet" href="/@fs/${version(file)}">`).join('\n '),
script: renderModuleEntry(RESOLVED_PREVIEW_RUNTIME_ID),
};
}
/**
* Renders the HTML document of the story sandbox iframe.
* @param server Dev server versioning the document's stylesheet URLs.
*/
function renderSandboxHtml(server) {
const entry = renderSandboxEntry(file => versionModuleFile(server, file));
return `
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<meta name="description" content="">
${entry.head}
</head>
<body>
<div id="app"></div>
${renderVitestRunnerBootstrap(' ')}
<script>
// Hide spammy vite messages
const origConsoleLog = console.log
console.log = (...args) => {
if (typeof args[0] !== 'string' || !args[0].startsWith('[vite] connect')) {
origConsoleLog(...args)
}
}
</script>
<script>${ENTRY_RELOAD_SCRIPT}</script>
${entry.script}
</body>
</html>`;
}
/**
* Renders the HTML document of the Histoire app shell.
* @param ctx The histoire context, used for the configured favicon.
* @param server Exact dev server owning base path and ready module preload hints.
*/
function renderAppHtml(ctx, server) {
const base = server.config.base;
const preloads = getDevClientPreloads(server).map(url => `<link rel="modulepreload" crossorigin href="${url.replaceAll('&', '&').replaceAll('"', '"').replaceAll('<', '<')}">`).join('\n');
return `
<!DOCTYPE html>
<html>
<head>
<title></title>
<link rel="icon" href=""/>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<meta name="description" content="">
${renderAppShellAppearance(ctx.config.theme)}
${preloads}
${ctx.config.theme?.favicon ? `<link rel="icon" type="${lookupMime(ctx.config.theme.favicon)}" href="${base}${ctx.config.theme.favicon}"/>` : ''}
</head>
<body>
<div id="app"></div>
${renderVitestRunnerBootstrap(' ')}
<script>${ENTRY_RELOAD_SCRIPT}
${UNHANDLED_MODULE_FAILURE_SCRIPT}</script>
${renderModuleEntry(`/@fs/${APP_PATH}/bundle-main${getBundleSuffix()}.js`)}
</body>
</html>`;
}
/**
* Serves an HTML document through Vite's index transforms, which inject the HMR
* client and let other plugins post-process the markup.
*/
async function sendHtml(server, req, res, html) {
applyDevResponseHeaders(server, res);
res.statusCode = 200;
const transformed = await server.transformIndexHtml(req.url, html);
res.setHeader('content-type', 'text/html; charset=UTF-8');
res.end(transformed);
}
/**
* Middleware serving the story sandbox document on `<base>__sandbox`.
* @param server The Vite dev server the middleware is registered on.
*/
export function createSandboxHtmlMiddleware(server) {
return async (req, res, next) => {
if (req.url.startsWith(`${server.config.base}__sandbox`)) {
await sendHtml(server, req, res, renderSandboxHtml(server));
return;
}
next();
};
}
/**
* Middleware serving the Histoire app shell for every `.html` request.
*
* Registered after Vite's history fallback so real files still win.
* @param server The Vite dev server the middleware is registered on.
* @param ctx The histoire context.
*/
export function createAppHtmlMiddleware(server, ctx) {
return async (req, res, next) => {
if (req.url.endsWith('.html')) {
await sendHtml(server, req, res, renderAppHtml(ctx, server));
return;
}
next();
};
}