vite_plugin_pkg_json.ts

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.

// vite.config.ts 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:

// src/routes/pkg_json_keys.ts import {pkg_json_keys} from '@fuzdev/fuz_util/pkg_json.js'; export const pkg_json_keys_custom = [...pkg_json_keys, 'keywords'] as const; // vite.config.ts → vite_plugin_pkg_json({keys: pkg_json_keys_custom}) // src/routes/library.ts → library_json_from_modules(pkg_json, modules, pkg_json_keys_custom)

Declarations
#

2 declarations

view source

vite_plugin_pkg_json
#

vite_plugin_pkg_json.ts view source

(options?: VitePluginPkgJsonOptions): Plugin<any>

Creates the virtual:pkg.json plugin. Zero-config for canonical fuz usage — the publish-safe field set defaults to pkg_json_keys; widen it via keys.

options

default {}

returns

Plugin<any>

VitePluginPkgJsonOptions
#

vite_plugin_pkg_json.ts view source

VitePluginPkgJsonOptions

keys

The package.json fields to keep in the served subset. Defaults to the curated, publish-safe pkg_json_keys. Pass a wider list — usually `` [...pkg_json_keys, 'extra'] `` — to expose more fields; the same list must reach library_json_from_modules (see the module comment).

type ReadonlyArray<keyof PackageJson>

Depends on
#

Imported by
#