histoire
Version:
A fast story book powered by Vite
101 lines (100 loc) • 4.11 kB
JavaScript
import fs from 'node:fs';
import { join, resolve } from 'pathe';
import { findProjectPackageDirectories, withPackageDirs } from '../util/resolve-package.js';
/** Browser providers and filesystem globbing are Node-only. */
const NODE_ONLY_DEPENDENCIES = new Set(['playwright', 'playwright-core', '@playwright/test', 'globby']);
/**
* Tells whether an installed package extends Vite (it depends or peer-depends
* on `vite`). Such plugins only run in Node: prebundling one for the page pulls
* Vite and its native watcher addon (`fsevents` on macOS) that esbuild cannot load.
* @param root The project root the package is installed from.
* @param name The package name.
*/
function isVitePlugin(root, name) {
const directory = findProjectPackageDirectories(root, name)[0];
if (!directory)
return false;
try {
const manifest = JSON.parse(fs.readFileSync(join(directory, 'package.json'), 'utf8'));
return [manifest.dependencies, manifest.peerDependencies].some(dependencies => dependencies?.vite !== undefined);
}
catch {
return false;
}
}
/**
* Packages that must NEVER be prebundled for the browser run: Vitest and its
* expect/snapshot internals are resolved to the project's own copy by the
* resolve plugin, and optimizing them would fork that copy.
*/
export const VITEST_BROWSER_OPTIMIZER_EXCLUDES = [
...NODE_ONLY_DEPENDENCIES,
'vitest',
'expect-type',
'@vitest/expect',
'@vitest/snapshot',
'vitest > expect-type',
'vitest > @vitest/expect > chai',
'vitest > @vitest/snapshot > magic-string',
];
/**
* Builds the `optimizeDeps.include` list: every browser dependency name plus,
* when resolvable, its installed directory (so nested entry points are
* prebundled too).
* @param ctx The histoire context.
*/
export function getVitestBrowserOptimizeDeps(ctx) {
return [...new Set(withPackageDirs(getVitestBrowserDependencyNames(ctx)))];
}
/**
* Lists the packages force-included in the browser dependency optimizer.
*
* Deliberately broad (every project dependency AND devDependency): the browser
* run disables Vite's dep discovery, so a story importing a package that was not
* pre-bundled triggers a mid-run optimizer reload that kills the browser
* connection. Node-only dependencies and Vite plugins stay excluded because
* their server graphs can reference browser-incompatible modules and exports.
* @param ctx The histoire context, used for the project root and support plugins.
*/
export function getVitestBrowserDependencyNames(ctx) {
const deps = new Set(getVitestFrameworkDependencyNames(ctx));
try {
const packageJson = JSON.parse(fs.readFileSync(resolve(ctx.root, 'package.json'), 'utf8'));
for (const dep of [
...Object.keys(packageJson.dependencies ?? {}),
...Object.keys(packageJson.devDependencies ?? {}),
]) {
if (dep !== 'histoire'
&& !NODE_ONLY_DEPENDENCIES.has(dep)
&& dep !== 'vite'
&& dep !== 'vitest'
&& !dep.startsWith('@histoire/')
&& !dep.startsWith('@vitest/')
&& !dep.startsWith('@types/')
&& !dep.startsWith('@vue/')
&& !isVitePlugin(ctx.root, dep)) {
deps.add(dep);
}
}
}
catch { }
return [...deps];
}
/** Framework runtimes reached through virtual modules before ordinary entry scanning. */
export function getVitestFrameworkDependencyNames(ctx) {
const deps = new Set();
if (ctx.supportPlugins.some(plugin => plugin.id === 'vue3')) {
deps.add('vue');
deps.add('pinia');
deps.add('floating-vue');
deps.add('@vueuse/core');
}
if (ctx.supportPlugins.some(plugin => plugin.id === 'svelte4')) {
deps.add('svelte');
}
if (ctx.supportPlugins.some(plugin => plugin.id === 'react')) {
for (const name of ['react', 'react-dom', 'react-dom/client', 'react/jsx-runtime', 'react/jsx-dev-runtime'])
deps.add(name);
}
return [...deps];
}