Last active
December 11, 2015 03:27
-
-
Save thomasboyt/a882260d1ce53375cf32 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
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