fix: use globalThis instead of global (#59)

This commit is contained in:
pooya parsa 2021-04-06 14:10:57 +02:00 committed by GitHub
parent 7f97015c74
commit b12cbc5ed2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 20 additions and 27 deletions

View File

@ -1,11 +1,12 @@
import { $fetch } from 'ohmyfetch' import { $fetch } from 'ohmyfetch'
import _global from '@nuxt/un/runtime/global'
import logs from 'nuxt/app/plugins/logs.client.dev' import logs from 'nuxt/app/plugins/logs.client.dev'
import progress from 'nuxt/app/plugins/progress.client' import progress from 'nuxt/app/plugins/progress.client'
<% const plugins = app.plugins.filter(p => p.mode === 'client').map(p => p.src) %> <% const plugins = app.plugins.filter(p => p.mode === 'client').map(p => p.src) %>
<%= nxt.importSources(plugins) %> <%= nxt.importSources(plugins) %>
_global.$fetch = $fetch if (!globalThis.$fetch) {
globalThis.$fetch = $fetch
}
const plugins = [ const plugins = [
progress, progress,

View File

@ -50,7 +50,7 @@ export function dirnames (): Plugin {
name: 'dirnames', name: 'dirnames',
renderChunk (code, chunk) { renderChunk (code, chunk) {
return { 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 map: null
} }
} }

View File

@ -3,25 +3,18 @@ import type { Plugin, RenderedChunk } from 'rollup'
export interface Options { } export interface Options { }
const TIMING = 'global.__timing__' const TIMING = 'globalThis.__timing__'
const iife = code => `(function() { ${code.trim()} })();`.replace(/\n/g, '') const iife = code => `(function() { ${code.trim()} })();`.replace(/\n/g, '')
// https://gist.github.com/pi0/1476085924f8a2eb1df85929c20cb43f const HELPER = iife(`
const POLYFILL = `const global="undefined"!=typeof globalThis?globalThis:void 0!==o?o:"undefined"!=typeof self?self:{}; const start = () => Date.now();
global.process = global.process || {}; const end = s => Date.now() - s;
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 _s = {}; const _s = {};
const metrics = []; 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)'); }; 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 { export function timing (_opts: Options = {}): Plugin {

View File

@ -8,7 +8,7 @@ for (const type of ['private', 'public']) {
} }
} }
const $config = global.$config = { const $config = globalThis.$config = {
...runtimeConfig.public, ...runtimeConfig.public,
...runtimeConfig.private ...runtimeConfig.private
} }

View File

@ -1,9 +1,5 @@
import _global from '@nuxt/un/runtime/global'
import { $fetch } from 'ohmyfetch' import { $fetch } from 'ohmyfetch'
_global.process = _global.process || {}; if (!globalThis.$fetch) {
globalThis.$fetch = $fetch
// 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

View File

@ -20,5 +20,8 @@ app.use(() => import('../app/render').then(e => e.renderMiddleware), { lazy: tru
export const stack = app.stack export const stack = app.stack
export const handle = useBase(process.env.ROUTER_BASE, app) export const handle = useBase(process.env.ROUTER_BASE, app)
export const localCall = createCall(handle) export const localCall = createCall(handle)
export const localFetch = createLocalFetch(localCall, global.fetch) export const localFetch = createLocalFetch(localCall, globalThis.fetch)
export const $fetch = global.$fetch = createFetch({ fetch: localFetch })
export const $fetch = createFetch({ fetch: localFetch })
globalThis.$fetch = $fetch

View File

@ -1,4 +1,4 @@
export const globalTiming = global.__timing__ || { export const globalTiming = globalThis.__timing__ || {
start: () => 0, start: () => 0,
end: () => 0, end: () => 0,
metrics: [] metrics: []