|
2 | 2 | MIT License http://www.opensource.org/licenses/mit-license.php |
3 | 3 | Author Tobias Koppers @sokra |
4 | 4 | */ |
5 | | -function ProgressPlugin(options) { |
6 | | - if(typeof options === "function") { |
7 | | - options = { |
8 | | - handler: options |
9 | | - }; |
| 5 | +"use strict"; |
| 6 | + |
| 7 | +class ProgressPlugin { |
| 8 | + |
| 9 | + constructor(options) { |
| 10 | + if(typeof options === "function") { |
| 11 | + options = { |
| 12 | + handler: options |
| 13 | + }; |
| 14 | + } |
| 15 | + options = options || {}; |
| 16 | + this.profile = options.profile; |
| 17 | + this.handler = options.handler; |
10 | 18 | } |
11 | | - options = options || {}; |
12 | | - this.profile = options.profile; |
13 | | - this.handler = options.handler; |
14 | | -} |
15 | | -module.exports = ProgressPlugin; |
16 | 19 |
|
17 | | -ProgressPlugin.prototype.apply = function(compiler) { |
18 | | - var handler = this.handler || defaultHandler; |
19 | | - var profile = this.profile; |
20 | | - if(compiler.compilers) { |
21 | | - var states = new Array(compiler.compilers.length); |
22 | | - compiler.compilers.forEach(function(compiler, idx) { |
23 | | - compiler.apply(new ProgressPlugin(function(p, msg) { |
24 | | - states[idx] = Array.prototype.slice.apply(arguments); |
25 | | - handler.apply(null, [ |
26 | | - states.map(function(state) { |
27 | | - return state && state[0] || 0; |
28 | | - }).reduce(function(a, b) { |
29 | | - return a + b; |
30 | | - }) / states.length, |
31 | | - "[" + idx + "] " + msg |
32 | | - ].concat(Array.prototype.slice.call(arguments, 2))); |
33 | | - })); |
34 | | - }); |
35 | | - } else { |
36 | | - var lastModulesCount = 0; |
37 | | - var moduleCount = 500; |
38 | | - var doneModules = 0; |
39 | | - var activeModules = []; |
| 20 | + apply(compiler) { |
| 21 | + const handler = this.handler || defaultHandler; |
| 22 | + const profile = this.profile; |
| 23 | + if(compiler.compilers) { |
| 24 | + const states = new Array(compiler.compilers.length); |
| 25 | + compiler.compilers.forEach(function(compiler, idx) { |
| 26 | + compiler.apply(new ProgressPlugin((p, msg) => { |
| 27 | + states[idx] = Array.prototype.slice.apply(arguments); |
| 28 | + handler.apply(null, [ |
| 29 | + states.map(state => state && state[0] || 0).reduce((a, b) => a + b) / states.length, |
| 30 | + `[${idx}] ${msg}` |
| 31 | + ].concat(Array.prototype.slice.call(arguments, 2))); |
| 32 | + })); |
| 33 | + }); |
| 34 | + } else { |
| 35 | + let lastModulesCount = 0; |
| 36 | + let moduleCount = 500; |
| 37 | + let doneModules = 0; |
| 38 | + const activeModules = []; |
40 | 39 |
|
41 | | - var update = function update(module) { |
42 | | - handler( |
43 | | - 0.1 + (doneModules / Math.max(lastModulesCount, moduleCount)) * 0.6, |
44 | | - "building modules", |
45 | | - doneModules + "/" + moduleCount + " modules", |
46 | | - activeModules.length + " active", |
47 | | - activeModules[activeModules.length - 1] |
48 | | - ); |
49 | | - }; |
| 40 | + const update = function update(module) { |
| 41 | + handler( |
| 42 | + 0.1 + (doneModules / Math.max(lastModulesCount, moduleCount)) * 0.6, |
| 43 | + "building modules", |
| 44 | + `${doneModules}/${moduleCount} modules`, |
| 45 | + `${activeModules.length} active`, |
| 46 | + activeModules[activeModules.length - 1] |
| 47 | + ); |
| 48 | + }; |
50 | 49 |
|
51 | | - var moduleDone = function moduleDone(module) { |
52 | | - doneModules++; |
53 | | - var ident = module.identifier(); |
54 | | - if(ident) { |
55 | | - var idx = activeModules.indexOf(ident); |
56 | | - if(idx >= 0) activeModules.splice(idx, 1); |
57 | | - } |
58 | | - update(); |
59 | | - }; |
60 | | - compiler.plugin("compilation", function(compilation) { |
61 | | - if(compilation.compiler.isChild()) return; |
62 | | - lastModulesCount = moduleCount; |
63 | | - moduleCount = 0; |
64 | | - doneModules = 0; |
65 | | - handler(0, "compiling"); |
66 | | - compilation.plugin("build-module", function(module) { |
67 | | - moduleCount++; |
68 | | - var ident = module.identifier(); |
| 50 | + const moduleDone = function moduleDone(module) { |
| 51 | + doneModules++; |
| 52 | + const ident = module.identifier(); |
69 | 53 | if(ident) { |
70 | | - activeModules.push(ident); |
| 54 | + const idx = activeModules.indexOf(ident); |
| 55 | + if(idx >= 0) activeModules.splice(idx, 1); |
71 | 56 | } |
72 | 57 | update(); |
73 | | - }); |
74 | | - compilation.plugin("failed-module", moduleDone); |
75 | | - compilation.plugin("succeed-module", moduleDone); |
76 | | - var syncHooks = { |
77 | | - "seal": [0.71, "sealing"], |
78 | | - "optimize": [0.72, "optimizing"], |
79 | | - "optimize-modules-basic": [0.73, "basic module optimization"], |
80 | | - "optimize-modules": [0.74, "module optimization"], |
81 | | - "optimize-modules-advanced": [0.75, "advanced module optimization"], |
82 | | - "optimize-chunks-basic": [0.76, "basic chunk optimization"], |
83 | | - "optimize-chunks": [0.77, "chunk optimization"], |
84 | | - "optimize-chunks-advanced": [0.78, "advanced chunk optimization"], |
85 | | - // optimize-tree |
86 | | - "revive-modules": [0.80, "module reviving"], |
87 | | - "optimize-module-order": [0.81, "module order optimization"], |
88 | | - "optimize-module-ids": [0.82, "module id optimization"], |
89 | | - "revive-chunks": [0.83, "chunk reviving"], |
90 | | - "optimize-chunk-order": [0.84, "chunk order optimization"], |
91 | | - "optimize-chunk-ids": [0.85, "chunk id optimization"], |
92 | | - "before-hash": [0.86, "hashing"], |
93 | | - "before-module-assets": [0.87, "module assets processing"], |
94 | | - "before-chunk-assets": [0.88, "chunk assets processing"], |
95 | | - "additional-chunk-assets": [0.89, "additional chunk assets processing"], |
96 | | - "record": [0.90, "recording"] |
97 | 58 | }; |
98 | | - Object.keys(syncHooks).forEach(function(name) { |
99 | | - var pass = 0; |
100 | | - var settings = syncHooks[name]; |
101 | | - compilation.plugin(name, function() { |
102 | | - if(pass++ > 0) |
103 | | - handler(settings[0], settings[1], "pass " + pass); |
104 | | - else |
105 | | - handler(settings[0], settings[1]); |
| 59 | + compiler.plugin("compilation", function(compilation) { |
| 60 | + if(compilation.compiler.isChild()) return; |
| 61 | + lastModulesCount = moduleCount; |
| 62 | + moduleCount = 0; |
| 63 | + doneModules = 0; |
| 64 | + handler(0, "compiling"); |
| 65 | + compilation.plugin("build-module", function(module) { |
| 66 | + moduleCount++; |
| 67 | + const ident = module.identifier(); |
| 68 | + if(ident) { |
| 69 | + activeModules.push(ident); |
| 70 | + } |
| 71 | + update(); |
| 72 | + }); |
| 73 | + compilation.plugin("failed-module", moduleDone); |
| 74 | + compilation.plugin("succeed-module", moduleDone); |
| 75 | + const syncHooks = { |
| 76 | + "seal": [0.71, "sealing"], |
| 77 | + "optimize": [0.72, "optimizing"], |
| 78 | + "optimize-modules-basic": [0.73, "basic module optimization"], |
| 79 | + "optimize-modules": [0.74, "module optimization"], |
| 80 | + "optimize-modules-advanced": [0.75, "advanced module optimization"], |
| 81 | + "optimize-chunks-basic": [0.76, "basic chunk optimization"], |
| 82 | + "optimize-chunks": [0.77, "chunk optimization"], |
| 83 | + "optimize-chunks-advanced": [0.78, "advanced chunk optimization"], |
| 84 | + // optimize-tree |
| 85 | + "revive-modules": [0.80, "module reviving"], |
| 86 | + "optimize-module-order": [0.81, "module order optimization"], |
| 87 | + "optimize-module-ids": [0.82, "module id optimization"], |
| 88 | + "revive-chunks": [0.83, "chunk reviving"], |
| 89 | + "optimize-chunk-order": [0.84, "chunk order optimization"], |
| 90 | + "optimize-chunk-ids": [0.85, "chunk id optimization"], |
| 91 | + "before-hash": [0.86, "hashing"], |
| 92 | + "before-module-assets": [0.87, "module assets processing"], |
| 93 | + "before-chunk-assets": [0.88, "chunk assets processing"], |
| 94 | + "additional-chunk-assets": [0.89, "additional chunk assets processing"], |
| 95 | + "record": [0.90, "recording"] |
| 96 | + }; |
| 97 | + Object.keys(syncHooks).forEach(name => { |
| 98 | + let pass = 0; |
| 99 | + const settings = syncHooks[name]; |
| 100 | + compilation.plugin(name, () => { |
| 101 | + if(pass++ > 0) |
| 102 | + handler(settings[0], settings[1], `pass ${pass}`); |
| 103 | + else |
| 104 | + handler(settings[0], settings[1]); |
| 105 | + }); |
| 106 | + }); |
| 107 | + compilation.plugin("optimize-tree", (chunks, modules, callback) => { |
| 108 | + handler(0.79, "module and chunk tree optimization"); |
| 109 | + callback(); |
| 110 | + }); |
| 111 | + compilation.plugin("additional-assets", callback => { |
| 112 | + handler(0.91, "additional asset processing"); |
| 113 | + callback(); |
| 114 | + }); |
| 115 | + compilation.plugin("optimize-chunk-assets", (chunks, callback) => { |
| 116 | + handler(0.92, "chunk asset optimization"); |
| 117 | + callback(); |
| 118 | + }); |
| 119 | + compilation.plugin("optimize-assets", (assets, callback) => { |
| 120 | + handler(0.94, "asset optimization"); |
| 121 | + callback(); |
106 | 122 | }); |
107 | 123 | }); |
108 | | - compilation.plugin("optimize-tree", function(chunks, modules, callback) { |
109 | | - handler(0.79, "module and chunk tree optimization"); |
110 | | - callback(); |
111 | | - }); |
112 | | - compilation.plugin("additional-assets", function(callback) { |
113 | | - handler(0.91, "additional asset processing"); |
114 | | - callback(); |
115 | | - }); |
116 | | - compilation.plugin("optimize-chunk-assets", function(chunks, callback) { |
117 | | - handler(0.92, "chunk asset optimization"); |
| 124 | + compiler.plugin("emit", (compilation, callback) => { |
| 125 | + handler(0.95, "emitting"); |
118 | 126 | callback(); |
119 | 127 | }); |
120 | | - compilation.plugin("optimize-assets", function(assets, callback) { |
121 | | - handler(0.94, "asset optimization"); |
122 | | - callback(); |
| 128 | + compiler.plugin("done", () => { |
| 129 | + handler(1, ""); |
123 | 130 | }); |
124 | | - }); |
125 | | - compiler.plugin("emit", function(compilation, callback) { |
126 | | - handler(0.95, "emitting"); |
127 | | - callback(); |
128 | | - }); |
129 | | - compiler.plugin("done", function() { |
130 | | - handler(1, ""); |
131 | | - }); |
132 | | - } |
| 131 | + } |
133 | 132 |
|
134 | | - var lineCaretPosition = 0, |
135 | | - lastState, lastStateTime; |
| 133 | + let lineCaretPosition = 0, |
| 134 | + lastState, lastStateTime; |
136 | 135 |
|
137 | | - function defaultHandler(percentage, msg) { |
138 | | - var state = msg; |
139 | | - var details = Array.prototype.slice.call(arguments, 2); |
140 | | - if(percentage < 1) { |
141 | | - percentage = Math.floor(percentage * 100); |
142 | | - msg = percentage + "% " + msg; |
143 | | - if(percentage < 100) { |
144 | | - msg = " " + msg; |
145 | | - } |
146 | | - if(percentage < 10) { |
147 | | - msg = " " + msg; |
148 | | - } |
149 | | - details.forEach(function(detail) { |
150 | | - if(!detail) return; |
151 | | - if(detail.length > 40) { |
152 | | - detail = "..." + detail.substr(detail.length - 37); |
| 136 | + function defaultHandler(percentage, msg) { |
| 137 | + let state = msg; |
| 138 | + const details = Array.prototype.slice.call(arguments, 2); |
| 139 | + if(percentage < 1) { |
| 140 | + percentage = Math.floor(percentage * 100); |
| 141 | + msg = `${percentage}% ${msg}`; |
| 142 | + if(percentage < 100) { |
| 143 | + msg = ` ${msg}`; |
153 | 144 | } |
154 | | - msg += " " + detail; |
155 | | - }); |
156 | | - } |
157 | | - if(profile) { |
158 | | - state = state.replace(/^\d+\/\d+\s+/, ""); |
159 | | - if(percentage === 0) { |
160 | | - lastState = null; |
161 | | - lastStateTime = +new Date(); |
162 | | - } else if(state !== lastState || percentage === 1) { |
163 | | - var now = +new Date(); |
164 | | - if(lastState) { |
165 | | - var stateMsg = (now - lastStateTime) + "ms " + lastState; |
166 | | - goToLineStart(stateMsg); |
167 | | - process.stderr.write(stateMsg + "\n"); |
168 | | - lineCaretPosition = 0; |
| 145 | + if(percentage < 10) { |
| 146 | + msg = ` ${msg}`; |
169 | 147 | } |
170 | | - lastState = state; |
171 | | - lastStateTime = now; |
| 148 | + details.forEach(detail => { |
| 149 | + if(!detail) return; |
| 150 | + if(detail.length > 40) { |
| 151 | + detail = `...${detail.substr(detail.length - 37)}`; |
| 152 | + } |
| 153 | + msg += ` ${detail}`; |
| 154 | + }); |
172 | 155 | } |
| 156 | + if(profile) { |
| 157 | + state = state.replace(/^\d+\/\d+\s+/, ""); |
| 158 | + if(percentage === 0) { |
| 159 | + lastState = null; |
| 160 | + lastStateTime = +new Date(); |
| 161 | + } else if(state !== lastState || percentage === 1) { |
| 162 | + const now = +new Date(); |
| 163 | + if(lastState) { |
| 164 | + const stateMsg = `${now - lastStateTime}ms ${lastState}`; |
| 165 | + goToLineStart(stateMsg); |
| 166 | + process.stderr.write(stateMsg + "\n"); |
| 167 | + lineCaretPosition = 0; |
| 168 | + } |
| 169 | + lastState = state; |
| 170 | + lastStateTime = now; |
| 171 | + } |
| 172 | + } |
| 173 | + goToLineStart(msg); |
| 174 | + process.stderr.write(msg); |
173 | 175 | } |
174 | | - goToLineStart(msg); |
175 | | - process.stderr.write(msg); |
176 | | - } |
177 | 176 |
|
178 | | - function goToLineStart(nextMessage) { |
179 | | - var str = ""; |
180 | | - for(; lineCaretPosition > nextMessage.length; lineCaretPosition--) { |
181 | | - str += "\b \b"; |
182 | | - } |
183 | | - for(var i = 0; i < lineCaretPosition; i++) { |
184 | | - str += "\b"; |
| 177 | + function goToLineStart(nextMessage) { |
| 178 | + let str = ""; |
| 179 | + for(; lineCaretPosition > nextMessage.length; lineCaretPosition--) { |
| 180 | + str += "\b \b"; |
| 181 | + } |
| 182 | + for(var i = 0; i < lineCaretPosition; i++) { |
| 183 | + str += "\b"; |
| 184 | + } |
| 185 | + lineCaretPosition = nextMessage.length; |
| 186 | + if(str) process.stderr.write(str); |
185 | 187 | } |
186 | | - lineCaretPosition = nextMessage.length; |
187 | | - if(str) process.stderr.write(str); |
188 | 188 | } |
189 | | -}; |
| 189 | +} |
| 190 | +module.exports = ProgressPlugin; |
0 commit comments