histoire
Version:
A fast story book powered by Vite
85 lines (84 loc) • 4.41 kB
JavaScript
import fs from 'node:fs';
import path from 'pathe';
import pc from 'picocolors';
import { DEFAULT_RUN_STALL_TIMEOUT, getTestMaxWorkers } from '../util/test-timeouts.js';
import { resolveVisualConfig } from '../visual/config.js';
import { normalizeBrowserConfig } from './browser.js';
import { resolveEmbedConfig } from './embed.js';
import { resolveMatrixNumericValues } from './matrix.js';
/**
* Resolves the file paths held by the config against the project root and
* validates that they exist, in place on `ctx.config`.
*
* Runs once the Vite config is resolved: the favicon is checked against Vite's
* `public` directory, which is only known then.
*/
export async function processConfig(ctx) {
const { config, root } = ctx;
resolveVisualConfig(config.visual);
config.browser = normalizeBrowserConfig(config.browser, root);
config.embed = resolveEmbedConfig(config.embed);
config.ui = { ...config.ui, matrix: { ...config.ui?.matrix, numericValues: resolveMatrixNumericValues(config.ui?.matrix?.numericValues) } };
getTestMaxWorkers(ctx);
if (config.test?.runTimeout !== undefined) {
console.warn(pc.yellow('Histoire config: test.runTimeout is deprecated, use test.runStallTimeout (a run now only times out when it stops making progress)'));
}
config.test = { ...config.test, runStallTimeout: config.test?.runStallTimeout ?? config.test?.runTimeout ?? DEFAULT_RUN_STALL_TIMEOUT };
// Resolve files paths
const resolveFsPath = (file, force = false) => {
if (force || file.startsWith('./') || file.startsWith('../')) {
return path.resolve(root, file);
}
return file;
};
const fileCheck = (file, resolvedFile, configPathForError) => {
if (!file.startsWith('http') && !file.startsWith('@') && !fs.existsSync(resolvedFile)) {
console.warn(pc.yellow(`Histoire config: ${configPathForError} file ${file} does not exist (resolved to ${resolvedFile}), check for typos in the path`));
}
};
config.outDir = resolveFsPath(config.outDir, true);
// Theme
if (config.theme?.logo?.square) {
const file = config.theme.logo.square;
config.theme.logo.square = resolveFsPath(file);
fileCheck(file, config.theme.logo.square, 'theme.logo.square');
}
if (config.theme?.logo?.light) {
const file = config.theme.logo.light;
config.theme.logo.light = resolveFsPath(file);
fileCheck(file, config.theme.logo.light, 'theme.logo.light');
}
if (config.theme?.logo?.dark) {
const file = config.theme.logo.dark;
config.theme.logo.dark = resolveFsPath(file);
fileCheck(file, config.theme.logo.dark, 'theme.logo.dark');
}
if (config.theme?.favicon) {
let file = config.theme.favicon;
if (file.startsWith('/')) {
file = file.slice(1);
}
if (!file.startsWith('http')) {
const publicDir = path.resolve(ctx.resolvedViteConfig.root, ctx.resolvedViteConfig.publicDir);
// Resolve URL path
if (file.startsWith('./') || file.startsWith('../')) {
const resolvedFile = resolveFsPath(file, true);
const relativeFile = path.relative(publicDir, resolvedFile);
if (relativeFile.startsWith('..')) {
throw new Error(pc.red(`Histoire config: theme.favicon seems to target a file that is not in the vite 'public' directory: ${file} (resolved as ${resolvedFile})`));
}
if (!fs.existsSync(resolvedFile)) {
throw new Error(pc.red(`Histoire config: theme.favicon seems to target a file that does not exist: ${file} (resolved as ${resolvedFile})`));
}
config.theme.favicon = relativeFile;
}
else {
// Check if URL path is valid
const resolvedFile = path.resolve(publicDir, file);
if (!fs.existsSync(resolvedFile)) {
throw new Error(pc.red(`Histoire config: theme.favicon seems to target a file that does not exist: ${file} (resolved as ${resolvedFile}).\nThe favicon file should be placed in the vite 'public' directory.\nExample: if the file is in <project>/public/img/favicon.ico, you can put 'img/favicon.ico' or './public/img/favicon.ico'.`));
}
}
}
}
}