Skip to content

Instantly share code, notes, and snippets.

@andywer
Last active July 15, 2017 11:42
Show Gist options
  • Select an option

  • Save andywer/50e4dca95d816c8bd5cac28ea9b3b3a5 to your computer and use it in GitHub Desktop.

Select an option

Save andywer/50e4dca95d816c8bd5cac28ea9b3b3a5 to your computer and use it in GitHub Desktop.
@webpack-blocks/happypack - Concept
const { group } = require('@webpack-blocks/core')
module.exports = happypack
function happypack (blocks) {
return group(blocks.map(happyfyBlock))
}
/**
* Returns a new block wrapping `block` that creates a happypack loader config.
*/
function happyfyBlock (block) {
const happyBlock = happyfySetter(block)
const pre = toArray(block.pre)
const post = toArray(block.post).map(postHook => happyfySetter(postHook))
return Object.assign(happyBlock, { pre, post })
}
/**
* Takes a block or a post hook and returns a wrapping function that creates a happypack loader config.
*/
function happyfySetter (setter) {
return (context, config) => happyfyConfig(setter(context, config), context)
}
/**
* Transforms a non-happypack loader config into a happypack loader config.
*/
function happyfyConfig (configSnippet, context) {
// TODO: If `module.loaders` is set then transform according to https://github.com/amireh/happypack#using-multiple-instances
}
/**
* Takes an array, a function or something falsy and returns the array, the
* function wrapped in an array or an empty array, respectively.
*/
function toArray (value) {
if (value) {
return Array.isArray(value) ? value : [ value ]
} else {
return []
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment