vitrify 0.2.4 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (47) hide show
  1. package/dist/app-urls.js +1 -2
  2. package/dist/bin/build.js +0 -43
  3. package/dist/bin/cli.js +29 -7
  4. package/dist/bin/dev.js +58 -67
  5. package/dist/frameworks/vue/fastify-ssr-plugin.js +67 -23
  6. package/dist/frameworks/vue/prerender.js +3 -3
  7. package/dist/frameworks/vue/server.js +9 -10
  8. package/dist/helpers/collect-css-ssr.js +57 -0
  9. package/dist/helpers/logger.js +0 -72
  10. package/dist/index.js +268 -122
  11. package/dist/plugins/quasar.js +13 -106
  12. package/dist/types/bin/build.d.ts +2 -2
  13. package/dist/types/bin/dev.d.ts +39 -3
  14. package/dist/types/frameworks/vue/fastify-ssr-plugin.d.ts +6 -3
  15. package/dist/types/frameworks/vue/prerender.d.ts +3 -3
  16. package/dist/types/frameworks/vue/server.d.ts +9 -5
  17. package/dist/types/helpers/collect-css-ssr.d.ts +10 -0
  18. package/dist/types/helpers/logger.d.ts +0 -19
  19. package/dist/types/helpers/routes.d.ts +1 -1
  20. package/dist/types/index.d.ts +1 -1
  21. package/dist/types/plugins/index.d.ts +1 -1
  22. package/dist/types/vitrify-config.d.ts +20 -16
  23. package/package.json +32 -32
  24. package/src/node/app-urls.ts +1 -2
  25. package/src/node/bin/build.ts +2 -49
  26. package/src/node/bin/cli.ts +36 -8
  27. package/src/node/bin/dev.ts +89 -75
  28. package/src/node/bin/test.ts +0 -3
  29. package/src/node/frameworks/vue/fastify-ssr-plugin.ts +80 -26
  30. package/src/node/frameworks/vue/prerender.ts +5 -5
  31. package/src/node/frameworks/vue/server.ts +22 -16
  32. package/src/node/helpers/collect-css-ssr.ts +77 -0
  33. package/src/node/helpers/logger.ts +0 -87
  34. package/src/node/index.ts +302 -137
  35. package/src/node/plugins/index.ts +1 -1
  36. package/src/node/plugins/quasar.ts +14 -111
  37. package/src/node/vitrify-config.ts +31 -16
  38. package/src/vite/fastify/entry.ts +11 -0
  39. package/src/vite/fastify/server.ts +10 -0
  40. package/src/vite/vue/index.html +1 -0
  41. package/src/vite/vue/main.ts +5 -20
  42. package/src/vite/vue/ssr/app.ts +25 -0
  43. package/src/vite/vue/ssr/entry-server.ts +13 -1
  44. package/src/vite/vue/ssr/fastify-ssr-plugin.ts +2 -118
  45. package/src/vite/vue/ssr/prerender.ts +2 -2
  46. package/src/vite/vue/ssr/server.ts +24 -15
  47. package/src/node/helpers/ssr.ts.bak +0 -52
@@ -2,7 +2,13 @@
2
2
  import cac from 'cac'
3
3
  import { getAppDir, parsePath } from '../app-urls.js'
4
4
  import { printHttpServerUrls } from '../helpers/logger.js'
5
- import type { ViteDevServer } from 'vite'
5
+ import type {
6
+ ConfigEnv,
7
+ ResolvedConfig,
8
+ UserConfig,
9
+ UserConfigExport,
10
+ ViteDevServer
11
+ } from 'vite'
6
12
  import type { Server } from 'net'
7
13
 
8
14
  const cli = cac('vitrify')
@@ -17,7 +23,7 @@ cli
17
23
  .action(async (options) => {
18
24
  const { build } = await import('./build.js')
19
25
  let appDir: URL
20
- let prerender, ssrFunctions
26
+ let prerender, onRenderedHooks
21
27
  if (options.appDir) {
22
28
  if (options.appDir.slice(-1) !== '/') options.appDir += '/'
23
29
  appDir = new URL(`file://${options.appDir}`)
@@ -45,6 +51,13 @@ cli
45
51
  outDir: new URL('spa/', baseOutDir).pathname
46
52
  })
47
53
  break
54
+ case 'fastify':
55
+ await build({
56
+ ssr: 'fastify',
57
+ ...args,
58
+ outDir: new URL('server/', baseOutDir).pathname
59
+ })
60
+ break
48
61
  case 'ssr':
49
62
  await build({
50
63
  ssr: 'client',
@@ -68,7 +81,7 @@ cli
68
81
  ...args,
69
82
  outDir: new URL('ssr/server/', baseOutDir).pathname
70
83
  })
71
- ;({ prerender, ssrFunctions } = await import(
84
+ ;({ prerender, onRenderedHooks } = await import(
72
85
  new URL('ssr/server/prerender.mjs', baseOutDir).pathname
73
86
  ))
74
87
  prerender({
@@ -78,7 +91,7 @@ cli
78
91
  .pathname,
79
92
  entryServerPath: new URL('ssr/server/entry-server.mjs', baseOutDir)
80
93
  .pathname,
81
- ssrFunctions
94
+ onRenderedHooks
82
95
  })
83
96
  break
84
97
  default:
@@ -96,26 +109,41 @@ cli
96
109
  { default: '127.0.0.1' }
97
110
  )
98
111
  .option('--appDir [appDir]', 'Application directory')
112
+ .option('--app [app]', 'Fastify app instance path')
99
113
  .option('--publicDir [publicDir]', 'Public directory')
100
114
  .action(async (options) => {
101
115
  let server: Server
102
- let vite: ViteDevServer
116
+ let config: ResolvedConfig
103
117
  if (options.host === true) {
104
118
  options.host = '0.0.0.0'
105
119
  }
106
120
  const { createServer } = await import('./dev.js')
107
121
  const cwd = (await import('../app-urls.js')).getCwd()
122
+ let app
123
+ const appPath = parsePath(options.app, cwd)?.pathname
124
+ if (appPath) {
125
+ app = await import(appPath)
126
+ }
127
+
108
128
  switch (options.mode) {
109
129
  case 'ssr':
110
- ;({ server, vite } = await createServer({
130
+ ;({ server, config } = await createServer({
111
131
  mode: 'ssr',
112
132
  host: options.host,
113
133
  appDir: parsePath(options.appDir, cwd),
114
134
  publicDir: parsePath(options.publicDir, cwd)
115
135
  }))
116
136
  break
137
+ case 'fastify':
138
+ ;({ server, config } = await createServer({
139
+ mode: 'fastify',
140
+ host: options.host,
141
+ appDir: parsePath(options.appDir, cwd),
142
+ publicDir: parsePath(options.publicDir, cwd)
143
+ }))
144
+ break
117
145
  default:
118
- ;({ server, vite } = await createServer({
146
+ ;({ server, config } = await createServer({
119
147
  host: options.host,
120
148
  appDir: parsePath(options.appDir, cwd),
121
149
  publicDir: parsePath(options.publicDir, cwd)
@@ -123,7 +151,7 @@ cli
123
151
  break
124
152
  }
125
153
  console.log('Dev server running at:')
126
- printHttpServerUrls(server, vite.config)
154
+ printHttpServerUrls(server, config)
127
155
  })
128
156
 
129
157
  cli.command('test').action(async (options) => {
@@ -1,12 +1,13 @@
1
- import type { ViteDevServer, LogLevel } from 'vite'
1
+ import type { LogLevel, InlineConfig } from 'vite'
2
+ import { ViteDevServer, mergeConfig } from 'vite'
2
3
  import { searchForWorkspaceRoot } from 'vite'
3
4
  import { baseConfig } from '../index.js'
4
5
  import type { Server } from 'net'
5
6
  import type { FastifyInstance } from 'fastify/types/instance'
6
7
  import fastify from 'fastify'
7
- import { readFileSync } from 'fs'
8
+ import { fastifySsrPlugin } from '../frameworks/vue/fastify-ssr-plugin.js'
8
9
 
9
- export async function createServer({
10
+ export async function createVitrifyDevServer({
10
11
  port = 3000,
11
12
  logLevel = 'info',
12
13
  mode = 'csr',
@@ -17,38 +18,44 @@ export async function createServer({
17
18
  }: {
18
19
  port?: number
19
20
  logLevel?: LogLevel
20
- mode?: 'csr' | 'ssr'
21
+ mode?: 'csr' | 'ssr' | 'fastify'
21
22
  framework?: 'vue'
22
23
  host?: string
23
24
  appDir?: URL
24
25
  publicDir?: URL
25
26
  }) {
26
- const { getAppDir, getCliDir, getCwd } = await import('../app-urls.js')
27
- const cwd = getCwd()
28
- const cliDir = getCliDir()
29
- if (!appDir) appDir = getAppDir()
30
- const { fastifySsrPlugin } = await import(
31
- `../${framework}/fastify-ssr-plugin.js`
27
+ const { getAppDir, getCliDir, getCliViteDir, getCwd } = await import(
28
+ '../app-urls.js'
32
29
  )
33
30
 
34
- /**
35
- * @type {import('vite').ViteDevServer}
36
- */
37
- const config = await baseConfig({
38
- ssr: mode === 'ssr' ? 'server' : undefined,
31
+ const cliDir = getCliDir()
32
+
33
+ if (!appDir) appDir = getAppDir()
34
+ let config: InlineConfig = {}
35
+ let ssrMode: 'server' | 'fastify' | undefined
36
+ if (mode === 'ssr') ssrMode = 'server'
37
+ if (mode === 'fastify') ssrMode = 'fastify'
38
+ config = await baseConfig({
39
+ framework,
40
+ ssr: ssrMode,
39
41
  command: 'dev',
40
42
  mode: 'development',
41
43
  appDir,
42
44
  publicDir
43
45
  })
46
+
44
47
  config.logLevel = logLevel
45
48
  config.server = {
49
+ https: config.server?.https,
46
50
  port,
47
- middlewareMode: mode === 'ssr' ? 'ssr' : undefined,
51
+ // middlewareMode: mode === 'ssr' ? 'ssr' : undefined,
52
+ middlewareMode: mode !== 'csr' ? 'ssr' : false,
48
53
  fs: {
49
54
  allow: [
50
55
  searchForWorkspaceRoot(process.cwd()),
51
- searchForWorkspaceRoot(appDir.pathname),
56
+ ...(Array.isArray(appDir)
57
+ ? appDir.map((dir) => searchForWorkspaceRoot(dir.pathname))
58
+ : [searchForWorkspaceRoot(appDir.pathname)]),
52
59
  searchForWorkspaceRoot(cliDir.pathname)
53
60
  // appDir.pathname,
54
61
  ]
@@ -61,78 +68,85 @@ export async function createServer({
61
68
  },
62
69
  host
63
70
  }
64
- const vite = await (
71
+ const vitrifyDevServer = await (
65
72
  await import('vite')
66
73
  ).createServer({
67
74
  configFile: false,
68
75
  ...config
69
76
  })
70
- const { productName = 'Product name' } = JSON.parse(
71
- readFileSync(new URL('package.json', appDir).pathname, {
72
- encoding: 'utf-8'
73
- })
77
+
78
+ return vitrifyDevServer
79
+ }
80
+
81
+ export async function createServer({
82
+ port = 3000,
83
+ logLevel = 'info',
84
+ mode = 'csr',
85
+ framework = 'vue',
86
+ host,
87
+ appDir,
88
+ publicDir
89
+ }: {
90
+ port?: number
91
+ logLevel?: LogLevel
92
+ mode?: 'csr' | 'ssr' | 'fastify'
93
+ framework?: 'vue'
94
+ host?: string
95
+ appDir?: URL
96
+ publicDir?: URL
97
+ }) {
98
+ const { getAppDir, getCliDir, getCliViteDir, getCwd } = await import(
99
+ '../app-urls.js'
74
100
  )
75
101
 
76
- let app: ViteDevServer | FastifyInstance
77
- let server: Server
78
- if (mode === 'ssr') {
79
- console.log('SSR mode')
80
- app = fastify()
81
- await app.register(fastifySsrPlugin, {
82
- appDir,
83
- cliDir,
84
- vite,
85
- productName
86
- })
87
- // await app.register(middie)
88
- // app.use(vite.middlewares)
102
+ const cliDir = getCliDir()
89
103
 
90
- // app.get('*', async (req, res) => {
91
- // try {
92
- // // const url = req.originalUrl
93
- // const url = req.raw.url
94
- // let template
95
- // let render
96
- // const ssrContext = {
97
- // req,
98
- // res
99
- // }
100
- // // always read fresh template in dev
101
- // // template = readFileSync(resolve('index.html'), 'utf-8')
102
- // template = readFileSync(new URL('index.html', cliDir)).toString()
104
+ const vite = await createVitrifyDevServer({
105
+ port,
106
+ logLevel,
107
+ mode,
108
+ framework,
109
+ host,
110
+ appDir,
111
+ publicDir
112
+ })
113
+ let entryUrl: string
103
114
 
104
- // // template = await vite.transformIndexHtml(url, template)
105
- // const entryUrl = new URL('ssr/entry-server.ts', cliDir).pathname
106
- // render = (await vite.ssrLoadModule(entryUrl)).render
107
- // let manifest
108
- // // TODO: https://github.com/vitejs/vite/issues/2282
109
- // try {
110
- // manifest = {}
111
- // } catch (e) {
112
- // manifest = {}
113
- // }
115
+ let setup
116
+ let server: Server
117
+
118
+ console.log(`Development mode: ${mode}`)
119
+ if (['ssr', 'fastify'].includes(mode)) {
120
+ const entryUrl =
121
+ mode === 'fastify'
122
+ ? new URL('src/vite/fastify/entry.ts', cliDir).pathname
123
+ : new URL(`src/vite/${framework}/ssr/entry-server.ts`, cliDir).pathname
114
124
 
115
- // const [appHtml, preloadLinks] = await render(url, manifest, ssrContext)
116
- // const html = template
117
- // .replace(`<!--preload-links-->`, preloadLinks)
118
- // .replace(`<!--app-html-->`, appHtml)
119
- // .replace('<!--product-name-->', productName)
125
+ ;({ setup } = await vite.ssrLoadModule(entryUrl))
120
126
 
121
- // res.code(200)
122
- // res.type('text/html')
123
- // res.send(html)
124
- // // res.status(200).set({ 'Content-Type': 'text/html' }).end(html)
125
- // } catch (e: any) {
126
- // console.error(e.stack)
127
- // vite && vite.ssrFixStacktrace(e)
128
- // res.code(500)
129
- // res.send(e.stack)
130
- // }
127
+ const app = fastify({
128
+ https:
129
+ typeof vite.config.server.https === 'object'
130
+ ? vite.config.server.https
131
+ : {}
132
+ })
133
+ if (setup) {
134
+ await setup({
135
+ fastify: app
136
+ })
137
+ }
138
+ // await app.register(fastifySsrPlugin, {
139
+ // appDir,
140
+ // vitrifyDir: new URL('../..', import.meta.url),
141
+ // mode: 'development'
131
142
  // })
132
- await app.listen(port || 3000, host)
143
+ await app.listen({
144
+ port: Number(port || 3000),
145
+ host
146
+ })
133
147
  server = app.server
134
148
  } else {
135
149
  server = (await vite.listen()).httpServer as Server
136
150
  }
137
- return { server, vite }
151
+ return { server, config: vite.config }
138
152
  }
@@ -15,9 +15,6 @@ export async function test(opts: { appDir: URL }) {
15
15
 
16
16
  globals: true,
17
17
  environment: 'happy-dom'
18
- // include: [
19
- // `${opts.appDir.pathname}**/*.{test,spec}.{js,mjs,cjs,ts,mts,cts,jsx,tsx}`
20
- // ]
21
18
  },
22
19
  config
23
20
  )
@@ -3,23 +3,25 @@ import type {
3
3
  FastifyRequest,
4
4
  FastifyReply
5
5
  } from 'fastify'
6
- import fastifyStatic from 'fastify-static'
6
+ import fastifyStatic from '@fastify/static'
7
7
  import { readFileSync } from 'fs'
8
- // import { injectSsrContext } from '../helpers/ssr.js'
8
+ import type { OnRenderedHook } from '../../vitrify-config.js'
9
+ import { componentsModules, collectCss } from '../../helpers/collect-css-ssr.js'
9
10
  import type { ViteDevServer } from 'vite'
10
- import type { SsrFunction } from '../../vitrify-config.js'
11
-
12
11
  export interface FastifySsrOptions {
13
12
  baseUrl?: string
14
13
  provide?: (
15
14
  req: FastifyRequest,
16
15
  res: FastifyReply
17
16
  ) => Promise<Record<string, unknown>>
17
+ vitrifyDir?: URL
18
18
  vite?: ViteDevServer
19
- cliDir?: URL
19
+ // frameworkDir?: URL
20
20
  appDir?: URL
21
+ publicDir?: URL
21
22
  productName?: string
22
- ssrFunctions?: SsrFunction[]
23
+ onRendered?: OnRenderedHook[]
24
+ mode?: string
23
25
  }
24
26
 
25
27
  const fastifySsrPlugin: FastifyPluginCallback<FastifySsrOptions> = async (
@@ -27,29 +29,75 @@ const fastifySsrPlugin: FastifyPluginCallback<FastifySsrOptions> = async (
27
29
  options,
28
30
  done
29
31
  ) => {
30
- if (import.meta.env.MODE === 'development') {
31
- if (!options.vite) throw new Error('Option vite cannot be undefined')
32
- const middie = (await import('middie')).default
32
+ options.vitrifyDir =
33
+ options.vitrifyDir || new URL('../../..', import.meta.url)
34
+ const frameworkDir = new URL('vite/vue/', options.vitrifyDir)
35
+ options.baseUrl = options.baseUrl || '/'
36
+ if (
37
+ options.baseUrl.charAt(options.baseUrl.length - 1) !== '/' ||
38
+ options.baseUrl.charAt(0) !== '/'
39
+ )
40
+ throw new Error('baseUrl should start and end with a /')
41
+ if (options.mode === 'development') {
42
+ if (!options.vitrifyDir)
43
+ throw new Error('Option vitrifyDir cannot be undefined')
44
+ // if (!options.vite) throw new Error('Option vite cannot be undefined')
45
+ // const { resolve } = await import('import-meta-resolve')
46
+ // const cliDir = new URL('../', await resolve('vitrify', import.meta.url))
47
+ options.appDir = options.appDir || new URL('../../..', import.meta.url)
48
+
49
+ const { createServer, searchForWorkspaceRoot } = await import('vite')
50
+ const { baseConfig } = await import('vitrify')
51
+ const cliDir = options.vitrifyDir
52
+ const config = await baseConfig({
53
+ ssr: 'server',
54
+ command: 'dev',
55
+ mode: 'development',
56
+ appDir: options.appDir,
57
+ publicDir: options.publicDir || new URL('public', options.appDir)
58
+ })
59
+
60
+ config.server = {
61
+ middlewareMode: true,
62
+ fs: {
63
+ allow: [
64
+ searchForWorkspaceRoot(process.cwd()),
65
+ searchForWorkspaceRoot(options.appDir.pathname),
66
+ searchForWorkspaceRoot(cliDir.pathname)
67
+ // appDir.pathname,
68
+ ]
69
+ },
70
+ watch: {
71
+ // During tests we edit the files too fast and sometimes chokidar
72
+ // misses change events, so enforce polling for consistency
73
+ usePolling: true,
74
+ interval: 100
75
+ }
76
+ }
77
+ const vite = await createServer({
78
+ configFile: false,
79
+ ...config
80
+ })
81
+
82
+ console.log('Dev mode')
83
+ const middie = (await import('@fastify/middie')).default
33
84
  await fastify.register(middie)
34
- fastify.use(options.vite.middlewares)
85
+ fastify.use(vite.middlewares)
35
86
 
36
- fastify.get('*', async (req, res) => {
87
+ fastify.get(`${options.baseUrl}*`, async (req, res) => {
37
88
  try {
38
- // const url = req.originalUrl
39
89
  const url = req.raw.url
40
90
  const ssrContext = {
41
91
  req,
42
92
  res
43
93
  }
44
- // always read fresh template in dev
45
- // template = readFileSync(resolve('index.html'), 'utf-8')
94
+
46
95
  const template = readFileSync(
47
- new URL('index.html', options.cliDir)
96
+ new URL('index.html', frameworkDir)
48
97
  ).toString()
49
98
 
50
- // template = await vite.transformIndexHtml(url, template)
51
- const entryUrl = new URL('ssr/entry-server.ts', options.cliDir).pathname
52
- const render = (await options.vite!.ssrLoadModule(entryUrl)).render
99
+ const entryUrl = new URL('ssr/entry-server.ts', frameworkDir).pathname
100
+ const render = (await vite!.ssrLoadModule(entryUrl)).render
53
101
  let manifest
54
102
  // TODO: https://github.com/vitejs/vite/issues/2282
55
103
  try {
@@ -58,11 +106,19 @@ const fastifySsrPlugin: FastifyPluginCallback<FastifySsrOptions> = async (
58
106
  manifest = {}
59
107
  }
60
108
 
109
+ const cssModules = [entryUrl]
110
+ // // @ts-ignore
111
+ // if (options.vite?.config.vitrify!.globalCss)
112
+ // cssModules.push(...options.vite?.config.vitrify.globalCss)
113
+ const matchedModules = componentsModules(cssModules, vite!)
114
+ const css = collectCss(matchedModules)
115
+
61
116
  const [appHtml, preloadLinks] = await render(url, manifest, ssrContext)
62
117
  const html = template
63
118
  .replace(`<!--preload-links-->`, preloadLinks)
64
119
  .replace(`<!--app-html-->`, appHtml)
65
120
  .replace('<!--product-name-->', options.productName || 'Product name')
121
+ .replace('<!--dev-ssr-css-->', css)
66
122
 
67
123
  res.code(200)
68
124
  res.type('text/html')
@@ -70,13 +126,13 @@ const fastifySsrPlugin: FastifyPluginCallback<FastifySsrOptions> = async (
70
126
  // res.status(200).set({ 'Content-Type': 'text/html' }).end(html)
71
127
  } catch (e: any) {
72
128
  console.error(e.stack)
73
- options.vite && options.vite.ssrFixStacktrace(e)
129
+ vite && vite.ssrFixStacktrace(e)
74
130
  res.code(500)
75
131
  res.send(e.stack)
76
132
  }
77
133
  })
78
134
  } else {
79
- options.baseUrl = options.baseUrl || '/'
135
+ options.appDir = options.appDir || new URL('../../..', import.meta.url)
80
136
  fastify.register(fastifyStatic, {
81
137
  root: new URL('./dist/ssr/client', options.appDir).pathname,
82
138
  wildcard: false,
@@ -85,7 +141,7 @@ const fastifySsrPlugin: FastifyPluginCallback<FastifySsrOptions> = async (
85
141
  })
86
142
 
87
143
  fastify.get(`${options.baseUrl}*`, async (req, res) => {
88
- const url = req.raw.url
144
+ const url = req.raw.url?.replace(options.baseUrl!, '/')
89
145
  const provide = options.provide ? await options.provide(req, res) : {}
90
146
  const ssrContext: Record<string, any> = {
91
147
  req,
@@ -93,9 +149,6 @@ const fastifySsrPlugin: FastifyPluginCallback<FastifySsrOptions> = async (
93
149
  provide
94
150
  }
95
151
 
96
- // template = readFileSync(new URL('../client/index.html', import.meta.url).pathname).toString()
97
- // manifest = JSON.parse(readFileSync(new URL('../client/ssr-manifest.json', import.meta.url)).toString())
98
- // render = (await import(new URL('./entry-server.mjs', import.meta.url).pathname)).render
99
152
  const template = readFileSync(
100
153
  new URL('./dist/ssr/client/index.html', options.appDir).pathname
101
154
  ).toString()
@@ -119,8 +172,8 @@ const fastifySsrPlugin: FastifyPluginCallback<FastifySsrOptions> = async (
119
172
  .replace(`<!--preload-links-->`, preloadLinks)
120
173
  .replace(`<!--app-html-->`, appHtml)
121
174
 
122
- if (options.ssrFunctions?.length) {
123
- for (const ssrFunction of options.ssrFunctions) {
175
+ if (options.onRendered?.length) {
176
+ for (const ssrFunction of options.onRendered) {
124
177
  html = ssrFunction(html, ssrContext)
125
178
  }
126
179
  }
@@ -135,3 +188,4 @@ const fastifySsrPlugin: FastifyPluginCallback<FastifySsrOptions> = async (
135
188
  }
136
189
 
137
190
  export { fastifySsrPlugin }
191
+ export type FastifySsrPlugin = typeof fastifySsrPlugin
@@ -1,19 +1,19 @@
1
1
  import { promises as fs } from 'fs'
2
2
  import { routesToPaths } from '../../helpers/routes.js'
3
- import type { SsrFunction } from '../../vitrify-config.js'
3
+ import type { OnRenderedHook } from '../../vitrify-config.js'
4
4
 
5
5
  export const prerender = async ({
6
6
  outDir,
7
7
  templatePath,
8
8
  manifestPath,
9
9
  entryServerPath,
10
- ssrFunctions
10
+ onRenderedHooks
11
11
  }: {
12
12
  outDir: string
13
13
  templatePath: string
14
14
  manifestPath: string
15
15
  entryServerPath: string
16
- ssrFunctions: SsrFunction[]
16
+ onRenderedHooks: OnRenderedHook[]
17
17
  }) => {
18
18
  const promises = []
19
19
  const template = (await fs.readFile(templatePath)).toString()
@@ -37,8 +37,8 @@ export const prerender = async ({
37
37
  .replace(`<!--preload-links-->`, preloadLinks)
38
38
  .replace(`<!--app-html-->`, appHtml)
39
39
 
40
- if (ssrFunctions?.length) {
41
- for (const ssrFunction of ssrFunctions) {
40
+ if (onRenderedHooks?.length) {
41
+ for (const ssrFunction of onRenderedHooks) {
42
42
  html = ssrFunction(html, ssrContext)
43
43
  }
44
44
  }
@@ -1,20 +1,26 @@
1
1
  import type { FastifyInstance } from 'fastify'
2
2
  import fastify from 'fastify'
3
- import type { SsrFunction } from '../../vitrify-config.js'
4
- // import { setup } from 'virtual:fastify-setup'
5
- import { fastifySsrPlugin } from './fastify-ssr-plugin.js'
6
- // import { getPkgJsonDir } from '../app-urls.js'
3
+ import type { ViteDevServer } from 'vite'
4
+ import { getCliDir, getCliViteDir } from '../../app-urls.js'
5
+ import type { OnRenderedHook, OnSetupFile } from '../../vitrify-config.js'
6
+ import type { FastifySsrPlugin } from './fastify-ssr-plugin.js'
7
7
 
8
8
  export const createApp = ({
9
- setup,
9
+ onSetup,
10
10
  appDir,
11
11
  baseUrl,
12
- ssrFunctions
12
+ onRendered,
13
+ fastifySsrPlugin,
14
+ vitrifyDir,
15
+ mode
13
16
  }: {
14
- setup: (fastify: FastifyInstance) => any
17
+ onSetup: OnSetupFile[]
15
18
  appDir: URL
16
19
  baseUrl?: string
17
- ssrFunctions?: SsrFunction[]
20
+ onRendered?: OnRenderedHook[]
21
+ fastifySsrPlugin: FastifySsrPlugin
22
+ vitrifyDir?: URL
23
+ mode: string
18
24
  }) => {
19
25
  const app = fastify({
20
26
  logger: true
@@ -23,16 +29,16 @@ export const createApp = ({
23
29
  app.register(fastifySsrPlugin, {
24
30
  baseUrl,
25
31
  appDir,
26
- ssrFunctions
32
+ onRendered,
33
+ vitrifyDir,
34
+ mode
27
35
  })
28
36
 
29
- setup(app)
37
+ // if (onSetup?.length) {
38
+ // for (const setup of onSetup) {
39
+ // setup(app)
40
+ // }
41
+ // }
30
42
 
31
43
  return app
32
44
  }
33
-
34
- // const app = createApp({
35
- // setup
36
- // })
37
-
38
- // app.listen(process.env.PORT || 3000, process.env.HOST || '127.0.0.1')