Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 503
joelrobichaud Avatar asked Dec 23 '14 21:12

joelrobichaud


People also ask

How to compile less files to CSS using gulp?

Gulp-Less is a Gulp plugin made specifically for compiling Less files to CSS. It provides us with a Less compiler. Which you can easily customize to fit your needs. First, create a file at the root of your project directory and name it gulpfile.js . It is how Gulp expects you to organize your tasks, so don’t name it differently.

Why should I use VSCode over gulp?

Even further, VSCode lets you define keyboard shortcuts for running tasks so that typing a command or clicking on Tasks won’t be a pain for you anymore. Note that running gulp default will keep running and watching files until you close the editor / command-line interpreter.

What is the use of gulp?

Gulp is generally known as build tool where we define single or multiple tasks at a time which help us to perform multiple activities at a time There are thousands of npm packages available on npm one of the important package we will try to use today is GULP

What is the use of NPM gulp?

Gulp is generally known as build tool where we define single or multiple tasks at a time which help us to perform multiple activities at a time There are thousands of npm packages available on npm one of the important package we will try to use today is GULP We need to install node.js and then we will install npm after then follow these steps :-


1 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)

like image 97
Bass Jobsen Avatar answered Sep 18 '22 18:09

Bass Jobsen