Geeks With Blogs

News Please visit me at my new blog!!

profile for Aligned at Stack Overflow, Q&A for professional and enthusiast programmers
"free in Christ Jesus from the law of sin and death." Romans 8:2 (ESV) Check out the Falling Plates video on YouTube.
more about the Gospel
And then listen to Francis Chan speaking at LifeLight in SD.



Programming and Learning from SD
1. Add a BeforeBuild in your csproj file. Edit the xml with a text editor.
  <Target Name="BeforeBuild">
    <Exec Condition="'$(Configuration)' == 'Release'" Command="script-optimize.bat" />
  </Target>
 
2. Create the script-optimize.bat
REM "%~dp0" maps to the directory where this file exists
cd %~dp0\..\YourProjectFolder
call npm uninstall grunt
call npm uninstall grunt
call npm install --cache-min 604800 -g grunt-cli
call npm install --cache-min 604800

grunt typescript requirejs copy less:compile less:mincompile
This grunt command will compile typescript, run the requireJs optimizer, complie and minimize less.
3. Make it use the minified code when the Web.config compilation debug is set to false

<!-- These CustomCollectFiles actions are used so that the Scripts-Release folder/files are included
        when publishing even though they are not project references -->
  <Target Name="CustomCollectFiles">
    <ItemGroup>
      <_CustomFiles Include="Scripts-Release\**\*" />
  </ItemGroup>
  </Target>

That should be all you need to get a Grunt task to minify and combine JS (plus other tasks) in Visual Studio Release build with debug = false.

This is a great video of Steve Sanderson talking about SPAs, npm, Knockout, Grunt, Gulp, ect. I highly recommend it.

Update: Scott Hanselman just wrote about Gulp/Grunt/Bowser/NPM being integrated into Visual Studio. This will be a nice addition

Posted on Tuesday, August 19, 2014 1:21 PM Build Tools , Grunt , Gulp | Back to top


Comments on this post: Run Grunt task in Visual Studio Release Build with a bat file

# re: Run Grunt task in Visual Studio Release Build with a bat file
Requesting Gravatar...
how does the bat file handle if any of the grunt operations fail?

Left by flux9998 on Feb 19, 2015 12:38 PM

# re: Run Grunt task in Visual Studio Release Build with a bat file
Requesting Gravatar...
If there is a failure in the Grunt operations bat file, the error list in Visual Studio says "The command "script-optimize.bat" exited with code 3. File is your project file name.
Left by Kevin on Feb 19, 2015 4:25 PM

# re: Run Grunt task in Visual Studio Release Build with a bat file
Requesting Gravatar...
Thanks for the quick reply!

Turns out grunt has a list of exit codes

http://gruntjs.com/api/exit-codes

and inside the bat file you can access these codes:

call grunt less
IF NOT ERRORLEVEL 0 GOTO BUILD_FAIL

So something like this may be helpful if you want more verbose action on what failed the batch script.

Thanks again!
Left by flux9998 on Feb 19, 2015 4:31 PM

Your comment:
 (will show your gravatar)


Copyright © Aligned | Powered by: GeeksWithBlogs.net