From e4f7e7e497f0e30690965f0eba6506aede3c37d8 Mon Sep 17 00:00:00 2001 From: Jirka Helmich Date: Tue, 30 Jun 2015 14:47:54 +0200 Subject: [PATCH] Docs update - using environment-dependent settings in configuration It might not be the best solution, take it just as a suggestion for a missing chapter in docs. It took me a lot of time to figure this out and sincerely, I'm looking forward to seeing the correct solution for this. My code is based on http://p373.net/2014/01/25/how-to-create-environment-based-builds-with-grunt/, which didn't work for me entirely. --- README.md | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/README.md b/README.md index a7eed92..d64d46c 100644 --- a/README.md +++ b/README.md @@ -122,6 +122,40 @@ yourtask : { } ``` +## Environment-specific configuration + +In order to configure your tasks based on the environment, you need to define a task and use templates: + +``` + +grunt.initConfig({ + env: { + dev: { + MY_CONST: 'a' + }, + prod: { + MY_CONST: 'b' + } + }, + myTask: { + options: { + myOpt: <%= MY_CONST %> + } + } +}); + +grunt.registerTask('loadconst', 'Load constants', function() { + grunt.config('MY_CONST', process.env.MY_CONST); +}); + +grunt.registerTask('default', [ + 'env:dev', + 'loadconst', + 'myTask' +]); + +``` + ## Important note on data types Environment variables are strings only. If you attempt to assign complex objects, they will be converted to strings.