Search code examples
gulpasp.net-coreasp.net-core-mvcbundling-and-minification

JavaScript and CSS not being bundled/minified by Gulp during build


I am working on an ASP.Net 5 project and it appears they have moved to having GULP bundle and minify CSS and JavaScrpt files. I wanted to test out to make sure this was working and it does not appear to be. After I build the package the site.min.js and site.min.css files are no where to be found. As well as running the web app does not pull the files in and process them. I am probably missing some stupid little thing but I have gone through a few tutorials looking and can't see it.

gulpfile.js:

/// <binding Clean='clean' />
"use strict";

var gulp = require("gulp"),
    rimraf = require("rimraf"),
    concat = require("gulp-concat"),
    cssmin = require("gulp-cssmin"),
    uglify = require("gulp-uglify");

var paths = {
    webroot: "./wwwroot/"
};

paths.js = paths.webroot + "js/**/*.js";
paths.minJs = paths.webroot + "js/**/*.min.js";
paths.css = paths.webroot + "css/**/*.css";
paths.minCss = paths.webroot + "css/**/*.min.css";
paths.concatJsDest = paths.webroot + "js/site.min.js";
paths.concatCssDest = paths.webroot + "css/site.min.css";

gulp.task("clean:js", function (cb) {
    rimraf(paths.concatJsDest, cb);
});

gulp.task("clean:css", function (cb) {
    rimraf(paths.concatCssDest, cb);
});

gulp.task("clean", ["clean:js", "clean:css"]);

gulp.task("min:js", function () {
    return gulp.src([paths.js, "!" + paths.minJs], { base: "." })
        .pipe(concat(paths.concatJsDest))
        .pipe(uglify())
        .pipe(gulp.dest("."));
});

gulp.task("min:css", function () {
    return gulp.src([paths.css, "!" + paths.minCss])
        .pipe(concat(paths.concatCssDest))
        .pipe(cssmin())
        .pipe(gulp.dest("."));
});

gulp.task("min", ["min:js", "min:css"]);

Relevant code from _Layout.cshtml:

<environment names="Development2">
    <link rel="stylesheet" href="~/lib/normalize-css/normalize.css" />
    <link rel="stylesheet" href="~/css/NavigationMenu.css" />
    <script src="~/js/jquery.easymenu.js"></script>
</environment>

<environment names="Production, Development">
    <link rel="stylesheet" href="~/css/css/site.min.css" />
    <script src="~/js/site.min.js"></script>
</environment>

Solution

  • I have found the solution. I needed to update the first line of the gulpfile.js file to read

    /// <binding BeforeBuild='clean, min' Clean='clean' />