Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

angular cli include/exclude scripts in index.html depending on the environment

I have angular 2 application (angular cli is used). And I want to build web and mobile (cordova) version for the app.

So I want to use ng build -e prod to build for production and ng build -e cordova --output-path mobile/www --base-href ./ to build for cordova project.

I want to include <script type="text/javascript" src="cordova.js"></script> if environment is cordova and exclude facebook web api script, and vise versa if environment is production

like image 626
Gleb Avatar asked Dec 21 '16 17:12

Gleb


1 Answers

Found a solution.

Based on the answer by Ionaru in this issue

In main.ts:

if (environment.production) {

  document.write(
    `
      <script type="text/javascript">
         // JS code here
      </script>
    `
  );

  enableProdMode();
}

I believe this to be the simplest way to conditionally embed code as once was done directly on index.html

like image 102
coiso Avatar answered Oct 24 '22 06:10

coiso