1 | var multimatch = require('multimatch')
|
2 | var mixy = require('mixy')
|
3 | var objectEach = mixy.each
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 | module.exports = function (originalGroupsMap, common) {
|
10 |
|
11 | var commonMap = Object.create(null)
|
12 |
|
13 | var bundleMap = Object.create(null)
|
14 |
|
15 |
|
16 | var groupsMap = Object.create(null)
|
17 |
|
18 | var groups = Object.keys(originalGroupsMap)
|
19 |
|
20 |
|
21 | ;[].concat(common).forEach(function (o) {
|
22 | if (!o) return
|
23 | if (typeof o === 'string') {
|
24 | o = { output: o }
|
25 | }
|
26 | if (!o.output) return
|
27 | var targets
|
28 | if (typeof o.filter === 'function') {
|
29 | targets = [].concat(o.filter(groups))
|
30 | } else {
|
31 | targets = o.filter ? multimatch(groups, o.filter) : groups.slice()
|
32 | }
|
33 | if (targets.length) {
|
34 | commonMap[o.output] = targets
|
35 | targets.forEach(function (bundleID) {
|
36 | bundleMap[bundleID] = bundleMap[bundleID] || []
|
37 | bundleMap[bundleID].push(o.output)
|
38 | })
|
39 | }
|
40 | })
|
41 |
|
42 | objectEach(commonMap, function (bundles, commonID) {
|
43 | var commonModules = getIntersection(bundles.map(function (bundleID) {
|
44 | return originalGroupsMap[bundleID]
|
45 | }))
|
46 | groupsMap[commonID] = commonModules
|
47 | })
|
48 | groups.forEach(function (bundleID) {
|
49 | groupsMap[bundleID] = originalGroupsMap[bundleID].filter(function (moduleID) {
|
50 | return !isCommonModule(bundleMap[bundleID], moduleID)
|
51 | })
|
52 | })
|
53 |
|
54 | function isCommonModule(commonBundles, moduleID) {
|
55 | return commonBundles && commonBundles.some(function (bundleID) {
|
56 | return groupsMap[bundleID].indexOf(moduleID) > -1
|
57 | })
|
58 | }
|
59 |
|
60 | return groupsMap
|
61 | }
|
62 |
|
63 | function getIntersection(arr) {
|
64 | var counts = {}
|
65 | var len = arr.length
|
66 | var ret = []
|
67 | arr.forEach(function (ar) {
|
68 | ar.forEach(function (e) {
|
69 | counts[e] = counts[e] || 0
|
70 | if (++counts[e] === len) {
|
71 | ret.push(e)
|
72 | }
|
73 | })
|
74 | })
|
75 | return ret
|
76 | }
|
77 |
|