-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathindex.mjs
155 lines (144 loc) · 4.68 KB
/
index.mjs
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
150
151
152
153
154
155
/**
* Copyright 2018 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
import escapeRegExp from 'escape-string-regexp';
import SizePluginCore from 'size-plugin-core';
const NAME = 'SizePlugin';
/**
* `new SizePlugin(options)`
* @param {Object} options
* @param {string} [options.compression] compression method(gzip/brotli) to use, default: 'gzip'
* @param {string} [options.pattern] minimatch pattern of files to track
* @param {string} [options.exclude] minimatch pattern of files NOT to track
* @param {string} [options.filename] file name to save filesizes to disk
* @param {boolean} [options.publish] option to publish filesizes to size-plugin-store
* @param {boolean} [options.writeFile] option to save filesizes to disk
* @param {function} [options.stripHash] custom function to remove/normalize hashed filenames for comparison
* @param {(item:Item)=>string?} [options.decorateItem] custom function to decorate items
* @param {(data:Data)=>string?} [options.decorateAfter] custom function to decorate all output
* @public
*/
export default class SizePlugin {
constructor(options) {
const pluginOptions=options||{};
const coreOptions={ ...pluginOptions,stripHash: pluginOptions.stripHash||this.stripHash.bind(this) };
const core = new SizePluginCore(coreOptions);
this.core = core;
}
reverseTemplate(filename, template) {
// @todo - find a way to actually obtain values here.
if (typeof template === 'function') {
template = template({
chunk: {
name: 'main'
}
});
}
const hashLength = this.output.hashDigestLength;
const replace = [];
let count = 0;
function replacer() {
let out = '';
for (let i = 1; i < arguments.length - 2; i++) {
// eslint-disable-next-line prefer-spread,prefer-rest-params
let value = arguments[i];
if (replace[i - 1]) value = value.replace(/./g, '*');
out += value;
}
return out;
}
const reg = template.replace(
/(^|.+?)(?:\[([a-z]+)(?::(\d))?\]|$)/g,
(s, before, type, size) => {
let out = '';
if (before) {
out += `(${escapeRegExp(before)})`;
replace[count++] = false;
}
if (type === 'hash' || type === 'contenthash' || type === 'chunkhash') {
const len = Math.round(size) || hashLength;
out += `([0-9a-zA-Z]{${len}})`;
replace[count++] = true;
}
else if (type) {
out += '(.*?)';
replace[count++] = false;
}
return out;
}
);
const matcher = new RegExp(`^${reg}$`);
return matcher.test(filename) && filename.replace(matcher, replacer);
}
stripHash(filename) {
return (
this.reverseTemplate(filename, this.output.filename) ||
this.reverseTemplate(filename, this.output.chunkFilename) ||
filename
);
}
async apply(compiler) {
this.output = compiler.options.output;
this.core.options.mode = compiler.options.mode;
const outputPath = compiler.options.output.path;
const afterEmit = (compilation, callback) => {
const assets = Object.keys(compilation.assets).reduce((agg, key) => {
agg[key] = {
source: compilation.assets[key].source()
};
return agg;
}, {});
// assets => {'a.js':{source:'console.log(1)'}}
this.core.execute(assets, outputPath)
.then(output => {
if (output) {
process.nextTick(() => {
console.log('\n' + output);
});
}
})
.catch(console.error)
.then(callback);
};
// for webpack version > 4
if (compiler.hooks && compiler.hooks.emit) {
compiler.hooks.emit.tapAsync(NAME, afterEmit);
}
else {
// for webpack version < 3
compiler.plugin('after-emit', afterEmit);
}
}
}
/**
* @name Item
* @typedef Item
* @property {string} name Filename of the item
* @property {number} sizeBefore Previous size, in kilobytes
* @property {number} size Current size, in kilobytes
* @property {string} sizeText Formatted current size
* @property {number} delta Difference from previous size, in kilobytes
* @property {string} deltaText Formatted size delta
* @property {string} msg Full item's default message
* @property {string} color The item's default CLI color
* @public
*/
/**
* @name Data
* @typedef Data
* @property {Item[]} sizes List of file size items
* @property {string} output Current buffered output
* @public
*/