| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
( function () { |
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
const inspect = mw.inspect, |
| 25 |
byteLength = require( 'mediawiki.String' ).byteLength, |
| 26 |
hasOwn = Object.prototype.hasOwnProperty; |
| 27 |
|
| 28 |
function sortByProperty( array, prop, descending ) { |
| 29 |
const order = descending ? -1 : 1; |
| 30 |
return array.sort( ( a, b ) => { |
| 31 |
if ( a[ prop ] === undefined || b[ prop ] === undefined ) { |
| 32 |
|
| 33 |
return a[ prop ] !== undefined ? -1 : b[ prop ] !== undefined ? 1 : 0; |
| 34 |
} |
| 35 |
return a[ prop ] > b[ prop ] ? order : a[ prop ] < b[ prop ] ? -order : 0; |
| 36 |
} ); |
| 37 |
} |
| 38 |
|
| 39 |
function humanSize( bytesInput ) { |
| 40 |
let bytes = +bytesInput; |
| 41 |
const units = [ '', ' KiB', ' MiB', ' GiB', ' TiB', ' PiB' ]; |
| 42 |
|
| 43 |
if ( bytes === 0 || isNaN( bytes ) ) { |
| 44 |
return bytesInput; |
| 45 |
} |
| 46 |
|
| 47 |
let i; |
| 48 |
for ( i = 0; bytes >= 1024; bytes /= 1024 ) { |
| 49 |
i++; |
| 50 |
} |
| 51 |
|
| 52 |
|
| 53 |
return bytes.toFixed( i > 0 ? 1 : 0 ) + units[ i ]; |
| 54 |
} |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
inspect.getDependencyGraph = function () { |
| 65 |
const modules = inspect.getLoadedModules(), |
| 66 |
graph = {}; |
| 67 |
|
| 68 |
modules.forEach( ( moduleName ) => { |
| 69 |
const dependencies = mw.loader.moduleRegistry[ moduleName ].dependencies || []; |
| 70 |
|
| 71 |
if ( !hasOwn.call( graph, moduleName ) ) { |
| 72 |
graph[ moduleName ] = { requiredBy: [] }; |
| 73 |
} |
| 74 |
graph[ moduleName ].requires = dependencies; |
| 75 |
|
| 76 |
dependencies.forEach( ( depName ) => { |
| 77 |
if ( !hasOwn.call( graph, depName ) ) { |
| 78 |
graph[ depName ] = { requiredBy: [] }; |
| 79 |
} |
| 80 |
graph[ depName ].requiredBy.push( moduleName ); |
| 81 |
} ); |
| 82 |
} ); |
| 83 |
return graph; |
| 84 |
}; |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
inspect.getModuleSize = function ( moduleName ) { |
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
const module = mw.loader.moduleRegistry[ moduleName ]; |
| 102 |
|
| 103 |
if ( module.state !== 'ready' ) { |
| 104 |
return null; |
| 105 |
} |
| 106 |
if ( !module.declarator ) { |
| 107 |
return 0; |
| 108 |
} |
| 109 |
return byteLength( module.declarator.toString() ); |
| 110 |
}; |
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
inspect.auditSelectors = function ( css ) { |
| 125 |
const selectors = { total: 0, matched: 0 }, |
| 126 |
style = document.createElement( 'style' ); |
| 127 |
|
| 128 |
style.textContent = css; |
| 129 |
document.body.appendChild( style ); |
| 130 |
const cssRules = style.sheet.cssRules; |
| 131 |
for ( const index in cssRules ) { |
| 132 |
const rule = cssRules[ index ]; |
| 133 |
selectors.total++; |
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
try { |
| 139 |
if ( document.querySelector( rule.selectorText ) !== null ) { |
| 140 |
selectors.matched++; |
| 141 |
} |
| 142 |
} catch ( e ) {} |
| 143 |
} |
| 144 |
document.body.removeChild( style ); |
| 145 |
return selectors; |
| 146 |
}; |
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
inspect.getLoadedModules = function () { |
| 156 |
return mw.loader.getModuleNames().filter( ( module ) => mw.loader.getState( module ) === 'ready' ); |
| 157 |
}; |
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
inspect.dumpTable = console.table; |
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
inspect.runReports = function () { |
| 179 |
const reports = arguments.length > 0 ? |
| 180 |
Array.prototype.slice.call( arguments ) : |
| 181 |
Object.keys( inspect.reports ); |
| 182 |
|
| 183 |
reports.forEach( ( name ) => { |
| 184 |
if ( console.group ) { |
| 185 |
console.group( 'mw.inspect ' + name + ' report' ); |
| 186 |
} else { |
| 187 |
console.log( 'mw.inspect ' + name + ' report' ); |
| 188 |
} |
| 189 |
inspect.dumpTable( inspect.reports[ name ]() ); |
| 190 |
if ( console.group ) { |
| 191 |
console.groupEnd( 'mw.inspect ' + name + ' report' ); |
| 192 |
} |
| 193 |
} ); |
| 194 |
}; |
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
inspect.grep = function ( pattern ) { |
| 207 |
if ( typeof pattern.test !== 'function' ) { |
| 208 |
|
| 209 |
pattern = new RegExp( mw.util.escapeRegExp( pattern ), 'g' ); |
| 210 |
} |
| 211 |
|
| 212 |
return inspect.getLoadedModules().filter( ( moduleName ) => { |
| 213 |
const module = mw.loader.moduleRegistry[ moduleName ]; |
| 214 |
|
| 215 |
|
| 216 |
if ( typeof module.script === 'function' && pattern.test( module.script.toString() ) ) { |
| 217 |
return true; |
| 218 |
} |
| 219 |
|
| 220 |
|
| 221 |
if ( |
| 222 |
$.isPlainObject( module.style ) && Array.isArray( module.style.css ) && |
| 223 |
pattern.test( module.style.css.join( '' ) ) |
| 224 |
) { |
| 225 |
|
| 226 |
return true; |
| 227 |
} |
| 228 |
|
| 229 |
return false; |
| 230 |
} ); |
| 231 |
}; |
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
inspect.reports = { |
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
size: function () { |
| 246 |
|
| 247 |
const modules = inspect.getLoadedModules().map( ( module ) => ( { |
| 248 |
name: module, |
| 249 |
size: inspect.getModuleSize( module ) |
| 250 |
} ) ); |
| 251 |
|
| 252 |
|
| 253 |
sortByProperty( modules, 'size', true ); |
| 254 |
|
| 255 |
|
| 256 |
modules.forEach( ( module ) => { |
| 257 |
module.sizeInBytes = module.size; |
| 258 |
module.size = humanSize( module.size ); |
| 259 |
} ); |
| 260 |
|
| 261 |
return modules; |
| 262 |
}, |
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
css: function () { |
| 271 |
const modules = []; |
| 272 |
|
| 273 |
inspect.getLoadedModules().forEach( ( name ) => { |
| 274 |
const module = mw.loader.moduleRegistry[ name ]; |
| 275 |
|
| 276 |
let css; |
| 277 |
try { |
| 278 |
css = module.style.css.join(); |
| 279 |
} catch ( e ) { |
| 280 |
|
| 281 |
return; |
| 282 |
} |
| 283 |
|
| 284 |
const stats = inspect.auditSelectors( css ); |
| 285 |
modules.push( { |
| 286 |
module: name, |
| 287 |
allSelectors: stats.total, |
| 288 |
matchedSelectors: stats.matched, |
| 289 |
percentMatched: stats.total !== 0 ? |
| 290 |
( stats.matched / stats.total * 100 ).toFixed( 2 ) + '%' : null |
| 291 |
} ); |
| 292 |
} ); |
| 293 |
sortByProperty( modules, 'allSelectors', true ); |
| 294 |
return modules; |
| 295 |
}, |
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
store: function () { |
| 305 |
const stats = { enabled: mw.loader.store.enabled }; |
| 306 |
if ( stats.enabled ) { |
| 307 |
Object.assign( stats, mw.loader.store.stats ); |
| 308 |
try { |
| 309 |
const raw = localStorage.getItem( mw.loader.store.key ); |
| 310 |
stats.totalSizeInBytes = byteLength( raw ); |
| 311 |
stats.totalSize = humanSize( byteLength( raw ) ); |
| 312 |
} catch ( e ) {} |
| 313 |
} |
| 314 |
return [ stats ]; |
| 315 |
}, |
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
time: function () { |
| 326 |
if ( !mw.loader.profiler ) { |
| 327 |
mw.log.warn( 'mw.inspect: The time report requires $wgResourceLoaderEnableJSProfiler.' ); |
| 328 |
return []; |
| 329 |
} |
| 330 |
|
| 331 |
const modules = inspect.getLoadedModules() |
| 332 |
.map( ( moduleName ) => mw.loader.profiler.getProfile( moduleName ) ) |
| 333 |
.filter( |
| 334 |
|
| 335 |
|
| 336 |
( perf ) => perf !== null |
| 337 |
); |
| 338 |
|
| 339 |
|
| 340 |
sortByProperty( modules, 'total', true ); |
| 341 |
|
| 342 |
|
| 343 |
modules.forEach( ( module ) => { |
| 344 |
module.totalInMs = module.total; |
| 345 |
module.total = module.totalInMs.toLocaleString() + ' ms'; |
| 346 |
} ); |
| 347 |
|
| 348 |
return modules; |
| 349 |
} |
| 350 |
}; |
| 351 |
|
| 352 |
if ( mw.config.get( 'debug' ) ) { |
| 353 |
mw.log( 'mw.inspect: reports are not available in debug mode.' ); |
| 354 |
} |
| 355 |
|
| 356 |
}() ); |