Skip to content

Commit 8221799

Browse files
committed
Add new option "excludeAssets" to Stats
1 parent c8732c8 commit 8221799

5 files changed

Lines changed: 46 additions & 21 deletions

File tree

‎lib/Stats.js‎

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,18 @@ class Stats {
7878

7979
const optionOrLocalFallback = (v, def) =>
8080
typeof v !== "undefined" ? v :
81-
typeof options.all !== "undefined" ? options.all : def;
81+
typeof options.all !== "undefined" ? options.all : def;
82+
83+
const testAgainstGivenOption = (item) => {
84+
if(typeof item === "string") {
85+
const regExp = new RegExp(`[\\\\/]${item.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&")}([\\\\/]|$|!|\\?)`); // eslint-disable-line no-useless-escape
86+
return ident => regExp.test(ident);
87+
}
88+
if(item && typeof item === "object" && typeof item.test === "function")
89+
return ident => item.test(ident);
90+
if(typeof item === "function")
91+
return item;
92+
};
8293

8394
const compilation = this.compilation;
8495
const context = optionOrFallback(options.context, process.cwd());
@@ -108,16 +119,8 @@ class Stats {
108119
const showWarnings = optionOrLocalFallback(options.warnings, true);
109120
const warningsFilter = optionOrFallback(options.warningsFilter, null);
110121
const showPublicPath = optionOrLocalFallback(options.publicPath, !forToString);
111-
const excludeModules = [].concat(optionOrFallback(options.exclude, [])).map(item => {
112-
if(typeof item === "string") {
113-
const regExp = new RegExp(`[\\\\/]${item.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&")}([\\\\/]|$|!|\\?)`); // eslint-disable-line no-useless-escape
114-
return ident => regExp.test(ident);
115-
}
116-
if(item && typeof item === "object" && typeof item.test === "function")
117-
return ident => item.test(ident);
118-
if(typeof item === "function")
119-
return item;
120-
});
122+
const excludeModules = [].concat(optionOrFallback(options.exclude, [])).map(testAgainstGivenOption);
123+
const excludeAssets = [].concat(optionOrFallback(options.excludeAssets, [])).map(testAgainstGivenOption);
121124
const maxModules = optionOrFallback(options.maxModules, forToString ? 15 : Infinity);
122125
const sortModules = optionOrFallback(options.modulesSort, "id");
123126
const sortChunks = optionOrFallback(options.chunksSort, "id");
@@ -140,6 +143,18 @@ class Stats {
140143
};
141144
};
142145

146+
const createAssetFilter = () => {
147+
return asset => {
148+
if(excludeAssets.length > 0) {
149+
const ident = asset.name;
150+
const excluded = excludeAssets.some(fn => fn(ident, asset));
151+
if(excluded)
152+
return false;
153+
}
154+
return showCachedAssets || asset.emitted;
155+
};
156+
};
157+
143158
const sortByFieldAndOrder = (fieldKey, a, b) => {
144159
if(a[fieldKey] === null && b[fieldKey] === null) return 0;
145160
if(a[fieldKey] === null) return 1;
@@ -247,7 +262,7 @@ class Stats {
247262

248263
assetsByFile[asset] = obj;
249264
return obj;
250-
}).filter(asset => showCachedAssets || asset.emitted);
265+
}).filter(createAssetFilter());
251266

252267
compilation.chunks.forEach(chunk => {
253268
chunk.files.forEach(asset => {
@@ -433,7 +448,7 @@ class Stats {
433448
if(useColors) {
434449
buf.push(
435450
(useColors === true || useColors[color] === undefined) ?
436-
defaultColors[color] : useColors[color]
451+
defaultColors[color] : useColors[color]
437452
);
438453
}
439454
buf.push(str);
@@ -443,8 +458,8 @@ class Stats {
443458
};
444459
return obj;
445460
}, {
446-
normal: (str) => buf.push(str)
447-
});
461+
normal: (str) => buf.push(str)
462+
});
448463

449464
const coloredTime = (time) => {
450465
let times = [800, 400, 200, 100];
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"data": "i will be copied"
3+
}
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
Hash: 9a949e8727d0583cb1c4
1+
Hash: 14e131b4e5c91ed5ce4a
22
Time: Xms
33
Asset Size Chunks Chunk Names
4-
bundle.js 2.74 kB 0 [emitted] main
5-
[0] (webpack)/test/statsCases/exclude-with-loader/index.js 46 bytes {0} [built]
4+
bundle.js 2.92 kB 0 [emitted] main
5+
[0] (webpack)/test/statsCases/exclude-with-loader/index.js 77 bytes {0} [built]
66
[1] (webpack)/test/statsCases/exclude-with-loader/a.txt 43 bytes {0} [built]
7-
+ 1 hidden module
7+
+ 2 hidden modules
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1-
require("./a.txt")
2-
require("./exclude/b.txt")
1+
require("./a.txt");
2+
require("./exclude/b.txt");
3+
require("./exclude/c.json");

‎test/statsCases/exclude-with-loader/webpack.config.js‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,18 @@ module.exports = {
77
exclude: [
88
"node_modules",
99
"exclude"
10+
],
11+
excludeAssets: [
12+
/\.json/
1013
]
1114
},
1215
module: {
1316
loaders: [{
1417
test: /\.txt/,
1518
loader: "raw-loader"
19+
}, {
20+
test: /\.json/,
21+
loader: "file-loader"
1622
}]
1723
}
1824
};

0 commit comments

Comments
 (0)