Vite plugin serving a curated, publish-safe package.json as the virtual
module virtual:pkg.json.
Consumers' src/routes/library.ts (and root layouts) need package identity —
name, version, the Fuz extension fields — in the client. Importing the root
package.json directly inlines the *whole* file into the client bundle
(scripts, dependencies, devDependencies, private config) and trips
SvelteKit's server.fs.allow on cold HMR reloads. This plugin reads the
project's package.json at build time, strips it to pkg_json_keys, and
serves only that subset. Consumers combine it with virtual:svelte-docinfo's
analyzed modules via library_json_from_modules to build a LibraryJson
(see src/routes/library.ts for the canonical pattern).
The .json suffix on the virtual id is load-bearing, mirroring how
vite_plugin_fuz_css's virtual:fuz.css relies on .css: load() returns
raw JSON text and Vite's built-in vite:json plugin transforms it into an ES
module (default export plus named exports), so consumers write
import package_json from 'virtual:pkg.json'. Do *not* return a JS module
here — the .json id would double-transform it.
import {vite_plugin_pkg_json} from '@fuzdev/fuz_ui/vite_plugin_pkg_json.js';
export default defineConfig({plugins: [vite_plugin_pkg_json(), sveltekit()]});The kept field set defaults to pkg_json_keys. To expose extra publish-safe
fields, pass a wider keys list — typically composed from the default with a
spread (`` keys: [...pkg_json_keys, 'keywords'] ``). Because library_json_from_modules
re-strips at runtime, the *same* list must reach that call (and the consumer's
virtual:pkg.json ambient type) for the extras to survive end to end — share
one const across all three sites:
import {pkg_json_keys} from '@fuzdev/fuz_util/pkg_json.js';
export const pkg_json_keys_custom = [...pkg_json_keys, 'keywords'] as const;