Detect release / debug in gulp using Visual Studio 2015 Detect release / debug in gulp using Visual Studio 2015 asp.net asp.net

Detect release / debug in gulp using Visual Studio 2015


I know this has been around for awhile but I recently ran into the same issue and was unhappy with the other solutions and workarounds that I found in answers here on SO. I found a very nice solution that we went with in the comments on the aspnet github page: https://github.com/aspnet/Home/issues/1231#issuecomment-182017985

I hope that it helps someone get to a solution they're happier with faster than I did.

Simply, add the following line to your pre-build event in project config

echo {"config" : "$(ConfigurationName)"} > ../buildConfig.json

With that little beauty sitting around you can read it in your tasks and react accordingly. I use it to prevent minifying files when I'm in debug mode like so:

gulp.task("min:js:bundlesFolder", function ()    {        var json = fs.readFileSync("./buildConfig.json", "utf8");        var host = JSON.parse(json.replace(/^\uFEFF/, ''));        host.isDebug = host.config == "Debug";        console.log(host.isDebug);        if (host.isDebug === true)        {            return;        }        return gulp.src([paths.jsbundleFolder + "/**/*.js", "!" + paths.jsbundleFolder + "/**/*.min.js"])        .pipe(uglify())        .pipe(gulp.dest(paths.jsbundleFolder));    });


In Visual Studio 2015, with the gulp integration, I like @edo limburg and @RamenChef's answer the best.

I have a single page angular app in the same solution as my web api.When building the SPA, I just wanted to replace the URLs to the API and OAuth2 (OIDC) authority servers in an html and a couple of JavaScript files.

I created a gulpfile.js with both a Debug and Release task. Note the case-sensitive spelling:

gulp.task('Debug', function () {  gulp.src("./callback.html")    .pipe(replace(uatAuthority,                    debugAuthority))    .pipe(gulp.dest('./'));...}gulp.task('Release', function () { gulp.src("./callback.html")    .pipe(replace(debugAuthority,                    uatAuthority))    .pipe(gulp.dest('./'));)}

FYI, I included gulp-string-replace to handle the replace tasks:

var replace = require('gulp-string-replace');

After I tested the tasks in the Task Runner Explorer, I unloaded the project file and edited it, adding the following code right before the end project tag:

<Target Name="BeforeBuild">    <Exec Command="gulp $(Configuration)" />  </Target>


In visual studio,

  • create a new console project
  • Install the nugget package 'MSBuild.NodeTools' in your project
  • Unload your .csproj file and edit it by adding the line in the after build target

       <Target Name="AfterBuild">       <MSBuild.Gulp   GulpFile="path to your gulpfile"/>      </Target>

Here there is all property you can use :

  • GulpFile: Path to gulpfile. Defaults to $(MSBuildProjectDirectory)\gulpfile.[js|coffee].
  • GulpWorkingDirectory: Directory in which context the gulp task gets executed. Defaults to $(MSBuildProjectDirectory).
  • GulpBuildTask: Gulp task that gets executed on build. Defaults to build-$(Configuration).
  • GulpCleanTask: Gulp task that gets executed on clean. Defaults to unset.

As you can see the task executed takes the name of the configuration in visual studio, so if you build your project in "debug", the task "build-debug" will be executed

You can also doing like this, if you want a custom task :

<Target Name="AfterBuild"><MSBuild.Gulp  Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU'"  GulpFile="path to your gulpfile"  GulpBuildTask="CustomDebug"/><MSBuild.Gulp  Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU'"  GulpFile="path to your gulpfile"  GulpBuildTask="CustomRelease"/></Target>