Skip to content

Commit

Permalink
fix: use globalThis instead of global (#59)
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 authored Apr 6, 2021
1 parent ecc85f0 commit fb56216
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 25 deletions.
2 changes: 1 addition & 1 deletion src/rollup/plugins/static.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export function dirnames (): Plugin {
name: 'dirnames',
renderChunk (code, chunk) {
return {
code: code + (chunk.isEntry ? 'global.mainDir="undefined"!=typeof __dirname?__dirname:require.main.filename;' : ''),
code: code + (chunk.isEntry ? 'globalThis.mainDir="undefined"!=typeof __dirname?__dirname:require.main.filename;' : ''),
map: null
}
}
Expand Down
19 changes: 6 additions & 13 deletions src/rollup/plugins/timing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,18 @@ import type { Plugin, RenderedChunk } from 'rollup'

export interface Options { }

const TIMING = 'global.__timing__'
const TIMING = 'globalThis.__timing__'

const iife = code => `(function() { ${code.trim()} })();`.replace(/\n/g, '')

// 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; };
const HELPER = iife(`
const start = () => Date.now();
const end = s => Date.now() - s;
const _s = {};
const metrics = [];
const logStart = id => { _s[id] = hrtime(); };
const logStart = id => { _s[id] = Date.now(); };
const logEnd = id => { const t = end(_s[id]); delete _s[id]; metrics.push([id, t]); console.debug('>', id + ' (' + t + 'ms)'); };
${TIMING} = { hrtime, start, end, metrics, logStart, logEnd };
${TIMING} = { start, end, metrics, logStart, logEnd };
`)

export function timing (_opts: Options = {}): Plugin {
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/app/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ for (const type of ['private', 'public']) {
}
}

const $config = global.$config = {
const $config = globalThis.$config = {
...runtimeConfig.public,
...runtimeConfig.private
}
Expand Down
10 changes: 3 additions & 7 deletions src/runtime/app/nitro.client.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
import _global from '@nuxt/un/runtime/global'
import { $fetch } from 'ohmyfetch'

_global.process = _global.process || {};

// eslint-disable-next-line
(function () { const o = Date.now(); const t = () => Date.now() - o; _global.process.hrtime = _global.process.hrtime || ((o) => { const e = Math.floor(0.001 * (Date.now() - t())); const a = 0.001 * t(); let l = Math.floor(a) + e; let n = Math.floor(a % 1 * 1e9); return o && (l -= o[0], n -= o[1], n < 0 && (l--, n += 1e9)), [l, n] }) })()

global.$fetch = $fetch
if (!globalThis.$fetch) {
globalThis.$fetch = $fetch
}
7 changes: 5 additions & 2 deletions src/runtime/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,8 @@ app.use(() => import('../app/render').then(e => e.renderMiddleware), { lazy: tru
export const stack = app.stack
export const handle = useBase(process.env.ROUTER_BASE, app)
export const localCall = createCall(handle)
export const localFetch = createLocalFetch(localCall, global.fetch)
export const $fetch = global.$fetch = createFetch({ fetch: localFetch })
export const localFetch = createLocalFetch(localCall, globalThis.fetch)

export const $fetch = createFetch({ fetch: localFetch })

globalThis.$fetch = $fetch
2 changes: 1 addition & 1 deletion src/runtime/server/timing.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export const globalTiming = global.__timing__ || {
export const globalTiming = globalThis.__timing__ || {
start: () => 0,
end: () => 0,
metrics: []
Expand Down

0 comments on commit fb56216

Please sign in to comment.