vitrify 0.2.5 → 0.5.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/README.md +2 -2
- package/dist/app-urls.js +1 -2
- package/dist/bin/build.js +9 -51
- package/dist/bin/cli.js +31 -9
- package/dist/bin/dev.js +72 -70
- package/dist/frameworks/vue/fastify-csr-plugin.js +38 -0
- package/dist/frameworks/vue/fastify-ssr-plugin.js +83 -25
- package/dist/frameworks/vue/prerender.js +3 -3
- package/dist/frameworks/vue/server.js +10 -11
- package/dist/helpers/collect-css-ssr.js +61 -0
- package/dist/helpers/logger.js +0 -72
- package/dist/index.js +310 -130
- package/dist/plugins/quasar.js +34 -111
- package/dist/types/bin/build.d.ts +2 -2
- package/dist/types/bin/dev.d.ts +42 -4
- package/dist/types/frameworks/vue/fastify-csr-plugin.d.ts +17 -0
- 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 +10 -5
- package/dist/types/helpers/collect-css-ssr.d.ts +14 -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 +4 -2
- package/dist/types/plugins/index.d.ts +1 -1
- package/dist/types/vitrify-config.d.ts +33 -17
- package/package.json +33 -32
- package/src/node/app-urls.ts +1 -2
- package/src/node/bin/build.ts +11 -57
- package/src/node/bin/cli.ts +38 -10
- package/src/node/bin/dev.ts +106 -80
- package/src/node/bin/test.ts +0 -3
- package/src/node/frameworks/vue/fastify-csr-plugin.ts +72 -0
- package/src/node/frameworks/vue/fastify-ssr-plugin.ts +99 -28
- package/src/node/frameworks/vue/prerender.ts +5 -5
- package/src/node/frameworks/vue/server.ts +24 -17
- package/src/node/helpers/collect-css-ssr.ts +85 -0
- package/src/node/helpers/logger.ts +0 -87
- package/src/node/index.ts +353 -146
- package/src/node/plugins/index.ts +1 -1
- package/src/node/plugins/quasar.ts +39 -116
- package/src/node/vitrify-config.ts +44 -17
- package/src/vite/fastify/entry.ts +11 -0
- package/src/vite/fastify/server.ts +12 -0
- package/src/vite/vue/csr/app.ts +25 -0
- package/src/vite/vue/csr/fastify-csr-plugin.ts +3 -0
- package/src/vite/vue/csr/server.ts +8 -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/build.ts
CHANGED
|
@@ -1,58 +1,11 @@
|
|
|
1
1
|
#!/usr/bin/node --experimental-specifier-resolution=node
|
|
2
2
|
import { baseConfig } from '../index.js'
|
|
3
|
-
// import { promises as fs } from 'fs'
|
|
4
|
-
// import { routesToPaths } from '../helpers/routes.js'
|
|
5
3
|
import { build as viteBuild } from 'vite'
|
|
6
|
-
// import { SsrFunction } from '../vitrify-config.js'
|
|
7
|
-
|
|
8
|
-
// export const prerender = async ({
|
|
9
|
-
// outDir,
|
|
10
|
-
// templatePath,
|
|
11
|
-
// manifestPath,
|
|
12
|
-
// entryServerPath,
|
|
13
|
-
// injectSsrContext
|
|
14
|
-
// }: {
|
|
15
|
-
// outDir: string
|
|
16
|
-
// templatePath: string
|
|
17
|
-
// manifestPath: string
|
|
18
|
-
// entryServerPath: string
|
|
19
|
-
// injectSsrContext: SsrFunction
|
|
20
|
-
// }) => {
|
|
21
|
-
// let template
|
|
22
|
-
// let manifest
|
|
23
|
-
// const promises = []
|
|
24
|
-
// template = (await fs.readFile(templatePath)).toString()
|
|
25
|
-
// manifest = await fs.readFile(manifestPath)
|
|
26
|
-
// let { render, getRoutes } = await import(entryServerPath)
|
|
27
|
-
// const routes = await getRoutes()
|
|
28
|
-
// const paths = routesToPaths(routes).filter(
|
|
29
|
-
// (i) => !i.includes(':') && !i.includes('*')
|
|
30
|
-
// )
|
|
31
|
-
// for (let url of paths) {
|
|
32
|
-
// const filename =
|
|
33
|
-
// (url.endsWith('/') ? 'index' : url.replace(/^\//g, '')) + '.html'
|
|
34
|
-
// console.log(`Generating ${filename}`)
|
|
35
|
-
// const ssrContext = {
|
|
36
|
-
// req: { headers: {}, url },
|
|
37
|
-
// res: {}
|
|
38
|
-
// }
|
|
39
|
-
// const [appHtml, preloadLinks] = await render(url, manifest, ssrContext)
|
|
40
|
-
|
|
41
|
-
// let html = template
|
|
42
|
-
// .replace(`<!--preload-links-->`, preloadLinks)
|
|
43
|
-
// .replace(`<!--app-html-->`, appHtml)
|
|
44
|
-
|
|
45
|
-
// html = injectSsrContext(html, ssrContext)
|
|
46
|
-
|
|
47
|
-
// promises.push(fs.writeFile(outDir + filename, html, 'utf-8'))
|
|
48
|
-
// }
|
|
49
|
-
// return Promise.all(promises)
|
|
50
|
-
// }
|
|
51
4
|
|
|
52
5
|
export async function build(opts: {
|
|
53
|
-
ssr?: 'client' | 'server' | 'ssg'
|
|
6
|
+
ssr?: 'client' | 'server' | 'ssg' | 'fastify'
|
|
54
7
|
base?: string
|
|
55
|
-
outDir
|
|
8
|
+
outDir: string
|
|
56
9
|
appDir?: URL
|
|
57
10
|
publicDir?: URL
|
|
58
11
|
}) {
|
|
@@ -61,7 +14,8 @@ export async function build(opts: {
|
|
|
61
14
|
mode: 'production',
|
|
62
15
|
ssr: opts?.ssr,
|
|
63
16
|
appDir: opts.appDir,
|
|
64
|
-
publicDir: opts.publicDir
|
|
17
|
+
publicDir: opts.publicDir,
|
|
18
|
+
base: opts.base
|
|
65
19
|
})
|
|
66
20
|
|
|
67
21
|
config.build = {
|
|
@@ -71,16 +25,16 @@ export async function build(opts: {
|
|
|
71
25
|
emptyOutDir: !!opts.outDir
|
|
72
26
|
}
|
|
73
27
|
|
|
74
|
-
if (opts.base) {
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
}
|
|
28
|
+
// if (opts.base) {
|
|
29
|
+
// config.define = {
|
|
30
|
+
// ...config.define,
|
|
31
|
+
// __BASE_URL__: `'${opts.base}'`
|
|
32
|
+
// }
|
|
33
|
+
// }
|
|
80
34
|
|
|
81
35
|
return viteBuild({
|
|
82
36
|
configFile: false,
|
|
83
|
-
base: opts.base,
|
|
37
|
+
// base: opts.base,
|
|
84
38
|
// logLevel: 'silent',
|
|
85
39
|
...config
|
|
86
40
|
})
|
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}`)
|
|
@@ -42,7 +48,14 @@ cli
|
|
|
42
48
|
case 'csr':
|
|
43
49
|
await build({
|
|
44
50
|
...args,
|
|
45
|
-
outDir: new URL('
|
|
51
|
+
outDir: new URL('csr/', baseOutDir).pathname
|
|
52
|
+
})
|
|
53
|
+
break
|
|
54
|
+
case 'fastify':
|
|
55
|
+
await build({
|
|
56
|
+
ssr: 'fastify',
|
|
57
|
+
...args,
|
|
58
|
+
outDir: new URL('server/', baseOutDir).pathname
|
|
46
59
|
})
|
|
47
60
|
break
|
|
48
61
|
case 'ssr':
|
|
@@ -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,
|
|
111
|
-
|
|
130
|
+
;({ server, config } = await createServer({
|
|
131
|
+
ssr: 'ssr',
|
|
132
|
+
host: options.host,
|
|
133
|
+
appDir: parsePath(options.appDir, cwd),
|
|
134
|
+
publicDir: parsePath(options.publicDir, cwd)
|
|
135
|
+
}))
|
|
136
|
+
break
|
|
137
|
+
case 'fastify':
|
|
138
|
+
;({ server, config } = await createServer({
|
|
139
|
+
ssr: 'fastify',
|
|
112
140
|
host: options.host,
|
|
113
141
|
appDir: parsePath(options.appDir, cwd),
|
|
114
142
|
publicDir: parsePath(options.publicDir, cwd)
|
|
115
143
|
}))
|
|
116
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,56 +1,73 @@
|
|
|
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'
|
|
9
|
+
import type { ServerOptions } from 'https'
|
|
8
10
|
|
|
9
|
-
export async function
|
|
11
|
+
export async function createVitrifyDevServer({
|
|
10
12
|
port = 3000,
|
|
11
13
|
logLevel = 'info',
|
|
12
|
-
mode = 'csr',
|
|
14
|
+
// mode = 'csr',
|
|
15
|
+
ssr,
|
|
13
16
|
framework = 'vue',
|
|
14
17
|
host,
|
|
15
18
|
appDir,
|
|
16
|
-
publicDir
|
|
19
|
+
publicDir,
|
|
20
|
+
base
|
|
17
21
|
}: {
|
|
18
22
|
port?: number
|
|
19
23
|
logLevel?: LogLevel
|
|
20
|
-
mode?: 'csr' | 'ssr'
|
|
24
|
+
// mode?: 'csr' | 'ssr' | 'fastify'
|
|
25
|
+
ssr?: 'ssr' | 'fastify'
|
|
21
26
|
framework?: 'vue'
|
|
22
27
|
host?: string
|
|
23
28
|
appDir?: URL
|
|
24
29
|
publicDir?: URL
|
|
30
|
+
base?: string
|
|
25
31
|
}) {
|
|
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`
|
|
32
|
+
const { getAppDir, getCliDir, getCliViteDir, getCwd } = await import(
|
|
33
|
+
'../app-urls.js'
|
|
32
34
|
)
|
|
33
35
|
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
36
|
+
const cliDir = getCliDir()
|
|
37
|
+
|
|
38
|
+
if (!appDir) appDir = getAppDir()
|
|
39
|
+
let config: InlineConfig = {}
|
|
40
|
+
let ssrMode: 'server' | 'fastify' | undefined
|
|
41
|
+
if (ssr === 'ssr') ssrMode = 'server'
|
|
42
|
+
if (ssr === 'fastify') ssrMode = 'fastify'
|
|
43
|
+
config = await baseConfig({
|
|
44
|
+
framework,
|
|
45
|
+
ssr: ssrMode,
|
|
39
46
|
command: 'dev',
|
|
40
47
|
mode: 'development',
|
|
41
48
|
appDir,
|
|
42
|
-
publicDir
|
|
49
|
+
publicDir,
|
|
50
|
+
base
|
|
43
51
|
})
|
|
52
|
+
|
|
44
53
|
config.logLevel = logLevel
|
|
54
|
+
|
|
55
|
+
console.log(searchForWorkspaceRoot(appDir.pathname))
|
|
45
56
|
config.server = {
|
|
57
|
+
https: config.server?.https,
|
|
58
|
+
hmr: {
|
|
59
|
+
protocol: config.server?.https ? 'wss' : 'ws'
|
|
60
|
+
},
|
|
46
61
|
port,
|
|
47
|
-
middlewareMode: mode === 'ssr' ? 'ssr' : undefined,
|
|
62
|
+
// middlewareMode: mode === 'ssr' ? 'ssr' : undefined,
|
|
63
|
+
middlewareMode: ssr ? 'ssr' : false,
|
|
48
64
|
fs: {
|
|
65
|
+
strict: false, // https://github.com/vitejs/vite/issues/8175
|
|
49
66
|
allow: [
|
|
50
67
|
searchForWorkspaceRoot(process.cwd()),
|
|
51
68
|
searchForWorkspaceRoot(appDir.pathname),
|
|
52
|
-
searchForWorkspaceRoot(cliDir.pathname)
|
|
53
|
-
|
|
69
|
+
searchForWorkspaceRoot(cliDir.pathname),
|
|
70
|
+
appDir.pathname
|
|
54
71
|
]
|
|
55
72
|
},
|
|
56
73
|
watch: {
|
|
@@ -61,78 +78,87 @@ export async function createServer({
|
|
|
61
78
|
},
|
|
62
79
|
host
|
|
63
80
|
}
|
|
64
|
-
const
|
|
81
|
+
const vitrifyDevServer = await (
|
|
65
82
|
await import('vite')
|
|
66
83
|
).createServer({
|
|
67
84
|
configFile: false,
|
|
68
85
|
...config
|
|
69
86
|
})
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
87
|
+
|
|
88
|
+
return vitrifyDevServer
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
export async function createServer({
|
|
92
|
+
port = 3000,
|
|
93
|
+
logLevel = 'info',
|
|
94
|
+
// mode = 'csr',
|
|
95
|
+
ssr,
|
|
96
|
+
framework = 'vue',
|
|
97
|
+
host,
|
|
98
|
+
appDir,
|
|
99
|
+
publicDir
|
|
100
|
+
}: {
|
|
101
|
+
port?: number
|
|
102
|
+
logLevel?: LogLevel
|
|
103
|
+
// mode?: 'csr' | 'ssr' | 'fastify'
|
|
104
|
+
ssr?: 'ssr' | 'fastify'
|
|
105
|
+
framework?: 'vue'
|
|
106
|
+
host?: string
|
|
107
|
+
appDir?: URL
|
|
108
|
+
publicDir?: URL
|
|
109
|
+
}) {
|
|
110
|
+
const { getAppDir, getCliDir, getCliViteDir, getCwd } = await import(
|
|
111
|
+
'../app-urls.js'
|
|
74
112
|
)
|
|
75
113
|
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
// app.use(vite.middlewares)
|
|
114
|
+
appDir = appDir || getAppDir()
|
|
115
|
+
const cliDir = getCliDir()
|
|
116
|
+
|
|
117
|
+
const vite = await createVitrifyDevServer({
|
|
118
|
+
port,
|
|
119
|
+
logLevel,
|
|
120
|
+
ssr,
|
|
121
|
+
framework,
|
|
122
|
+
host,
|
|
123
|
+
appDir,
|
|
124
|
+
publicDir
|
|
125
|
+
})
|
|
89
126
|
|
|
90
|
-
|
|
91
|
-
|
|
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()
|
|
127
|
+
let setup
|
|
128
|
+
let server: Server
|
|
103
129
|
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
// manifest = {}
|
|
111
|
-
// } catch (e) {
|
|
112
|
-
// manifest = {}
|
|
113
|
-
// }
|
|
130
|
+
console.log(`Development mode: ${ssr ? ssr : 'csr'}`)
|
|
131
|
+
if (ssr) {
|
|
132
|
+
const entryUrl =
|
|
133
|
+
ssr === 'fastify'
|
|
134
|
+
? new URL('src/vite/fastify/entry.ts', cliDir).pathname
|
|
135
|
+
: new URL(`src/vite/${framework}/ssr/entry-server.ts`, cliDir).pathname
|
|
114
136
|
|
|
115
|
-
|
|
116
|
-
// const html = template
|
|
117
|
-
// .replace(`<!--preload-links-->`, preloadLinks)
|
|
118
|
-
// .replace(`<!--app-html-->`, appHtml)
|
|
119
|
-
// .replace('<!--product-name-->', productName)
|
|
137
|
+
;({ setup } = await vite.ssrLoadModule(entryUrl))
|
|
120
138
|
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
139
|
+
const app = fastify({
|
|
140
|
+
logger: true,
|
|
141
|
+
https: vite.config.server.https as ServerOptions
|
|
142
|
+
})
|
|
143
|
+
if (process.env) process.env.MODE = 'development'
|
|
144
|
+
if (setup) {
|
|
145
|
+
await setup({
|
|
146
|
+
fastify: app
|
|
147
|
+
})
|
|
148
|
+
}
|
|
149
|
+
if (ssr === 'ssr') {
|
|
150
|
+
await app.register(fastifySsrPlugin, {
|
|
151
|
+
appDir,
|
|
152
|
+
mode: 'development'
|
|
153
|
+
})
|
|
154
|
+
}
|
|
155
|
+
await app.listen({
|
|
156
|
+
port: Number(port || 3000),
|
|
157
|
+
host
|
|
158
|
+
})
|
|
133
159
|
server = app.server
|
|
134
160
|
} else {
|
|
135
161
|
server = (await vite.listen()).httpServer as Server
|
|
136
162
|
}
|
|
137
|
-
return { server, vite }
|
|
163
|
+
return { server, config: vite.config }
|
|
138
164
|
}
|
package/src/node/bin/test.ts
CHANGED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
FastifyPluginCallback,
|
|
3
|
+
FastifyRequest,
|
|
4
|
+
FastifyReply
|
|
5
|
+
} from 'fastify'
|
|
6
|
+
import fastifyStatic from '@fastify/static'
|
|
7
|
+
import type { OnRenderedHook } from '../../vitrify-config.js'
|
|
8
|
+
import type { ViteDevServer } from 'vite'
|
|
9
|
+
|
|
10
|
+
export interface FastifySsrOptions {
|
|
11
|
+
baseUrl?: string
|
|
12
|
+
provide?: (
|
|
13
|
+
req: FastifyRequest,
|
|
14
|
+
res: FastifyReply
|
|
15
|
+
) => Promise<Record<string, unknown>>
|
|
16
|
+
vitrifyDir?: URL
|
|
17
|
+
vite?: ViteDevServer
|
|
18
|
+
// frameworkDir?: URL
|
|
19
|
+
appDir?: URL
|
|
20
|
+
publicDir?: URL
|
|
21
|
+
productName?: string
|
|
22
|
+
onRendered?: OnRenderedHook[]
|
|
23
|
+
mode?: string
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const fastifyCsrPlugin: FastifyPluginCallback<FastifySsrOptions> = async (
|
|
27
|
+
fastify,
|
|
28
|
+
options,
|
|
29
|
+
done
|
|
30
|
+
) => {
|
|
31
|
+
options.vitrifyDir =
|
|
32
|
+
options.vitrifyDir || (await import('vitrify')).vitrifyDir
|
|
33
|
+
const frameworkDir = new URL('src/vite/vue/', options.vitrifyDir)
|
|
34
|
+
options.baseUrl = options.baseUrl || '/'
|
|
35
|
+
options.mode = options.mode || process.env.MODE || import.meta.env.MODE
|
|
36
|
+
options.appDir = options.appDir || new URL('../../..', import.meta.url)
|
|
37
|
+
|
|
38
|
+
if (
|
|
39
|
+
options.baseUrl.charAt(options.baseUrl.length - 1) !== '/' ||
|
|
40
|
+
options.baseUrl.charAt(0) !== '/'
|
|
41
|
+
)
|
|
42
|
+
throw new Error('baseUrl should start and end with a /')
|
|
43
|
+
if (options.mode === 'development') {
|
|
44
|
+
options.appDir = options.appDir || new URL('../..', import.meta.url)
|
|
45
|
+
|
|
46
|
+
const { createVitrifyDevServer } = await import('vitrify/dev')
|
|
47
|
+
const vite = await createVitrifyDevServer({
|
|
48
|
+
appDir: options.appDir,
|
|
49
|
+
framework: 'vue',
|
|
50
|
+
base: options.baseUrl
|
|
51
|
+
})
|
|
52
|
+
|
|
53
|
+
console.log('Dev mode')
|
|
54
|
+
if (!('use' in fastify)) {
|
|
55
|
+
const middie = (await import('@fastify/middie')).default
|
|
56
|
+
await fastify.register(middie)
|
|
57
|
+
}
|
|
58
|
+
fastify.use(vite.middlewares)
|
|
59
|
+
} else {
|
|
60
|
+
options.appDir = options.appDir || new URL('../../..', import.meta.url)
|
|
61
|
+
fastify.register(fastifyStatic, {
|
|
62
|
+
root: new URL('./dist/csr', options.appDir).pathname,
|
|
63
|
+
wildcard: false,
|
|
64
|
+
index: false,
|
|
65
|
+
prefix: options.baseUrl
|
|
66
|
+
})
|
|
67
|
+
}
|
|
68
|
+
done()
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export { fastifyCsrPlugin }
|
|
72
|
+
export type FastifyCsrPlugin = typeof fastifyCsrPlugin
|