import { readdirSync, readFileSync, statSync } from 'node:fs' import path from 'node:path' import { describe, expect, test } from 'vitest' import { pluginVersion } from '../src/modules/doctor/status.js' import { PLUGIN_VERSION } from '../src/version.js' const root = path.resolve(import.meta.dirname, '..') const walk = (dir: string): string[] => readdirSync(dir).flatMap((name) => { const file = path.join(dir, name) return statSync(file).isDirectory() ? walk(file) : [file] }) describe('the plugin version', () => { test('src/version.ts says what package.json says (node scripts/write-version.mjs after a bump)', () => { const { version } = JSON.parse(readFileSync(path.join(root, 'package.json'), 'utf8')) as { version: string } expect(PLUGIN_VERSION).toBe(version) expect(pluginVersion()).toBe(version) }) // A file named through `new URL(…, import.meta.url)` (or imported) in code a site bundles becomes an ASSET of // that site: Turbopack put the plugin's whole package.json into the public /_next/static/media/. test('no code a site bundles names package.json as a module-relative file', () => { const cli = /^src\/modules\/(?:starter|doctor\/(?:cli|project|run|probe|rules))/ // run by the bin scripts, on the project's own disk const offenders = walk(path.join(root, 'src')) .filter((file) => /\.(?:ts|tsx|mjs|js)$/.test(file)) .map((file) => ({ code: readFileSync(file, 'utf8'), rel: path.relative(root, file) })) .filter(({ code, rel }) => (!cli.test(rel) && /new URL\([^)]*import\.meta\.url/.test(code)) || /(?:from|import\()\s*['"][^'"]*package\.json['"]/.test(code)) .map(({ rel }) => rel) expect(offenders).toEqual([]) }) })