Spring + Gulp useref

I am having problem using Gulp useref with spring mvc framework.

I created my jsp pages using useref format to combine and minify assets.

JSP page            

My gulpfile.js

var gulp = require('gulp');
var useref = require('gulp-useref');
var gulpif = require('gulp-if');
var uglify = require('gulp-uglify');
var minifyCss = require('gulp-minify-css');

const ROOT_DIR = 'src/main/webapp';
const TEMP_DIR = './tmp';

const ROOT_DIR = 'src/main/webapp';
const TEMP_DIR = './tmp';
// replace <script> and <link> references with minified versions
gulp.task('html', function () {
    var assets = useref.assets({
       searchPath: ROOT_DIR
    });

    return gulp.src(ROOT_DIR + '/**/layout*.jsp')
        .pipe(assets)
        // concatenate and minify JS
        .pipe(gulpif('*.js', uglify({})))
        // concatenate and minify CSS
        .pipe(gulpif('*.css', minifyCss()))
        .pipe(assets.restore())
        .pipe(useref())
        .pipe(gulp.dest(TEMP_DIR));
});

gulp.task('default', ['html']);

      

when I run gulp, useref replaces the script and css html references correctly, but unfortunately it doesn't generate mini versions of assets.

I also tried to debug Gulp and useref by adding a log statement to the useref index file and printing globs var:

this is from the useref index file

// Get relative file paths and join with search paths to send to vinyl-fs
globs = filepaths
    .filter(isRelativeUrl)
    .map(function (filepath) {
        if (opts.transformPath) {
            filepath = opts.transformPath(filepath);
        }
        if (searchPaths.length) {
            return searchPaths.map(function (searchPath) {
                return getSearchPaths(file.cwd, searchPath, filepath);
            });
        } else {
            return path.join(file.base, filepath);
        }
    });

      

Output on my console:

[ [ '/Users/user/Documents/projects/test/src/main/webapp/${contextPath}/includes/css/combined.css' ],
  [ '/Users/user/Documents/projects/test/src/main/webapp/${contextPath}/includes/css/typography.css' ] ]

      

Unfortunately, $ {contextPath} does not have to be part of the resource path.

In the previous version of the Gulp file, I used the Gulp package and it worked fine:

// minify JS and CSS files
gulp.task('bundle', bundle(ROOT_DIR + '/**/layout*.jsp', {
        appDir: ROOT_DIR,
        buildDir: TEMP_DIR,
        minify: true
    })
);

      

Is there something I am missing or doing wrong?

+3


source to share


2 answers


I'm sure it ${contextPath}

doesn't come from gulp-useref

. It looks like a template variable. I am assuming this is coming from your code somehow. If so, it needs to be inserted before it gets to useref

, or you can use gulp-replace

gulp to insert it from the task.



0


source


In your jsp files:

<head>
<title><fmt:message key="application.name"/> - ${title}</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>

<!-- build:css ${contextPath}/includes/css/layout_ana.min.css-->
<link rel="stylesheet" href="includes/css/combined.css" type="text/css"/>
<link rel="stylesheet" href="includes/css/typography.css" type="text/css"/>
<!-- endbuild -->

<link rel="icon" href="${contextPath}/favicon.ico" type="image/x-icon"/>
<link rel="shortcut icon" href="${contextPath}/favicon.ico" type="image/x-icon"/>

<!--[if lte IE 7]>
<link rel="stylesheet" href="${contextPath}/includes/css/ie6and7.css" type="text/css"/>
<![endif]-->

<!--[if IE]>
<link rel="stylesheet" href="${contextPath}/includes/css/ie.css" type="text/css"/>
<![endif]-->

      



In your gulp file:

// replace <script> and <link> references with minified versions
gulp.task('html', ['clean'], function () {
var assets = useref.assets({
    searchPath: ROOT_DIR
});

return gulp.src(ROOT_DIR + '/**/layout*.jsp')
    .pipe(assets)
    // concatenate and minify JS
    .pipe(gulpif('*.js', uglify({})))
    // concatenate and minify CSS
    .pipe(gulpif('*.css', minifyCss()))
    .pipe(assets.restore())
    .pipe(useref())
    .pipe(debug({title: 'USEREF ASSET:'}))
    .pipe(gulp.dest(TEMP_DIR));
});

      

0


source







All Articles