Skip to content

Commit d8ac285

Browse files
authored
refactor: add a very simple server adapter api (#23)
* refactor: add a very simple server adapter api * refactor: abstract handler deps to the virtual file
1 parent 8a9756f commit d8ac285

14 files changed

+367
-238
lines changed

adex/package.json

+8-2
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,17 @@
7171
"@preact/preset-vite": "^2.8.2",
7272
"@types/node": "^20.14.10",
7373
"autoprefixer": "^10.4.19",
74-
"postcss": "^8.4.39",
7574
"tailwindcss": "^3.4.4",
76-
"vite": "^5.3.1"
75+
"vite": "^5.3.1",
76+
"adex-adapter-node": "workspace:*"
77+
},
78+
"peerDependenciesMeta": {
79+
"adex-adapter-node": {
80+
"optional": true
81+
}
7782
},
7883
"peerDependencies": {
84+
"adex-adapter-node": ">=0.0.15",
7985
"@preact/preset-vite": ">=2.8.2",
8086
"preact": "^10.22.0"
8187
}

adex/runtime/server.js

-214
This file was deleted.

adex/src/ssr.d.ts

+2
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,5 @@ export { toStatic } from 'hoofd/preact'
22
export { renderToString } from 'preact-render-to-string'
33
export { parse as pathToRegex } from 'regexparam'
44
export { use as useMiddleware } from '@barelyhuman/tiny-use'
5+
export { default as sirv } from 'sirv'
6+
export { default as mri } from 'mri'

adex/src/vite.d.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
import { Plugin } from 'vite'
2-
import type { Options as FontOptions } from './fonts'
2+
import type { Options as FontOptions } from './fonts.js'
3+
4+
export type Adapters = 'node'
35

46
export interface AdexOptions {
57
fonts?: FontOptions
68
islands?: boolean
9+
adapter?: Adapters
710
}
811

912
export function adex(options: AdexOptions): Plugin[]

adex/src/vite.js

+72-2
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,19 @@ const cwd = process.cwd()
2323
const islandsDir = join(cwd, '.islands')
2424
let runningIslandBuild = false
2525

26+
const adapterMap = {
27+
node: 'adex-adapter-node',
28+
}
29+
2630
/**
2731
* @param {import("./vite.js").AdexOptions} [options]
2832
* @returns
2933
*/
30-
export function adex({ fonts, islands = false } = {}) {
34+
export function adex({
35+
fonts,
36+
islands = false,
37+
adapter: adapter = 'node',
38+
} = {}) {
3139
return [
3240
preactPages({
3341
root: '/src/pages',
@@ -49,7 +57,68 @@ export function adex({ fonts, islands = false } = {}) {
4957
),
5058
createVirtualModule(
5159
'virtual:adex:server',
52-
readFileSync(join(__dirname, '../runtime/server.js'), 'utf8')
60+
`import { createServer } from '${adapterMap[adapter]}'
61+
import { dirname, join } from 'node:path'
62+
import { fileURLToPath } from 'node:url'
63+
import { existsSync, readFileSync } from 'node:fs'
64+
import { env } from 'adex/env'
65+
66+
import 'virtual:adex:font.css'
67+
import 'virtual:adex:global.css'
68+
69+
const __dirname = dirname(fileURLToPath(import.meta.url))
70+
71+
const PORT = parseInt(env.get('PORT', '3000'), 10)
72+
const HOST = env.get('HOST', 'localhost')
73+
74+
const paths = {
75+
assets: join(__dirname, './assets'),
76+
islands: join(__dirname, './islands'),
77+
client: join(__dirname, '../client'),
78+
}
79+
80+
function getServerManifest() {
81+
const manifestPath = join(__dirname, 'manifest.json')
82+
if (existsSync(manifestPath)) {
83+
const manifestFile = readFileSync(manifestPath, 'utf8')
84+
return parseManifest(manifestFile)
85+
}
86+
return {}
87+
}
88+
89+
function getClientManifest() {
90+
const manifestPath = join(__dirname, '../client/manifest.json')
91+
if (existsSync(manifestPath)) {
92+
const manifestFile = readFileSync(manifestPath, 'utf8')
93+
return parseManifest(manifestFile)
94+
}
95+
return {}
96+
}
97+
98+
function parseManifest(manifestString) {
99+
try {
100+
const manifestJSON = JSON.parse(manifestString)
101+
return manifestJSON
102+
} catch (err) {
103+
return {}
104+
}
105+
}
106+
107+
const server = createServer({
108+
port: PORT,
109+
host: HOST,
110+
adex:{
111+
manifests:{server:getServerManifest(),client:getClientManifest()},
112+
paths,
113+
}
114+
})
115+
116+
if ('run' in server) {
117+
server.run()
118+
}
119+
120+
export default server.fetch
121+
`
53122
),
54123
createVirtualModule(
55124
'virtual:adex:client',
@@ -333,6 +402,7 @@ function adexServerBuilder({ islands = false } = {}) {
333402
input: {
334403
index: input,
335404
},
405+
external: ['adex/ssr'],
336406
},
337407
},
338408
}

lerna.json

+1-5
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,5 @@
33
"version": "0.0.15",
44
"changelogPreset": "angular",
55
"npmClient": "pnpm",
6-
"packages": [
7-
"adex",
8-
"create-adex",
9-
"playground"
10-
]
6+
"packages": ["adex", "create-adex", "playground", "adex-adapter-node"]
117
}

package.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
"play": "pnpm --filter='playground' -r dev",
1111
"test": "echo 'true'",
1212
"publish:ci": "lerna publish from-git --registry 'https://registry.npmjs.org' --yes",
13-
"next": "lerna version --sync-workspace-lock"
13+
"next": "lerna version --sync-workspace-lock",
14+
"nuke": "pnpm -r exec rm -rvf node_modules"
1415
},
1516
"license": "MIT",
1617
"prettier": "@barelyhuman/prettier-config",
@@ -23,8 +24,7 @@
2324
},
2425
"pnpm": {
2526
"overrides": {
26-
"cross-spawn@>=7.0.0 <7.0.5": ">=7.0.5",
27-
"nanoid@<3.3.8": ">=3.3.8"
27+
"cross-spawn@>=7.0.0 <7.0.5": ">=7.0.5"
2828
}
2929
}
3030
}

packages/adaptors/node/lib/index.d.ts

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
type ServerOut = {
2+
run: () => any
3+
fetch: undefined
4+
}
5+
6+
export const createServer: ({ port: number, host: string }) => ServerOut

0 commit comments

Comments
 (0)