import { existsSync, readdirSync, readFileSync, statSync } from 'node:fs' import path from 'node:path' import { describe, expect, test } from 'vitest' const root = path.resolve(import.meta.dirname, '..') const pkg = JSON.parse(readFileSync(path.join(root, 'package.json'), 'utf8')) as { exports: Record } function files(dir: string): string[] { return readdirSync(dir).flatMap((name) => { const p = path.join(dir, name) return statSync(p).isDirectory() ? files(p) : /\.(?:md|ts|tsx|mjs)$/.test(name) ? [p] : [] }) } describe('package exports', () => { test('every @intecion/ipal-kit/ used in docs and src is exported', () => { const used = new Map() for (const file of [...files(path.join(root, 'docs')), ...files(path.join(root, 'src'))]) { for (const m of readFileSync(file, 'utf8').matchAll(/@intecion\/ipal-kit(?\/[a-z0-9./-]+)?(?=['"#\s`)])/g)) { const sub = m.groups?.sub ? `.${m.groups.sub}` : '.' // node_modules/@intecion/ipal-kit/dist|docs|package.json — filesystem paths in prose, not imports if (/^\.\/(?:dist|docs|package\.json)(?:\/|$)/.test(sub)) {continue} if (!used.has(sub)) {used.set(sub, path.relative(root, file))} } } const missing = [...used].filter(([sub]) => !(sub in pkg.exports)).map(([sub, file]) => `${sub} (e.g. ${file})`) expect(missing).toEqual([]) }) test('every export points at a file the build produces from src', () => { for (const [sub, target] of Object.entries(pkg.exports)) { const out = typeof target === 'string' ? target : (target as { import: string }).import const src = out.replace('./dist/', 'src/').replace(/\.js$/, '.ts') expect(existsSync(path.join(root, src)) || existsSync(path.join(root, src.replace(/\.ts$/, '.tsx'))), `${sub} → ${src}`).toBe(true) } }) }) describe('client entry', () => { // Wszystko, co importuje /client, trafia do bundla strony publicznej — panel (@payloadcms/ui) to ~235 KB gz test('does not pull admin UI (@payloadcms/ui, @payloadcms/next) into the site bundle', () => { const seen = new Set() const offenders: string[] = [] const resolve = (from: string, spec: string) => { const base = path.resolve(path.dirname(from), spec).replace(/\.js$/, '') return ['.ts', '.tsx', '/index.ts', '/index.tsx'].map((ext) => base + ext).find((f) => existsSync(f)) } const walk = (file: string) => { if (seen.has(file)) {return} seen.add(file) for (const m of readFileSync(file, 'utf8').matchAll(/(?:import|export)\s[^'"]*?from\s+['"](?[^'"]+)['"]/g)) { const spec = m.groups!.spec if (/^@payloadcms\/(?:ui|next)(?:\/|$)/.test(spec)) {offenders.push(`${path.relative(root, file)} → ${spec}`)} if (spec.startsWith('.')) { const next = resolve(file, spec) if (next) {walk(next)} } } } walk(path.join(root, 'src/exports/client.ts')) expect(offenders).toEqual([]) }) })