Last active
August 12, 2017 06:53
-
-
Save loopmode/0ede29b27067f516e4fa82799e03514b to your computer and use it in GitHub Desktop.
desperate-commons-chunks-attempt
This file contains hidden or 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 gpCommons = [ | |
{name: 'shared', path: 'src/shared', props: {async: 'common.shared'}}, | |
{name: 'stores', path: 'src/stores', props: {async: 'common.stores'}}, | |
{name: 'mediaplayer', path: 'src/mediaplayer', props: {async: 'common.mediaplayer'}}, | |
{name: 'cms', path: 'src/cms', props: {async: 'common.cms'}}, | |
{name: 'ordering', path: ['src/basket', 'src/footage-request', 'src/footage-orders'], props: {async: 'common.ordering'}}, | |
]; | |
const mpCommons = gpCommons.concat([ | |
{name: 'licensing', path: 'src/clipbin', props: {async: 'common.licensing'}}, | |
]); | |
const vendors = [{name: 'vendor', path: 'node_modules', props: {async: false}}]; | |
module.exports = { | |
gp: gpCommons.concat(vendors), | |
mp: mpCommons.concat(vendors) | |
}; |
This file contains hidden or 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 webpack = require('webpack'); | |
// names should be an array of entry names, e.g. ['main'] | |
function createCommonsChunks(names, definitions) { | |
if (!names) throw new Error('Please specify names - an array of the entry chunk names'); | |
if (!definitions) throw new Error('Please specify definitions - see commons.definitions.js'); | |
return definitions.map((currentDef, idx) => { | |
// keep track of all previous names because the following plugins must extract from them | |
names.push(currentDef.name); | |
const plugin = new webpack.optimize.CommonsChunkPlugin(Object.assign({}, currentDef.props, { | |
names: names.reduce(unique, []), | |
minChunks: function getMinChunks(module) { | |
// keep track of all next definitions because each instance must pass-though | |
return definitions.slice(idx).some(def => { | |
if (Array.isArray(def.path)) { | |
return def.path.some(p => isModuleOf(module, p)); | |
} | |
else { | |
return isModuleOf(module, def.path); | |
} | |
}); | |
} | |
})); | |
if (currentDef.props && currentDef.props.async) names.push(currentDef.props.async); | |
return plugin; | |
}); | |
} | |
function isModuleOf(module, match) { | |
const {context} = module; | |
if (typeof context !== 'string') { | |
return false; | |
} | |
const isMatch = context && context.replace(/\\/g, '/').indexOf(match) > -1; | |
// console.log('>> isModuleOf', {context, match, isMatch}); | |
return isMatch; | |
} | |
function unique(result, entry) { | |
if (result.indexOf(entry) > -1) return result; | |
result.push(entry); | |
return result; | |
} | |
module.exports = createCommonsChunks; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment