-
Notifications
You must be signed in to change notification settings - Fork 273
/
mapped-list.js
149 lines (127 loc) · 4.3 KB
/
mapped-list.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
// TODO refactor this smelly code!
const loaderDefaults = require('../config').loader;
const getAllModules = require('./get-all-modules');
const isModuleShouldBeExtracted = require('./is-module-should-be-extracted');
const getModuleChunk = require('./get-module-chunk');
const interpolate = require('./interpolate');
const getMatchedRule = require('./get-matched-rule');
class MappedListItem {
/**
* @param {SpriteSymbol} symbol
* @param {NormalModule} module
* @param {string} spriteFilename
*/
constructor(symbol, module, spriteFilename) {
this.symbol = symbol;
this.module = module;
this.resource = symbol.request.file;
this.spriteFilename = spriteFilename;
}
get url() {
return `${this.spriteFilename}#${this.symbol.id}`;
}
get useUrl() {
return `${this.spriteFilename}#${this.symbol.useId}`;
}
}
class MappedList {
/**
* @param {SpriteSymbol[]} symbols
* @param {Compilation} compilation
*/
constructor(symbols, compilation, shouldLog = false) {
const { compiler, moduleGraph, chunkGraph } = compilation;
this.symbols = symbols;
this.rule = getMatchedRule(compiler);
this.allModules = getAllModules(compilation);
this.moduleGraph = moduleGraph;
this.chunkGraph = chunkGraph;
this.spriteModules = this.allModules.filter(module => isModuleShouldBeExtracted(module, moduleGraph));
this.shouldLog = shouldLog;
this.items = this.create();
}
/**
* @param {MappedListItem[]} data
* @return {Object<string, MappedListItem>}
*/
static groupItemsBySpriteFilename(data) {
return data
.map(item => item.spriteFilename)
.filter((value, index, self) => self.indexOf(value) === index)
.reduce((acc, spriteFilename) => {
acc[spriteFilename] = data.filter(item => item.spriteFilename === spriteFilename);
return acc;
}, {});
}
/**
* @param {MappedListItem[]} data
* @param {Function} [mapper] Custom grouper function
* @return {Object<string, MappedListItem>}
*/
static groupItemsBySymbolFile(data, mapper) {
return data.reduce((acc, item) => {
if (mapper) {
mapper(acc, item);
} else {
acc[item.resource] = item;
}
return acc;
}, {});
}
/**
* @return {MappedListItem[]}
*/
create() {
const { symbols, spriteModules, allModules, rule, moduleGraph, chunkGraph } = this;
const data = symbols.reduce((acc, symbol) => {
const resource = symbol.request.file;
const module = spriteModules.find((m) => {
return 'resource' in m ? m.resource.split('?')[0] === resource : false;
});
let spriteFilename = rule.spriteFilename || loaderDefaults.spriteFilename;
const chunk = module ? getModuleChunk(module, allModules, moduleGraph, chunkGraph) : null;
if (typeof spriteFilename !== 'function' && chunk && chunk.name) {
spriteFilename = spriteFilename.replace('[chunkname]', chunk.name);
} else if (typeof spriteFilename === 'function') {
spriteFilename = spriteFilename(resource);
}
if (rule && module) {
acc.push(new MappedListItem(symbol, module, spriteFilename));
}
return acc;
}, []);
// Additional pass to interpolate [hash] in spriteFilename
const itemsBySpriteFilename = MappedList.groupItemsBySpriteFilename(data);
const filenames = Object.keys(itemsBySpriteFilename);
filenames.forEach((filename) => {
if (!filename.includes('hash')) {
return;
}
const items = itemsBySpriteFilename[filename];
const spriteSymbols = items.map(item => item.symbol);
const content = spriteSymbols.map(s => s.render()).join('');
const interpolatedName = interpolate(filename, {
resourcePath: filename,
content
});
items
.filter(item => item.spriteFilename !== interpolatedName)
.forEach(item => item.spriteFilename = interpolatedName);
});
return data;
}
/**
* @return {Object<string, MappedListItem>}
*/
groupItemsBySpriteFilename() {
return MappedList.groupItemsBySpriteFilename(this.items);
}
/**
* @param {Function} [mapper] Custom grouper function
* @return {Object<string, MappedListItem>}
*/
groupItemsBySymbolFile(mapper) {
return MappedList.groupItemsBySymbolFile(this.items, mapper);
}
}
module.exports = MappedList;