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.
- package/dist/app-urls.js +1 -2
- package/dist/bin/build.js +0 -43
- package/dist/bin/cli.js +29 -7
- package/dist/bin/dev.js +58 -67
- package/dist/frameworks/vue/fastify-ssr-plugin.js +67 -23
- package/dist/frameworks/vue/prerender.js +3 -3
- package/dist/frameworks/vue/server.js +9 -10
- package/dist/helpers/collect-css-ssr.js +57 -0
- package/dist/helpers/logger.js +0 -72
- package/dist/index.js +268 -122
- package/dist/plugins/quasar.js +13 -106
- package/dist/types/bin/build.d.ts +2 -2
- package/dist/types/bin/dev.d.ts +39 -3
- package/dist/types/frameworks/vue/fastify-ssr-plugin.d.ts +6 -3
- package/dist/types/frameworks/vue/prerender.d.ts +3 -3
- package/dist/types/frameworks/vue/server.d.ts +9 -5
- package/dist/types/helpers/collect-css-ssr.d.ts +10 -0
- package/dist/types/helpers/logger.d.ts +0 -19
- package/dist/types/helpers/routes.d.ts +1 -1
- package/dist/types/index.d.ts +1 -1
- package/dist/types/plugins/index.d.ts +1 -1
- package/dist/types/vitrify-config.d.ts +20 -16
- package/package.json +32 -32
- package/src/node/app-urls.ts +1 -2
- package/src/node/bin/build.ts +2 -49
- package/src/node/bin/cli.ts +36 -8
- package/src/node/bin/dev.ts +89 -75
- package/src/node/bin/test.ts +0 -3
- package/src/node/frameworks/vue/fastify-ssr-plugin.ts +80 -26
- package/src/node/frameworks/vue/prerender.ts +5 -5
- package/src/node/frameworks/vue/server.ts +22 -16
- package/src/node/helpers/collect-css-ssr.ts +77 -0
- package/src/node/helpers/logger.ts +0 -87
- package/src/node/index.ts +302 -137
- package/src/node/plugins/index.ts +1 -1
- package/src/node/plugins/quasar.ts +14 -111
- package/src/node/vitrify-config.ts +31 -16
- package/src/vite/fastify/entry.ts +11 -0
- package/src/vite/fastify/server.ts +10 -0
- package/src/vite/vue/index.html +1 -0
- package/src/vite/vue/main.ts +5 -20
- package/src/vite/vue/ssr/app.ts +25 -0
- package/src/vite/vue/ssr/entry-server.ts +13 -1
- package/src/vite/vue/ssr/fastify-ssr-plugin.ts +2 -118
- package/src/vite/vue/ssr/prerender.ts +2 -2
- package/src/vite/vue/ssr/server.ts +24 -15
- package/src/node/helpers/ssr.ts.bak +0 -52
package/src/node/bin/cli.ts
CHANGED
|
@@ -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 {
|
|
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,
|
|
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,
|
|
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
|
-
|
|
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
|
|
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,
|
|
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,
|
|
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,
|
|
154
|
+
printHttpServerUrls(server, config)
|
|
127
155
|
})
|
|
128
156
|
|
|
129
157
|
cli.command('test').action(async (options) => {
|
package/src/node/bin/dev.ts
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
|
-
import type {
|
|
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 {
|
|
8
|
+
import { fastifySsrPlugin } from '../frameworks/vue/fastify-ssr-plugin.js'
|
|
8
9
|
|
|
9
|
-
export async function
|
|
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(
|
|
27
|
-
|
|
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
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
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
|
-
|
|
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
|
|
71
|
+
const vitrifyDevServer = await (
|
|
65
72
|
await import('vite')
|
|
66
73
|
).createServer({
|
|
67
74
|
configFile: false,
|
|
68
75
|
...config
|
|
69
76
|
})
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
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
|
-
|
|
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
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
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
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
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
|
-
|
|
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
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
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(
|
|
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
|
}
|
package/src/node/bin/test.ts
CHANGED
|
@@ -3,23 +3,25 @@ import type {
|
|
|
3
3
|
FastifyRequest,
|
|
4
4
|
FastifyReply
|
|
5
5
|
} from 'fastify'
|
|
6
|
-
import fastifyStatic from 'fastify
|
|
6
|
+
import fastifyStatic from '@fastify/static'
|
|
7
7
|
import { readFileSync } from 'fs'
|
|
8
|
-
|
|
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
|
-
|
|
19
|
+
// frameworkDir?: URL
|
|
20
20
|
appDir?: URL
|
|
21
|
+
publicDir?: URL
|
|
21
22
|
productName?: string
|
|
22
|
-
|
|
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
|
-
|
|
31
|
-
|
|
32
|
-
|
|
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(
|
|
85
|
+
fastify.use(vite.middlewares)
|
|
35
86
|
|
|
36
|
-
fastify.get(
|
|
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
|
-
|
|
45
|
-
// template = readFileSync(resolve('index.html'), 'utf-8')
|
|
94
|
+
|
|
46
95
|
const template = readFileSync(
|
|
47
|
-
new URL('index.html',
|
|
96
|
+
new URL('index.html', frameworkDir)
|
|
48
97
|
).toString()
|
|
49
98
|
|
|
50
|
-
|
|
51
|
-
const
|
|
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
|
-
|
|
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.
|
|
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.
|
|
123
|
-
for (const ssrFunction of options.
|
|
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 {
|
|
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
|
-
|
|
10
|
+
onRenderedHooks
|
|
11
11
|
}: {
|
|
12
12
|
outDir: string
|
|
13
13
|
templatePath: string
|
|
14
14
|
manifestPath: string
|
|
15
15
|
entryServerPath: string
|
|
16
|
-
|
|
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 (
|
|
41
|
-
for (const ssrFunction of
|
|
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 {
|
|
4
|
-
|
|
5
|
-
import {
|
|
6
|
-
|
|
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
|
-
|
|
9
|
+
onSetup,
|
|
10
10
|
appDir,
|
|
11
11
|
baseUrl,
|
|
12
|
-
|
|
12
|
+
onRendered,
|
|
13
|
+
fastifySsrPlugin,
|
|
14
|
+
vitrifyDir,
|
|
15
|
+
mode
|
|
13
16
|
}: {
|
|
14
|
-
|
|
17
|
+
onSetup: OnSetupFile[]
|
|
15
18
|
appDir: URL
|
|
16
19
|
baseUrl?: string
|
|
17
|
-
|
|
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
|
-
|
|
32
|
+
onRendered,
|
|
33
|
+
vitrifyDir,
|
|
34
|
+
mode
|
|
27
35
|
})
|
|
28
36
|
|
|
29
|
-
|
|
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')
|