vitrify 0.3.0 → 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 (36) hide show
  1. package/dist/app-urls.js +1 -1
  2. package/dist/bin/cli.js +26 -4
  3. package/dist/bin/dev.js +59 -27
  4. package/dist/frameworks/vue/fastify-ssr-plugin.js +67 -16
  5. package/dist/frameworks/vue/server.js +9 -4
  6. package/dist/helpers/collect-css-ssr.js +57 -0
  7. package/dist/index.js +255 -69
  8. package/dist/plugins/quasar.js +9 -4
  9. package/dist/types/bin/build.d.ts +2 -2
  10. package/dist/types/bin/dev.d.ts +39 -3
  11. package/dist/types/frameworks/vue/fastify-ssr-plugin.d.ts +6 -3
  12. package/dist/types/frameworks/vue/server.d.ts +9 -5
  13. package/dist/types/helpers/collect-css-ssr.d.ts +10 -0
  14. package/dist/types/helpers/routes.d.ts +1 -1
  15. package/dist/types/index.d.ts +1 -1
  16. package/dist/types/plugins/index.d.ts +1 -1
  17. package/dist/types/vitrify-config.d.ts +3 -4
  18. package/package.json +32 -32
  19. package/src/node/app-urls.ts +1 -1
  20. package/src/node/bin/build.ts +2 -2
  21. package/src/node/bin/cli.ts +33 -5
  22. package/src/node/bin/dev.ts +92 -34
  23. package/src/node/frameworks/vue/fastify-ssr-plugin.ts +80 -18
  24. package/src/node/frameworks/vue/server.ts +22 -8
  25. package/src/node/helpers/collect-css-ssr.ts +77 -0
  26. package/src/node/index.ts +285 -81
  27. package/src/node/plugins/index.ts +1 -1
  28. package/src/node/plugins/quasar.ts +9 -4
  29. package/src/node/vitrify-config.ts +7 -4
  30. package/src/vite/fastify/entry.ts +11 -0
  31. package/src/vite/fastify/server.ts +10 -0
  32. package/src/vite/vue/index.html +1 -0
  33. package/src/vite/vue/main.ts +0 -1
  34. package/src/vite/vue/ssr/app.ts +25 -0
  35. package/src/vite/vue/ssr/entry-server.ts +13 -1
  36. package/src/vite/vue/ssr/server.ts +23 -14
package/src/node/index.ts CHANGED
@@ -1,6 +1,11 @@
1
1
  import vuePlugin from '@vitejs/plugin-vue'
2
2
  import type { InlineConfig, UserConfig } from 'vite'
3
+ import { resolveConfig } from 'vite'
3
4
  import { mergeConfig } from 'vite'
5
+ import { build } from 'esbuild'
6
+ import fs from 'fs'
7
+ import path from 'path'
8
+ import { pathToFileURL } from 'url'
4
9
  import { readFileSync } from 'fs'
5
10
  import builtinModules from 'builtin-modules'
6
11
  import { resolve } from 'import-meta-resolve'
@@ -11,13 +16,39 @@ import type {
11
16
  VitrifyConfig,
12
17
  OnRenderedHook,
13
18
  OnBootHook,
14
- OnSetupHook
19
+ OnSetupFile
15
20
  } from './vitrify-config.js'
16
21
  import type { VitrifyContext } from './bin/run.js'
17
22
  import type { VitrifyPlugin } from './plugins/index.js'
18
23
  import { getPkgJsonDir } from './app-urls.js'
24
+ import type { RollupOptions } from 'rollup'
19
25
 
20
- const serverModules = ['fastify', 'middie']
26
+ const serverModules = [
27
+ // 'fs',
28
+ // 'path',
29
+ // 'url',
30
+ // 'module',
31
+ // 'crypto',
32
+ // 'node:fs',
33
+ 'util',
34
+ 'node:url',
35
+ 'node:util',
36
+ 'node:fs',
37
+ 'vitrify',
38
+ 'vite',
39
+ 'fastify',
40
+ 'middie',
41
+ 'knex',
42
+ 'bcrypt',
43
+ 'objection',
44
+ '@fastify/formbody',
45
+ '@fastify/static',
46
+ '@fastify/cors',
47
+ '@fastify/cookie',
48
+ 'mercurius',
49
+ 'jose',
50
+ 'oidc-provider'
51
+ ]
21
52
 
22
53
  const configPluginMap: Record<string, () => Promise<VitrifyPlugin>> = {
23
54
  quasar: () =>
@@ -32,6 +63,64 @@ export const VIRTUAL_MODULES = [
32
63
  'virtual:static-imports'
33
64
  ]
34
65
 
66
+ async function bundleConfigFile(
67
+ fileName: string,
68
+ isESM = false
69
+ ): Promise<{ code: string; dependencies: string[] }> {
70
+ const result = await build({
71
+ absWorkingDir: process.cwd(),
72
+ entryPoints: [fileName],
73
+ outfile: 'out.js',
74
+ write: false,
75
+ platform: 'node',
76
+ bundle: true,
77
+ format: 'esm',
78
+ sourcemap: 'inline',
79
+ metafile: true,
80
+ plugins: [
81
+ {
82
+ name: 'externalize-deps',
83
+ setup(build) {
84
+ build.onResolve({ filter: /.*/ }, (args) => {
85
+ const id = args.path
86
+ if (id[0] !== '.' && !path.isAbsolute(id)) {
87
+ return {
88
+ external: true
89
+ }
90
+ }
91
+ })
92
+ }
93
+ },
94
+ {
95
+ name: 'replace-import-meta',
96
+ setup(build) {
97
+ build.onLoad({ filter: /\.[jt]s$/ }, async (args) => {
98
+ const contents = await fs.promises.readFile(args.path, 'utf8')
99
+ return {
100
+ loader: args.path.endsWith('.ts') ? 'ts' : 'js',
101
+ contents: contents
102
+ .replace(
103
+ /\bimport\.meta\.url\b/g,
104
+ JSON.stringify(pathToFileURL(args.path).href)
105
+ )
106
+ .replace(
107
+ /\b__dirname\b/g,
108
+ JSON.stringify(path.dirname(args.path))
109
+ )
110
+ .replace(/\b__filename\b/g, JSON.stringify(args.path))
111
+ }
112
+ })
113
+ }
114
+ }
115
+ ]
116
+ })
117
+ const { text } = result.outputFiles[0]
118
+ return {
119
+ code: text,
120
+ dependencies: result.metafile ? Object.keys(result.metafile.inputs) : []
121
+ }
122
+ }
123
+
35
124
  export const baseConfig = async ({
36
125
  ssr,
37
126
  appDir,
@@ -41,7 +130,7 @@ export const baseConfig = async ({
41
130
  framework = 'vue',
42
131
  pwa = false
43
132
  }: {
44
- ssr?: 'client' | 'server' | 'ssg'
133
+ ssr?: 'client' | 'server' | 'ssg' | 'fastify'
45
134
  appDir?: URL
46
135
  publicDir?: URL
47
136
  command?: 'build' | 'dev' | 'test'
@@ -59,22 +148,7 @@ export const baseConfig = async ({
59
148
  const cliDir = getCliDir()
60
149
  const cliViteDir = getCliViteDir(cliDir)
61
150
  const frameworkDir = new URL(`${framework}/`, cliViteDir)
62
-
63
- const localPackages = ['vue', 'vue-router']
64
- const cliPackages = ['vitest']
65
- const packageUrls: Record<string, URL> = {}
66
- await (async () => {
67
- for (const val of localPackages)
68
- packageUrls[val] = getPkgJsonDir(
69
- new URL(await resolve(val, appDir!.href))
70
- )
71
- })()
72
- await (async () => {
73
- for (const val of cliPackages)
74
- packageUrls[val] = getPkgJsonDir(
75
- new URL(await resolve(val, cliDir!.href))
76
- )
77
- })()
151
+ const fastifyDir = new URL('fastify/', cliViteDir)
78
152
 
79
153
  if (!publicDir) publicDir = new URL('public/', appDir)
80
154
  /**
@@ -82,24 +156,62 @@ export const baseConfig = async ({
82
156
  */
83
157
  let vitrifyConfig:
84
158
  | VitrifyConfig
85
- | (({ mode, command }: { mode: string; command: string }) => VitrifyConfig)
159
+ | (({
160
+ mode,
161
+ command
162
+ }: {
163
+ mode: string
164
+ command: string
165
+ }) => Promise<VitrifyConfig> | VitrifyConfig)
86
166
 
87
167
  try {
88
- vitrifyConfig = (
89
- await import(new URL('vitrify.config.js', appDir).pathname)
90
- ).default
168
+ if (fs.existsSync(new URL('vitrify.config.ts', appDir).pathname)) {
169
+ const configPath = new URL('vitrify.config.ts', appDir).pathname
170
+ const bundledConfig = await bundleConfigFile(
171
+ new URL('vitrify.config.ts', appDir).pathname
172
+ )
173
+ fs.writeFileSync(configPath + '.js', bundledConfig.code)
174
+ vitrifyConfig = (await import(configPath + '.js')).default
175
+ // fs.unlinkSync(configPath + '.js')
176
+ } else {
177
+ vitrifyConfig = (
178
+ await import(new URL('vitrify.config.js', appDir).pathname)
179
+ ).default
180
+ }
91
181
  if (typeof vitrifyConfig === 'function')
92
- vitrifyConfig = vitrifyConfig({ mode, command })
182
+ vitrifyConfig = await vitrifyConfig({ mode, command })
93
183
  } catch (e) {
94
- console.error(e)
95
- console.log('No vitrify.config.js file found, using defaults')
184
+ console.log('No vitrify.config.(ts|js) file found, using defaults')
96
185
  vitrifyConfig = {}
97
186
  }
98
- let { productName = 'Product name' } = JSON.parse(
99
- readFileSync(new URL('package.json', appDir).pathname, {
100
- encoding: 'utf-8'
101
- })
102
- )
187
+
188
+ const localPackages = ['vue', 'vue-router']
189
+ const cliPackages = []
190
+ const packageUrls: Record<string, URL> =
191
+ vitrifyConfig.vitrify?.urls?.packages || {}
192
+ await (async () => {
193
+ for (const val of localPackages)
194
+ packageUrls[val] = getPkgJsonDir(
195
+ new URL(await resolve(val, appDir!.href))
196
+ )
197
+ })()
198
+ // await (async () => {
199
+ // for (const val of cliPackages)
200
+ // packageUrls[val] = getPkgJsonDir(
201
+ // new URL(await resolve(val, cliDir!.href))
202
+ // )
203
+ // })()
204
+
205
+ let productName = 'Product name'
206
+ try {
207
+ ;({ productName } = JSON.parse(
208
+ readFileSync(new URL('package.json', appDir).pathname, {
209
+ encoding: 'utf-8'
210
+ })
211
+ ))
212
+ } catch (e) {
213
+ console.error('package.json not found')
214
+ }
103
215
 
104
216
  const ssrTransformCustomDir = () => {
105
217
  return {
@@ -124,7 +236,7 @@ export const baseConfig = async ({
124
236
  let onBootHooks: OnBootHook[]
125
237
  let onRenderedHooks: OnRenderedHook[]
126
238
  let onMountedHooks: OnMountedHook[]
127
- let onSetupHooks: OnSetupHook[]
239
+ let onSetupFiles: OnSetupFile[]
128
240
  let globalCss: string[]
129
241
  let staticImports: StaticImports
130
242
  let sassVariables: Record<string, string>
@@ -132,6 +244,7 @@ export const baseConfig = async ({
132
244
 
133
245
  const plugins: UserConfig['plugins'] = [
134
246
  vuePlugin({
247
+ compiler: await import('vue/compiler-sfc'),
135
248
  template: {
136
249
  ssr: !!ssr,
137
250
  compilerOptions: {
@@ -159,7 +272,7 @@ export const baseConfig = async ({
159
272
  onBootHooks = config.vitrify?.hooks?.onBoot || []
160
273
  onRenderedHooks = config.vitrify?.hooks?.onRendered || []
161
274
  onMountedHooks = config.vitrify?.hooks?.onMounted || []
162
- onSetupHooks = config?.vitrify?.hooks?.onSetup || []
275
+ onSetupFiles = config?.vitrify?.hooks?.onSetup || []
163
276
  globalCss = config.vitrify?.globalCss || []
164
277
  staticImports = config.vitrify?.staticImports || {}
165
278
  sassVariables = config.vitrify?.sass?.variables || {}
@@ -188,9 +301,17 @@ export const baseConfig = async ({
188
301
  }
189
302
  },
190
303
  resolveId(id) {
191
- if (VIRTUAL_MODULES.includes(id)) return id
304
+ if (VIRTUAL_MODULES.includes(id))
305
+ return { id, moduleSideEffects: false }
192
306
  return
193
307
  },
308
+ transform: (code, id) => {
309
+ if (id.endsWith('main.ts') && id.includes('vitrify')) {
310
+ code =
311
+ `${globalCss.map((css) => `import '${css}'`).join('\n')}\n` + code
312
+ }
313
+ return code
314
+ },
194
315
  load(id) {
195
316
  if (id === 'virtual:vitrify-hooks') {
196
317
  return `export const onBoot = [${onBootHooks
@@ -202,11 +323,27 @@ export const baseConfig = async ({
202
323
  export const onRendered = [${onRenderedHooks
203
324
  .map((fn) => `${String(fn)}`)
204
325
  .join(', ')}]
205
- export const onSetup = [${onSetupHooks
206
- .map((fn) => `${String(fn)}`)
207
- .join(', ')}]`
208
- } else if (id === 'virtual:global-css') {
209
- return `${globalCss.map((css) => `import '${css}'`).join('\n')}`
326
+ export const onSetup = []
327
+ ${onSetupFiles
328
+ .map(
329
+ (url, index) =>
330
+ `import ${url.pathname
331
+ .replaceAll('/', '')
332
+ .replaceAll('.', '')} from '${
333
+ url.pathname
334
+ }'; onSetup.push(${url.pathname
335
+ .replaceAll('/', '')
336
+ .replaceAll('.', '')})`
337
+ )
338
+ .join('\n')}`
339
+ // export const onSetup = [${onSetupHooks
340
+ // .map((fn) => `${String(fn)}`)
341
+ // .join(', ')}]`
342
+ /**
343
+ * CSS imports in virtual files do not seem to work. Using transform() instead
344
+ */
345
+ // } else if (id === 'virtual:global-css') {
346
+ // return `${globalCss.map((css) => `import '${css}'`).join('\n')}`
210
347
  } else if (id === 'virtual:static-imports') {
211
348
  return `${Object.entries(staticImports)
212
349
  .map(
@@ -242,6 +379,9 @@ export const baseConfig = async ({
242
379
  case 'client':
243
380
  entry = new URL('ssr/entry-client.ts', frameworkDir).pathname
244
381
  break
382
+ case 'fastify':
383
+ entry = new URL('entry.ts', fastifyDir).pathname
384
+ break
245
385
  default:
246
386
  entry = new URL('csr/entry.ts', frameworkDir).pathname
247
387
  }
@@ -277,19 +417,87 @@ export const baseConfig = async ({
277
417
  { find: 'cwd', replacement: cwd.pathname },
278
418
  { find: 'boot', replacement: new URL('boot/', srcDir).pathname },
279
419
  { find: 'assets', replacement: new URL('assets/', srcDir).pathname },
280
- { find: 'vue', replacement: packageUrls['vue'].pathname },
281
- { find: 'vue-router', replacement: packageUrls['vue-router'].pathname },
282
- { find: 'vitrify', replacement: cliDir.pathname }
420
+ ...Object.entries(packageUrls).map(([key, value]) => ({
421
+ find: key,
422
+ replacement: value.pathname
423
+ }))
424
+ // { find: 'vue', replacement: packageUrls['vue'].pathname },
425
+ // { find: 'vue-router', replacement: packageUrls['vue-router'].pathname },
426
+ // { find: 'vitrify', replacement: cliDir.pathname }
283
427
  ]
284
428
  if (command === 'test')
285
429
  alias.push({
286
430
  find: 'vitest',
287
- replacement: packageUrls.vitest.pathname
431
+ replacement: new URL(await resolve('vitest', cliDir!.href)).pathname
288
432
  })
289
433
 
434
+ let rollupOptions: RollupOptions
435
+ let noExternal: RegExp[] | string[] = []
436
+ const external = [...builtinModules, ...serverModules]
437
+ if (ssr === 'server') {
438
+ rollupOptions = {
439
+ input: [
440
+ new URL('ssr/entry-server.ts', frameworkDir).pathname,
441
+ new URL('ssr/prerender.ts', frameworkDir).pathname,
442
+ new URL('ssr/server.ts', frameworkDir).pathname
443
+ ],
444
+ external,
445
+ output: {
446
+ minifyInternalExports: false,
447
+ entryFileNames: '[name].mjs',
448
+ chunkFileNames: '[name].mjs',
449
+ // format: 'es',
450
+ manualChunks: (id) => {
451
+ if (id.includes('vitrify/src/vite/')) {
452
+ const name = id.split('/').at(-1)?.split('.').at(0)
453
+ if (name && manualChunks.includes(name)) return name
454
+ } else if (id.includes('node_modules')) {
455
+ return 'vendor'
456
+ }
457
+ }
458
+ }
459
+ }
460
+ // Create a SSR bundle
461
+ noExternal = [
462
+ new RegExp(`^(?!(${[...builtinModules, ...serverModules].join('|')}))`)
463
+ // new RegExp(`^(?!.*(${[...builtinModules, ...serverModules].join('|')}))`)
464
+ ]
465
+ } else if (ssr === 'fastify') {
466
+ rollupOptions = {
467
+ input: [new URL('server.ts', fastifyDir).pathname],
468
+ external,
469
+ output: {
470
+ minifyInternalExports: false,
471
+ entryFileNames: '[name].mjs',
472
+ chunkFileNames: '[name].mjs',
473
+ // format: 'es',
474
+ manualChunks: (id) => {
475
+ if (id.includes('vitrify/src/vite/')) {
476
+ const name = id.split('/').at(-1)?.split('.').at(0)
477
+ if (name && manualChunks.includes(name)) return name
478
+ } else if (id.includes('node_modules')) {
479
+ return 'vendor'
480
+ }
481
+ }
482
+ }
483
+ }
484
+ // Create a SSR bundle
485
+ noExternal = [
486
+ new RegExp(`^(?!(${[...builtinModules, ...serverModules].join('|')}))`)
487
+ ]
488
+ } else {
489
+ rollupOptions = {
490
+ // input: [new URL('index.html', frameworkDir).pathname],
491
+ // output: {
492
+ // format: 'es'
493
+ // }
494
+ }
495
+ }
496
+
290
497
  const config = {
291
- root: frameworkDir.pathname,
498
+ root: ssr === 'fastify' ? appDir.pathname : frameworkDir.pathname,
292
499
  publicDir: publicDir.pathname,
500
+ envDir: appDir.pathname,
293
501
  vitrify: {
294
502
  productName,
295
503
  urls: {
@@ -302,7 +510,7 @@ export const baseConfig = async ({
302
510
  },
303
511
  plugins,
304
512
  optimizeDeps: {
305
- exclude: ['vue']
513
+ exclude: ['vue', ...serverModules, ...builtinModules]
306
514
  },
307
515
  resolve: {
308
516
  // Dedupe uses require which breaks ESM SSR builds
@@ -313,46 +521,42 @@ export const baseConfig = async ({
313
521
  alias
314
522
  },
315
523
  build: {
316
- target: ssr === 'server' ? 'esnext' : 'modules',
317
- ssr: ssr === 'server' ? true : false,
524
+ target: ssr === 'server' || ssr === 'fastify' ? 'esnext' : 'modules',
525
+ ssr: ssr === 'server' || ssr === 'fastify' ? true : false,
318
526
  ssrManifest: ssr === 'client' || ssr === 'ssg',
319
- rollupOptions:
320
- ssr === 'server'
321
- ? {
322
- input: [
323
- new URL('ssr/entry-server.ts', frameworkDir).pathname,
324
- new URL('ssr/prerender.ts', frameworkDir).pathname,
325
- new URL('ssr/server.ts', frameworkDir).pathname
326
- ],
327
- output: {
328
- minifyInternalExports: false,
329
- entryFileNames: '[name].mjs',
330
- chunkFileNames: '[name].mjs',
331
- format: 'es',
332
- manualChunks: (id) => {
333
- if (id.includes('vitrify/src/vite/')) {
334
- const name = id.split('/').at(-1)?.split('.').at(0)
335
- console.log(name)
336
- if (manualChunks.includes(id)) return name
337
- } else if (id.includes('node_modules')) {
338
- return 'vendor'
339
- }
340
- }
341
- }
342
- }
343
- : {
344
- output: {
345
- format: 'es'
346
- }
347
- }
527
+ rollupOptions
528
+ // ssr === 'server'
529
+ // ? {
530
+ // input: [
531
+ // new URL('ssr/entry-server.ts', frameworkDir).pathname,
532
+ // new URL('ssr/prerender.ts', frameworkDir).pathname,
533
+ // new URL('ssr/server.ts', frameworkDir).pathname
534
+ // ],
535
+ // output: {
536
+ // minifyInternalExports: false,
537
+ // entryFileNames: '[name].mjs',
538
+ // chunkFileNames: '[name].mjs',
539
+ // format: 'es',
540
+ // manualChunks: (id) => {
541
+ // if (id.includes('vitrify/src/vite/')) {
542
+ // const name = id.split('/').at(-1)?.split('.').at(0)
543
+ // if (name && manualChunks.includes(name)) return name
544
+ // } else if (id.includes('node_modules')) {
545
+ // return 'vendor'
546
+ // }
547
+ // }
548
+ // }
549
+ // }
550
+ // : {
551
+ // output: {
552
+ // format: 'es'
553
+ // }
554
+ // }
348
555
  },
349
556
  ssr: {
350
557
  // Create a SSR bundle
351
- noExternal: [
352
- new RegExp(
353
- `^(?!.*(${[...builtinModules, ...serverModules].join('|')}))`
354
- )
355
- ]
558
+ external,
559
+ noExternal
356
560
  },
357
561
  define: {
358
562
  __BASE_URL__: `'/'`
@@ -5,7 +5,7 @@ export type VitrifyPlugin = ({
5
5
  mode,
6
6
  command
7
7
  }: {
8
- ssr?: 'server' | 'client' | 'ssg' | false
8
+ ssr?: 'server' | 'client' | 'ssg' | 'fastify' | false
9
9
  pwa?: boolean
10
10
  mode?: 'production' | 'development'
11
11
  command?: 'build' | 'dev' | 'test'
@@ -121,7 +121,7 @@ export const QuasarPlugin: VitrifyPlugin = async ({
121
121
  const directives = await import('quasar/src/directives.js')
122
122
 
123
123
  app.use(
124
- staticImports.Quasar,
124
+ staticImports?.Quasar,
125
125
  {
126
126
  plugins: quasarPlugins,
127
127
  directives
@@ -221,9 +221,14 @@ export const QuasarPlugin: VitrifyPlugin = async ({
221
221
  replacement: new URL('src/', urls?.packages?.quasar).pathname
222
222
  },
223
223
  {
224
- find: 'quasar',
225
- replacement: new URL('src/', urls?.packages?.quasar).pathname
224
+ find: new RegExp('^quasar$'),
225
+ replacement: new URL('src/index.all.js', urls?.packages?.quasar)
226
+ .pathname
226
227
  },
228
+ // {
229
+ // find: 'quasar',
230
+ // replacement: new URL('src/index.all.js', urls?.packages?.quasar).pathname
231
+ // },
227
232
  {
228
233
  find: `@quasar/extras`,
229
234
  replacement: new URL('.', urls?.packages?.['@quasar/extras'])
@@ -251,7 +256,7 @@ export const QuasarPlugin: VitrifyPlugin = async ({
251
256
  config: async (config, env) => ({
252
257
  resolve: {
253
258
  alias: [
254
- { find: new RegExp('^quasar$'), replacement: 'virtual:quasar' }
259
+ // { find: new RegExp('^quasar$'), replacement: 'virtual:quasar' }
255
260
  ]
256
261
  }
257
262
  }),
@@ -19,7 +19,7 @@ export type OnBootHook = ({
19
19
  }: {
20
20
  app: any
21
21
  ssrContext: Record<string, unknown>
22
- staticImports: Record<string, any>
22
+ staticImports?: Record<string, any>
23
23
  }) => Promise<void> | void
24
24
  export type OnMountedHook = (
25
25
  instance: ComponentInternalInstance
@@ -33,8 +33,11 @@ export type OnRenderedHook = (
33
33
  html: string,
34
34
  ssrContext: Record<string, any>
35
35
  ) => string
36
- export type OnSetupHook = (fastify: FastifyInstance) => any
37
-
36
+ // export type OnSetupHook = (
37
+ // fastify: FastifyInstance,
38
+ // staticImports?: Record<string, any>
39
+ // ) => any
40
+ export type OnSetupFile = URL
38
41
  export interface VitrifyConfig extends UserConfig {
39
42
  vitrify?: {
40
43
  /**
@@ -49,7 +52,7 @@ export interface VitrifyConfig extends UserConfig {
49
52
  /**
50
53
  * setup() is called directly after instantiating fastify. Use it to register your own plugins, routes etc.
51
54
  */
52
- onSetup?: OnSetupHook[]
55
+ onSetup?: OnSetupFile[]
53
56
  /**
54
57
  * Functions which run in the onMounted hook of the app
55
58
  */
@@ -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
+ }
@@ -0,0 +1,10 @@
1
+ import Fastify from 'fastify'
2
+ import { setup } from './entry'
3
+
4
+ const fastify = Fastify()
5
+ await setup({ fastify })
6
+
7
+ fastify.listen({
8
+ port: Number(process.env.PORT || 3000),
9
+ host: process.env.HOST || '127.0.0.1'
10
+ })
@@ -10,6 +10,7 @@
10
10
  <body>
11
11
  <!-- Do not add whitespace or newlines to #app, this will cause hydration errors -->
12
12
  <div id="app"><!--app-html--></div>
13
+ <!--dev-ssr-css-->
13
14
  <!--entry-script-->
14
15
  <!--initial-state-->
15
16
  </body>
@@ -9,7 +9,6 @@ import {
9
9
  } from 'vue'
10
10
  import { onBoot, onMounted } from 'virtual:vitrify-hooks'
11
11
  import routes from 'src/router/routes'
12
- import 'virtual:global-css'
13
12
  import * as staticImports from 'virtual:static-imports'
14
13
 
15
14
  interface ssrContext {
@@ -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 '@vue/server-renderer'
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, {