How to set AngularjJS base URL dynamically based on fetched environment variable?

后端 未结 4 1156
耶瑟儿~
耶瑟儿~ 2020-12-10 05:18

I have a development and production environment in which my URL\'s differ:

production:

www.exmaple.com/page

development:

d

4条回答
  •  孤街浪徒
    2020-12-10 05:39

    I personnaly do this kind of stuff with grunt.

    When I run my angular-app I have multiple tasks :

    > grunt run --target=dev
    > grunt run --target=prod
    > grunt build --target=dev
    > grunt build --target=prod
    > etc...
    

    Then grunt do strings replacement with the help of the grunt-preprocess module :

    my constants.tpl.js file gets parsed :

    [...]
    baseUrl:           '/* @echo ENV_WS_URL */',
    [...]
    

    and the url is populated.

    There are endless possibilities (string replacements, file copy, etc).

    Doing it with grunt ensure that dev config files do not go in production for example..

    I can put more details if you're interested but I only wanted to show you a different approach.

    edit gruntFile example :

    'use strict';
    
    module.exports = function(grunt) {
    
        /**
         * Retrieving current target
         */
        var target = grunt.option('target') || 'dev';
        var availableTargets = [
            'dev',
            'prod'
        ];
    
        /**
         * Load environment-specific variables
         */
        var envConfig = grunt.file.readJSON('conf.' + target + '.json');
    
        /**
         * This is the configuration object Grunt uses to give each plugin its
         * instructions.
         */
        grunt.initConfig({
            env: envConfig,       
    
            /*****************************************/
            /* Build files to a specific env or mode */
            /*****************************************/
            preprocess: {
                options: {
                    context: {
                        ENV_WS_URL: '<%= env.wsUrl %>'
                    }
                },
                constants: {
                    src: 'constants.tpl.js',
                    dest: 'constants.js'
                }
            },
    
            karma: {
                unit: {
                    configFile: '<%= src.karma %>',
                    autoWatch: false,
                    singleRun: true
                },
                watch: {
                    configFile: '<%= src.karma %>',
                    autoWatch: true,
                    singleRun: false
                }
            }
    
        });
    
    
        /****************/
        /* Plugins load */
        /****************/
        grunt.loadNpmTasks('grunt-preprocess');
    
        /*******************/
        /* Available tasks */
        /*******************/
        grunt.registerTask('run', 'Run task, launch web server for dev part', ['preprocess:constants']);
    
    };
    

    Now, the command :

    > grunt run --target=dev
    

    will create a new file with an url

提交回复
热议问题