Skip to content
This repository was archived by the owner on Jul 6, 2025. It is now read-only.

Commit 6d46e39

Browse files
author
ije
committed
refactor: improve dom polyfill
1 parent b0092ad commit 6d46e39

File tree

3 files changed

+29
-30
lines changed

3 files changed

+29
-30
lines changed

cli.ts

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -160,33 +160,6 @@ async function main() {
160160
}
161161
}
162162

163-
// add virtual browser global objects
164-
if (command !== 'init' && command !== 'upgrade') {
165-
const { createHTMLDocument } = await import('./vendor/deno-dom/document.ts')
166-
Object.assign(globalThis, {
167-
document: createHTMLDocument(),
168-
location: {
169-
protocol: 'http:',
170-
host: 'localhost',
171-
hostname: 'localhost',
172-
port: '',
173-
href: 'https://localhost/',
174-
origin: 'https://localhost',
175-
pathname: '/',
176-
search: '',
177-
hash: '',
178-
reload() { },
179-
replace() { },
180-
toString() { return this.href },
181-
},
182-
innerWidth: 1920,
183-
innerHeight: 1080,
184-
devicePixelRatio: 1,
185-
$RefreshReg$: () => { },
186-
$RefreshSig$: () => (type: any) => type,
187-
})
188-
}
189-
190163
const { default: cmd } = await import(`./cli/${command}.ts`)
191164
if (command === 'upgrade') {
192165
await cmd(argOptions.v || argOptions.version || 'latest')

project.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { colors, ensureDir, fromStreamReader, path, ServerRequest, Sha1, walk }
1313
import { compile } from './tsc/compile.ts'
1414
import type { AlephEnv, APIHandler, Config, RouterURL } from './types.ts'
1515
import util, { hashShort, MB, reHashJs, reHttp, reLocaleID, reMDExt, reModuleExt, reStyleModuleExt } from './util.ts'
16+
import { createHTMLDocument } from './vendor/deno-dom/document.ts'
1617
import less from './vendor/less/less.js'
1718
import { version } from './version.ts'
1819

@@ -1450,6 +1451,30 @@ export class Project {
14501451
}
14511452
}
14521453

1454+
Object.assign(globalThis, {
1455+
__createHTMLDocument: () => createHTMLDocument(),
1456+
document: createHTMLDocument(),
1457+
location: {
1458+
protocol: 'http:',
1459+
host: 'localhost',
1460+
hostname: 'localhost',
1461+
port: '',
1462+
href: 'https://localhost/',
1463+
origin: 'https://localhost',
1464+
pathname: '/',
1465+
search: '',
1466+
hash: '',
1467+
reload() { },
1468+
replace() { },
1469+
toString() { return this.href },
1470+
},
1471+
innerWidth: 1920,
1472+
innerHeight: 1080,
1473+
devicePixelRatio: 1,
1474+
$RefreshReg$: () => { },
1475+
$RefreshSig$: () => (type: any) => type,
1476+
})
1477+
14531478
/** inject HMR and React Fast Referesh helper code */
14541479
export function injectHmr({ id, sourceFilePath, jsContent }: Module): string {
14551480
let hmrImportPath = getRelativePath(

renderer.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import unescape from 'https://esm.sh/lodash/unescape?no-check'
21
import React, { ComponentType, ReactElement } from 'https://esm.sh/react'
32
import { renderToString } from 'https://esm.sh/react-dom/server'
43
import { RendererContext, RouterContext } from './context.ts'
@@ -63,13 +62,15 @@ export async function renderPage(
6362
headElements: new Map(),
6463
scriptsElements: new Map()
6564
}
65+
const { __createHTMLDocument } = (window as any)
6666
const { __buildMode, __buildTarget } = (window as any).ALEPH.ENV as AlephEnv
6767
const data: Record<string, any> = {}
6868
const useDenEvent = `useDeno://${url.pathname + '?' + url.query.toString()}`
6969
const useDenoAsyncCalls: Array<Promise<any>> = []
7070

7171
Object.assign(window, {
7272
[`__asyncData_${useDenEvent}`]: {},
73+
document: __createHTMLDocument(),
7374
location: {
7475
protocol: 'http:',
7576
host: 'localhost',
@@ -155,9 +156,9 @@ export async function renderPage(
155156
if (dangerouslySetInnerHTML && util.isNEString(dangerouslySetInnerHTML.__html)) {
156157
ret.scripts.push({ ...attrs, innerText: dangerouslySetInnerHTML.__html })
157158
} if (util.isNEString(children)) {
158-
ret.scripts.push({ ...attrs, innerText: unescape(children) })
159+
ret.scripts.push({ ...attrs, innerText: children })
159160
} else if (util.isNEArray(children)) {
160-
ret.scripts.push({ ...attrs, innerText: unescape(children.join('')) })
161+
ret.scripts.push({ ...attrs, innerText: children.join('') })
161162
} else {
162163
ret.scripts.push(props)
163164
}

0 commit comments

Comments
 (0)