pluginVersion() named package.json relative to its own module. A bundler takes such a file for an asset: a Turbopack build of a site copied the plugin's whole package.json (dependency lists, scripts, the registry and repository addresses; no secret) to /_next/static/media/package.<hash>.json, served to anyone. Webpack kept it on the server side. The version now comes from src/version.ts (scripts/write-version.mjs, run by the `version` script; a test fails when it differs from package.json). A smoke test of the built site: nothing under .next/static is a package manifest. Co-Authored-By: Claude Fable 5.2 <[email protected]>
33 lines
1.7 KiB
TypeScript
33 lines
1.7 KiB
TypeScript
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([])
|
|
})
|
|
})
|