histoire
Version:
A fast story book powered by Vite
24 lines (23 loc) • 1.24 kB
JavaScript
import { importResolvedModule } from '../../util/import-resolved.js';
import { tryResolveDependency } from '../../util/resolve-package.js';
import { PreviewError } from './errors.js';
/** Resolve project-first optional peer without evaluating it during catalog reads. */
export async function resolvePreviewBrowser(root) {
const resolved = tryResolveDependency(root, 'playwright');
if (!resolved)
throw new PreviewError('DEPENDENCY_MISSING', 'Install playwright in project: pnpm add -D playwright');
const module = await importResolvedModule(resolved);
// require.resolve selects Playwright's CommonJS entry. Node may expose that
// module only through default rather than synthesizing its re-export names.
const chromium = module.chromium ?? module.default?.chromium;
if (!chromium?.launch)
throw new PreviewError('DEPENDENCY_MISSING', 'Project playwright does not provide Chromium');
return async (options) => {
try {
return await chromium.launch(options);
}
catch {
throw new PreviewError('BROWSER_UNAVAILABLE', 'Chromium could not launch. Install browser with: pnpm exec playwright install chromium --only-shell', true);
}
};
}