Compiling LESS using gulp-useref and gulp-less?
Asked Answered
F

3

7

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!

Feltner answered 23/12, 2014 at 21:1 Comment(0)
E
3

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)

Ewer answered 27/12, 2014 at 21:22 Comment(0)
V
0

Try to add custom type

var assets = useref.assets({
   searchPath: '.', 
   types: ['js', 'css', 'less']
});
Venesection answered 25/12, 2014 at 22:21 Comment(0)
R
0
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" >
Redemptioner answered 20/12, 2016 at 15:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.