我想显示包中声明的版本。我的网站页脚中的json文件
我该怎么做?
我发现 this 常见问题解答解释在他们的文档中,但不幸的是,我不知道如何从我的组件访问它
// svelte.config.js import { readFileSync } from 'fs'; import { fileURLToPath } from 'url'; const file = fileURLToPath(new URL('package.json', import.meta.url)); const json = readFileSync(file, 'utf8'); const pkg = JSON.parse(json);
你可以用 vite.define 为此:
vite.define
const config = { kit: { vite: { define: { VERSION: pkg } } } };
在您的组件中:
<script> const version = VERSION; </script> <span>{version}</span>