Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

376
Visualizações
How do I add a version number to a SvelteKit/Vite app?

I'm trying to create a system in my SvelteKit app where it shows you info about the current app version (ideally a Git commit hash and description) on a certain page. I tried using Vite's define feature to do this at build time but it doesn't seem to work. How do I add something like this?

Here's an example of what I tried to do:

Vite config in svelte.config.js

vite: () => ({
    define: {
        '__APP_VERSION__': JSON.stringify('testfornow')
    }
})

index.svelte:

<script lang="ts">
    const version: string = __APP_VERSION__;
</script>

<p>Current App version: {version}</p>
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

This is how I managed to make it work:

  • Get the package.json data as explained in the SvelteKit FAQ, and load it as a constant in Vite config:
// 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);

const config = {
  kit: {
    // ...
    vite: {
      define: {
        '__APP_VERSION__': JSON.stringify(pkg.version),
      }
    },
  },
  // ...
};

  • Use the variable in any svelte file:
<h2>Version: {__APP_VERSION__}</h2>

Quite similar to your example, hope it helps!

about 4 years ago · Juan Pablo Isaza Relatório

0

I have a blank file version.txt in my static directory. Then I ​use something like this in my packaging script.

git describe --tags 2> /dev/null || git rev-parse --short HEAD > build/assets/version.txt

This tries to find a tag (if any) or just outputs latest commitId. In my app I fetch /version.txt and display its content where appropriate.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda