简体   繁体   中英

Compiling LESS using gulp-useref and gulp-less?

I'm trying to compile my LESS files using the gulp-useref plugin, but it is as if the gulp-less plugin never outputs a compiled version of my LESS files in the pipeline. The LESS files get concatenated with the other CSS files without being compiled.

I tried compiling my LESS separately using only gulp-less and it is working well, but I have no idea why it seems to conflict with the gulp-useref plugin.

Here is my gulpfile :

var gulp = require('gulp');
var rm = require('gulp-rimraf');
var gulpif = require('gulp-if');
var less = require('gulp-less');
var cssmin = require('gulp-minify-css');
var useref = require('gulp-useref');

gulp.task('clean', function () {
    return gulp.src(['public'])
        .pipe(rm({force: true}));
});

gulp.task('refs', ['clean'], function () {
    var assets = useref.assets({searchPath: '.'});

    return gulp.src(['templates/**/*.html'])
        .pipe(assets)
        .pipe(gulpif('*.less', less()))
        .pipe(gulpif('*.css', cssmin()))
        .pipe(assets.restore())
        .pipe(gulpif('*.html', useref()))
        .pipe(gulp.dest('public/templates'));
});

Thanks in advance for answers!

As far as i understand is what you try not possible.

with:

<!-- build:css css/combined.css -->
<link href="css/one.css" rel="stylesheet">
<link href="css/two.css" rel="stylesheet">
<link rel="stylesheet/less" type="text/less" href="less/website.less" />
<!-- endbuild -->

useref.assets creats a stream for css/combined.css which contains the content of css/one.css, css/two.css less/website.less. Because of the name of your stream is css/combined.css only the .pipe(gulpif('*.css', cssmin())) matches.

If you use .pipe(gulpif('*.css', less())) , the less compiler will compile the content of all three files into css/combined.css.

So you can use:

    .pipe(gulpif('*.css', less()))
    .pipe(gulpif('*.css', cssmin()))

The above compiles both your *.css and *.less files with the Less compiler (cause Less can compile css, the result may as expected)

Try to add custom type

var assets = useref.assets({
   searchPath: '.', 
   types: ['js', 'css', 'less']
});
var gulp = require('gulp'); 
var sass = require('gulp-sass');
var concat= require('gulp-concat');
var minifyCss  = require('gulp-cssnano');

var uglify = require('gulp-uglify');
var pump = require('pump');
var useref = require('gulp-useref');

gulp.task('sass', function(){
  gulp.src(['css/**/*.scss' ,'css/**/*.css' ])
    .pipe(sass()) // Using gulp-sass
    .pipe(minifyCss())  
    .pipe(concat('all.css')) 
    .pipe(gulp.dest('build/css'))
});


gulp.task('compressJS', function (cb) {

 return gulp.src(['lib/*.js'])
        .pipe(concat('concat.js'))
        .pipe(uglify())
        .pipe(gulp.dest('build/js'))


});
var gulp = require('gulp'),
  useref = require('gulp-useref');

gulp.task('default',  ['sass'] , function () {

 //нема

  return gulp.src('*.html')
    .pipe(useref())
    .pipe(gulp.dest('dist')); 
});

then include file all.css genereted by gulp.task('sass')

<link rel="stylesheet" href="css/all.css" >

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM