fix: move hrtime polyfill to timing plugin

This commit is contained in:
Pooya Parsa 2020-11-15 03:43:43 +01:00
parent e874d4db59
commit 047761f8b7
2 changed files with 7 additions and 13 deletions

View File

@ -7,7 +7,12 @@ const TIMING = 'global.__timing__'
const iife = code => `(function() { ${code.trim()} })();`.replace(/\n/g, '') const iife = code => `(function() { ${code.trim()} })();`.replace(/\n/g, '')
const HELPER = TIMING + '=' + iife(` // https://gist.github.com/pi0/1476085924f8a2eb1df85929c20cb43f
const POLYFILL = `const global="undefined"!=typeof globalThis?globalThis:void 0!==o?o:"undefined"!=typeof self?self:{};
global.process = global.process || {};
const o=Date.now(),t=()=>Date.now()-o;global.process.hrtime=global.process.hrtime||(o=>{const e=Math.floor(.001*(Date.now()-t())),a=.001*t();let l=Math.floor(a)+e,n=Math.floor(a%1*1e9);return o&&(l-=o[0],n-=o[1],n<0&&(l--,n+=1e9)),[l,n]});`
const HELPER = POLYFILL + iife(`
const hrtime = global.process.hrtime; const hrtime = global.process.hrtime;
const start = () => hrtime(); const start = () => hrtime();
const end = s => { const d = hrtime(s); return ((d[0] * 1e9) + d[1]) / 1e6; }; const end = s => { const d = hrtime(s); return ((d[0] * 1e9) + d[1]) / 1e6; };
@ -16,7 +21,7 @@ const _s = {};
const metrics = []; const metrics = [];
const logStart = id => { _s[id] = hrtime(); }; const logStart = id => { _s[id] = hrtime(); };
const logEnd = id => { const t = end(_s[id]); delete _s[id]; metrics.push([id, t]); console.log('◈', id, t, 'ms'); }; const logEnd = id => { const t = end(_s[id]); delete _s[id]; metrics.push([id, t]); console.log('◈', id, t, 'ms'); };
return { hrtime, start, end, metrics, logStart, logEnd }; ${TIMING} = { hrtime, start, end, metrics, logStart, logEnd };
`) `)
export function timing (_opts: Options = {}): Plugin { export function timing (_opts: Options = {}): Plugin {

View File

@ -1,22 +1,11 @@
import { SLSTarget } from '../config' import { SLSTarget } from '../config'
// https://gist.github.com/pi0/1476085924f8a2eb1df85929c20cb43f
const polyfill = `
const exports = {};
const module = { exports };
const global = typeof globalThis !== 'undefined' ? globalThis : "undefined" !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {};
global.process = global.process || {};
(function(){const o=Date.now(),t=()=>Date.now()-o;global.process.hrtime=o=>{const e=Math.floor(.001*(Date.now()-t())),n=.001*t();let a=Math.floor(n)+e,r=Math.floor(n%1*1e9);return o&&(a-=o[0],r-=o[1],r<0&&(a--,r+=1e9)),[a,r]};})();
`
export const worker: SLSTarget = { export const worker: SLSTarget = {
entry: null, // Abstract entry: null, // Abstract
node: false, node: false,
minify: true, minify: true,
hooks: { hooks: {
'rollup:before' ({ rollupConfig }) { 'rollup:before' ({ rollupConfig }) {
rollupConfig.output.intro = polyfill + rollupConfig.output.intro
rollupConfig.output.format = 'iife' rollupConfig.output.format = 'iife'
} }
} }