Skip to content

Instantly share code, notes, and snippets.

@thomasboyt
Last active December 11, 2015 03:27
Show Gist options
  • Save thomasboyt/a882260d1ce53375cf32 to your computer and use it in GitHub Desktop.
Save thomasboyt/a882260d1ce53375cf32 to your computer and use it in GitHub Desktop.
var webpack = require('webpack');
var path = require('path');
var _ = require('lodash');
var minimatch = require('minimatch');
function matchesPatterns(module, patterns) {
var modulePath = module.resource;
if (!modulePath) {
return false;
}
for (var i in patterns) {
var matched = minimatch(modulePath, patterns[i]);
if (matched) {
return true;
}
}
return false;
}
// Example usage:
//
// plugins: [
// createGlobChunk({
// name: 'vendor',
// filename: 'vendor.bundle.js',
//
// patterns: [
// './node_modules/**/*.js',
// './vendor/**/*.js'
// ]
// })
// ]
function createGlobChunk(opts) {
var patterns = opts.patterns.map(function(pattern) {
return path.resolve(process.cwd(), pattern);
});
var pluginOpts = _.merge({
minChunks: function(module) {
return matchesPatterns(module, patterns);
}
}, opts);
return new webpack.optimize.CommonsChunkPlugin(pluginOpts);
}
module.exports = createGlobChunk;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment