vitrify 0.4.0 → 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.
@@ -2,19 +2,20 @@
2
2
  import type { LogLevel, InlineConfig } from 'vite';
3
3
  import { ViteDevServer } from 'vite';
4
4
  import type { Server } from 'net';
5
- export declare function createVitrifyDevServer({ port, logLevel, mode, framework, host, appDir, publicDir }: {
5
+ export declare function createVitrifyDevServer({ port, logLevel, ssr, framework, host, appDir, publicDir, base }: {
6
6
  port?: number;
7
7
  logLevel?: LogLevel;
8
- mode?: 'csr' | 'ssr' | 'fastify';
8
+ ssr?: 'ssr' | 'fastify';
9
9
  framework?: 'vue';
10
10
  host?: string;
11
11
  appDir?: URL;
12
12
  publicDir?: URL;
13
+ base?: string;
13
14
  }): Promise<ViteDevServer>;
14
- export declare function createServer({ port, logLevel, mode, framework, host, appDir, publicDir }: {
15
+ export declare function createServer({ port, logLevel, ssr, framework, host, appDir, publicDir }: {
15
16
  port?: number;
16
17
  logLevel?: LogLevel;
17
- mode?: 'csr' | 'ssr' | 'fastify';
18
+ ssr?: 'ssr' | 'fastify';
18
19
  framework?: 'vue';
19
20
  host?: string;
20
21
  appDir?: URL;
@@ -41,6 +42,7 @@ export declare function createServer({ port, logLevel, mode, framework, host, ap
41
42
  server: import("vite").ResolvedServerOptions;
42
43
  build: Required<import("vite").BuildOptions>;
43
44
  preview: import("vite").ResolvedPreviewOptions;
45
+ ssr: import("vite").ResolvedSSROptions | undefined;
44
46
  assetsInclude: (file: string) => boolean;
45
47
  logger: import("vite").Logger;
46
48
  createResolver: (options?: Partial<import("vite").InternalResolveOptions> | undefined) => import("vite").ResolveFn;
@@ -0,0 +1,17 @@
1
+ import type { FastifyPluginCallback, FastifyRequest, FastifyReply } from 'fastify';
2
+ import type { OnRenderedHook } from '../../vitrify-config.js';
3
+ import type { ViteDevServer } from 'vite';
4
+ export interface FastifySsrOptions {
5
+ baseUrl?: string;
6
+ provide?: (req: FastifyRequest, res: FastifyReply) => Promise<Record<string, unknown>>;
7
+ vitrifyDir?: URL;
8
+ vite?: ViteDevServer;
9
+ appDir?: URL;
10
+ publicDir?: URL;
11
+ productName?: string;
12
+ onRendered?: OnRenderedHook[];
13
+ mode?: string;
14
+ }
15
+ declare const fastifyCsrPlugin: FastifyPluginCallback<FastifySsrOptions>;
16
+ export { fastifyCsrPlugin };
17
+ export declare type FastifyCsrPlugin = typeof fastifyCsrPlugin;
@@ -1,13 +1,14 @@
1
1
  /// <reference types="node" />
2
2
  import type { FastifyInstance } from 'fastify';
3
3
  import type { OnRenderedHook, OnSetupFile } from '../../vitrify-config.js';
4
+ import type { FastifyCsrPlugin } from './fastify-csr-plugin.js';
4
5
  import type { FastifySsrPlugin } from './fastify-ssr-plugin.js';
5
- export declare const createApp: ({ onSetup, appDir, baseUrl, onRendered, fastifySsrPlugin, vitrifyDir, mode }: {
6
+ export declare const createApp: ({ onSetup, appDir, baseUrl, onRendered, fastifyPlugin, vitrifyDir, mode }: {
6
7
  onSetup: OnSetupFile[];
7
8
  appDir: URL;
8
9
  baseUrl?: string | undefined;
9
10
  onRendered?: OnRenderedHook[] | undefined;
10
- fastifySsrPlugin: FastifySsrPlugin;
11
+ fastifyPlugin: FastifySsrPlugin | FastifyCsrPlugin;
11
12
  vitrifyDir?: URL | undefined;
12
13
  mode: string;
13
14
  }) => FastifyInstance<import("http").Server, import("http").IncomingMessage, import("http").ServerResponse, pino.Logger, import("fastify").FastifyTypeProviderDefault> & PromiseLike<FastifyInstance<import("http").Server, import("http").IncomingMessage, import("http").ServerResponse, pino.Logger, import("fastify").FastifyTypeProviderDefault>>;
@@ -3,7 +3,11 @@ import type { ViteDevServer, ModuleNode } from 'vite';
3
3
  * Collect SSR CSS for Vite
4
4
  */
5
5
  export declare const componentsModules: (components: string[], vite: ViteDevServer) => Set<ModuleNode>;
6
- export declare const collectCss: (mods: Set<ModuleNode>, styles?: Map<string, string>, checkedComponents?: Set<unknown>) => string;
6
+ export declare const collectCss: ({ mods, styles, checkedComponents }: {
7
+ mods: Set<ModuleNode>;
8
+ styles?: Map<string, string> | undefined;
9
+ checkedComponents?: Set<unknown> | undefined;
10
+ }) => string;
7
11
  /**
8
12
  * Client listener to detect updated modules through HMR, and remove the initial styled attached to the head
9
13
  */
@@ -3,13 +3,15 @@ import type { BootFunction, VitrifyConfig } from './vitrify-config.js';
3
3
  import type { VitrifyContext } from './bin/run.js';
4
4
  import type { VitrifyPlugin } from './plugins/index.js';
5
5
  export declare const VIRTUAL_MODULES: string[];
6
- export declare const baseConfig: ({ ssr, appDir, publicDir, command, mode, framework, pwa }: {
6
+ export declare const baseConfig: ({ ssr, appDir, publicDir, base, command, mode, framework, pwa }: {
7
7
  ssr?: "server" | "client" | "ssg" | "fastify" | undefined;
8
8
  appDir?: URL | undefined;
9
9
  publicDir?: URL | undefined;
10
+ base?: string | undefined;
10
11
  command?: "build" | "dev" | "test" | undefined;
11
12
  mode?: "production" | "development" | undefined;
12
13
  framework?: "vue" | undefined;
13
14
  pwa?: boolean | undefined;
14
15
  }) => Promise<InlineConfig>;
16
+ export declare const vitrifyDir: URL;
15
17
  export type { VitrifyConfig, VitrifyPlugin, VitrifyContext, BootFunction };
@@ -1,4 +1,4 @@
1
- import type { UserConfig } from 'vite';
1
+ import type { Alias, UserConfig } from 'vite';
2
2
  import type { QuasarConf } from './plugins/quasar.js';
3
3
  import type { ComponentInternalInstance } from '@vue/runtime-core';
4
4
  export declare type BootFunction = ({ app, ssrContext, staticImports }: {
@@ -65,6 +65,18 @@ export interface VitrifyConfig extends UserConfig {
65
65
  cwd?: URL;
66
66
  packages?: Record<string, URL>;
67
67
  };
68
+ /**
69
+ * SSR specific configuration
70
+ */
71
+ ssr?: {
72
+ serverModules?: string[];
73
+ };
74
+ /**
75
+ * Development only configuration
76
+ */
77
+ dev?: {
78
+ alias?: Alias[];
79
+ };
68
80
  };
69
81
  quasar?: QuasarConf;
70
82
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vitrify",
3
- "version": "0.4.0",
3
+ "version": "0.5.0",
4
4
  "license": "MIT",
5
5
  "author": "Stefan van Herwijnen",
6
6
  "description": "Pre-configured Vite CLI for your framework",
@@ -71,7 +71,7 @@
71
71
  "sass": "1.52.3",
72
72
  "ts-node": "^10.8.1",
73
73
  "unplugin-vue-components": "^0.19.6",
74
- "vite": "^3.0.0-alpha.9",
74
+ "vite": "^3.0.0-alpha.11",
75
75
  "vitest": "^0.14.1"
76
76
  },
77
77
  "devDependencies": {
@@ -81,11 +81,11 @@
81
81
  "@types/node": "^17.0.41",
82
82
  "@types/ws": "^8.5.3",
83
83
  "@vue/runtime-core": "^3.2.37",
84
- "import-meta-resolve": "^2.0.2",
84
+ "import-meta-resolve": "^2.0.3",
85
85
  "quasar": "^2.7.1",
86
86
  "rollup": "^2.75.6",
87
87
  "typescript": "^4.7.3",
88
- "vite": "^3.0.0-alpha.9",
88
+ "vite": "^3.0.0-alpha.11",
89
89
  "vue": "^3.2.37",
90
90
  "vue-router": "^4.0.15"
91
91
  },
@@ -95,7 +95,8 @@
95
95
  "fastify-plugin": "^3.0.1",
96
96
  "quasar": "^2.7.1",
97
97
  "vue": "^3.2.37",
98
- "vue-router": "^4.0.15"
98
+ "vue-router": "^4.0.15",
99
+ "import-meta-resolve": "^2.0.3"
99
100
  },
100
101
  "publishConfig": {
101
102
  "access": "public",
@@ -14,7 +14,8 @@ export async function build(opts: {
14
14
  mode: 'production',
15
15
  ssr: opts?.ssr,
16
16
  appDir: opts.appDir,
17
- publicDir: opts.publicDir
17
+ publicDir: opts.publicDir,
18
+ base: opts.base
18
19
  })
19
20
 
20
21
  config.build = {
@@ -24,16 +25,16 @@ export async function build(opts: {
24
25
  emptyOutDir: !!opts.outDir
25
26
  }
26
27
 
27
- if (opts.base) {
28
- config.define = {
29
- ...config.define,
30
- __BASE_URL__: `'${opts.base}'`
31
- }
32
- }
28
+ // if (opts.base) {
29
+ // config.define = {
30
+ // ...config.define,
31
+ // __BASE_URL__: `'${opts.base}'`
32
+ // }
33
+ // }
33
34
 
34
35
  return viteBuild({
35
36
  configFile: false,
36
- base: opts.base,
37
+ // base: opts.base,
37
38
  // logLevel: 'silent',
38
39
  ...config
39
40
  })
@@ -48,7 +48,7 @@ cli
48
48
  case 'csr':
49
49
  await build({
50
50
  ...args,
51
- outDir: new URL('spa/', baseOutDir).pathname
51
+ outDir: new URL('csr/', baseOutDir).pathname
52
52
  })
53
53
  break
54
54
  case 'fastify':
@@ -128,7 +128,7 @@ cli
128
128
  switch (options.mode) {
129
129
  case 'ssr':
130
130
  ;({ server, config } = await createServer({
131
- mode: 'ssr',
131
+ ssr: 'ssr',
132
132
  host: options.host,
133
133
  appDir: parsePath(options.appDir, cwd),
134
134
  publicDir: parsePath(options.publicDir, cwd)
@@ -136,7 +136,7 @@ cli
136
136
  break
137
137
  case 'fastify':
138
138
  ;({ server, config } = await createServer({
139
- mode: 'fastify',
139
+ ssr: 'fastify',
140
140
  host: options.host,
141
141
  appDir: parsePath(options.appDir, cwd),
142
142
  publicDir: parsePath(options.publicDir, cwd)
@@ -6,23 +6,28 @@ import type { Server } from 'net'
6
6
  import type { FastifyInstance } from 'fastify/types/instance'
7
7
  import fastify from 'fastify'
8
8
  import { fastifySsrPlugin } from '../frameworks/vue/fastify-ssr-plugin.js'
9
+ import type { ServerOptions } from 'https'
9
10
 
10
11
  export async function createVitrifyDevServer({
11
12
  port = 3000,
12
13
  logLevel = 'info',
13
- mode = 'csr',
14
+ // mode = 'csr',
15
+ ssr,
14
16
  framework = 'vue',
15
17
  host,
16
18
  appDir,
17
- publicDir
19
+ publicDir,
20
+ base
18
21
  }: {
19
22
  port?: number
20
23
  logLevel?: LogLevel
21
- mode?: 'csr' | 'ssr' | 'fastify'
24
+ // mode?: 'csr' | 'ssr' | 'fastify'
25
+ ssr?: 'ssr' | 'fastify'
22
26
  framework?: 'vue'
23
27
  host?: string
24
28
  appDir?: URL
25
29
  publicDir?: URL
30
+ base?: string
26
31
  }) {
27
32
  const { getAppDir, getCliDir, getCliViteDir, getCwd } = await import(
28
33
  '../app-urls.js'
@@ -33,31 +38,36 @@ export async function createVitrifyDevServer({
33
38
  if (!appDir) appDir = getAppDir()
34
39
  let config: InlineConfig = {}
35
40
  let ssrMode: 'server' | 'fastify' | undefined
36
- if (mode === 'ssr') ssrMode = 'server'
37
- if (mode === 'fastify') ssrMode = 'fastify'
41
+ if (ssr === 'ssr') ssrMode = 'server'
42
+ if (ssr === 'fastify') ssrMode = 'fastify'
38
43
  config = await baseConfig({
39
44
  framework,
40
45
  ssr: ssrMode,
41
46
  command: 'dev',
42
47
  mode: 'development',
43
48
  appDir,
44
- publicDir
49
+ publicDir,
50
+ base
45
51
  })
46
52
 
47
53
  config.logLevel = logLevel
54
+
55
+ console.log(searchForWorkspaceRoot(appDir.pathname))
48
56
  config.server = {
49
57
  https: config.server?.https,
58
+ hmr: {
59
+ protocol: config.server?.https ? 'wss' : 'ws'
60
+ },
50
61
  port,
51
62
  // middlewareMode: mode === 'ssr' ? 'ssr' : undefined,
52
- middlewareMode: mode !== 'csr' ? 'ssr' : false,
63
+ middlewareMode: ssr ? 'ssr' : false,
53
64
  fs: {
65
+ strict: false, // https://github.com/vitejs/vite/issues/8175
54
66
  allow: [
55
67
  searchForWorkspaceRoot(process.cwd()),
56
- ...(Array.isArray(appDir)
57
- ? appDir.map((dir) => searchForWorkspaceRoot(dir.pathname))
58
- : [searchForWorkspaceRoot(appDir.pathname)]),
59
- searchForWorkspaceRoot(cliDir.pathname)
60
- // appDir.pathname,
68
+ searchForWorkspaceRoot(appDir.pathname),
69
+ searchForWorkspaceRoot(cliDir.pathname),
70
+ appDir.pathname
61
71
  ]
62
72
  },
63
73
  watch: {
@@ -81,7 +91,8 @@ export async function createVitrifyDevServer({
81
91
  export async function createServer({
82
92
  port = 3000,
83
93
  logLevel = 'info',
84
- mode = 'csr',
94
+ // mode = 'csr',
95
+ ssr,
85
96
  framework = 'vue',
86
97
  host,
87
98
  appDir,
@@ -89,7 +100,8 @@ export async function createServer({
89
100
  }: {
90
101
  port?: number
91
102
  logLevel?: LogLevel
92
- mode?: 'csr' | 'ssr' | 'fastify'
103
+ // mode?: 'csr' | 'ssr' | 'fastify'
104
+ ssr?: 'ssr' | 'fastify'
93
105
  framework?: 'vue'
94
106
  host?: string
95
107
  appDir?: URL
@@ -99,47 +111,47 @@ export async function createServer({
99
111
  '../app-urls.js'
100
112
  )
101
113
 
114
+ appDir = appDir || getAppDir()
102
115
  const cliDir = getCliDir()
103
116
 
104
117
  const vite = await createVitrifyDevServer({
105
118
  port,
106
119
  logLevel,
107
- mode,
120
+ ssr,
108
121
  framework,
109
122
  host,
110
123
  appDir,
111
124
  publicDir
112
125
  })
113
- let entryUrl: string
114
126
 
115
127
  let setup
116
128
  let server: Server
117
129
 
118
- console.log(`Development mode: ${mode}`)
119
- if (['ssr', 'fastify'].includes(mode)) {
130
+ console.log(`Development mode: ${ssr ? ssr : 'csr'}`)
131
+ if (ssr) {
120
132
  const entryUrl =
121
- mode === 'fastify'
133
+ ssr === 'fastify'
122
134
  ? new URL('src/vite/fastify/entry.ts', cliDir).pathname
123
135
  : new URL(`src/vite/${framework}/ssr/entry-server.ts`, cliDir).pathname
124
136
 
125
137
  ;({ setup } = await vite.ssrLoadModule(entryUrl))
126
138
 
127
139
  const app = fastify({
128
- https:
129
- typeof vite.config.server.https === 'object'
130
- ? vite.config.server.https
131
- : {}
140
+ logger: true,
141
+ https: vite.config.server.https as ServerOptions
132
142
  })
143
+ if (process.env) process.env.MODE = 'development'
133
144
  if (setup) {
134
145
  await setup({
135
146
  fastify: app
136
147
  })
137
148
  }
138
- // await app.register(fastifySsrPlugin, {
139
- // appDir,
140
- // vitrifyDir: new URL('../..', import.meta.url),
141
- // mode: 'development'
142
- // })
149
+ if (ssr === 'ssr') {
150
+ await app.register(fastifySsrPlugin, {
151
+ appDir,
152
+ mode: 'development'
153
+ })
154
+ }
143
155
  await app.listen({
144
156
  port: Number(port || 3000),
145
157
  host
@@ -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
@@ -8,6 +8,7 @@ import { readFileSync } from 'fs'
8
8
  import type { OnRenderedHook } from '../../vitrify-config.js'
9
9
  import { componentsModules, collectCss } from '../../helpers/collect-css-ssr.js'
10
10
  import type { ViteDevServer } from 'vite'
11
+
11
12
  export interface FastifySsrOptions {
12
13
  baseUrl?: string
13
14
  provide?: (
@@ -30,72 +31,86 @@ const fastifySsrPlugin: FastifyPluginCallback<FastifySsrOptions> = async (
30
31
  done
31
32
  ) => {
32
33
  options.vitrifyDir =
33
- options.vitrifyDir || new URL('../../..', import.meta.url)
34
- const frameworkDir = new URL('vite/vue/', options.vitrifyDir)
34
+ options.vitrifyDir || (await import('vitrify')).vitrifyDir
35
+ const frameworkDir = new URL('src/vite/vue/', options.vitrifyDir)
35
36
  options.baseUrl = options.baseUrl || '/'
37
+ options.mode = options.mode || process.env.MODE || import.meta.env.MODE
38
+ options.appDir = options.appDir || new URL('../../..', import.meta.url)
39
+
36
40
  if (
37
41
  options.baseUrl.charAt(options.baseUrl.length - 1) !== '/' ||
38
42
  options.baseUrl.charAt(0) !== '/'
39
43
  )
40
44
  throw new Error('baseUrl should start and end with a /')
41
45
  if (options.mode === 'development') {
42
- if (!options.vitrifyDir)
43
- throw new Error('Option vitrifyDir cannot be undefined')
46
+ // if (!options.vitrifyDir)
47
+ // throw new Error('Option vitrifyDir cannot be undefined')
44
48
  // if (!options.vite) throw new Error('Option vite cannot be undefined')
45
49
  // const { resolve } = await import('import-meta-resolve')
46
50
  // const cliDir = new URL('../', await resolve('vitrify', import.meta.url))
47
51
  options.appDir = options.appDir || new URL('../../..', import.meta.url)
48
52
 
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',
53
+ const { createVitrifyDevServer } = await import('vitrify/dev')
54
+ const vite = await createVitrifyDevServer({
56
55
  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
56
+ ssr: 'ssr',
57
+ framework: 'vue',
58
+ base: options.baseUrl
80
59
  })
60
+ // const { createServer, searchForWorkspaceRoot } = await import('vite')
61
+ // const { baseConfig } = await import('vitrify')
62
+ // const cliDir = options.vitrifyDir
63
+ // const config = await baseConfig({
64
+ // ssr: 'server',
65
+ // command: 'dev',
66
+ // mode: 'development',
67
+ // appDir: options.appDir,
68
+ // publicDir: options.publicDir || new URL('public', options.appDir)
69
+ // })
70
+
71
+ // config.server = {
72
+ // middlewareMode: true,
73
+ // fs: {
74
+ // allow: [
75
+ // searchForWorkspaceRoot(process.cwd()),
76
+ // searchForWorkspaceRoot(options.appDir.pathname),
77
+ // searchForWorkspaceRoot(cliDir.pathname)
78
+ // // appDir.pathname,
79
+ // ]
80
+ // },
81
+ // watch: {
82
+ // // During tests we edit the files too fast and sometimes chokidar
83
+ // // misses change events, so enforce polling for consistency
84
+ // usePolling: true,
85
+ // interval: 100
86
+ // }
87
+ // }
88
+ // const vite = await createServer({
89
+ // configFile: false,
90
+ // ...config
91
+ // })
81
92
 
82
93
  console.log('Dev mode')
83
- const middie = (await import('@fastify/middie')).default
84
- await fastify.register(middie)
94
+ if (!('use' in fastify)) {
95
+ const middie = (await import('@fastify/middie')).default
96
+ await fastify.register(middie)
97
+ }
85
98
  fastify.use(vite.middlewares)
86
99
 
87
100
  fastify.get(`${options.baseUrl}*`, async (req, res) => {
88
101
  try {
89
- const url = req.raw.url
102
+ const url = req.raw.url?.replace(options.baseUrl!, '/')
90
103
  const ssrContext = {
91
104
  req,
92
105
  res
93
106
  }
94
107
 
95
- const template = readFileSync(
108
+ let template = readFileSync(
96
109
  new URL('index.html', frameworkDir)
97
110
  ).toString()
98
111
 
112
+ template = await vite.transformIndexHtml(url!, template)
113
+
99
114
  const entryUrl = new URL('ssr/entry-server.ts', frameworkDir).pathname
100
115
  const render = (await vite!.ssrLoadModule(entryUrl)).render
101
116
  let manifest
@@ -111,7 +126,9 @@ const fastifySsrPlugin: FastifyPluginCallback<FastifySsrOptions> = async (
111
126
  // if (options.vite?.config.vitrify!.globalCss)
112
127
  // cssModules.push(...options.vite?.config.vitrify.globalCss)
113
128
  const matchedModules = componentsModules(cssModules, vite!)
114
- const css = collectCss(matchedModules)
129
+ const css = collectCss({
130
+ mods: matchedModules
131
+ })
115
132
 
116
133
  const [appHtml, preloadLinks] = await render(url, manifest, ssrContext)
117
134
  const html = template
@@ -3,6 +3,7 @@ import fastify from 'fastify'
3
3
  import type { ViteDevServer } from 'vite'
4
4
  import { getCliDir, getCliViteDir } from '../../app-urls.js'
5
5
  import type { OnRenderedHook, OnSetupFile } from '../../vitrify-config.js'
6
+ import type { FastifyCsrPlugin } from './fastify-csr-plugin.js'
6
7
  import type { FastifySsrPlugin } from './fastify-ssr-plugin.js'
7
8
 
8
9
  export const createApp = ({
@@ -10,7 +11,7 @@ export const createApp = ({
10
11
  appDir,
11
12
  baseUrl,
12
13
  onRendered,
13
- fastifySsrPlugin,
14
+ fastifyPlugin,
14
15
  vitrifyDir,
15
16
  mode
16
17
  }: {
@@ -18,7 +19,7 @@ export const createApp = ({
18
19
  appDir: URL
19
20
  baseUrl?: string
20
21
  onRendered?: OnRenderedHook[]
21
- fastifySsrPlugin: FastifySsrPlugin
22
+ fastifyPlugin: FastifySsrPlugin | FastifyCsrPlugin
22
23
  vitrifyDir?: URL
23
24
  mode: string
24
25
  }) => {
@@ -26,7 +27,7 @@ export const createApp = ({
26
27
  logger: true
27
28
  })
28
29
 
29
- app.register(fastifySsrPlugin, {
30
+ app.register(fastifyPlugin, {
30
31
  baseUrl,
31
32
  appDir,
32
33
  onRendered,
@@ -16,11 +16,15 @@ export const componentsModules = (
16
16
  return matchedModules
17
17
  }
18
18
 
19
- export const collectCss = (
20
- mods: Set<ModuleNode>,
19
+ export const collectCss = ({
20
+ mods,
21
21
  styles = new Map<string, string>(),
22
22
  checkedComponents = new Set()
23
- ) => {
23
+ }: {
24
+ mods: Set<ModuleNode>
25
+ styles?: Map<string, string>
26
+ checkedComponents?: Set<unknown>
27
+ }) => {
24
28
  for (const mod of mods) {
25
29
  if (
26
30
  (mod.file?.endsWith('.scss') ||
@@ -32,7 +36,11 @@ export const collectCss = (
32
36
  }
33
37
  if (mod.importedModules.size > 0 && !checkedComponents.has(mod.id)) {
34
38
  checkedComponents.add(mod.id)
35
- collectCss(mod.importedModules, styles, checkedComponents)
39
+ collectCss({
40
+ mods: mod.importedModules,
41
+ styles,
42
+ checkedComponents
43
+ })
36
44
  }
37
45
  }
38
46
  let result = ''