Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inject build timestamp into vue-cli build output files to verify deployments with yarn

It is needed verify whether the latest build was deployed. I would like add timestamp on build log and in each file of the build output. I am using a Vue framework and yarn.

like image 808
sarnei Avatar asked Oct 04 '19 15:10

sarnei


People also ask

How do I use vue app with yarn?

success Installed "[email protected]" with binaries: - create-vite - cva ✔ Select a framework: › vue ✔ Select a variant: › vue Scaffolding project in /Users/tony/src/tmp/my-vite-project... Done. Now run: cd my-vite-project yarn yarn dev ✨ Done in 38.38s.

How do I check my vue CLI service?

You can verify that it is properly installed by simply running vue , which should present you with a help message listing all available commands.

What does vue CLI build do?

The CLI ( @vue/cli ) is a globally installed npm package and provides the vue command in your terminal. It provides the ability to quickly scaffold a new project via vue create . You can also manage your projects using a graphical user interface via vue ui .

Does vue CLI service use webpack?

It is the standard tooling baseline for using Vue JS which provides you with the Vue CLI service which is a runtime dependency, built on Webpack with 0 configs. It has a very extensive collection of plugins you can add to your project with a line of command.


1 Answers

I needed to have a build timestamp in the output Vue app, not the logs.

(You could write to build logs by adding a console.log(new Date().toIsoString()) in the webpack part of vue.config.js.)

One way to get the build timestamp into to the app itself is to make use of the fact that webpack uses a simple template language in the HTML itself.

In the Vue app index.html (for example), I inserted a data attribute on the root <html> element:

<html data-build-timestamp-utc="<%= new Date().toISOString() %>">
  ...
</html>

That's easily retrieved:

document.documentElement.dataset.buildTimestampUtc

You can then add that as a getter on the root App component, @Provide it to other components as "buildtime", etc.

This works on a main build as well as a development "serve" build - but remember the root HTML itself doesn't hot-module-reload, so although the build timestamp is being updated, you'll have to refresh the page to see it.

like image 68
Andrew E Avatar answered Sep 22 '22 16:09

Andrew E