Skip to content

Commit

Permalink
refactor(config): simplify(?) syntax for multiple builds
Browse files Browse the repository at this point in the history
  • Loading branch information
Florens Verschelde committed Jul 18, 2016
1 parent a65e183 commit 1650ada
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 39 deletions.
41 changes: 18 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,42 +121,37 @@ Task script: `gulp/builders/svgsprite.js`

### Several builds per task

Each task can accept several config objects, using the `builds` property. You can provide an array of complete config objects in the `builds` property, or let it inherit from the task’s config:
Each task can accept an array of config objects, instead of a single config object:

```js
{
…,
sass: {
dest: 'public/css',
browsers: ['last 3 versions', 'ie >= 11'],
builds: [
{ src: 'assets/styles/main.scss', watch: 'assets/styles/**/*.scss' },
{ src: 'assets/styles/other.scss', outputStyle: 'compact' }
]
},
sass: [
{ src: 'assets/styles/main.scss', watch: 'assets/styles/**/*.scss',
dest: 'public/css', browsers: ['last 3 versions', 'ie >= 11'] },
{ src: 'assets/styles/other.scss', outputStyle: 'compact',
dest: 'public/css', browsers: ['last 3 versions', 'ie >= 11'] }
]
}
```

is equivalent to:
You can also share settings between objects, using an array-like syntax where:

- text keys are shared between configs
- number keys represent individual build configs

The following example is equivalent to the previous example:

```js
{
…,
sass: {
builds: [
{ src: 'assets/styles/main.scss',
watch: 'assets/styles/**/*.scss',
dest: 'public/css',
browsers: ['last 3 versions', 'ie >= 11']
},
{ src: 'assets/styles/other.scss',
dest: 'public/css',
browsers: ['last 3 versions', 'ie >= 11'],
outputStyle: 'compact',
}
]
},
dest: 'public/css',
browsers: ['last 3 versions', 'ie >= 11'],
0: { src: 'assets/styles/main.scss', watch: 'assets/styles/**/*.scss' },
1: { src: 'assets/styles/other.scss', outputStyle: 'compact' }
}
}
```
Expand Down
42 changes: 26 additions & 16 deletions gulp/helpers/normalize.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,35 @@ var notify = require('./notify.js')
* @returns {Array}
*/
module.exports = function(baseConfig) {
var base = {}
var list = []
// Make full config objects if we have several build entries
if (typeof baseConfig === 'object' && Array.isArray(baseConfig.builds)) {
var common = Object.keys(baseConfig).filter(function(x){return x!=='builds'})
baseConfig.builds.forEach(function(build) {
var config = {}
common.concat(Object.keys(build)).forEach(function(key) {
config[key] = key in build ? build[key] : baseConfig[key]
})
list.push(config)
})
// Number or number-like keys are separate builds, other keys are shared config
Object.keys(baseConfig).forEach(function(key) {
var value = baseConfig[key]
if (isNaN(Number(key))) {
base[key] = value
}
else if (typeof value === 'object') {
list.push(value)
}
})
// Only one build config
if (list.length === 0) {
list.push(base)
}
// Or add base values to individual configs
else {
list.push(baseConfig)
list = list.map(function(config) {
Object.keys(base).forEach(function(key) {
if (!(key in config)) config[key] = base[key]
})
return config
})
}
// Normalize the src and watch properties
var normalized = list.map(function(config) {
if (typeof config !== 'object') return config
var normalized = list.filter(function(config) {
return typeof config === 'object'
}).map(function(config) {
config.src = [].concat(config.src).filter(function(x) {
return typeof x === 'string' && x.trim() !== ''
})
Expand All @@ -35,10 +46,9 @@ module.exports = function(baseConfig) {
}
return config
})
// And filter final configs objects
// Finally, only keep valid configs objects
return normalized.filter(function(config) {
var ok = typeof config === 'object' &&
typeof config.dest === 'string' &&
var ok = typeof config.dest === 'string' &&
Array.isArray(config.src) &&
config.src.length > 0
if (!ok) notify({
Expand Down

0 comments on commit 1650ada

Please sign in to comment.