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
|
@@ -4,6 +4,7 @@ import Components from 'unplugin-vue-components/vite'
|
|
|
4
4
|
// import { prepareQuasarConf } from './quasar-conf-file.js'
|
|
5
5
|
import type {
|
|
6
6
|
BootFunction,
|
|
7
|
+
OnBootHook,
|
|
7
8
|
OnMountedHook,
|
|
8
9
|
VitrifyConfig
|
|
9
10
|
} from '../vitrify-config.js'
|
|
@@ -75,43 +76,12 @@ export const injectSsrContext = (
|
|
|
75
76
|
}${end}${ssrContext._meta.bodyTags || ''}`
|
|
76
77
|
})
|
|
77
78
|
|
|
78
|
-
// export interface Configuration {
|
|
79
|
-
// ssr?: 'server' | 'client' | 'ssg' | false
|
|
80
|
-
// }
|
|
81
|
-
|
|
82
79
|
export const QuasarPlugin: VitrifyPlugin = async ({
|
|
83
80
|
ssr = false,
|
|
84
81
|
pwa = false
|
|
85
82
|
}): Promise<Plugin[]> => {
|
|
86
|
-
// const extraPlugins: Plugin[] = []
|
|
87
|
-
// const ctx = {
|
|
88
|
-
// prod: process.env.MODE === 'production',
|
|
89
|
-
// dev: process.env.MODE === 'development',
|
|
90
|
-
// mode: {
|
|
91
|
-
// ssr: !!ssr,
|
|
92
|
-
// pwa: !!pwa
|
|
93
|
-
// }
|
|
94
|
-
// }
|
|
95
|
-
|
|
96
|
-
// let bootFilePaths: Record<string, any> = {}
|
|
97
|
-
// let components: string[] = []
|
|
98
83
|
let plugins: string[] = []
|
|
99
|
-
// let css: string[] = []
|
|
100
|
-
let extras: string[] = []
|
|
101
84
|
return [
|
|
102
|
-
// {
|
|
103
|
-
// name: 'legacy-support',
|
|
104
|
-
// enforce: 'pre',
|
|
105
|
-
// transform (code, id) {
|
|
106
|
-
// /**
|
|
107
|
-
// * ESM does not resolve an import to .default when there are multiple exports. The following is required to make the VuePlugin import of QCalendar work.
|
|
108
|
-
// */
|
|
109
|
-
// if (code.includes('app.use(VuePlugin)')) {
|
|
110
|
-
// code = code.replace(/app\.use\(VuePlugin\)/g, `app.use(VuePlugin.install ? VuePlugin : VuePlugin.default)`)
|
|
111
|
-
// }
|
|
112
|
-
// return code
|
|
113
|
-
// }
|
|
114
|
-
// },
|
|
115
85
|
Components({
|
|
116
86
|
resolvers: [QuasarResolver()]
|
|
117
87
|
}),
|
|
@@ -143,17 +113,15 @@ export const QuasarPlugin: VitrifyPlugin = async ({
|
|
|
143
113
|
}
|
|
144
114
|
]
|
|
145
115
|
|
|
146
|
-
const
|
|
116
|
+
const onBootHooks: OnBootHook[] = [
|
|
147
117
|
async ({ app, ssrContext, staticImports }) => {
|
|
148
|
-
// @ts-ignore
|
|
149
|
-
// const quasarVuePlugin = (await import('quasar/vue-plugin')).default
|
|
150
118
|
// @ts-ignore
|
|
151
119
|
const quasarPlugins = await import('virtual:quasar-plugins')
|
|
152
120
|
// @ts-ignore
|
|
153
121
|
const directives = await import('quasar/src/directives.js')
|
|
154
122
|
|
|
155
123
|
app.use(
|
|
156
|
-
staticImports
|
|
124
|
+
staticImports?.Quasar,
|
|
157
125
|
{
|
|
158
126
|
plugins: quasarPlugins,
|
|
159
127
|
directives
|
|
@@ -166,14 +134,14 @@ export const QuasarPlugin: VitrifyPlugin = async ({
|
|
|
166
134
|
return {
|
|
167
135
|
vitrify: {
|
|
168
136
|
urls,
|
|
169
|
-
bootFunctions,
|
|
170
|
-
ssrFunctions: [injectSsrContext],
|
|
171
137
|
globalCss,
|
|
172
138
|
staticImports: {
|
|
173
139
|
quasar: ['Quasar']
|
|
174
140
|
},
|
|
175
141
|
hooks: {
|
|
176
|
-
|
|
142
|
+
onBoot: onBootHooks,
|
|
143
|
+
onMounted: onMountedHooks,
|
|
144
|
+
onRendered: [injectSsrContext]
|
|
177
145
|
},
|
|
178
146
|
sass: {
|
|
179
147
|
additionalData: [`@import 'quasar/src/css/index.sass'`]
|
|
@@ -185,22 +153,7 @@ export const QuasarPlugin: VitrifyPlugin = async ({
|
|
|
185
153
|
{
|
|
186
154
|
name: 'vite-plugin-quasar',
|
|
187
155
|
enforce: 'post',
|
|
188
|
-
// transformIndexHtml: {
|
|
189
|
-
// enforce: 'post',
|
|
190
|
-
// transform: (html) => {
|
|
191
|
-
// return html.replace(
|
|
192
|
-
// '<!--product-name-->',
|
|
193
|
-
// productName
|
|
194
|
-
// )
|
|
195
|
-
// }
|
|
196
|
-
// },
|
|
197
156
|
config: async (config: VitrifyConfig, env) => {
|
|
198
|
-
// let appDir: URL
|
|
199
|
-
// let cliDir: URL
|
|
200
|
-
// let cwd: URL
|
|
201
|
-
// let quasarDir: URL
|
|
202
|
-
// let quasarConf: QuasarConf | undefined
|
|
203
|
-
|
|
204
157
|
const { quasar: quasarConf, vitrify: { urls } = {} } = config
|
|
205
158
|
|
|
206
159
|
const quasarPkgJsonPath = new URL(
|
|
@@ -211,33 +164,6 @@ export const QuasarPlugin: VitrifyPlugin = async ({
|
|
|
211
164
|
readFileSync(quasarPkgJsonPath, { encoding: 'utf-8' })
|
|
212
165
|
)
|
|
213
166
|
|
|
214
|
-
// if (quasarConf?.boot) {
|
|
215
|
-
// bootFilePaths = (quasarConf.boot as (Record<string, any> | string)[])
|
|
216
|
-
// .filter(entry => {
|
|
217
|
-
// if (typeof entry === 'object') return (entry.server && (ssr === 'server'))
|
|
218
|
-
// else if (entry !== '') return true
|
|
219
|
-
// })
|
|
220
|
-
// .map(entry => {
|
|
221
|
-
// if (typeof entry === 'string') return entry
|
|
222
|
-
// else if (typeof entry === 'object') return entry.path
|
|
223
|
-
// })
|
|
224
|
-
// .reduce((acc, entry) => {
|
|
225
|
-
// if (entry[0] === '~') {
|
|
226
|
-
// const split = entry.substring(1).split('/')
|
|
227
|
-
// const name = split[0].replace(/[|&;$%@"<>()+,]/g, "");
|
|
228
|
-
// acc[name] = {
|
|
229
|
-
// path: new URL(`node_modules/${entry.substring(1)}`, urls?.app).pathname
|
|
230
|
-
// }
|
|
231
|
-
// } else {
|
|
232
|
-
// const name = entry.split('.')[0]
|
|
233
|
-
// acc[name] = {
|
|
234
|
-
// path: `src/boot/${entry}`
|
|
235
|
-
// }
|
|
236
|
-
// }
|
|
237
|
-
// return acc
|
|
238
|
-
// }, {})
|
|
239
|
-
// }
|
|
240
|
-
|
|
241
167
|
/**
|
|
242
168
|
* All components should have been auto-imported
|
|
243
169
|
*/
|
|
@@ -249,14 +175,6 @@ export const QuasarPlugin: VitrifyPlugin = async ({
|
|
|
249
175
|
plugins = quasarConf?.framework.plugins
|
|
250
176
|
}
|
|
251
177
|
|
|
252
|
-
// css = (quasarConf?.css || []).map((v => {
|
|
253
|
-
// if (v[0] === '~') {
|
|
254
|
-
// return v.slice(1)
|
|
255
|
-
// }
|
|
256
|
-
// return v
|
|
257
|
-
// })).map((v) => `@import '${v}'`)
|
|
258
|
-
extras = quasarConf?.extras || []
|
|
259
|
-
|
|
260
178
|
return {
|
|
261
179
|
resolve: {
|
|
262
180
|
alias: [
|
|
@@ -302,22 +220,20 @@ export const QuasarPlugin: VitrifyPlugin = async ({
|
|
|
302
220
|
find: 'quasar/src',
|
|
303
221
|
replacement: new URL('src/', urls?.packages?.quasar).pathname
|
|
304
222
|
},
|
|
305
|
-
// ...extras.map(extra => ({
|
|
306
|
-
// find: `@quasar/extras/${extra}/${extra}.css`,
|
|
307
|
-
// replacement: new URL(`${extra}/${extra}.css`, urls?.packages?.['@quasar/extras']).pathname
|
|
308
|
-
// })
|
|
309
|
-
// ),
|
|
310
223
|
{
|
|
311
|
-
find: 'quasar',
|
|
312
|
-
replacement: new URL('src/', urls?.packages?.quasar)
|
|
224
|
+
find: new RegExp('^quasar$'),
|
|
225
|
+
replacement: new URL('src/index.all.js', urls?.packages?.quasar)
|
|
226
|
+
.pathname
|
|
313
227
|
},
|
|
228
|
+
// {
|
|
229
|
+
// find: 'quasar',
|
|
230
|
+
// replacement: new URL('src/index.all.js', urls?.packages?.quasar).pathname
|
|
231
|
+
// },
|
|
314
232
|
{
|
|
315
233
|
find: `@quasar/extras`,
|
|
316
234
|
replacement: new URL('.', urls?.packages?.['@quasar/extras'])
|
|
317
235
|
.pathname
|
|
318
236
|
}
|
|
319
|
-
// { find: new RegExp('^quasar$'), replacement: new URL('src/index.all.js', urls?.packages?.quasar).pathname },
|
|
320
|
-
// { find: new RegExp('^quasar$'), replacement: 'virtual:quasar' },
|
|
321
237
|
]
|
|
322
238
|
},
|
|
323
239
|
define: {
|
|
@@ -328,19 +244,6 @@ export const QuasarPlugin: VitrifyPlugin = async ({
|
|
|
328
244
|
__QUASAR_SSR_CLIENT__: ssr === 'client',
|
|
329
245
|
__QUASAR_SSR_PWA__: ssr === 'client' && pwa
|
|
330
246
|
},
|
|
331
|
-
// css: {
|
|
332
|
-
// preprocessorOptions: {
|
|
333
|
-
// sass: {
|
|
334
|
-
// additionalData: `@import 'quasar/src/css/index.sass'`
|
|
335
|
-
// // [
|
|
336
|
-
// // // ...extras.map(extra => `@import "@quasar/extras/${extra}/${extra}.css"`),
|
|
337
|
-
// // // ...extras.map(extra => `@import ${new URL(`${extra}/${extra}.css`, urls?.packages?.['@quasar/extras']).pathname}`) || [],
|
|
338
|
-
// // // config.css?.preprocessorOptions?.sass?.additionalData,
|
|
339
|
-
// // `@import 'quasar/src/css/index.sass'`
|
|
340
|
-
// // ].join('\n')
|
|
341
|
-
// }
|
|
342
|
-
// }
|
|
343
|
-
// },
|
|
344
247
|
ssr: {
|
|
345
248
|
noExternal: ['quasar']
|
|
346
249
|
}
|
|
@@ -353,7 +256,7 @@ export const QuasarPlugin: VitrifyPlugin = async ({
|
|
|
353
256
|
config: async (config, env) => ({
|
|
354
257
|
resolve: {
|
|
355
258
|
alias: [
|
|
356
|
-
{ find: new RegExp('^quasar$'), replacement: 'virtual:quasar' }
|
|
259
|
+
// { find: new RegExp('^quasar$'), replacement: 'virtual:quasar' }
|
|
357
260
|
]
|
|
358
261
|
}
|
|
359
262
|
}),
|
|
@@ -12,6 +12,15 @@ export type BootFunction = ({
|
|
|
12
12
|
ssrContext: Record<string, unknown>
|
|
13
13
|
staticImports: Record<string, any>
|
|
14
14
|
}) => Promise<void> | void
|
|
15
|
+
export type OnBootHook = ({
|
|
16
|
+
app,
|
|
17
|
+
ssrContext,
|
|
18
|
+
staticImports
|
|
19
|
+
}: {
|
|
20
|
+
app: any
|
|
21
|
+
ssrContext: Record<string, unknown>
|
|
22
|
+
staticImports?: Record<string, any>
|
|
23
|
+
}) => Promise<void> | void
|
|
15
24
|
export type OnMountedHook = (
|
|
16
25
|
instance: ComponentInternalInstance
|
|
17
26
|
) => Promise<void> | void
|
|
@@ -20,30 +29,42 @@ export type SsrFunction = (
|
|
|
20
29
|
html: string,
|
|
21
30
|
ssrContext: Record<string, any>
|
|
22
31
|
) => string
|
|
23
|
-
|
|
32
|
+
export type OnRenderedHook = (
|
|
33
|
+
html: string,
|
|
34
|
+
ssrContext: Record<string, any>
|
|
35
|
+
) => string
|
|
36
|
+
// export type OnSetupHook = (
|
|
37
|
+
// fastify: FastifyInstance,
|
|
38
|
+
// staticImports?: Record<string, any>
|
|
39
|
+
// ) => any
|
|
40
|
+
export type OnSetupFile = URL
|
|
24
41
|
export interface VitrifyConfig extends UserConfig {
|
|
25
42
|
vitrify?: {
|
|
26
43
|
/**
|
|
27
44
|
* Global CSS imports
|
|
28
45
|
*/
|
|
29
46
|
globalCss?: string[]
|
|
30
|
-
/**
|
|
31
|
-
* Functions which run after initializing the app
|
|
32
|
-
*/
|
|
33
|
-
bootFunctions?: BootFunction[]
|
|
34
|
-
/**
|
|
35
|
-
* Functions which run on the server after rendering the app
|
|
36
|
-
*/
|
|
37
|
-
ssrFunctions?: SsrFunction[]
|
|
38
47
|
/**
|
|
39
48
|
* Static imports in the app: packageName: [imports]
|
|
40
49
|
*/
|
|
41
50
|
staticImports?: StaticImports
|
|
42
51
|
hooks?: {
|
|
52
|
+
/**
|
|
53
|
+
* setup() is called directly after instantiating fastify. Use it to register your own plugins, routes etc.
|
|
54
|
+
*/
|
|
55
|
+
onSetup?: OnSetupFile[]
|
|
43
56
|
/**
|
|
44
57
|
* Functions which run in the onMounted hook of the app
|
|
45
58
|
*/
|
|
46
|
-
onMounted
|
|
59
|
+
onMounted?: OnMountedHook[]
|
|
60
|
+
/**
|
|
61
|
+
* Functions which run after initializing the app
|
|
62
|
+
*/
|
|
63
|
+
onBoot?: OnBootHook[]
|
|
64
|
+
/**
|
|
65
|
+
* Functions which run after rendering the app (SSR)
|
|
66
|
+
*/
|
|
67
|
+
onRendered?: OnRenderedHook[]
|
|
47
68
|
}
|
|
48
69
|
/**
|
|
49
70
|
* Global SASS variables
|
|
@@ -52,12 +73,6 @@ export interface VitrifyConfig extends UserConfig {
|
|
|
52
73
|
variables?: Record<string, string>
|
|
53
74
|
additionalData?: string[]
|
|
54
75
|
}
|
|
55
|
-
fastify?: {
|
|
56
|
-
/**
|
|
57
|
-
* setup() is called directly after instantiating fastify. Use it to register your own plugins, routes etc.
|
|
58
|
-
*/
|
|
59
|
-
setup: (fastify: FastifyInstance) => any
|
|
60
|
-
}
|
|
61
76
|
/**
|
|
62
77
|
* Product name of the application. Will be used for the HTML title tag
|
|
63
78
|
*/
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { FastifyInstance } from 'fastify'
|
|
2
|
+
import { onSetup } from 'virtual:vitrify-hooks'
|
|
3
|
+
|
|
4
|
+
export const setup = async ({ fastify }: { fastify: FastifyInstance }) => {
|
|
5
|
+
if (onSetup?.length) {
|
|
6
|
+
for (const setup of onSetup) {
|
|
7
|
+
await setup(fastify)
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
return fastify
|
|
11
|
+
}
|
package/src/vite/vue/index.html
CHANGED
package/src/vite/vue/main.ts
CHANGED
|
@@ -4,18 +4,11 @@ import {
|
|
|
4
4
|
createSSRApp,
|
|
5
5
|
createApp as createVueApp,
|
|
6
6
|
h,
|
|
7
|
-
onMounted,
|
|
7
|
+
onMounted as onMountedVue,
|
|
8
8
|
getCurrentInstance
|
|
9
9
|
} from 'vue'
|
|
10
|
-
|
|
11
|
-
// import quasarPlugins from 'virtual:quasar-plugins'
|
|
12
|
-
// import bootFunctions from 'virtual:quasar-boot'
|
|
13
|
-
import bootFunctions from 'virtual:boot-functions'
|
|
14
|
-
import onMountedHooks from 'virtual:on-mounted-hooks'
|
|
15
|
-
// import 'virtual:quasar-extras'
|
|
16
|
-
// import * as directives from 'quasar/directives'
|
|
10
|
+
import { onBoot, onMounted } from 'virtual:vitrify-hooks'
|
|
17
11
|
import routes from 'src/router/routes'
|
|
18
|
-
import 'virtual:global-css'
|
|
19
12
|
import * as staticImports from 'virtual:static-imports'
|
|
20
13
|
|
|
21
14
|
interface ssrContext {
|
|
@@ -34,13 +27,10 @@ export async function createApp(
|
|
|
34
27
|
setup(props) {
|
|
35
28
|
const instance = getCurrentInstance()
|
|
36
29
|
|
|
37
|
-
|
|
38
|
-
for (let fn of
|
|
30
|
+
onMountedVue(async () => {
|
|
31
|
+
for (let fn of onMounted) {
|
|
39
32
|
await fn(instance, staticImports)
|
|
40
33
|
}
|
|
41
|
-
// onAppMounted()
|
|
42
|
-
// const { proxy: { $q } } = getCurrentInstance()
|
|
43
|
-
// $q.onSSRHydrated !== void 0 && $q.onSSRHydrated()
|
|
44
34
|
})
|
|
45
35
|
|
|
46
36
|
return () => h(App, props)
|
|
@@ -63,11 +53,6 @@ export async function createApp(
|
|
|
63
53
|
next()
|
|
64
54
|
})
|
|
65
55
|
|
|
66
|
-
// app.use(Quasar, {
|
|
67
|
-
// plugins: quasarPlugins,
|
|
68
|
-
// directives
|
|
69
|
-
// }, ssrContext)
|
|
70
|
-
|
|
71
56
|
let provide: Record<string, unknown> = {}
|
|
72
57
|
if (import.meta.env.SSR) {
|
|
73
58
|
if (ssrContext?.provide) {
|
|
@@ -81,7 +66,7 @@ export async function createApp(
|
|
|
81
66
|
app.provide(key, provide[key])
|
|
82
67
|
}
|
|
83
68
|
|
|
84
|
-
for (let fn of
|
|
69
|
+
for (let fn of onBoot) {
|
|
85
70
|
await fn({ app, ssrContext, staticImports })
|
|
86
71
|
}
|
|
87
72
|
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { createApp } from '../../../node/frameworks/vue/server.js'
|
|
2
|
+
import { getAppDir } from '../../../node/app-urls.js'
|
|
3
|
+
// import { setup } from 'virtual:fastify-setup'
|
|
4
|
+
import { onRendered, onSetup } from 'virtual:vitrify-hooks'
|
|
5
|
+
import { fastifySsrPlugin } from './fastify-ssr-plugin.js'
|
|
6
|
+
import type { ViteDevServer } from 'vite'
|
|
7
|
+
import * as imr from 'import-meta-resolve'
|
|
8
|
+
const { resolve } = imr
|
|
9
|
+
// const appDir = getPkgJsonDir(import.meta.url)
|
|
10
|
+
const getString = (str?: string) => str
|
|
11
|
+
let baseUrl = getString(__BASE_URL__)
|
|
12
|
+
const appDir = getAppDir()
|
|
13
|
+
|
|
14
|
+
export const setupApp = async () => {
|
|
15
|
+
const vitrifyDir = new URL('../', await resolve('vitrify', import.meta.url))
|
|
16
|
+
return createApp({
|
|
17
|
+
onSetup,
|
|
18
|
+
appDir,
|
|
19
|
+
baseUrl,
|
|
20
|
+
onRendered,
|
|
21
|
+
fastifySsrPlugin,
|
|
22
|
+
vitrifyDir,
|
|
23
|
+
mode: import.meta.env.MODE
|
|
24
|
+
})
|
|
25
|
+
}
|
|
@@ -1,9 +1,21 @@
|
|
|
1
1
|
import { createApp } from '../main.js'
|
|
2
|
-
import { renderToString } from '
|
|
2
|
+
import { renderToString } from 'vue/server-renderer'
|
|
3
|
+
import type { FastifyInstance } from 'fastify'
|
|
3
4
|
// import * as ApolloSSR from '@vue/apollo-ssr'
|
|
4
5
|
// import { ApolloClients } from '@vue/apollo-composable'
|
|
5
6
|
// import serialize from 'serialize-javascript'
|
|
6
7
|
|
|
8
|
+
import { onSetup } from 'virtual:vitrify-hooks'
|
|
9
|
+
|
|
10
|
+
export const setup = async ({ fastify }: { fastify: FastifyInstance }) => {
|
|
11
|
+
if (onSetup?.length) {
|
|
12
|
+
for (const setup of onSetup) {
|
|
13
|
+
await setup(fastify)
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
return fastify
|
|
17
|
+
}
|
|
18
|
+
|
|
7
19
|
const initializeApp = async (url, ssrContext) => {
|
|
8
20
|
const onRenderedList = []
|
|
9
21
|
Object.assign(ssrContext, {
|
|
@@ -1,120 +1,4 @@
|
|
|
1
1
|
import { fastifySsrPlugin } from '../../../node/frameworks/vue/fastify-ssr-plugin.js'
|
|
2
|
-
import
|
|
2
|
+
import { onRendered } from 'virtual:vitrify-hooks'
|
|
3
3
|
|
|
4
|
-
export { fastifySsrPlugin,
|
|
5
|
-
|
|
6
|
-
// import { FastifyPluginCallback, FastifyRequest, FastifyReply } from 'fastify'
|
|
7
|
-
// import fastifyStatic from 'fastify-static'
|
|
8
|
-
// import { readFileSync } from 'fs'
|
|
9
|
-
// import { injectSsrContext } from '../../node/helpers/ssr.js'
|
|
10
|
-
// import type { ViteDevServer } from 'vite'
|
|
11
|
-
|
|
12
|
-
// export interface FastifySsrOptions {
|
|
13
|
-
// baseUrl?: string
|
|
14
|
-
// provide?: (req: FastifyRequest, res: FastifyReply) => Promise<Record<string, unknown>>
|
|
15
|
-
// vite?: ViteDevServer
|
|
16
|
-
// cliDir?: URL
|
|
17
|
-
// appDir?: URL
|
|
18
|
-
// productName?: string
|
|
19
|
-
// }
|
|
20
|
-
|
|
21
|
-
// const fastifySsrPlugin: FastifyPluginCallback<FastifySsrOptions> = async (fastify, options, done) => {
|
|
22
|
-
// if (import.meta.env.MODE === 'development') {
|
|
23
|
-
// if (!options.vite) throw new Error('Option vite cannot be undefined')
|
|
24
|
-
// const middie = (await import('middie')).default
|
|
25
|
-
// await fastify.register(middie)
|
|
26
|
-
// fastify.use(options.vite.middlewares)
|
|
27
|
-
|
|
28
|
-
// fastify.get('*', async (req, res) => {
|
|
29
|
-
// try {
|
|
30
|
-
// // const url = req.originalUrl
|
|
31
|
-
// const url = req.raw.url
|
|
32
|
-
// let template
|
|
33
|
-
// let render
|
|
34
|
-
// const ssrContext = {
|
|
35
|
-
// req,
|
|
36
|
-
// res
|
|
37
|
-
// }
|
|
38
|
-
// // always read fresh template in dev
|
|
39
|
-
// // template = readFileSync(resolve('index.html'), 'utf-8')
|
|
40
|
-
// template = readFileSync(new URL('index.html', options.cliDir)).toString()
|
|
41
|
-
|
|
42
|
-
// // template = await vite.transformIndexHtml(url, template)
|
|
43
|
-
// const entryUrl = new URL('ssr/entry-server.ts', options.cliDir).pathname
|
|
44
|
-
// render = (await options.vite!.ssrLoadModule(entryUrl)).render
|
|
45
|
-
// let manifest
|
|
46
|
-
// // TODO: https://github.com/vitejs/vite/issues/2282
|
|
47
|
-
// try {
|
|
48
|
-
// manifest = {}
|
|
49
|
-
// } catch (e) {
|
|
50
|
-
// manifest = {}
|
|
51
|
-
// }
|
|
52
|
-
|
|
53
|
-
// const [appHtml, preloadLinks] = await render(url, manifest, ssrContext)
|
|
54
|
-
// const html = template
|
|
55
|
-
// .replace(`<!--preload-links-->`, preloadLinks)
|
|
56
|
-
// .replace(`<!--app-html-->`, appHtml)
|
|
57
|
-
// .replace('<!--product-name-->', options.productName || 'Product name')
|
|
58
|
-
|
|
59
|
-
// res.code(200)
|
|
60
|
-
// res.type('text/html')
|
|
61
|
-
// res.send(html)
|
|
62
|
-
// // res.status(200).set({ 'Content-Type': 'text/html' }).end(html)
|
|
63
|
-
// } catch (e: any) {
|
|
64
|
-
// console.error(e.stack)
|
|
65
|
-
// options.vite && options.vite.ssrFixStacktrace(e)
|
|
66
|
-
// res.code(500)
|
|
67
|
-
// res.send(e.stack)
|
|
68
|
-
// }
|
|
69
|
-
// })
|
|
70
|
-
// } else {
|
|
71
|
-
// options.baseUrl = options.baseUrl || '/'
|
|
72
|
-
// fastify.register(fastifyStatic, {
|
|
73
|
-
// root: new URL('./dist/ssr/client', options.appDir).pathname,
|
|
74
|
-
// wildcard: false,
|
|
75
|
-
// index: false,
|
|
76
|
-
// prefix: options.baseUrl
|
|
77
|
-
// })
|
|
78
|
-
|
|
79
|
-
// fastify.get(`${options.baseUrl}*`, async (req, res) => {
|
|
80
|
-
// const url = req.raw.url
|
|
81
|
-
// const provide = options.provide ? await options.provide(req, res) : {}
|
|
82
|
-
// let template
|
|
83
|
-
// let render
|
|
84
|
-
// let manifest
|
|
85
|
-
// const ssrContext: Record<string, any> = {
|
|
86
|
-
// req,
|
|
87
|
-
// res,
|
|
88
|
-
// provide
|
|
89
|
-
// }
|
|
90
|
-
|
|
91
|
-
// // template = readFileSync(new URL('../client/index.html', import.meta.url).pathname).toString()
|
|
92
|
-
// // manifest = JSON.parse(readFileSync(new URL('../client/ssr-manifest.json', import.meta.url)).toString())
|
|
93
|
-
// // render = (await import(new URL('./entry-server.mjs', import.meta.url).pathname)).render
|
|
94
|
-
// template = readFileSync(new URL('./dist/ssr/client/index.html', options.appDir).pathname).toString()
|
|
95
|
-
// manifest = JSON.parse(readFileSync(new URL('./dist/ssr/client/ssr-manifest.json', options.appDir)).toString())
|
|
96
|
-
// render = (await import(new URL('./dist/ssr/server/entry-server.mjs', options.appDir).pathname)).render
|
|
97
|
-
|
|
98
|
-
// const [appHtml, preloadLinks] = await render(url, manifest, ssrContext)
|
|
99
|
-
|
|
100
|
-
// if (!ssrContext.initialState) ssrContext.initialState = {}
|
|
101
|
-
// ssrContext.initialState.provide = provide
|
|
102
|
-
|
|
103
|
-
// let html = template
|
|
104
|
-
// .replace(`<!--preload-links-->`, preloadLinks)
|
|
105
|
-
// .replace(`<!--app-html-->`, appHtml)
|
|
106
|
-
// html = injectSsrContext(html, ssrContext)
|
|
107
|
-
|
|
108
|
-
// res.code(200)
|
|
109
|
-
// res.type('text/html')
|
|
110
|
-
// res.send(html)
|
|
111
|
-
// })
|
|
112
|
-
// }
|
|
113
|
-
|
|
114
|
-
// done()
|
|
115
|
-
|
|
116
|
-
// }
|
|
117
|
-
|
|
118
|
-
// export {
|
|
119
|
-
// fastifySsrPlugin
|
|
120
|
-
// }
|
|
4
|
+
export { fastifySsrPlugin, onRendered }
|
|
@@ -1,18 +1,27 @@
|
|
|
1
|
-
import { createApp } from '../../../node/frameworks/vue/server.js'
|
|
2
|
-
import { getAppDir } from '../../../node/app-urls.js'
|
|
3
|
-
import { setup } from 'virtual:fastify-setup'
|
|
4
|
-
import
|
|
5
|
-
|
|
1
|
+
// import { createApp } from '../../../node/frameworks/vue/server.js'
|
|
2
|
+
// import { getAppDir } from '../../../node/app-urls.js'
|
|
3
|
+
// import { setup } from 'virtual:fastify-setup'
|
|
4
|
+
// import { onRendered, onSetup } from 'virtual:vitrify-hooks'
|
|
5
|
+
// import { fastifySsrPlugin } from './fastify-ssr-plugin.js'
|
|
6
|
+
import { setupApp } from './app.js'
|
|
7
|
+
// import * as staticImports from 'virtual:static-imports'
|
|
6
8
|
// const appDir = getPkgJsonDir(import.meta.url)
|
|
7
|
-
const getString = (str?: string) => str
|
|
8
|
-
let baseUrl = getString(__BASE_URL__)
|
|
9
|
-
const appDir = getAppDir()
|
|
9
|
+
// const getString = (str?: string) => str
|
|
10
|
+
// let baseUrl = getString(__BASE_URL__)
|
|
11
|
+
// const appDir = getAppDir()
|
|
10
12
|
|
|
11
|
-
const app = createApp({
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
13
|
+
// const app = createApp({
|
|
14
|
+
// onSetup,
|
|
15
|
+
// appDir,
|
|
16
|
+
// baseUrl,
|
|
17
|
+
// onRendered,
|
|
18
|
+
// fastifySsrPlugin,
|
|
19
|
+
// mode: import.meta.env.MODE
|
|
20
|
+
// })
|
|
17
21
|
|
|
18
|
-
app
|
|
22
|
+
const app = await setupApp()
|
|
23
|
+
|
|
24
|
+
app.listen({
|
|
25
|
+
port: Number(process.env.PORT || 3000),
|
|
26
|
+
host: process.env.HOST || '127.0.0.1'
|
|
27
|
+
})
|
|
@@ -1,52 +0,0 @@
|
|
|
1
|
-
export const injectSsrContext = (html: string, ssrContext: Record<string, any>) => html.replace(
|
|
2
|
-
/(<html[^>]*)(>)/i,
|
|
3
|
-
(found, start, end) => {
|
|
4
|
-
let matches
|
|
5
|
-
|
|
6
|
-
matches = found.match(/\sdir\s*=\s*['"]([^'"]*)['"]/i)
|
|
7
|
-
if (matches) {
|
|
8
|
-
start = start.replace(matches[0], '')
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
matches = found.match(/\slang\s*=\s*['"]([^'"]*)['"]/i)
|
|
12
|
-
if (matches) {
|
|
13
|
-
start = start.replace(matches[0], '')
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
return `${start} ${ssrContext._meta.htmlAttrs || ''} ${end}`
|
|
17
|
-
}
|
|
18
|
-
)
|
|
19
|
-
.replace(
|
|
20
|
-
/(<head[^>]*)(>)/i,
|
|
21
|
-
(_, start, end) => `${start}${end}${ssrContext._meta.headTags || ''}`
|
|
22
|
-
)
|
|
23
|
-
.replace(
|
|
24
|
-
/(<\/head>)/i,
|
|
25
|
-
(_, tag) => `${ssrContext._meta.resourceStyles || ''}${ssrContext._meta.endingHeadTags || ''}${tag}`
|
|
26
|
-
)
|
|
27
|
-
.replace(
|
|
28
|
-
/(<body[^>]*)(>)/i,
|
|
29
|
-
(found, start, end) => {
|
|
30
|
-
let classes = ssrContext._meta.bodyClasses || ''
|
|
31
|
-
|
|
32
|
-
const matches = found.match(/\sclass\s*=\s*['"]([^'"]*)['"]/i)
|
|
33
|
-
|
|
34
|
-
if (matches) {
|
|
35
|
-
if (matches[1].length > 0) {
|
|
36
|
-
classes += ` ${matches[1]}`
|
|
37
|
-
}
|
|
38
|
-
start = start.replace(matches[0], '')
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
return `${start} class="${classes.trim()}" ${ssrContext._meta.bodyAttrs || ''}${end}${ssrContext._meta.bodyTags || ''}`
|
|
42
|
-
}
|
|
43
|
-
)
|
|
44
|
-
.replace(`<!--initial-state-->`, `<script>
|
|
45
|
-
window.__INITIAL_STATE__ = ${JSON.stringify(ssrContext.initialState)}
|
|
46
|
-
</script>
|
|
47
|
-
`)
|
|
48
|
-
|
|
49
|
-
export const injectInitialState = (html: string, ssrContext: Record<string, any>) =>
|
|
50
|
-
html.replace(`<!--initial-state-->`, `<script>
|
|
51
|
-
window.__INITIAL_STATE__ = ${JSON.stringify(ssrContext.initialState)}
|
|
52
|
-
</script>`)
|