-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
56 lines (49 loc) · 1.39 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
'use strict';
function DynamicDualImportPlugin(options) {
this.options = options || {};
}
DynamicDualImportPlugin.prototype.apply = function(compiler) {
const publicPath = compiler.options.output.publicPath;
compiler.plugin("emit", (compilation, callback) => {
const cssChunksHash = {};
const cssFilePatt = /\.css$/;
const cssRTLFilePatt = /\.rtl\.css$/;
for (let i = 0; i < compilation.chunks.length; i++) {
const chunk = compilation.chunks[i];
const name = chunk.name || chunk.id;
if (name) {
for (let j = 0; j < chunk.files.length; j++) {
var filename = chunk.files[j];
if (cssFilePatt.test(filename)) {
var fullPath = publicPath + filename;
if (!cssChunksHash[name]) {
cssChunksHash[name] = {};
}
if (cssRTLFilePatt.test(filename)) {
cssChunksHash[name]['rtl'] = fullPath;
} else {
cssChunksHash[name]['default'] = fullPath;
}
}
}
}
}
const hashSource = 'window.__CSS_CHUNKS_HASH__ = ' + JSON.stringify(cssChunksHash) + ';';
compilation.assets[this.options.cssChunksHashFilename || 'css_chunks_hash.js'] = {
source: () => {
return hashSource;
},
size: () => {
return hashSource.length;
}
};
callback();
});
};
DynamicDualImportPlugin.loader = function(options) {
return {
loader: require.resolve('./loader'),
options: options
};
};
module.exports = DynamicDualImportPlugin;