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

Commit 1d75cc5

Browse files
author
Je
committed
refactor: improve custom scripts render
1 parent 87c0533 commit 1d75cc5

File tree

3 files changed

+20
-8
lines changed

3 files changed

+20
-8
lines changed

head.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,16 @@ export async function renderHead(styles?: { url: string, hash: string, async?: b
4848
return tags
4949
}
5050

51-
export async function renderScripts() {
51+
export function renderScripts() {
5252
const scripts: Record<string, any>[] = []
5353
serverScriptsElements.forEach(({ props }) => {
54-
const { children, ...attrs } = props
55-
if (util.isNEString(children)) {
56-
scripts.push({ ...attrs, innerText: unescape(children).trim() })
54+
const { children, dangerouslySetInnerHTML, ...attrs } = props
55+
if (dangerouslySetInnerHTML && util.isNEString(dangerouslySetInnerHTML.__html)) {
56+
scripts.push({ ...attrs, innerText: dangerouslySetInnerHTML.__html })
57+
} if (util.isNEString(children)) {
58+
scripts.push({ ...attrs, innerText: unescape(children) })
5759
} else if (util.isNEArray(children)) {
58-
scripts.push({ ...attrs, innerText: unescape(children.join('')).trim() })
60+
scripts.push({ ...attrs, innerText: unescape(children.join('')) })
5961
} else {
6062
scripts.push(props)
6163
}

html.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ export function createHtml({
3131
return `<script>${v}</script>`
3232
} else if (util.isNEString(v.innerText)) {
3333
const { innerText, ...rest } = v
34-
return `<script${attrString(rest)}>${innerText}</script>`
34+
return `<script${attrString(rest)}>${eol}${innerText}${eol}${indent}</script>`
3535
} else if (util.isNEString(v.src) && !v.preload) {
3636
return `<script${attrString(v)}></script>`
3737
} else {

project.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1294,7 +1294,12 @@ export class Project {
12941294
...pageModuleTree.map(({ id }) => this._lookupAsyncDeps(id).filter(({ url }) => reStyleModuleExt.test(url)))
12951295
].flat())
12961296
ret.head = head
1297-
ret.scripts = await this.#renderer.renderScripts()
1297+
ret.scripts = await Promise.all(this.#renderer.renderScripts().map(async (script: Record<string, any>) => {
1298+
if (!this.isDev && script.innerText) {
1299+
return { ...script, innerText: (await minify(script.innerText)).code }
1300+
}
1301+
return script
1302+
}))
12981303
ret.body = `<main>${html}</main>`
12991304
ret.data = data
13001305
this.#rendered.get(url.pagePath)!.set(key, ret)
@@ -1320,7 +1325,12 @@ export class Project {
13201325
e404Module ? this._lookupAsyncDeps(e404Module.id).filter(({ url }) => reStyleModuleExt.test(url)) : []
13211326
].flat())
13221327
ret.head = head
1323-
ret.scripts = await this.#renderer.renderScripts()
1328+
ret.scripts = await Promise.all(this.#renderer.renderScripts().map(async (script: Record<string, any>) => {
1329+
if (!this.isDev && script.innerText) {
1330+
return { ...script, innerText: (await minify(script.innerText)).code }
1331+
}
1332+
return script
1333+
}))
13241334
ret.body = `<main>${html}</main>`
13251335
ret.data = data
13261336
} catch (err) {

0 commit comments

Comments
 (0)