代码之家  ›  专栏  ›  技术社区  ›  Roman Mahotskyi

如何显示一个包。网站页脚中的json版本?

  •  0
  • Roman Mahotskyi  · 技术社区  · 4 年前

    我想显示包中声明的版本。我的网站页脚中的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);
    
    1 回复  |  直到 4 年前
        1
  •  2
  •   Stephane Vanraes    4 年前

    你可以用 vite.define 为此:

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

    在您的组件中:

    <script>
      const version = VERSION;
    </script>
    
    <span>{version}</span>