Skip to content

Commit

Permalink
feat(browser): strip node code from currentTime during browser build
Browse files Browse the repository at this point in the history
  • Loading branch information
sranka committed Jan 8, 2020
1 parent 87c156d commit e15abc4
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
6 changes: 5 additions & 1 deletion packages/core/rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,13 @@ function createConfig({browser, format, out, name, target, noTerser}) {
browser
? {
'./impl/node/NodeHttpTransport': './impl/browser/FetchTransport',
'process.env.ROLLUP_BROWSER': 'true',
delimiters: ['', ''],
}
: {
'process.env.ROLLUP_BROWSER': 'false',
delimiters: ['', ''],
}
: {}
),
typescript({
tsconfig: tsBuildConfigPath,
Expand Down
11 changes: 8 additions & 3 deletions packages/core/src/util/currentTime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@ const zeroPadding = '000000000'
let useHrTime = false

export function useProcessHrtime(use: boolean): boolean {
return (useHrTime = use && process && typeof process.hrtime === 'function')
/* istanbul ignore else */
if (!process.env.ROLLUP_BROWSER) {
return (useHrTime = use && process && typeof process.hrtime === 'function')
} else {
return false
}
}
useProcessHrtime(true) // preffer node

Expand All @@ -14,7 +19,7 @@ let startHrTime: [number, number] | undefined = undefined
let lastMillis = Date.now()
let stepsInMillis = 0
function nanos(): string {
if (useHrTime) {
if (!process.env.ROLLUP_BROWSER && useHrTime) {
const hrTime = process.hrtime() as [number, number]
let millis = Date.now()
if (!startHrTime) {
Expand Down Expand Up @@ -49,7 +54,7 @@ function nanos(): string {
}

function micros(): string {
if (useHrTime) {
if (!process.env.ROLLUP_BROWSER && useHrTime) {
const hrTime = process.hrtime() as [number, number]
const micros = String(Math.trunc(hrTime[1] / 1000) % 1000)
return (
Expand Down

0 comments on commit e15abc4

Please sign in to comment.