如何在网站页脚显示 package.json 版本?

How to display a package.json version in the footer of the site?

我想在我网站的页脚显示 package.json 文件中声明的版本

我该怎么做?

我在他们的文档中找到了 this FAQ 解释,但不幸的是我不知道如何从我的组件访问它

// 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 来执行此操作:

const config = {
  kit: {
    vite: {
      define: {
        VERSION: pkg
      }
    }
  }
};

在你的组件中:

<script>
  const version = VERSION;
</script>

<span>{version}</span>