-
Notifications
You must be signed in to change notification settings - Fork 12k
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
Comments
I'm now loading a "sandbox" script using the "scripts" section in angular-cli.json. |
Following... |
Suggested workaround:
Then you should rename your Hope that helps while we wait for an official solution |
What's wrong with having |
@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. |
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? |
@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:
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 e.g. // ... other imports ...
import {environment} from "../../environments/environment";
// ... other code ...
constructor(logger:Logger){
ga('create', environment.analyticsCode, 'auto');
}
// ... other code ... |
Hello @fcalderon , yes, I actually solved it approximately the same way you did.
kind regards |
I created duplicate commands with prod suffix in the script and used those commands when needed. |
@LeonLiber I like your solution. The nature of the problem is more build related. |
Why does this have a "nice to have" priority? This is a must have feature for a build tool. |
I'd like to share another solution that leaves only little boilerplate code in 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. |
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. |
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. |
Kinda need this badly as well. |
Check this. |
For anyone still looking for a solution, in your
It's just a workaround but gets the job done for me. |
The "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 |
Closing as Angular CLI 6.0+ supports a far richer configuration file format that allows for the customizations described above. |
This issue has been automatically locked due to inactivity. Read more about our automatic conversation locking policy. This action has been performed automatically by a bot. |
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
The text was updated successfully, but these errors were encountered: