How to insert a Build Number or Timestamp at build time in AngularCLI

前端 未结 8 909
故里飘歌
故里飘歌 2020-12-04 08:51

I want to have a timestamp or build number somewhere on my Angular2 App so I can tell if a user is using an old cached version or not.

How to do this with AngularCLI

相关标签:
8条回答
  • 2020-12-04 09:14

    Add this step to your build (ie Jenkins-Job):

    echo export class MyVersion {public static readonly number = '%SVN_REVISION%'} > src\my-version.ts
    

    You can access the number like this:

    import {MyVersion} from "../my-version";
    
    export class AppComponent {
        constructor() {
            console.log("Welcome to MyApp version " + MyVersion.number);
        }
    }
    

    This solution is + lightweight, + easy to read, + robust.

    0 讨论(0)
  • 2020-12-04 09:25

    May be a bit late for the discussion, but hopefully I can help someone else looking at the same problem.

    I have written a small npm package called angular-build-info which sums up some information about the current build such as a build timestamp, the git user which built the app, a shortened commit hash and the apps version from your projects package.json file.

    Implementing the package is also pretty easy, it creates a build.ts file under src/build.ts which you can then import in your Angular app and display the information anywhere.

    An example of implementing it could look as follows: (app.component.ts)

    import { Component } from "@angular/core";
    import { buildInfo } from "../build";
    import { environment } from "../environments/environment";
    
    @Component({
        selector: "app-root",
        templateUrl: "./app.component.html",
        styleUrls: ["./app.component.css"]
    })
    export class AppComponent {
        constructor() {
            console.log(
                `\n%cBuild Info:\n\n%c ❯ Environment: %c${
                    environment.production ? "production                                                                     
    0 讨论(0)
提交回复
热议问题