Skip to content

[question] How to load variables from .env files #47

@juan88

Description

@juan88

I'm using grunt-replace to perform some substitutions of variables according to the environment (dev/testing/production). Those variables are set like this:

config: {
            dev: {
                options: {
                    variables: {
                        base_url: 'localhost'
                    }
                }
            },
            testing: {
                options: {
                    variables: {
                        base_url: 'testing.example.com'
                    }
                }
            },
            prod: {
                options: {
                    variables: {
                        base_url: 'production.example.com'
                    }
                }
            }
        },

Then I'm replacing the variables with grunt-replace, like this:

replace: {
            main: {
                options: {
                    patterns: [
                        {
                            match: 'BASE_URL',
                            replacement: '<%= grunt.config.get("base_url") %>'
                        }
                    ]
                },
                src: 'src/main.js' ,
                dest: 'dist/main.js'
            },
}

Since I'm using an .env file configured for the mentioned environments (.env.development, .env.testing / .env.production), is it possible to tell grunt-env to load that file and perform the replacements with this instead with grunt-config? I would like to do this so that to avoid having duplicated code for setting those variables in each environment.

Thanks!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions