Skip to content

Commit

Permalink
fix: move hrtime polyfill to timing plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Nov 15, 2020
1 parent 9d8a95b commit fe9d073
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 13 deletions.
9 changes: 7 additions & 2 deletions src/rollup/timing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@ const TIMING = 'global.__timing__'

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 start = () => hrtime();
const end = s => { const d = hrtime(s); return ((d[0] * 1e9) + d[1]) / 1e6; };
Expand All @@ -16,7 +21,7 @@ const _s = {};
const metrics = [];
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'); };
return { hrtime, start, end, metrics, logStart, logEnd };
${TIMING} = { hrtime, start, end, metrics, logStart, logEnd };
`)

export function timing (_opts: Options = {}): Plugin {
Expand Down
11 changes: 0 additions & 11 deletions src/targets/worker.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,11 @@
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 = {
entry: null, // Abstract
node: false,
minify: true,
hooks: {
'rollup:before' ({ rollupConfig }) {
rollupConfig.output.intro = polyfill + rollupConfig.output.intro
rollupConfig.output.format = 'iife'
}
}
Expand Down

0 comments on commit fe9d073

Please sign in to comment.