This commit is contained in:
Julian Martin 2022-03-03 20:35:42 +01:00 committed by GitHub
parent 8b5e52f2a9
commit 9f506ded60
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -17,15 +17,26 @@ const logEnd = id => { const t = end(_s[id]); delete _s[id]; metrics.push([id, t
${TIMING} = { start, end, metrics, logStart, logEnd };
`)
const HELPERIMPORT = "import './timing.js';"
export function timing (_opts: Options = {}): Plugin {
return {
name: 'timing',
generateBundle () {
this.emitFile({
type: 'asset',
fileName: 'timing.js',
source: HELPER
})
},
renderChunk (code, chunk: RenderedChunk) {
let name = chunk.fileName || ''
name = name.replace(extname(name), '')
const logName = name === 'index' ? 'Cold Start' : ('Load ' + name)
return {
code: (chunk.isEntry ? HELPER : '') + `${TIMING}.logStart('${logName}');` + code + `;${TIMING}.logEnd('${logName}');`,
code: (chunk.isEntry ? HELPERIMPORT : '') + `${TIMING}.logStart('${logName}');` + code + `;${TIMING}.logEnd('${logName}');`,
map: null
}
}