@@ -23,11 +23,19 @@ const cwd = process.cwd()
2323const islandsDir = join ( cwd , '.islands' )
2424let 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 }
0 commit comments