Skip to content

Instantly share code, notes, and snippets.

@jeandat
Last active January 8, 2017 18:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jeandat/170a6fbc1ace0ed37ce74f9803770087 to your computer and use it in GitHub Desktop.
Save jeandat/170a6fbc1ace0ed37ce74f9803770087 to your computer and use it in GitHub Desktop.
Allow using different configuration files in order to instrument the build

ExtendedDefinePlugin will replace ENV variables by their configuration value. Uglify will remove dead code.

1. Copy and modify webpack.config.js

// EDIT by Jean DAT
// Custom code in order to load different configuration files based on context (targeted environment).
// In a configuration file, I store things like the API url to use, etc.
var ExtendedDefinePlugin = require('extended-define-webpack-plugin');
var optimist = require('optimist');
var argv = optimist.argv;
if (!argv.env) argv.env = process.env.IONIC_ENV;
var envParameters = require('../env/' + argv.env + '.js');
envParameters.prod = argv.env === 'prod';
envParameters.dev = argv.env === 'dev';
console.log('Using env parameters (' + argv.env + '):', JSON.stringify(envParameters, null, '    '));
// END EDIT

module.exports = {

...

plugins: [
    // EDIT by Jean DAT
    new ExtendedDefinePlugin({ENV: envParameters}),
    // END EDIT
    ionicWebpackFactory.getIonicEnvironmentPlugin()
  ],
  
...

2. Create one js file per environment inside env folder

// ./env/dev.js
module.exports = {
  apiUrl: 'http://example.com/api/dev'
};

3. Declare custom webpack config file inside package.json

4. Declare ENV variable inside declaration.d.ts

declare let ENV: any;

5. Use in code

if(ENV.dev) console.log('## DEV ##');

...

export class Api {
  url: string = ENV.apiUrl;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment