Skip to content

Commit

Permalink
fix: fonts.css isn't loaded in build mode (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
barelyhuman authored Jan 2, 2025
1 parent 3604bc4 commit 2dd82f7
Show file tree
Hide file tree
Showing 6 changed files with 678 additions and 651 deletions.
1 change: 1 addition & 0 deletions adex/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
"next": "bumpp"
},
"dependencies": {
"@barelyhuman/tiny-use": "^0.0.2",
"@dumbjs/preland": "^0.0.2",
"bumpp": "^9.4.1",
"dotenv": "^16.4.5",
Expand Down
53 changes: 33 additions & 20 deletions adex/runtime/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ import { dirname, join } from 'node:path'
import { fileURLToPath } from 'node:url'

//@ts-expect-error internal requires
import { mri, sirv } from 'adex/ssr'
import { mri, sirv, useMiddleware } from 'adex/ssr'

//@ts-expect-error vite virtual import
import { handler } from 'virtual:adex:handler'

import 'virtual:adex:global.css'
import 'virtual:adex:font.css'

const flags = mri(process.argv.slice(2))

Expand All @@ -27,6 +28,7 @@ const serverAssets = sirv(join(__dirname, './assets'), {

let islandsWereGenerated = existsSync(join(__dirname, './islands'))

// @ts-ignore
let islandAssets = (req, res, next) => {
next()
}
Expand All @@ -41,6 +43,7 @@ if (islandsWereGenerated) {

let clientWasGenerated = existsSync(join(__dirname, '../client'))

// @ts-ignore
let clientAssets = (req, res, next) => {
next()
}
Expand Down Expand Up @@ -163,17 +166,15 @@ function addDependencyAssets(template, pageRoute) {
const manifest = getClientManifest()
const filePath = pageRoute.startsWith('/') ? pageRoute.slice(1) : pageRoute

const { links: serverLinks, scripts: serverScripts } = manifestToHTML(
serverManifest,
filePath
)
const { links: serverLinks } = manifestToHTML(serverManifest, filePath)

const { links: clientLinks, scripts: clientScripts } = manifestToHTML(
manifest,
filePath
)

const links = [...serverLinks, ...clientLinks]
const scripts = [...serverScripts, ...clientScripts]
const scripts = [...clientScripts]

return template.replace(
'</head>',
Expand All @@ -182,20 +183,32 @@ function addDependencyAssets(template, pageRoute) {
}

http
.createServer((req, res) => {
const originalUrl = req.url
req.url = originalUrl.replace(/(\/?assets\/?)/, '/')
serverAssets(req, res, () => {
req.url = originalUrl.replace(/(\/?islands\/?)/, '/')
return islandAssets(req, res, () => {
req.url = originalUrl.replace(/(\/?client\/?)/, '/')
return clientAssets(req, res, () => {
req.url = originalUrl
defaultHandler(req, res)
})
})
})
})
.createServer(
useMiddleware(
async (req, res, next) => {
// @ts-expect-error shared-state between the middlewares
req.__originalUrl = req.url
// @ts-expect-error shared-state between the middlewares
req.url = req.__originalUrl.replace(/(\/?assets\/?)/, '/')
return serverAssets(req, res, next)
},
async (req, res, next) => {
// @ts-expect-error shared-state between the middlewares
req.url = req.__originalUrl.replace(/(\/?islands\/?)/, '/')
return islandAssets(req, res, next)
},
async (req, res, next) => {
// @ts-expect-error shared-state between the middlewares
req.url = req.__originalUrl.replace(/(\/?client\/?)/, '/')
return clientAssets(req, res, next)
},
async (req, res) => {
// @ts-expect-error shared-state between the middlewares
req.url = req.__originalUrl
return defaultHandler(req, res)
}
)
)
.listen(PORT, HOST, () => {
console.log(`Listening on ${HOST}:${PORT}`)
})
1 change: 1 addition & 0 deletions adex/src/ssr.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export { toStatic } from 'hoofd/preact'
export { renderToString } from 'preact-render-to-string'
export { parse as pathToRegex } from 'regexparam'
export { use as useMiddleware } from '@barelyhuman/tiny-use'
1 change: 1 addition & 0 deletions adex/src/ssr.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ export { default as sirv } from 'sirv'
export { default as mri } from 'mri'
export { parse as pathToRegex } from 'regexparam'
export { toStatic } from 'hoofd/preact'
export { use as useMiddleware } from '@barelyhuman/tiny-use'
12 changes: 7 additions & 5 deletions adex/src/vite.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export function adex({ fonts, islands = false } = {}) {
'virtual:adex:client',
readFileSync(join(__dirname, '../runtime/client.js'), 'utf8')
),
fonts && Object.keys(fonts).length > 0 && addFontsPlugin(fonts),
addFontsPlugin(fonts),
adexServerBuilder({ islands }),
!islands && adexClientBuilder(),
islands && adexIslandsBuilder(),
Expand Down Expand Up @@ -345,10 +345,12 @@ function adexServerBuilder({ islands = false } = {}) {
if (!importer) return
const importerFromRoot = importer.replace(resolve(cfg.root), '')
const resolvedCss = await this.resolve(id, importer, meta)
devCSSMap.set(
importerFromRoot,
(devCSSMap.get(importer) ?? []).concat(resolvedCss.id)
)
if (resolvedCss) {
devCSSMap.set(
importerFromRoot,
(devCSSMap.get(importer) ?? []).concat(resolvedCss.id)
)
}
return
}
},
Expand Down
Loading

0 comments on commit 2dd82f7

Please sign in to comment.