Created
February 20, 2017 13:15
-
-
Save fenos/a3d69ff5060948700f9e0ce5ab2eda88 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { group } = require('@webpack-blocks/core') | |
const HappyPack = require('happypack'); | |
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) { | |
if (configSnippet.module && configSnippet.module.loaders) { | |
const plugins = []; | |
const loaders = configSnippet.module.loaders.map((loader, id) => { | |
const happypackplugin = new HappyPack({ | |
id: `loader${id}`, | |
loaders: loader.loaders, | |
}); | |
plugins.push(happypackplugin); | |
return { | |
test: loader.test, | |
loader: `happypack/loader?id=loader${id}`, | |
exclude: loader.exclude || [], | |
} | |
}); | |
return { | |
module: { | |
loaders, | |
}, | |
plugins, | |
} | |
} | |
return {}; | |
} | |
/** | |
* 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