Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any way to compile less files with @imports and concatenate them with Gulp, all in the same stream?

I`m triyng this in my gulpfile.js:

gulp.task('buildStyles', function() { 
  return gulp.src([
    './styles/less/main.less',
    './styles/less/widgets/widgets.less',
    './styles/less/pages/pages.less',
    './styles/less/themes/default.less',
    './styles/less/rtl/rtl.less'
  ])
  .pipe(plumber())
  .pipe(sourcemaps.init())
  .pipe(less())
  .pipe(concat('allmin.css'))
  .pipe(sourcemaps.write('./maps'))
  .pipe(gulp.dest('./dest'));
});

But I always get a message like:

$ gulp buildStyles
[18:46:31] Using gulpfile /Library/WebServer/Documents/qjt2015/trunk/gulpfile.js
[18:46:31] Starting 'buildStyles'...
gulp-sourcemap-write: source file not found:/Library/WebServer/Documents/qjt2015/trunk/styles/less/support-tickets.less
gulp-sourcemap-write: source file not found:/Library/WebServer/Documents/qjt2015/trunk/styles/less/comments.less
gulp-sourcemap-write: source file not found:/Library/WebServer/Documents/qjt2015/trunk/styles/less/article-comments.less
gulp-sourcemap-write: source file not found:/Library/WebServer/Documents/qjt2015/trunk/styles/less/threads.less
gulp-sourcemap-write: source file not found:/Library/WebServer/Documents/qjt2015/trunk/styles/less/chat.less

I`m using @imports within all my less files, and the references are those that appear as not found in the message. For example, my widgets.less file is as follows:

// ### Bootstrap's variables andmixins
@import "../../../bower_components/bootstrap/less/variables.less";
@import "../../../bower_components/bootstrap/less/mixins.less";
@import '../variables.less';
@import '../mixins.less';

@import './support-tickets.less';
@import './comments.less';
@import './article-comments.less';
@import './threads.less';
@import './chat.less';

Am I missing something? In advance, thanks for the help!

like image 710
Gustavo Leindecker Avatar asked Sep 29 '22 21:09

Gustavo Leindecker


1 Answers

You simply need to specify the paths option for gulp-less to tell it where it should start searching for files specified with @import directives.

For e.g., in widgets.less, its trying to find

  • support-tickets.less
  • comments.less
  • article-comments.less
  • etc..

    in Library/WebServer/Documents/qjt2015/trunk/styles/less/ (see the error message that you posted) but I believe it should be in Library/WebServer/Documents/qjt2015/trunk/styles/less/widgets/ (relative to your widgets.less file)

Simply set the path(s) like so:

gulp.task('buildStyles', function() { 
  return gulp.src([
     // ...
  ])
  // ... other gulp tasks here
  .pipe(less({
     paths: [
       '/Library/WebServer/Documents/qjt2015/trunk/styles/less/',
       '/Library/WebServer/Documents/qjt2015/trunk/styles/less/widgets/',
       '/Library/WebServer/Documents/qjt2015/trunk/styles/less/pages/',
       // add additional paths here for less to find imports in
       // less will tell you which import files it can't find, so simply tell it where to find them.

     ]
  ))
  .pipe(concat('allmin.css'))
  // ...
});

```

like image 94
Hafiz Ismail Avatar answered Oct 10 '22 04:10

Hafiz Ismail