Skip to content

Commit ff49706

Browse files
author
Florens Verschelde
committed
feat: add a less task
1 parent c22cc9a commit ff49706

File tree

8 files changed

+111
-21
lines changed

8 files changed

+111
-21
lines changed

README.md

+6-5
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ A collection of configurable [gulp](http://gulpjs.com/) tasks we use to build fr
1313
- [Using tasks](#using-tasks)
1414
- [Enable system notifications](#enable-system-notifications)
1515
- Built-in tasks:
16+
- [less](doc/task-less.md) (LESS and Autoprefixer)
1617
- [sass](doc/task-sass.md) (Sass and Autoprefixer)
1718
- [svgsymbols](doc/task-svgsymbols.md) (SVG symbol sprites)
1819
- [jsconcat](doc/task-jsconcat.md) (Concatenate and minify JS scripts)
@@ -146,11 +147,11 @@ const gulp = require('gulp')
146147
const tools = require('../tasktools.js')
147148
const doSomething = require('gulp-do-something')
148149

149-
module.exports = function mytask(conf) {
150-
return gulp.src(conf.src) // take some files
151-
.pipe(tools.errors()) // tell gulp to show errors and continue
152-
.pipe(doSomething()) // use a gulp plugin to transform content
150+
module.exports = function mytaskBuilder(conf) {
151+
return gulp.src(conf.src) // take some files
152+
.pipe(tools.errors()) // tell gulp to show errors and continue
153+
.pipe(doSomething()) // use a gulp plugin to transform content
153154
.pipe(tools.size(conf.dest)) // log resulting file path/names and size
154-
.pipe(gulp.dest(conf.dest)) // write resulting files to destination
155+
.pipe(gulp.dest(conf.dest)) // write resulting files to destination
155156
}
156157
```

assets-builder/tasks/jsconcat.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,14 @@ const uglify = require('gulp-uglify')
1717
*/
1818
module.exports = function jsconcatBuilder(conf) {
1919
// Opt-out of minification with any falsy value
20-
const minify = 'minify' in conf ? Boolean(conf.minify) : true
20+
const doMinify = 'minify' in conf ? Boolean(conf.minify) : true
2121
const dest = path.parse(conf.dest)
2222

2323
return gulp.src(conf.src)
2424
.pipe( tools.errors() )
2525
.pipe( tools.sourcemap.init() )
2626
.pipe( tools.concat(dest.base) )
27-
.pipe( tools.if(minify, uglify()) )
27+
.pipe( tools.if(doMinify, uglify()) )
2828
.pipe( tools.size(dest.dir) )
2929
.pipe( tools.sourcemap.write('.') )
3030
.pipe( gulp.dest(dest.dir) )

assets-builder/tasks/less.js

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**
2+
* @file LESS + Autoprefixer
3+
*/
4+
'use strict'
5+
6+
const gulp = require('gulp')
7+
const path = require('path')
8+
const less = require('gulp-less')
9+
const tools = require('../tasktools.js')
10+
const autoprefixer = require('gulp-autoprefixer');
11+
12+
/**
13+
* Build one or more LESS stylesheets
14+
* @param {Object} conf
15+
* @property {Array|string} conf.src - glob patterns of LESS files to build
16+
* @property {string} conf.dest - output folder or file name
17+
* @property {string} conf.browsers - Autoprefixer browser compat
18+
* @property {Array} conf.includePaths - Folders to look for '@import's in
19+
* @returns {*}
20+
*/
21+
module.exports = function lessBuilder(conf) {
22+
const prefixOptions = {}
23+
const lessOptions = {}
24+
if (conf.browsers) prefixOptions.browsers = conf.browsers
25+
if (conf.includePaths) lessOptions.paths = conf.includePaths
26+
27+
const dest = path.parse(conf.dest)
28+
const doConcat = dest.ext === '.css'
29+
const file = doConcat ? dest.base : 'all.css'
30+
const dir = doConcat ? dest.dir : conf.dest
31+
32+
return gulp.src( conf.src )
33+
.pipe( tools.errors() )
34+
.pipe( tools.sourcemap.init() )
35+
.pipe( less(lessOptions) )
36+
.pipe( autoprefixer(prefixOptions) )
37+
.pipe( tools.if(doConcat, tools.concat(file)) )
38+
.pipe( tools.size(dir) )
39+
.pipe( tools.sourcemap.write('.') )
40+
.pipe( gulp.dest(dir) )
41+
}

assets-builder/tasks/less.json

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"dependencies": {
3+
"gulp-less": "^3.3"
4+
}
5+
}

assets-builder/tasks/sass.js

+18-10
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
'use strict'
55

66
const gulp = require('gulp')
7+
const path = require('path')
78
const sass = require('gulp-sass')
89
const tools = require('../tasktools.js')
910
const autoprefixer = require('gulp-autoprefixer');
@@ -12,23 +13,30 @@ const autoprefixer = require('gulp-autoprefixer');
1213
* Build one or more Sass stylesheets
1314
* @param {Object} conf
1415
* @property {Array|string} conf.src - glob patterns of Sass files to build
15-
* @property {string} conf.dest - output file name
16+
* @property {string} conf.dest - output folder or file name
1617
* @property {string} conf.browsers - Autoprefixer browser compat
18+
* @property {string} conf.outputStyle - Sass output style
19+
* @property {Array} conf.includePaths - Folders to look for '@import's in
1720
* @returns {*}
1821
*/
1922
module.exports = function sassBuilder(conf) {
20-
let sassOpts = {
21-
outputStyle: conf.outputStyle || 'compressed',
22-
includePaths: conf.includePaths || []
23-
}
24-
let prefixOpts = conf.browsers ? {browsers: conf.browsers} : {}
23+
const prefixOptions = {}
24+
const sassOptions = {outputStyle: conf.outputStyle || 'compressed'}
25+
if (conf.browsers) prefixOptions.browsers = conf.browsers
26+
if (conf.includePaths) sassOptions.paths = conf.includePaths
27+
28+
const dest = path.parse(conf.dest)
29+
const doConcat = dest.ext === '.css'
30+
const file = doConcat ? dest.base : 'all.css'
31+
const dir = doConcat ? dest.dir : conf.dest
2532

2633
return gulp.src( conf.src )
2734
.pipe( tools.errors() )
2835
.pipe( tools.sourcemap.init() )
29-
.pipe( sass(sassOpts) )
30-
.pipe( autoprefixer(prefixOpts) )
31-
.pipe( tools.size(conf.dest) )
36+
.pipe( sass(sassOptions) )
37+
.pipe( autoprefixer(prefixOptions) )
38+
.pipe( tools.if(doConcat, tools.concat(file)) )
39+
.pipe( tools.size(dir) )
3240
.pipe( tools.sourcemap.write('.') )
33-
.pipe( gulp.dest(conf.dest) )
41+
.pipe( gulp.dest(dir) )
3442
}

doc/task-less.md

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Task: compile LESS with Autoprefixer
2+
3+
- Config key: `less`
4+
- Source: `assets-builder/tasks/less.js`
5+
6+
Compile with gulp-less and run through Autoprefixer. Optionally concatenates files.
7+
8+
## Options
9+
10+
```js
11+
{
12+
// (Required) Source can be a single Sass stylesheet
13+
// or pattern, or an array of paths or patterns
14+
src: 'assets/styles/*.less',
15+
16+
// (Required) Destination can be a folder name, or a filename ending in '.css'.
17+
// The main difference is when building more than one Sass stylesheet.
18+
// - Filename ending in '.css': the result will be concatenated
19+
// - Otherwise: original filenames are used, and put in the destination folder.
20+
dest: 'public/css',
21+
22+
// (Optional) File patterns to watch for changes
23+
watch: 'assets/styles/**/*.less',
24+
25+
// (Optional) Autoprefixer: target browsers
26+
browsers: ['last 3 versions'],
27+
28+
// (Optional) LESS include paths (for resolving @import)
29+
includePaths: ['node_modules']
30+
}
31+
```

doc/task-sass.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@ Runs node-sass and autoprefixer.
1313
// or pattern, or an array of paths or patterns
1414
src: 'assets/styles/*.scss',
1515

16-
// (Required) Destination must be a folder
16+
// (Required) Destination can be a folder name, or a filename ending in '.css'.
17+
// The main difference is when building more than one Sass stylesheet.
18+
// - Filename ending in '.css': the result will be concatenated
19+
// - Otherwise: original filenames are used, and put in the destination folder.
1720
dest: 'public/css',
1821

1922
// (Optional) File patterns to watch for changes

gulpfile.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,15 @@ require('./assets-builder')({
99
// If you want to use the built-in 'sass', 'jsconcat' or 'svgsymbols'
1010
// tasks, remove the '_' and adapt the configs to your folder structure.
1111

12-
sass: {
12+
_sass: {
1313
src: 'source/styles/*.scss',
1414
watch: 'source/styles/**/*.scss',
1515
dest: 'public/css',
16+
includePaths: ['node_modules'],
1617
browsers: ['last 3 versions', 'ie >= 11', 'ios >= 9', 'android >= 5']
1718
},
1819

19-
jsconcat: {
20+
_jsconcat: {
2021
src: [
2122
'node_modules/jquery/dist/jquery.slim.min.js',
2223
'source/scripts/*.js'
@@ -25,7 +26,7 @@ require('./assets-builder')({
2526
watch: true // watch the src files
2627
},
2728

28-
svgsymbols: [
29+
_svgsymbols: [
2930
{
3031
src: 'source/icons/critical/*.svg',
3132
dest: 'public/svg/critical.svg',

0 commit comments

Comments
 (0)