1 | const {inspect, types} = require('util');
|
2 | const _ = require('lodash');
|
3 | const opener = require('opener');
|
4 |
|
5 | const MONTHS = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
|
6 |
|
7 | exports.createAssetsFilter = createAssetsFilter;
|
8 |
|
9 | function createAssetsFilter(excludePatterns) {
|
10 | const excludeFunctions = _(excludePatterns)
|
11 | .castArray()
|
12 | .compact()
|
13 | .map(pattern => {
|
14 | if (typeof pattern === 'string') {
|
15 | pattern = new RegExp(pattern, 'u');
|
16 | }
|
17 |
|
18 | if (types.isRegExp(pattern)) {
|
19 | return (asset) => pattern.test(asset);
|
20 | }
|
21 |
|
22 | if (typeof pattern !== 'function') {
|
23 | throw new TypeError(
|
24 | `Pattern should be either string, RegExp or a function, but "${inspect(pattern, {depth: 0})}" got.`
|
25 | );
|
26 | }
|
27 |
|
28 | return pattern;
|
29 | })
|
30 | .value();
|
31 |
|
32 | if (excludeFunctions.length) {
|
33 | return (asset) => excludeFunctions.every(fn => fn(asset) !== true);
|
34 | } else {
|
35 | return () => true;
|
36 | }
|
37 | }
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 | exports.defaultTitle = function () {
|
44 | const time = new Date();
|
45 | const year = time.getFullYear();
|
46 | const month = MONTHS[time.getMonth()];
|
47 | const day = time.getDate();
|
48 | const hour = `0${time.getHours()}`.slice(-2);
|
49 | const minute = `0${time.getMinutes()}`.slice(-2);
|
50 |
|
51 | const currentTime = `${day} ${month} ${year} at ${hour}:${minute}`;
|
52 |
|
53 | return `${process.env.npm_package_name || 'Webpack Bundle Analyzer'} [${currentTime}]`;
|
54 | };
|
55 |
|
56 |
|
57 |
|
58 |
|
59 | exports.open = function (uri, logger) {
|
60 | try {
|
61 | opener(uri);
|
62 | } catch (err) {
|
63 | logger.debug(`Opener failed to open "${uri}":\n${err}`);
|
64 | }
|
65 | };
|