Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Including license file in production only environment using angular-cli

I use a third party library in my project. The third party library provides two licenses. A development license and a production license. The license files are java script files.

I use anglular-cli to build my project.

Obviously I want my development builds to be fired with development license. However when the production build is fired (--prod flag), I want the production license to be bundled. Would like to know how to do this.

Below are my environment details

@angular/cli: 1.2.0
node: 6.10.3
os: win32 x64

As seen below in the .angular-cli.json file, license is included in the scripts section

"scripts": [
      "./assets/js/cloak.js",
      "../license.js"

Below is my complete .angular-cli.json

{
  "$schema": "./node_modules/@angular/cli/lib/config/schema.json",
  "project": {
    "name": "cricinfo-ui"
  },
  "apps": [
    {
      "root": "src",
      "outDir": "dist",
      "assets": [
        "assets",
        "favicon.ico",
        "cloak.json",
        "environment.json",
        "cricinfo.png"
      ],
      "index": "index.html",
      "main": "main.ts",
      "polyfills": "polyfills.ts",
      "test": "test.ts",
      "tsconfig": "tsconfig.app.json",
      "testTsconfig": "tsconfig.spec.json",
      "prefix": "app",
      "styles": [
        "styles.css"
      ],
      "scripts": [
      "./assets/js/cloak.js",
      "../license.js"

      ],"environmentSource": "environments/environment.ts",
      "environments": {
        "dev": "environments/environment.ts",
        "prod": "environments/environment.prod.ts"
      }
    }
  ],
  "e2e": {
    "protractor": {
      "config": "./protractor.conf.js"
    }
  },
  "lint": [
    {
      "project": "src/tsconfig.app.json"
    },
    {
      "project": "src/tsconfig.spec.json"
    },
    {
      "project": "e2e/tsconfig.e2e.json"
    }
  ],
  "test": {
    "karma": {
      "config": "./karma.conf.js"
    }
  },
  "defaults": {
    "styleExt": "css",
    "component": {}
  }
}
like image 684
TheMonkWhoSoldHisCode Avatar asked Dec 05 '25 14:12

TheMonkWhoSoldHisCode


1 Answers

You need to use environment.ts and environment.prod.ts files. Import the corresponding license into each file and that's it.

import '../relative/path/to/the/license.js';

The only problem would be that when you use -prod flag the license file might be treated as unnecessary and might be excluded from the build. However this should not be a problem in most of the cases.

Another way is to register a second app in your .angular-cli.json and pass different licenses there. But this is too much overhead compare to the first way.

like image 197
smnbbrv Avatar answered Dec 07 '25 04:12

smnbbrv



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!