Incremental gulp less build

后端 未结 2 481
遥遥无期
遥遥无期 2021-02-02 00:56

In my office we are using gulp to build our less files. I wanted to improve the build task as it took over a second to build on a large project we recently worked on. The idea w

2条回答
  •  孤城傲影
    2021-02-02 01:16

    So when I want to do incremental builds in gulp I do it by abstracting out the inner process of the gulp task, this way I don't have to worry about keeping a cache.

    // Create a function that does just the processing
    var runCompile = function( src, dest, opts ){
      return gulp.src( src )
        .pipe(less( opts ))
        .pipe(gulp.dest( dest ));
    };
    
    // Leverage the function to create the task
    gulp.task( 'less', function(){
      return runCompile( fileGlob, 'output', {} );
    });
    
    // Use it again in the watch task
    gulp.task( 'less:watch', function(){
      return gulp.watch( fileGlob )
        .on( "change", function( event ){
          // might need to play with the dest dir here
          return runCompile( event.path, 'output', {} );
        });
    });
    

    This works great for me and I use this pattern all over my gulp tasks. However I have noticed that sometime gulp will squash paths during the watch "on change" if it gets a single file. In that case I do the path manipulation my self, something like path.dirname(srcPath.replace( srcDir, outputDir )) as the argument dest for the runCompile function.

    Edit: Just realized this probably isn't going to solve your "lost variables" problem. I don't have anything off the top of my head to solve that one since I organize my LESS files with a heavy use of imports, so every file that would need a set of variables would have an import statement insuring they are there.

提交回复
热议问题