From 517891d3aabcddf456321a52b9a7d477663da47e Mon Sep 17 00:00:00 2001 From: Adam Bradley Date: Tue, 28 Jul 2020 09:52:48 -0500 Subject: [PATCH] fix(sys): node sys prerender applyPrerenderGlobalPatch --- .../prerender/prerender-global-patch.ts | 39 ------------------- src/compiler/prerender/prerender-worker.ts | 11 ++++-- src/declarations/stencil-public-compiler.ts | 1 + src/sys/node/node-sys.ts | 28 +++++++++++++ 4 files changed, 36 insertions(+), 43 deletions(-) delete mode 100644 src/compiler/prerender/prerender-global-patch.ts diff --git a/src/compiler/prerender/prerender-global-patch.ts b/src/compiler/prerender/prerender-global-patch.ts deleted file mode 100644 index c3bb0685135..00000000000 --- a/src/compiler/prerender/prerender-global-patch.ts +++ /dev/null @@ -1,39 +0,0 @@ -import { join } from 'path'; -import { requireFunc } from '@utils'; - -export const patchNodeGlobal = (nodeGlobal: any, devServerHostUrl: string) => { - if (typeof nodeGlobal.fetch !== 'function') { - // webpack work-around/hack - const nodeFetch = requireFunc(join(__dirname, '..', 'sys', 'node', 'node-fetch.js')); - - nodeGlobal.fetch = (input: any, init: any) => { - if (typeof input === 'string') { - // fetch(url) w/ url string - const urlStr = normalizeUrl(input, devServerHostUrl); - return nodeFetch.fetch(urlStr, init); - } else { - // fetch(Request) w/ request object - input.url = normalizeUrl(input.url, devServerHostUrl); - return nodeFetch.fetch(input, init); - } - }; - - nodeGlobal.Headers = nodeFetch.Headers; - nodeGlobal.Request = nodeFetch.Request; - nodeGlobal.Response = nodeFetch.Response; - nodeGlobal.FetchError = nodeFetch.FetchError; - } -}; - -const normalizeUrl = (inputUrl: string, devServerHostUrl: string) => { - const requestUrl = new URL(inputUrl, devServerHostUrl); - return requestUrl.href; -}; - -export const patchWindowGlobal = (nodeGlobal: any, win: any) => { - win.fetch = nodeGlobal.fetch; - win.Headers = nodeGlobal.Headers; - win.Request = nodeGlobal.Request; - win.Response = nodeGlobal.Response; - win.FetchError = nodeGlobal.FetchError; -}; diff --git a/src/compiler/prerender/prerender-worker.ts b/src/compiler/prerender/prerender-worker.ts index 95df66a6af0..3dd5bc2f025 100644 --- a/src/compiler/prerender/prerender-worker.ts +++ b/src/compiler/prerender/prerender-worker.ts @@ -1,10 +1,9 @@ import * as d from '../../declarations'; import { addModulePreloads, excludeStaticComponents, minifyScriptElements, minifyStyleElements, removeModulePreloads, removeStencilScripts } from './prerender-optimize'; -import { catchError, isPromise, isRootPath, normalizePath, requireFunc } from '@utils'; +import { catchError, isPromise, isRootPath, normalizePath, requireFunc, isFunction } from '@utils'; import { crawlAnchorsForNextUrls } from './crawl-urls'; import { getHydrateOptions } from './prerender-hydrate-options'; import { getPrerenderConfig } from './prerender-config'; -import { patchNodeGlobal, patchWindowGlobal } from './prerender-global-patch'; const prerenderCtx = { componentGraph: null as Map, @@ -39,8 +38,12 @@ export const prerenderWorker = async (sys: d.CompilerSystem, prerenderRequest: d const doc = win.document; // patch this new window - patchNodeGlobal(globalThis, prerenderRequest.devServerHostUrl); - patchWindowGlobal(globalThis, win); + if (isFunction(sys.applyPrerenderGlobalPatch)) { + sys.applyPrerenderGlobalPatch({ + devServerHostUrl: prerenderRequest.devServerHostUrl, + window: win, + }); + } if (prerenderCtx.prerenderConfig == null) { prerenderCtx.prerenderConfig = getPrerenderConfig(results.diagnostics, prerenderRequest.prerenderConfigPath); diff --git a/src/declarations/stencil-public-compiler.ts b/src/declarations/stencil-public-compiler.ts index 94d73040deb..e9f26931220 100644 --- a/src/declarations/stencil-public-compiler.ts +++ b/src/declarations/stencil-public-compiler.ts @@ -823,6 +823,7 @@ export interface CompilerSystem { */ accessSync(p: string): boolean; applyGlobalPatch?(fromDir: string): Promise; + applyPrerenderGlobalPatch?(opts: { devServerHostUrl: string; window: any }): void; cacheStorage?: CacheStorage; checkVersion?: (logger: Logger, currentVersion: string) => Promise<() => void>; copy?(copyTasks: Required[], srcDir: string): Promise; diff --git a/src/sys/node/node-sys.ts b/src/sys/node/node-sys.ts index 4b861be8e76..6a2ed0a1cb2 100644 --- a/src/sys/node/node-sys.ts +++ b/src/sys/node/node-sys.ts @@ -118,6 +118,34 @@ export function createNodeSysNoWatch(c: { process?: any } = {}) { removeDestory(cb) { destroys.delete(cb); }, + applyPrerenderGlobalPatch(opts) { + if (typeof global.fetch !== 'function') { + const nodeFetch = require(path.join(__dirname, 'node-fetch.js')); + + global.fetch = (input: any, init: any) => { + if (typeof input === 'string') { + // fetch(url) w/ url string + const urlStr = new URL(input, opts.devServerHostUrl).href; + return nodeFetch.fetch(urlStr, init); + } else { + // fetch(Request) w/ request object + input.url = new URL(input.url, opts.devServerHostUrl).href; + return nodeFetch.fetch(input, init); + } + }; + + global.Headers = nodeFetch.Headers; + global.Request = nodeFetch.Request; + global.Response = nodeFetch.Response; + (global as any).FetchError = nodeFetch.FetchError; + } + + opts.window.fetch = global.fetch; + opts.window.Headers = global.Headers; + opts.window.Request = global.Request; + opts.window.Response = global.Response; + opts.window.FetchError = (global as any).FetchError; + }, checkVersion, copyFile(src, dst) { return new Promise(resolve => {