vitrify 0.9.1 → 0.9.2

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 CHANGED
@@ -1,9 +1,10 @@
1
1
  # Vitrify
2
2
 
3
- > Pre-configured Vite CLI for your framework
3
+ > Vite as your Full Stack development tool
4
4
 
5
5
  - Use a simple configuration file to configure and integrate required Vite plugins into your project.
6
- - Client-Side Rendering (CSR), Server-Side Rendering (SSR) and Fastify server build and development modes.
6
+ - Client-Side Rendering (CSR), Server-Side Rendering (SSR), Static Site Generation (SSG) and Fastify server build and development modes.
7
+ - Run both your frontend- and backend code through Vite at development and build time.
7
8
 
8
9
  ## Features
9
10
 
@@ -73,14 +74,24 @@ Options:
73
74
  node/bin/cli.ts-->node/bin/dev.ts;
74
75
  node/bin/cli.ts-->node/bin/test.ts;
75
76
  node/bin/cli.ts-->node/bin/run.ts;
76
- node/bin/build.ts-->node/index.ts{Load baseConfig};
77
- node/bin/dev.ts-->node/index.ts{Load baseConfig};
78
- node/index.ts-->vitrify.config.js{Load vitrify.config.js};
77
+ node/bin/build.ts-->node/index.ts
78
+ node/bin/dev.ts-->node/index.ts
79
+ node/index.ts{Load baseConfig}-->vitrify.config.js{Load vitrify.config.js};
80
+ subgraph baseconfig
79
81
  vitrify.config.js-->node/plugins{Load plugins};
80
- node/plugins-->framework{Load framework entrypoints from vite/...};
81
- framework-->merge{Merge vitrify.config.js with Vitrify configuration};
82
- merge-->build{Build the application};
83
- merge-->dev{Spin up dev server};
82
+ node/plugins-->merge{Merge vitrify.config.js with Vitrify configuration};
83
+ end
84
+ merge-- mode: fastify -->frameworkFastify{Load framework entrypoints from fastify/...};
85
+ merge-- mode: csr/ssr/ssg -->framework{Load framework entrypoints from vite/...};
86
+ frameworkFastify-->fastifyBuild{Build the application};
87
+ frameworkFastify-->fastifyDev{Start Fastify dev server};
88
+ fastifySetup{onSetup / onRendered}-->fastifyDev
89
+ fastifySetup{onSetup / onRendered}-->fastifyBuild
90
+ framework-->build{Build the application};
91
+ build-- mode: ssg -->prerender{Run prerender.js}
92
+ framework-->dev{Start Vite dev server};
93
+ frameworkSetup{onBoot / onMounted}-->dev
94
+ frameworkSetup{onBoot / onMounted}-->build
84
95
  node/bin/test.ts-->test{Run a pre-configured Vitest instance};
85
96
  node/bin/run.ts-->run{Inject context into script and run script};
86
97
  ```
package/dist/bin/cli.js CHANGED
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env node
2
2
  import cac from 'cac';
3
3
  import { getAppDir, parsePath } from '../app-urls.js';
4
- import { printHttpServerUrls } from '../helpers/logger.js';
4
+ import { printHttpServerUrls, exitLogs } from '../helpers/logger.js';
5
5
  const cli = cac('vitrify');
6
6
  cli
7
7
  .command('build')
@@ -135,6 +135,9 @@ cli
135
135
  }
136
136
  console.log('Dev server running at:');
137
137
  printHttpServerUrls(server, config);
138
+ for (const line of exitLogs) {
139
+ config.logger.warn(line);
140
+ }
138
141
  });
139
142
  cli.command('test').action(async (options) => {
140
143
  const { test } = await import('./test.js');
package/dist/bin/dev.js CHANGED
@@ -3,6 +3,7 @@ import { baseConfig } from '../index.js';
3
3
  import fastify from 'fastify';
4
4
  import { fastifySsrPlugin } from '../frameworks/vue/fastify-ssr-plugin.js';
5
5
  import isPortReachable from 'is-port-reachable';
6
+ import { exitLogs } from '../helpers/logger.js';
6
7
  const getFirstOpenPort = async (portNumber) => {
7
8
  if (!(await isPortReachable(portNumber, { host: 'localhost' }))) {
8
9
  return portNumber;
@@ -32,11 +33,17 @@ ssr, framework = 'vue', host, appDir, publicDir, base }) {
32
33
  base
33
34
  });
34
35
  config.logLevel = logLevel;
36
+ config.define = {
37
+ ...config.define,
38
+ __HOST__: `'${host}'`
39
+ };
40
+ const wsPort = await getFirstOpenPort(24678);
41
+ exitLogs.push(`[warning] HTTPS mode enabled. Visit https://{hostname}:${wsPort} to enable a security exception for HMR.`);
35
42
  config.server = {
36
43
  https: config.server?.https,
37
44
  hmr: {
38
45
  protocol: config.server?.https ? 'wss' : 'ws',
39
- port: await getFirstOpenPort(24678)
46
+ port: wsPort
40
47
  },
41
48
  port,
42
49
  // middlewareMode: mode === 'ssr' ? 'ssr' : undefined,
@@ -107,7 +114,8 @@ ssr, framework = 'vue', host, appDir, publicDir }) {
107
114
  await app.register(fastifySsrPlugin, {
108
115
  appDir,
109
116
  mode: 'development',
110
- onRendered
117
+ onRendered,
118
+ host
111
119
  });
112
120
  }
113
121
  await app.listen({
@@ -23,7 +23,8 @@ const fastifySsrPlugin = async (fastify, options, done) => {
23
23
  appDir: options.appDir,
24
24
  ssr: 'ssr',
25
25
  framework: 'vue',
26
- base: options.baseUrl
26
+ base: options.baseUrl,
27
+ host: options.host
27
28
  });
28
29
  // const { createServer, searchForWorkspaceRoot } = await import('vite')
29
30
  // const { baseConfig } = await import('vitrify')
@@ -91,11 +92,17 @@ const fastifySsrPlugin = async (fastify, options, done) => {
91
92
  mods: matchedModules
92
93
  });
93
94
  const [appHtml, preloadLinks] = await render(url, manifest, ssrContext);
95
+ if (!ssrContext.initialState)
96
+ ssrContext.initialState = {};
97
+ ssrContext.initialState.provide = provide;
94
98
  let html = template
95
99
  .replace(`<!--preload-links-->`, preloadLinks)
96
100
  .replace(`<!--app-html-->`, appHtml)
97
101
  .replace('<!--product-name-->', options.productName || 'Product name')
98
- .replace('<!--dev-ssr-css-->', css);
102
+ .replace('<!--dev-ssr-css-->', css)
103
+ .replace('<!--initial-state-->', `<script>
104
+ __INITIAL_STATE__ = '${JSON.stringify(ssrContext.initialState)}'
105
+ </script>`);
99
106
  if (options.onRendered?.length) {
100
107
  for (const ssrFunction of options.onRendered) {
101
108
  html = ssrFunction(html, ssrContext);
@@ -139,7 +146,10 @@ const fastifySsrPlugin = async (fastify, options, done) => {
139
146
  ssrContext.initialState.provide = provide;
140
147
  let html = template
141
148
  .replace(`<!--preload-links-->`, preloadLinks)
142
- .replace(`<!--app-html-->`, appHtml);
149
+ .replace(`<!--app-html-->`, appHtml)
150
+ .replace('<!--initial-state-->', `<script>
151
+ __INITIAL_STATE__ = '${JSON.stringify(ssrContext.initialState)}'
152
+ </script>`);
143
153
  if (options.onRendered?.length) {
144
154
  for (const ssrFunction of options.onRendered) {
145
155
  html = ssrFunction(html, ssrContext);
@@ -2,6 +2,7 @@
2
2
  import chalk from 'chalk';
3
3
  import os from 'os';
4
4
  import { resolveHostname } from '../helpers/utils.js';
5
+ export const exitLogs = [];
5
6
  export function printHttpServerUrls(server, config) {
6
7
  const address = server.address();
7
8
  const isAddressInfo = (x) => x?.address;
package/dist/index.js CHANGED
@@ -460,6 +460,7 @@ export const baseConfig = async ({ ssr, appDir, publicDir, base = '/', command =
460
460
  noExternal
461
461
  },
462
462
  define: {
463
+ __HOST__: 'localhost',
463
464
  __BASE_URL__: `'${base}'`
464
465
  }
465
466
  };
@@ -11,6 +11,7 @@ export interface FastifySsrOptions {
11
11
  publicDir?: URL;
12
12
  productName?: string;
13
13
  mode?: string;
14
+ host?: string;
14
15
  }
15
16
  declare const fastifySsrPlugin: FastifyPluginCallback<FastifySsrOptions>;
16
17
  export { fastifySsrPlugin };
@@ -1,4 +1,5 @@
1
1
  /// <reference types="node" />
2
2
  import type { Server } from 'net';
3
3
  import type { ResolvedConfig } from 'vite';
4
+ export declare const exitLogs: string[];
4
5
  export declare function printHttpServerUrls(server: Server, config: ResolvedConfig): void;
@@ -1,8 +1,8 @@
1
1
  import type { VitrifyPlugin } from './index.js';
2
2
  export interface QuasarConf {
3
- ctx: Record<string, any>;
4
- css: string[];
5
- boot: string[];
3
+ ctx?: Record<string, any>;
4
+ css?: string[];
5
+ boot?: string[];
6
6
  framework: {
7
7
  components?: string[];
8
8
  directives?: string[];
@@ -10,8 +10,7 @@ export interface QuasarConf {
10
10
  lang?: string;
11
11
  iconSet?: string;
12
12
  };
13
- animations: string[];
14
- extras: string[];
13
+ extras?: string[];
15
14
  }
16
15
  export declare const injectSsrContext: (html: string, ssrContext: Record<string, any>) => string;
17
16
  export declare const QuasarPlugin: VitrifyPlugin;
package/package.json CHANGED
@@ -1,9 +1,9 @@
1
1
  {
2
2
  "name": "vitrify",
3
- "version": "0.9.1",
3
+ "version": "0.9.2",
4
4
  "license": "MIT",
5
5
  "author": "Stefan van Herwijnen",
6
- "description": "Pre-configured Vite CLI for your framework",
6
+ "description": "Vite as your Full Stack development tool",
7
7
  "type": "module",
8
8
  "bin": {
9
9
  "vitrify": "./dist/bin/cli.js"
@@ -110,5 +110,10 @@
110
110
  "src",
111
111
  "!dist/**/*.test.js",
112
112
  "!dist/**/test.js"
113
+ ],
114
+ "keywords": [
115
+ "vite",
116
+ "full stack",
117
+ "fastify"
113
118
  ]
114
119
  }
@@ -1,14 +1,8 @@
1
1
  #!/usr/bin/env node
2
2
  import cac from 'cac'
3
3
  import { getAppDir, parsePath } from '../app-urls.js'
4
- import { printHttpServerUrls } from '../helpers/logger.js'
5
- import type {
6
- ConfigEnv,
7
- ResolvedConfig,
8
- UserConfig,
9
- UserConfigExport,
10
- ViteDevServer
11
- } from 'vite'
4
+ import { printHttpServerUrls, exitLogs } from '../helpers/logger.js'
5
+ import type { ResolvedConfig } from 'vite'
12
6
  import type { Server } from 'net'
13
7
 
14
8
  const cli = cac('vitrify')
@@ -157,6 +151,9 @@ cli
157
151
  }
158
152
  console.log('Dev server running at:')
159
153
  printHttpServerUrls(server, config)
154
+ for (const line of exitLogs) {
155
+ config.logger.warn(line)
156
+ }
160
157
  })
161
158
 
162
159
  cli.command('test').action(async (options) => {
@@ -7,6 +7,7 @@ import type { FastifyServerOptions } from 'fastify'
7
7
  import { fastifySsrPlugin } from '../frameworks/vue/fastify-ssr-plugin.js'
8
8
  import type { OnRenderedHook, VitrifyConfig } from '../vitrify-config.js'
9
9
  import isPortReachable from 'is-port-reachable'
10
+ import { exitLogs } from '../helpers/logger.js'
10
11
 
11
12
  const getFirstOpenPort = async (portNumber: number): Promise<number> => {
12
13
  if (!(await isPortReachable(portNumber, { host: 'localhost' }))) {
@@ -59,11 +60,21 @@ export async function createVitrifyDevServer({
59
60
 
60
61
  config.logLevel = logLevel
61
62
 
63
+ config.define = {
64
+ ...config.define,
65
+ __HOST__: `'${host}'`
66
+ }
67
+
68
+ const wsPort = await getFirstOpenPort(24678)
69
+ exitLogs.push(
70
+ `[warning] HTTPS mode enabled. Visit https://{hostname}:${wsPort} to enable a security exception for HMR.`
71
+ )
72
+
62
73
  config.server = {
63
74
  https: config.server?.https,
64
75
  hmr: {
65
76
  protocol: config.server?.https ? 'wss' : 'ws',
66
- port: await getFirstOpenPort(24678)
77
+ port: wsPort
67
78
  },
68
79
  port,
69
80
  // middlewareMode: mode === 'ssr' ? 'ssr' : undefined,
@@ -161,7 +172,8 @@ export async function createServer({
161
172
  await app.register(fastifySsrPlugin, {
162
173
  appDir,
163
174
  mode: 'development',
164
- onRendered
175
+ onRendered,
176
+ host
165
177
  })
166
178
  }
167
179
  await app.listen({
@@ -8,7 +8,6 @@ import { readFileSync } from 'fs'
8
8
  import { componentsModules, collectCss } from '../../helpers/collect-css-ssr.js'
9
9
  import type { ViteDevServer } from 'vite'
10
10
  import type { OnRenderedHook } from 'src/node/vitrify-config.js'
11
-
12
11
  export interface FastifySsrOptions {
13
12
  baseUrl?: string
14
13
  provide?: (
@@ -23,6 +22,7 @@ export interface FastifySsrOptions {
23
22
  publicDir?: URL
24
23
  productName?: string
25
24
  mode?: string
25
+ host?: string
26
26
  }
27
27
 
28
28
  const fastifySsrPlugin: FastifyPluginCallback<FastifySsrOptions> = async (
@@ -55,7 +55,8 @@ const fastifySsrPlugin: FastifyPluginCallback<FastifySsrOptions> = async (
55
55
  appDir: options.appDir,
56
56
  ssr: 'ssr',
57
57
  framework: 'vue',
58
- base: options.baseUrl
58
+ base: options.baseUrl,
59
+ host: options.host
59
60
  })
60
61
  // const { createServer, searchForWorkspaceRoot } = await import('vite')
61
62
  // const { baseConfig } = await import('vitrify')
@@ -101,7 +102,7 @@ const fastifySsrPlugin: FastifyPluginCallback<FastifySsrOptions> = async (
101
102
  const url = req.raw.url?.replace(options.baseUrl!, '/')
102
103
  const provide = options.provide ? await options.provide(req, res) : {}
103
104
 
104
- const ssrContext = {
105
+ const ssrContext: Record<string, any> = {
105
106
  req,
106
107
  res,
107
108
  provide
@@ -133,11 +134,21 @@ const fastifySsrPlugin: FastifyPluginCallback<FastifySsrOptions> = async (
133
134
  })
134
135
 
135
136
  const [appHtml, preloadLinks] = await render(url, manifest, ssrContext)
137
+
138
+ if (!ssrContext.initialState) ssrContext.initialState = {}
139
+ ssrContext.initialState.provide = provide
140
+
136
141
  let html = template
137
142
  .replace(`<!--preload-links-->`, preloadLinks)
138
143
  .replace(`<!--app-html-->`, appHtml)
139
144
  .replace('<!--product-name-->', options.productName || 'Product name')
140
145
  .replace('<!--dev-ssr-css-->', css)
146
+ .replace(
147
+ '<!--initial-state-->',
148
+ `<script>
149
+ __INITIAL_STATE__ = '${JSON.stringify(ssrContext.initialState)}'
150
+ </script>`
151
+ )
141
152
 
142
153
  if (options.onRendered?.length) {
143
154
  for (const ssrFunction of options.onRendered) {
@@ -196,6 +207,12 @@ const fastifySsrPlugin: FastifyPluginCallback<FastifySsrOptions> = async (
196
207
  let html = template
197
208
  .replace(`<!--preload-links-->`, preloadLinks)
198
209
  .replace(`<!--app-html-->`, appHtml)
210
+ .replace(
211
+ '<!--initial-state-->',
212
+ `<script>
213
+ __INITIAL_STATE__ = '${JSON.stringify(ssrContext.initialState)}'
214
+ </script>`
215
+ )
199
216
 
200
217
  if (options.onRendered?.length) {
201
218
  for (const ssrFunction of options.onRendered) {
@@ -6,6 +6,8 @@ import os from 'os'
6
6
  import type { Hostname } from '../helpers/utils.js'
7
7
  import { resolveHostname } from '../helpers/utils.js'
8
8
 
9
+ export const exitLogs: string[] = []
10
+
9
11
  export function printHttpServerUrls(
10
12
  server: Server,
11
13
  config: ResolvedConfig
package/src/node/index.ts CHANGED
@@ -539,6 +539,7 @@ export const baseConfig = async ({
539
539
  noExternal
540
540
  },
541
541
  define: {
542
+ __HOST__: 'localhost',
542
543
  __BASE_URL__: `'${base}'`
543
544
  }
544
545
  } as VitrifyConfig
@@ -9,9 +9,9 @@ import type {
9
9
  import { QuasarResolver } from 'unplugin-vue-components/resolvers'
10
10
  import type { VitrifyPlugin } from './index.js'
11
11
  export interface QuasarConf {
12
- ctx: Record<string, any>
13
- css: string[]
14
- boot: string[]
12
+ ctx?: Record<string, any>
13
+ css?: string[]
14
+ boot?: string[]
15
15
  framework: {
16
16
  components?: string[]
17
17
  directives?: string[]
@@ -19,8 +19,8 @@ export interface QuasarConf {
19
19
  lang?: string
20
20
  iconSet?: string
21
21
  }
22
- animations: string[]
23
- extras: string[]
22
+ // animations: string[]
23
+ extras?: string[]
24
24
  }
25
25
 
26
26
  export const injectSsrContext = (
@@ -55,6 +55,7 @@ export async function createApp(
55
55
  next()
56
56
  })
57
57
 
58
+ let initialState: Record<string, any>
58
59
  let provide: Record<string, unknown> = {}
59
60
  if (import.meta.env.SSR) {
60
61
  if (ssrContext?.provide) {
@@ -62,7 +63,11 @@ export async function createApp(
62
63
  }
63
64
  } else {
64
65
  // @ts-ignore
65
- provide = window.__INITIAL_STATE__?.provide
66
+ if (window.__INITIAL_STATE__) {
67
+ // @ts-ignore
68
+ initialState = JSON.parse(window.__INITIAL_STATE__)
69
+ provide = initialState.provide
70
+ }
66
71
  }
67
72
  for (let key in provide) {
68
73
  app.provide(key, provide[key])