Skip to content

Commit c22cc9a

Browse files
author
Florens Verschelde
committed
task dependencies in json files
1 parent 3d95c4a commit c22cc9a

16 files changed

+390
-314
lines changed

README.md

+90-45
Original file line numberDiff line numberDiff line change
@@ -5,107 +5,152 @@ A collection of configurable [gulp](http://gulpjs.com/) tasks we use to build fr
55

66
- Flexible configuration
77
- Useful logs and system alerts
8-
- Built-in tasks: Sass with Autoprefixer, SVG sprites and JS minification
9-
- Easy to remove or edit built-in tasks, or write your own
10-
11-
*Requirements:*
12-
13-
- [Node.js](https://nodejs.org) >= 4.0
8+
- Use the built-in tasks (Sass with Autoprefixer, SVG sprites, and simple JS minification) or write your own
149

1510
*Table of contents:*
1611

1712
- [Installation and usage](#installation-and-usage)
18-
- [Configuring tasks](#configuring-tasks)
19-
- [How to write a new task](#how-to-write-a-new-task)
13+
- [Using tasks](#using-tasks)
14+
- [Enable system notifications](#enable-system-notifications)
2015
- Built-in tasks:
2116
- [sass](doc/task-sass.md) (Sass and Autoprefixer)
2217
- [svgsymbols](doc/task-svgsymbols.md) (SVG symbol sprites)
2318
- [jsconcat](doc/task-jsconcat.md) (Concatenate and minify JS scripts)
19+
- [Multiple builds per task](#multiple-builds-per-task)
20+
- [How to write a new task](#how-to-write-a-new-task)
2421

2522

2623
Installation and usage
2724
----------------------
2825

29-
Install:
26+
Requirements: [Node.js](https://nodejs.org) 4.x or above
27+
28+
Installing:
29+
30+
1. [Download a ZIP with the assets-builder scripts and config](https://github.com/gradientz/assets-builder/archive/master.zip) and unzip it in your projet directory.
31+
3. Change the config object in `gulpfile.js`.
32+
4. Finally, in a command prompt, go to your project dir and run:
33+
`npm install`
34+
35+
Usage:
36+
37+
- `npm run build`: build assets once
38+
- `npm run watch`: run in the background and build assets when source files are changed
39+
40+
41+
Using tasks
42+
-----------
43+
44+
To use one of the built-in tasks, you will need to do two things: 1) Configure the task and 2) Install its dependencies (if any).
45+
46+
### Task configuration
47+
48+
Configuration goes in your `gulpfile.js` and generally looks like this:
3049

31-
1. [Download a ZIP with the assets-builder scripts and config](https://github.com/gradientz/assets-builder/archive/master.zip),
32-
2. move its contents to your project dir,
33-
3. and modify the config object in `gulpfile.js` to suit your needs.
34-
4. Finally, in a command prompt, `cd` to your project dir and run `npm install`.
50+
```js
51+
require('./assets-builder')({
52+
taskname: {
53+
src: ['some/files/to/use/*.*', 'some/other/files/*.*'],
54+
dest: 'where/to/write/the/result',
55+
watch: true // will watch the 'src' files
56+
}
57+
})
58+
```
59+
60+
The `src`, `dest` and `watch` keys are common to all tasks. Some tasks may offer other options too. For the built-in tasks, those options are documented in [the doc folder](doc).
3561

36-
There are two commands than you can use:
62+
### Installing dependencies
3763

38-
- `npm run build` (build CSS/JS/SVG once)
39-
- `npm run watch` (build CSS/JS/SVG when files are changed)
64+
Task dependencies are *not* installed by default. The first time you try to run a task, with `npm run build`, you might see an error like this:
4065

41-
Important: if you’re using Git, don’t forget to add `node_modules` to your `.gitignore`, to avoid versioning the many files installed by npm.
66+
```
67+
[21:28:37] Error: missing dependencies for 'sass', 'jsconcat', 'svgsymbols'
68+
69+
To fix this, install missing dependencies:
70+
71+
npm install -D "gulp-autoprefixer@^3.1" \
72+
"gulp-sass@^2.3" \
73+
"gulp-uglify@^1.5" \
74+
"gulp-svgmin@^1.2.3" \
75+
"gulp-svg-symbols@^2.0.2"
76+
77+
```
4278

79+
You can run the provided command to install the missing dependencies. Then run `npm run build` again.
4380

44-
Configuring tasks
45-
-----------------
4681

47-
Config objects have the following properties:
82+
Enable system notifications
83+
---------------------------
4884

49-
- Required `src`: paths or glob patterns (string or array of strings).
50-
- Required `dest`: a single path (string).
51-
- Optional `watch`: either the value `true` to reuse the `src` config, or a different set of paths or glob patterns (string or array of strings).
52-
- Some tasks may offer additional options (see the next section for details).
85+
To enable system notifications, install the `node-notifier` package:
5386

54-
You can make multiple builds by providing an array of config objects, instead of a single config object:
87+
- Locally to the project: `npm install node-notifier`
88+
- Globally on your OS: `npm install -g node-notifier`
89+
90+
You could also add it to the `devDependencies` (`npm install -D node-notifier`), but that’s not ideal if you want to put this code as a build step on a server.
91+
92+
93+
Multiple builds per task
94+
------------------------
95+
96+
Each task can accept an array of config objects:
5597

5698
```js
5799
{
58-
…,
59100
sass: [
60-
{ src: 'assets/styles/main.scss', watch: 'assets/styles/**/*.scss',
61-
dest: 'public/css', browsers: ['last 3 versions', 'ie >= 11'] },
62-
{ src: 'assets/styles/other.scss', watch: false, outputStyle: 'compact',
63-
dest: 'public/css', browsers: ['last 3 versions', 'ie >= 11'] }
101+
{
102+
src: 'assets/styles/main.scss',
103+
watch: 'assets/styles/**/*.scss',
104+
dest: 'public/css',
105+
browsers: ['last 3 versions', 'ie >= 11']
106+
},
107+
{
108+
src: 'assets/styles/other.scss',
109+
dest: 'public/css',
110+
browsers: ['last 3 versions', 'ie >= 11'],
111+
outputStyle: 'compact'
112+
},
64113
]
65-
66114
}
67115
```
68116

69-
You can also share settings between objects, using an array-like syntax where string keys are shared between configs, and number keys represent individual build configs. This is equivalent to the previous example:
117+
You can also share settings between objects, with an array-like syntax. This is equivalent to the previous example:
70118

71119
```js
72120
{
73-
…,
74121
sass: {
122+
// base settings
75123
dest: 'public/css',
76124
browsers: ['last 3 versions', 'ie >= 11'],
125+
// builds with specific settings
77126
0: { src: 'assets/styles/main.scss', watch: 'assets/styles/**/*.scss' },
78-
1: { src: 'assets/styles/other.scss', watch: false, outputStyle: 'compact' }
127+
1: { src: 'assets/styles/other.scss', outputStyle: 'compact' }
79128
}
80-
81129
}
82130
```
83131

84132

85133
How to write a new task
86134
-----------------------
87135

88-
If you want to write a new or different task (for example, one that transpiles ES6 code with Babel and makes a JS bundle), you can `write a task script in `assets-builder/tasks`.
89-
90-
This script should export a function that takes a config object and does… what you want to do. See the existing tasks for examples!
136+
Write your own task script in `assets-builder/tasks`. This script should export a function that accepts a config object.
91137

92-
Here is a minimal example:
138+
### Minimal task example
93139

94140
```js
95141
/**
96142
* assets-builder/tasks/mytask.js
97143
*/
98144

99-
var gulp = require('gulp')
100-
var __ = require('./../taskutils.js')
101-
102-
// Specific
103-
var doSomething = require('gulp-do-something')
145+
const gulp = require('gulp')
146+
const tools = require('../tasktools.js')
147+
const doSomething = require('gulp-do-something')
104148

105149
module.exports = function mytask(conf) {
106150
return gulp.src(conf.src) // take some files
107-
.pipe(__.logerrors()) // tell gulp to show errors and continue
151+
.pipe(tools.errors()) // tell gulp to show errors and continue
108152
.pipe(doSomething()) // use a gulp plugin to transform content
153+
.pipe(tools.size(conf.dest)) // log resulting file path/names and size
109154
.pipe(gulp.dest(conf.dest)) // write resulting files to destination
110155
}
111156
```

assets-builder/helpers/load.js

-46
This file was deleted.

assets-builder/helpers/missing.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict'
22

3-
var glob = require('glob')
4-
var notify = require('./notify.js')
3+
const glob = require('glob')
4+
const notify = require('./notify.js')
55

66
/**
77
* Check that a glob patterns actually matches at least one file,
@@ -10,12 +10,12 @@ var notify = require('./notify.js')
1010
* @param {string} taskId
1111
*/
1212
module.exports = function missing(pattern, taskId) {
13-
glob(pattern, function(err, found) {
13+
glob(pattern, (err, found) => {
1414
if (err) notify(err)
15-
if (found.length === 0) {
15+
else if (found.length === 0) {
1616
notify({
17-
title: 'Missing \'' + taskId + '\' sources',
18-
message: 'Nothing at \'' + pattern + '\''
17+
title: 'Warning: missing \'' + taskId + '\' sources: ' + pattern,
18+
warn: true
1919
})
2020
}
2121
})

0 commit comments

Comments
 (0)