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

Commit 0036d1e

Browse files
committed
refactor(framework): move style module to core
1 parent 6e17a8d commit 0036d1e

File tree

5 files changed

+42
-48
lines changed

5 files changed

+42
-48
lines changed

framework/core/style.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import util from '../../shared/util.ts'
2+
3+
export const serverStyles: Map<string, string> = new Map()
4+
5+
export function removeCSS(id: string) {
6+
const { document } = window as any
7+
Array.from(document.head.children).forEach((el: any) => {
8+
if (el.getAttribute('data-module-id') === id) {
9+
document.head.removeChild(el)
10+
}
11+
})
12+
}
13+
14+
export function applyCSS(id: string, css: string) {
15+
if (util.inDeno()) {
16+
serverStyles.set(id, css)
17+
} else {
18+
const { document } = window as any
19+
const ssrStyle = Array.from<any>(document.head.children).find((el: any) => {
20+
return el.getAttribute('data-module-id') === id && el.hasAttribute('ssr')
21+
})
22+
if (ssrStyle) {
23+
ssrStyle.removeAttribute('ssr')
24+
} else {
25+
const prevStyleEls = Array.from(document.head.children).filter((el: any) => {
26+
return el.getAttribute('data-module-id') === id
27+
})
28+
const styleEl = document.createElement('style')
29+
styleEl.type = 'text/css'
30+
styleEl.appendChild(document.createTextNode(css))
31+
styleEl.setAttribute('data-module-id', id)
32+
document.head.appendChild(styleEl)
33+
if (prevStyleEls.length > 0) {
34+
prevStyleEls.forEach(el => document.head.removeChild(el))
35+
}
36+
return styleEl
37+
}
38+
}
39+
}

framework/react/context.ts

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,6 @@ export const RouterContext = createNamedContext<RouterURL>({
1010
query: new URLSearchParams(),
1111
}, 'RouterContext')
1212

13-
type SuspenseContextProps = {
14-
loading: boolean
15-
}
16-
export const SuspenseContext = createNamedContext<SuspenseContextProps>({
17-
loading: false
18-
}, 'SuspenseContext')
19-
2013
type RendererContextProps = {
2114
headElements: Map<string, { type: string, props: Record<string, any> }>
2215
scriptsElements: Map<string, { type: string, props: Record<string, any> }>

framework/react/renderer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ import { renderToString } from 'https://esm.sh/react-dom/server'
44
import util from '../../shared/util.ts'
55
import type { RenderResult, RouterURL } from '../../types.ts'
66
import events from '../core/events.ts'
7+
import { serverStyles } from "../core/style.ts"
78
import { RendererContext, RouterContext } from './context.ts'
89
import { AsyncUseDenoError, E400MissingComponent, E404Page } from './error.ts'
910
import { createPageProps } from './pageprops.ts'
10-
import { serverStyles } from './style.ts'
1111
import { isLikelyReactComponent } from './util.ts'
1212

1313
export async function render(

framework/react/style.ts

Lines changed: 1 addition & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import type { StyleHTMLAttributes } from 'https://esm.sh/react'
22
import { useEffect } from 'https://esm.sh/react'
3-
import util from '../../shared/util.ts'
4-
5-
export const serverStyles: Map<string, string> = new Map()
3+
import { applyCSS, removeCSS } from "../core/style.ts"
64

75
export default function Style({ children, ...rest }: StyleHTMLAttributes<{}>) {
86
const css = children?.toLocaleString()
@@ -16,39 +14,3 @@ export default function Style({ children, ...rest }: StyleHTMLAttributes<{}>) {
1614

1715
return null
1816
}
19-
20-
export function removeCSS(id: string) {
21-
const { document } = window as any
22-
Array.from(document.head.children).forEach((el: any) => {
23-
if (el.getAttribute('data-module-id') === id) {
24-
document.head.removeChild(el)
25-
}
26-
})
27-
}
28-
29-
export function applyCSS(id: string, css: string) {
30-
if (util.inDeno()) {
31-
serverStyles.set(id, css)
32-
} else {
33-
const { document } = window as any
34-
const ssrStyle = Array.from<any>(document.head.children).find((el: any) => {
35-
return el.getAttribute('data-module-id') === id && el.hasAttribute('ssr')
36-
})
37-
if (ssrStyle) {
38-
ssrStyle.removeAttribute('ssr')
39-
} else {
40-
const prevStyleEls = Array.from(document.head.children).filter((el: any) => {
41-
return el.getAttribute('data-module-id') === id
42-
})
43-
const styleEl = document.createElement('style')
44-
styleEl.type = 'text/css'
45-
styleEl.appendChild(document.createTextNode(css))
46-
styleEl.setAttribute('data-module-id', id)
47-
document.head.appendChild(styleEl)
48-
if (prevStyleEls.length > 0) {
49-
prevStyleEls.forEach(el => document.head.removeChild(el))
50-
}
51-
return styleEl
52-
}
53-
}
54-
}

server/app.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1021,7 +1021,7 @@ export class Appliaction {
10211021
if (loader === 'css') {
10221022
const css = await this.preprocessCSS(sourceCode)
10231023
sourceCode = [
1024-
`import { applyCSS } from "${alephPkgUrl}/framework/${this.config.framework}/style.ts";`,
1024+
`import { applyCSS } from "${alephPkgUrl}/framework/core/style.ts";`,
10251025
`export default function __applyCSS() {`,
10261026
` applyCSS(${JSON.stringify(url)}, ${JSON.stringify(css)});`,
10271027
`}`,

0 commit comments

Comments
 (0)