Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature Request: load scripts in angular-cli.json conditionally? #4288

Closed
Ismaestro opened this issue Jan 30, 2017 · 20 comments
Closed

Feature Request: load scripts in angular-cli.json conditionally? #4288

Ismaestro opened this issue Jan 30, 2017 · 20 comments
Labels
feature Issue that requests a new feature P5 The team acknowledges the request but does not plan to address it, it remains open for discussion

Comments

@Ismaestro
Copy link

Ismaestro commented Jan 30, 2017

As I mention in this stackoverflow question, I think It would be awesome if we could load scripts depending on the environment.

So In production I want to load a script while in develop not.

The syntax's in angular-cli.json could be:
{ "input": "script-only-for-dev.js", "environment": "dev" }

BR

@Ismaestro Ismaestro changed the title Feature Request: load script conditionally? Feature Request: load scripts in angular-cli.json conditionally? Jan 30, 2017
@filipesilva filipesilva added P5 The team acknowledges the request but does not plan to address it, it remains open for discussion type: enhancement labels Jan 30, 2017
@arliber
Copy link

arliber commented Feb 5, 2017

I'm now loading a "sandbox" script using the "scripts" section in angular-cli.json.
This "sandbox" script must be removed from the production build, any suggestion how to do so in the meantime?

@galta
Copy link

galta commented Feb 5, 2017

Following...
having the same problem... :-(

@arliber
Copy link

arliber commented Feb 5, 2017

Suggested workaround:
You can edit your "scripts" section under package.json file to have that:

"start": "cp angular-cli-dev.json angular-cli.json && ng serve"

"build": "cp angular-cli-prod.json angular-cli.json && ng build"

Then you should rename your angular-cli.json file to angular-cli-dev.json and angular-cli-prod.json
Each should have different configuration - in my case, different scripts in "scripts" section.

Hope that helps while we wait for an official solution

@hansl
Copy link
Contributor

hansl commented Apr 7, 2017

What's wrong with having import 'my-prod-only-script' inside your environment.prod.ts?

@Ismaestro
Copy link
Author

@hansl It's not an import. It's a script that should be in included and minified with the others scripts of the application. So I think the right place to do that is in the .angular-cli.json file.

@LanderBeeuwsaert
Copy link

A practical use case about this:

If I want to add my google analytics tracker code only when I go to production, how do I go about to do this?
I've tried to add it to environment.prod.ts ,
I've even tried https://github.com/angulartics/angulartics2 only to try to solve this.
Up until now, I have not succeeded to find a satisfying solution. Any ideas?

@fcalderon
Copy link

@LanderBeeuwsaert I have a very similar use case as yours. I may have found a workaround that might make things more maintainable and be a better design than having to separate scripts.

So the way I would do it with the service:

  • Edit environment.prod.ts and add a variable, e.g. enableAnalytics, you could also simply use the existing enableProduction
  • Go to your analytics service, and check whether this flag is true in order to send the analytic event.

e.g:

    public pageChange(pageName) {
        if (!!ga && !!environment.enableProduction) {
                ga('send', 'pageView', pageName);
        }
    }

If you have more than one analytics script, you can use the code of the script and store that in the environment. Then you could load ga with the appropriate code in the service constructor.

e.g.

// ... other imports ...
import {environment} from "../../environments/environment";

// ... other code ...

    constructor(logger:Logger){
        ga('create', environment.analyticsCode, 'auto');
    }

// ... other code ...

@LanderBeeuwsaert
Copy link

Hello @fcalderon ,

yes, I actually solved it approximately the same way you did.
In my app.component.ts file I wrote:

if (environment.production) {
  ga("create", gaCode, "auto");
  ga("send", "pageview", "index.html");
}

kind regards

@filipesilva filipesilva self-assigned this May 22, 2017
@rupeshtiwari
Copy link

I created duplicate commands with prod suffix in the script and used those commands when needed.
like "test": " ng test", I created "test-prod": "ng test --browsers PhantomJS" , "build-prod" : "ng build -prodng build --prod --output-hashing false" and so on. Now in my Continuous integration I use test-prod and build-prod scripts.

@garysui
Copy link

garysui commented Aug 2, 2017

@LeonLiber I like your solution. The nature of the problem is more build related.

@ohcibi
Copy link

ohcibi commented Aug 14, 2017

Why does this have a "nice to have" priority? This is a must have feature for a build tool.

@ohcibi
Copy link

ohcibi commented Aug 14, 2017

I'd like to share another solution that leaves only little boilerplate code in main.ts using dynamic import which I borrowed from here (simplyfied code for umd module):

main.ts

//...
import { environment } from './environments/environment';

declare global {
  interface System {
    import (request: string): Promise<any>;
  }

  const System: System;
}

if (!environment.production) {
  System.import('library').then(library => ).catch(error => );
}
//...

which only leaves something like

if (!__WEBPACK_IMPORTED_MODULE_3__environments_environment__["a" /* environment */].production) {
  __webpack_require__.e/* import() */(6).then(__webpack_require__.bind(null, "library")).then(function (library) {
    }).catch(function (e) {
    });
}

in the production build.

@val-samonte
Copy link

val-samonte commented Aug 31, 2017

any updates on this? my use case is, I want to be able to include external scripts from the CDN, (which will be easily done by adding this on the script section) but of course, I want to specifically include the dev script when in dev environment.

@richh94
Copy link

richh94 commented Sep 15, 2017

Same question for me, are there any updates on this?

For now I fixed it this way #4451 (comment) (thanks!) but that is REALLY ugly if you want to do that for Google Analytics, Hotjar AND Application Insights in 3 different environments.

@hansl hansl added feature Issue that requests a new feature and removed type: enhancement labels Jan 30, 2018
@alan-agius4
Copy link
Collaborator

Kinda need this badly as well.

@Ismaestro
Copy link
Author

Check this.

@MadMango
Copy link

For anyone still looking for a solution, in your main.ts, you could do:

import { environment } from './environments/environment';

const src = environment.your.cdn.url;
document.write(`<script src=${src}></script>`);

It's just a workaround but gets the job done for me.

@rwibawa
Copy link

rwibawa commented Mar 28, 2018

The .angular-cli.json supports configuration for multiple apps. Therefore we can put completely different configuration with different scripts/styles for the same app.

"apps": [
    {
      "styles": [
        "styles.css"
      ],
      "scripts": [ ],
      "environmentSource": "environments/environment.ts",
      "environments": {
        "dev": "environments/environment.ts",
        "prod": "environments/environment.prod.ts",
        "local": "environments/environment.local.ts"
      }
    },
    {
      "styles": [
        "styles.css",
        "../node_modules/select2/dist/css/select2.min.css"
      ],
      "scripts": [
        "../node_modules/jquery/dist/jquery.min.js",
        "../node_modules/select2/dist/js/select2.full.min.js"
      ],
      "environmentSource": "environments/environment.ts",
      "environments": {
        "dev": "environments/environment.ts",
        "prod": "environments/environment.prod.ts",
        "local": "environments/environment.local.ts"
      }
    }

To serve the default app:

$ ng serve --app=0 

or 

$ ng serve --app 0

To serve the app with local environment:

$ ng serve --app=1 --env=local --port=8091

@clydin
Copy link
Member

clydin commented Dec 6, 2018

Closing as Angular CLI 6.0+ supports a far richer configuration file format that allows for the customizations described above.

@clydin clydin closed this as completed Dec 6, 2018
@angular-automatic-lock-bot
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Sep 9, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
feature Issue that requests a new feature P5 The team acknowledges the request but does not plan to address it, it remains open for discussion
Projects
None yet
Development

Successfully merging a pull request may close this issue.