1

I'm trying to convert an angular2 with webpack2 project to angular-cli based project.

if I understood correctly, angular-cli now supports webpack. in my original project I injected the following app.config.js:

module.exports =  {
    webServer: {
        appBaseHref : JSON.stringify("/")
    },
    auth0: {
        apiKey: JSON.stringify("API_KEY"),
        domain: JSON.stringify("DOMAIN.auth0.com"),
        callbackUrl: JSON.stringify("CALLBACK_URL")
    }
};

by adding the following to webpack.config.js file:

const appConfig = require("./config/app.config");
const DefinePlugin = require("webpack/lib/DefinePlugin");
...
module.exports = {
    plugins: [
       new DefinePlugin({
           APP_CONFIG: appConfig
       }),
...

then in my typescript project i would declare APP_CONFIG with declare var APP_CONFIG:any; and then I would use it's properties. how can I inject such an object in an angular-cli project?

thanks

4
  • Can't you just make a service for this? Commented Oct 14, 2016 at 5:49
  • @Chrillewoodz - yes.. I can.. trying to figure out why I did it with webpack in the first place but i got nothing :) what do I do ? do you want to post it as an answer? do I delete this question ? Commented Oct 14, 2016 at 6:13
  • Gimme a minute. Commented Oct 14, 2016 at 6:18
  • So I want this same thing - so that I can inject the build numbers. Not sure the service answer will work for me. I would like to have a global like APP_VERSION and set it to childProcess.execSync('git rev-list HEAD --count').toString(); Commented Mar 17, 2017 at 18:46

1 Answer 1

2

Here's a simple example that just uses a normal service with a set and get method:

import {Injectable} from '@angular/core';

@Injectable()

export class AppConfigService {

  public config: any = {
    apiKey: '[api_key]',
    domain: '[domain]',
    callbackUrl: '[callbackUrl]'
  }

  constructor() {}

  /* Allows you to update any of the values dynamically */
  set(k: string, v: any): void {
    this.config[k] = v;
  }

  /* Returns the entire config object or just one value if key is provided */
  get(k: string): any {
    return k ? this.config[k] : this.config;
  }
}

For more complex use cases you might want to convert it to an observable instead but I don't see that being necessary in this simple case.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.