GithubHelp home page GithubHelp logo

geeklearningio / gl-ionic2-env-configuration Goto Github PK

View Code? Open in Web Editor NEW
24.0 6.0 3.0 1.02 MB

An Ionic2 Service to load an environment specific configuration before everything else

License: MIT License

JavaScript 17.99% TypeScript 65.47% HTML 5.97% CSS 10.56%
ionic2 ionic configuration configuration-loader

gl-ionic2-env-configuration's Introduction

Introduction

This is an environment configuration package for Ionic2. It allows you to dynamically load a json configuration file at the root of your app (www). This json will be loaded before everything else in your App.

Usecase

For example, in a continuous integration system, we often need different API endpoints between developpment, preproduction, production and so on. This system will allow you to change this kind of parameter in the configuration file without having to recompile your Typescript code all over.

Example

To make it simpler, this repository contains a test-app folder which follows the "How to use it section". So if you prefer reading the code, go ahead!

How to use it

Install the module

Install it with npm:

npm install gl-ionic2-env-configuration --save

Create or copy your configuration json file

Simple way

Add a env-configuration.json file in your www folder containing your env configuration variables. There is no required key needed, it's your configuration. It will surely contain your api url, your Google Analytics ID and more... To copy the right configuration for the right environment, you will need a copy executable, as explained in the next section.

Automatic way (use a copy executable)

I made a simpe executable that will copy the right configuration file for a specified environment: copy-env-config.js which is available here.

Here how to use it:

node copy-env-config.js --env YOURENV

This will copy the file src/env-configuration/YOURENV.json to www/env-configuration.json

Even more automatic

Just run this command before your ionic:serve or ionic:build command to specify the right environment configuration to copy. For example, to build you app in prod and serve in dev, just update your package.json scripts like that:

"ionic:build": "node copy-env-config.js --env prod | ionic-app-scripts build",
"ionic:serve": "node copy-env-config.js --env dev | ionic-app-scripts serve"

Use the module in your Ionic 2 app

import { NgModule } from '@angular/core';
import { IonicApp, IonicModule } from 'ionic-angular';
import { MyApp } from './app.component';
import { HomePage } from '../pages/home/home';

// Import the module
import { GLIonic2EnvConfigurationModule } from 'gl-ionic2-env-configuration';

@NgModule({
  declarations: [
    MyApp,
    HomePage
  ],
  imports: [
    IonicModule.forRoot(MyApp),

    GLIonic2EnvConfigurationModule // Import the module here
  ],
  bootstrap: [IonicApp],
  entryComponents: [
    MyApp,
    HomePage
  ],
  providers: []
})
export class AppModule {}

Get the configuration in your app

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

// Import the module
import {EnvConfigurationProvider} from "gl-ionic2-env-configuration";

// Import your configuration typings
// You can specify a typing for your configuration to get nice and neat autocompletion
import {ITestAppEnvConfiguration} from "../../env-configuration/ITestAppEnvConfiguration";

@Component({
  selector: 'page-home',
  templateUrl: 'home.html'
})
export class HomePage {

  // inject the EnvConfigurationProvider and specify the configuration typings
  constructor(private envConfiguration: EnvConfigurationProvider<ITestAppEnvConfiguration>) {
    let config: ITestAppEnvConfiguration = envConfiguration.getConfig();
    console.log(config); // And here you have your nice configuration
  }

}

How it works

This modules uses a specific way to load the EnvConfigurationProvider which basically tells Angular to wait for this provider to load the configuration json file before loading the rest. So for example, your API service will have the right endpoint even in its constructor.

To learn more about this specific loading, you can see this discussion and this GIST file.

gl-ionic2-env-configuration's People

Contributors

lanceli avatar mvidailhet avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

gl-ionic2-env-configuration's Issues

Build for production failed

Hi, i am currently using ionic 2 RC 4. I have followed the instruction and it runs well on development mode, but when I tried to build in production e.g ionic run android --prod it failed.

[17:41:38]  build prod failed: Error encountered resolving symbol values statically. Reference to a local (non-exported)
            symbol 'GLIonic2EnvConfigurationModule'. Consider exporting the symbol (position 30:14 in the original .ts file), resolving symbol GLIonic2EnvConfigurationModule in
            /Users/tomyismail/my-app/node_modules/gl-ionic2-env-configuration/dist/gl-ionic2-env-configuration.module.d.ts,
...
...

below is my ionic information:

$ ionic info

Your system information:

Cordova CLI: 6.4.0
Ionic Framework Version: 2.0.0-rc.4
Ionic CLI Version: 2.1.18
Ionic App Lib Version: 2.1.9
Ionic App Scripts Version: 0.0.47
ios-deploy version: 1.9.0
ios-sim version: 5.0.13
OS: macOS Sierra
Node Version: v6.9.2
Xcode version: Xcode 8.2.1 Build version 8C1002

Thanks.

Question about doc

When you talk about env-configuration.json file.
It's like prod-configuration.json and dev-configuration.json or only a single file with variables for each environment ?
How do you set this file ?
Need some specific keys inside ?

Do you have an example of config file ?

edit:
I just re read readme.
I supose this answer me:
This will copy the file src/env-configuration/YOURENV.json to www/env-configuration.json

Runtime Error: Cannot find module "lodash/merge"

After adding the module to my project, I do only get the following error at runtime:

Runtime Error
Cannot find module "lodash/merge"

webpackMissingModule@http://localhost:8100/build/main.js:76736:78
@http://localhost:8100/build/main.js:76736:45
__webpack_require__@http://localhost:8100/build/main.js:20:12
@http://localhost:8100/build/main.js:124054:96
__webpack_require__@http://localhost:8100/build/main.js:20:12
@http://localhost:8100/build/main.js:53507:94
__webpack_require__@http://localhost:8100/build/main.js:20:12
@http://localhost:8100/build/main.js:76424:86
__webpack_require__@http://localhost:8100/build/main.js:20:12
@http://localhost:8100/build/main.js:76676:73
__webpack_require__@http://localhost:8100/build/main.js:20:12
@http://localhost:8100/build/main.js:123731:75
__webpack_require__@http://localhost:8100/build/main.js:20:12
@http://localhost:8100/build/main.js:108580:73
__webpack_require__@http://localhost:8100/build/main.js:20:12
@http://localhost:8100/build/main.js:134647:70
__webpack_require__@http://localhost:8100/build/main.js:20:12
@http://localhost:8100/build/main.js:66:18
@http://localhost:8100/build/main.js:1:11

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.