Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: add a very simple server adapter api #23

Merged
merged 2 commits into from
Jan 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions adex/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,17 @@
"@preact/preset-vite": "^2.8.2",
"@types/node": "^20.14.10",
"autoprefixer": "^10.4.19",
"postcss": "^8.4.39",
"tailwindcss": "^3.4.4",
"vite": "^5.3.1"
"vite": "^5.3.1",
"adex-adapter-node": "workspace:*"
},
"peerDependenciesMeta": {
"adex-adapter-node": {
"optional": true
}
},
"peerDependencies": {
"adex-adapter-node": ">=0.0.15",
"@preact/preset-vite": ">=2.8.2",
"preact": "^10.22.0"
}
Expand Down
214 changes: 0 additions & 214 deletions adex/runtime/server.js

This file was deleted.

2 changes: 2 additions & 0 deletions adex/src/ssr.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@ 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'
export { default as sirv } from 'sirv'
export { default as mri } from 'mri'
5 changes: 4 additions & 1 deletion adex/src/vite.d.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import { Plugin } from 'vite'
import type { Options as FontOptions } from './fonts'
import type { Options as FontOptions } from './fonts.js'

export type Adapters = 'node'

export interface AdexOptions {
fonts?: FontOptions
islands?: boolean
adapter?: Adapters
}

export function adex(options: AdexOptions): Plugin[]
74 changes: 72 additions & 2 deletions adex/src/vite.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,19 @@ const cwd = process.cwd()
const islandsDir = join(cwd, '.islands')
let runningIslandBuild = false

const adapterMap = {
node: 'adex-adapter-node',
}

/**
* @param {import("./vite.js").AdexOptions} [options]
* @returns
*/
export function adex({ fonts, islands = false } = {}) {
export function adex({
fonts,
islands = false,
adapter: adapter = 'node',
} = {}) {
return [
preactPages({
root: '/src/pages',
Expand All @@ -49,7 +57,68 @@ export function adex({ fonts, islands = false } = {}) {
),
createVirtualModule(
'virtual:adex:server',
readFileSync(join(__dirname, '../runtime/server.js'), 'utf8')
`import { createServer } from '${adapterMap[adapter]}'
import { dirname, join } from 'node:path'
import { fileURLToPath } from 'node:url'
import { existsSync, readFileSync } from 'node:fs'
import { env } from 'adex/env'

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

const __dirname = dirname(fileURLToPath(import.meta.url))

const PORT = parseInt(env.get('PORT', '3000'), 10)
const HOST = env.get('HOST', 'localhost')

const paths = {
assets: join(__dirname, './assets'),
islands: join(__dirname, './islands'),
client: join(__dirname, '../client'),
}

function getServerManifest() {
const manifestPath = join(__dirname, 'manifest.json')
if (existsSync(manifestPath)) {
const manifestFile = readFileSync(manifestPath, 'utf8')
return parseManifest(manifestFile)
}
return {}
}

function getClientManifest() {
const manifestPath = join(__dirname, '../client/manifest.json')
if (existsSync(manifestPath)) {
const manifestFile = readFileSync(manifestPath, 'utf8')
return parseManifest(manifestFile)
}
return {}
}

function parseManifest(manifestString) {
try {
const manifestJSON = JSON.parse(manifestString)
return manifestJSON
} catch (err) {
return {}
}
}

const server = createServer({
port: PORT,
host: HOST,
adex:{
manifests:{server:getServerManifest(),client:getClientManifest()},
paths,
}
})

if ('run' in server) {
server.run()
}

export default server.fetch
`
),
createVirtualModule(
'virtual:adex:client',
Expand Down Expand Up @@ -333,6 +402,7 @@ function adexServerBuilder({ islands = false } = {}) {
input: {
index: input,
},
external: ['adex/ssr'],
},
},
}
Expand Down
6 changes: 1 addition & 5 deletions lerna.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,5 @@
"version": "0.0.15",
"changelogPreset": "angular",
"npmClient": "pnpm",
"packages": [
"adex",
"create-adex",
"playground"
]
"packages": ["adex", "create-adex", "playground", "adex-adapter-node"]
}
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
"play": "pnpm --filter='playground' -r dev",
"test": "echo 'true'",
"publish:ci": "lerna publish from-git --registry 'https://registry.npmjs.org' --yes",
"next": "lerna version --sync-workspace-lock"
"next": "lerna version --sync-workspace-lock",
"nuke": "pnpm -r exec rm -rvf node_modules"
},
"license": "MIT",
"prettier": "@barelyhuman/prettier-config",
Expand All @@ -23,8 +24,7 @@
},
"pnpm": {
"overrides": {
"cross-spawn@>=7.0.0 <7.0.5": ">=7.0.5",
"nanoid@<3.3.8": ">=3.3.8"
"cross-spawn@>=7.0.0 <7.0.5": ">=7.0.5"
}
}
}
6 changes: 6 additions & 0 deletions packages/adaptors/node/lib/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
type ServerOut = {
run: () => any
fetch: undefined
}

export const createServer: ({ port: number, host: string }) => ServerOut
Loading
Loading