UNPKG

histoire

Version:

A fast story book powered by Vite

103 lines (102 loc) • 2.77 kB
import fs from 'node:fs'; /** * Reads raw source for virtual or disk-backed story files. */ function getStorySource(file) { return file.moduleCode ?? (!file.virtual && fs.existsSync(file.path) ? fs.readFileSync(file.path, 'utf8') : ''); } /** * Detects whether a story file contains Vitest mock calls that require a full * iframe reload to recover cleanly after HMR. */ export function fileHasVitestMocks(file) { return /\bvi\.(?:mock|doMock|unmock|doUnmock)\s*\(/.test(stripNonCodeSegments(getStorySource(file))); } /** * Strips comments and quoted literals so source regex checks only inspect code. */ function stripNonCodeSegments(source) { let output = ''; let index = 0; while (index < source.length) { const char = source[index]; const next = source[index + 1]; if (char === '/' && next === '/') { index = consumeLineComment(source, index + 2); continue; } if (char === '/' && next === '*') { const consumed = consumeBlockComment(source, index + 2); output += consumed.blank; index = consumed.index; continue; } if (char === '\'' || char === '"' || char === '`') { const consumed = consumeQuotedLiteral(source, index + 1, char); output += consumed.blank; index = consumed.index; continue; } output += char; index++; } return output; } /** * Consumes `//` comment. Newline stays in outer loop. */ function consumeLineComment(source, start) { let index = start; while (index < source.length && source[index] !== '\n') { index++; } return index; } /** * Consumes block comment while preserving newline layout. */ function consumeBlockComment(source, start) { let blank = ' '; let index = start; while (index < source.length) { const char = source[index]; const next = source[index + 1]; if (char === '*' && next === '/') { blank += ' '; index += 2; break; } blank += char === '\n' ? '\n' : ' '; index++; } return { blank, index, }; } /** * Consumes quoted literal while preserving newlines. */ function consumeQuotedLiteral(source, start, quote) { let blank = ' '; let index = start; while (index < source.length) { const char = source[index]; if (char === '\\') { blank += ' '; index += 2; continue; } blank += char === '\n' ? '\n' : ' '; index++; if (char === quote) { break; } } return { blank, index, }; }