Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add git hash to Vue.js Component

I want to create a vue.js component which will display the package.json version number and hash of most recent git commit. Here is the code so far:

<template>
  <div class="versionLabel">Version: {{version}} (HASH)</div>
</template>

<script lang="ts">
import { Component, Prop, Vue } from 'vue-property-decorator';
import { version } from '../../package.json';

@Component
export default class VersionLabel extends Vue {
  get version() {
    return version;
  }
}
</script>

<style scoped lang="scss">
div {
  background-color: rgb(60, 172, 60);
  color: lightgray;
}
</style>

I am deploying to Heroku using the commands

"postinstall": "if test \"$NODE_ENV\" = \"production\" ; then npm run build ; fi ",
"start": "node server.js",

in package.json and this simple server:

const express = require('express');
const serveStatic = require("serve-static")

app = express();
app.use(serveStatic(__dirname + '/dist'));

const port = process.env.PORT || 5000;
app.listen(port);

The version number is working (Although suggestions for improvement are welcome) but how can I add the git hash in place of HASH?

like image 674
user9540018 Avatar asked Dec 28 '18 18:12

user9540018


People also ask

How to add external JavaScript scripts to Vue JS components?

To add external JavaScript scripts to Vue.js components, we can add a script element with document.createElement.

How do I import a Vue script into a single file?

If you're not using a starter template, you have to set up webpack and vue-loader. You can then import your scripts to the relevant (single file) components. Before that, you have to export from your scripts what you want to import to your components.

What is a Vue component?

When not using a build step, a Vue component can be defined as a plain JavaScript object containing Vue-specific options: The template is inlined as a JavaScript string here, which Vue will compile on the fly.

Why should I use Vue instead of HTML?

It's common for an app to be organized into a tree of nested components: This is very similar to how we nest native HTML elements, but Vue implements its own component model that allow us to encapsulate custom content and logic in each component. Vue also plays nicely with native Web Components.


1 Answers

Install git-describe as a dev dependency (e.g. yarn add --dev git-describe).

In vue.config.js add:

const {gitDescribe, gitDescribeSync} = require('git-describe');
process.env.VUE_APP_GIT_HASH = gitDescribeSync().hash

Now, in every component, we have process.env.VUE_APP_GIT_HASH variable.

Here is how I added it to my app: https://github.com/Quantum-Game/quantum-game-2/pull/164 (with some discussion).

Other approaches

There are other approaches, e.g. using git-revision-webpack-plugin (example for the Vue forum):

const GitRevisionPlugin = require('git-revision-webpack-plugin')

module.exports = {
  'chainWebpack': config => {
    config.plugin('define').tap(args => {
      const gitRevisionPlugin = new GitRevisionPlugin()
      args[0]['process.env']['VUE_APP_COMMIT_HASH'] = JSON.stringify(gitRevisionPlugin.commithash())
      return args
    })
  }
}

Another way is to use git directly, with child-process.

See also

  • Including git commit hash and date in webpack build
  • Get hash of most recent git commit in Node
  • Git log output to XML, JSON, or YAML?
like image 94
Piotr Migdal Avatar answered Oct 05 '22 10:10

Piotr Migdal