forked from BohdanTkachenko/webpack-split-by-path
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
129 lines (107 loc) · 3.5 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// Based on Split by Name Webpack Plugin – https://github.com/soundcloud/split-by-name-webpack-plugin
function regExpQuote(str) {
return (str + '').replace(/^[:!,]|[\\^$.*+?()[\]{}|\/]|(^[0-9a-fA-Fnrtuvx])|([\n\r\u2028\u2029])/g, '\\$&');
}
function SplitByPathPlugin(buckets, config) {
config = config || {};
config.ignore = config.ignore || [];
if (!Array.isArray(config.ignore)) {
config.ignore = [ config.ignore ];
}
config.ignore = config.ignore.map(function (item) {
if (item instanceof RegExp) {
return item;
}
return new RegExp('^' + regExpQuote(item));
});
this.ignore = config.ignore;
this.buckets = buckets.slice(0).map(function (bucket) {
if (!Array.isArray(bucket.path)) {
bucket.path = [ bucket.path ];
}
bucket.path = bucket.path.map(function (path) {
if (path instanceof RegExp) {
return path;
}
return new RegExp('^' + regExpQuote(path));
});
return bucket;
});
}
SplitByPathPlugin.prototype.apply = function(compiler) {
var buckets = this.buckets;
var ignore = this.ignore;
function findMatchingBucket(chunk) {
var match = null;
if (!chunk.userRequest) {
return match;
}
var userRequest = chunk.userRequest;
var lastIndex = userRequest.lastIndexOf('!');
if (lastIndex !== -1) {
userRequest = userRequest.substring(lastIndex + 1);
}
for (var i in ignore) {
if (ignore[i].test(userRequest)) {
return match;
}
}
buckets.some(function (bucket) {
return bucket.path.some(function (path) {
if (path.test(userRequest)) {
match = bucket;
return true;
}
});
});
return match;
}
compiler.plugin('compilation', function (compilation) {
var extraChunks = {};
// Find the chunk which was already created by this bucket.
// This is also the grossest function name I've written today.
function bucketToChunk(bucket) {
return extraChunks[bucket.name];
}
compilation.plugin('optimize-chunks', function (chunks) {
var addChunk = this.addChunk.bind(this);
chunks
// only parse the entry chunk
.filter(function (chunk) {
return chunk.entry && chunk.name;
})
.forEach(function (chunk) {
chunk.modules.slice().forEach(function (mod) {
var bucket = findMatchingBucket(mod),
newChunk;
if (!bucket) {
// it stays in the original bucket
return;
}
if (!(newChunk = bucketToChunk(bucket))) {
newChunk = extraChunks[bucket.name] = addChunk(bucket.name);
}
// add the module to the new chunk
newChunk.addModule(mod);
mod.addChunk(newChunk);
// remove it from the existing chunk
mod.removeChunk(chunk);
});
buckets
.map(bucketToChunk)
.filter(Boolean)
.concat(chunk)
.forEach(function (bucket, index, allChunks) { // allChunks = [bucket0, bucket1, .. bucketN, orig]
if (index) { // not the first one, they get the first chunk as a parent
bucket.parents = [allChunks[0]];
} else { // the first chunk, it gets the others as 'sub' chunks
bucket.chunks = allChunks.slice(1);
}
bucket.initial = true;
bucket.entry = !index;
});
});
});
});
};
module.exports = SplitByPathPlugin;