Search code examples
asp.netvisual-studio-2015bowerasp.net-core-mvckpm

Restore bower components on Visual Studio build


I've just created my first ASP.NET 5 MVC 6 project in VS 2015. The web application project that comes out of the box.

I figured out that bower components are stored in wwwroot/lib folder. I made an experiment and deleted it. After that I rebuilt the project and the bower components were not restored.

Calling bower install manually in console restored the files.

How do I make Visual Studio restore bower files automatically on build?


Solution

  • I figured out a way to do this, though I don't really like it. I'll be happy to accept a better answer.

    In project's gulpfile.js the first line is a comment"

    /// <binding Clean='clean'/>
    

    It hooks to Visual Studio Clean event and binds clean Gulp task to it. You can hook to other events e.g. After Build. I bound it with a task that uses gulp-bower to restore the components.

    gulpfile.js

    /// <binding Clean='clean' AfterBuild='after_build'/>
    
    var gulp = require("gulp"),
        bower = require('gulp-bower');
    
    ...
    
    gulp.task("after_build", function() {
        return bower()
            .pipe(gulp.dest(paths.webroot + 'lib/'));
    });
    

    You can see the bindings in Task Runner Explorer: enter image description here

    This solution seems kind of clumsy though. I'd rather have the kpm-utility do the job.

    EDIT

    Actually there is no need for restoring the packages on build. They are restored when you open the project in VS. However I don't know what happens when you update your sources from code repository.