vitrify 0.4.0 → 0.5.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 +2 -2
- package/dist/bin/build.js +10 -8
- package/dist/bin/cli.js +6 -4
- package/dist/bin/dev.js +36 -24
- package/dist/frameworks/vue/fastify-csr-plugin.js +38 -0
- package/dist/frameworks/vue/fastify-ssr-plugin.js +53 -39
- package/dist/frameworks/vue/server.js +2 -2
- package/dist/helpers/collect-css-ssr.js +6 -2
- package/dist/index.js +78 -57
- package/dist/plugins/quasar.js +22 -10
- package/dist/types/bin/build.d.ts +1 -0
- package/dist/types/bin/dev.d.ts +8 -5
- package/dist/types/frameworks/vue/fastify-csr-plugin.d.ts +17 -0
- package/dist/types/frameworks/vue/server.d.ts +3 -2
- package/dist/types/helpers/collect-css-ssr.d.ts +5 -1
- package/dist/types/index.d.ts +4 -1
- package/dist/types/vitrify-config.d.ts +13 -1
- package/package.json +6 -4
- package/src/node/bin/build.ts +11 -8
- package/src/node/bin/cli.ts +7 -4
- package/src/node/bin/dev.ts +41 -28
- package/src/node/frameworks/vue/fastify-csr-plugin.ts +72 -0
- package/src/node/frameworks/vue/fastify-ssr-plugin.ts +57 -40
- package/src/node/frameworks/vue/server.ts +4 -3
- package/src/node/helpers/collect-css-ssr.ts +12 -4
- package/src/node/index.ts +89 -56
- package/src/node/plugins/quasar.ts +33 -10
- package/src/node/vitrify-config.ts +13 -2
- package/src/vite/fastify/server.ts +3 -1
- 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/ssr/app.ts +1 -1
package/dist/plugins/quasar.js
CHANGED
|
@@ -37,6 +37,19 @@ export const QuasarPlugin = async ({ ssr = false, pwa = false }) => {
|
|
|
37
37
|
Components({
|
|
38
38
|
resolvers: [QuasarResolver()]
|
|
39
39
|
}),
|
|
40
|
+
{
|
|
41
|
+
name: 'vite-plugin-quasar-transform',
|
|
42
|
+
enforce: 'pre',
|
|
43
|
+
transform: (code, id, options) => {
|
|
44
|
+
const { ssr } = options || {};
|
|
45
|
+
code = code
|
|
46
|
+
.replaceAll('__QUASAR_SSR__', ssr ? ssr.toString() : 'false')
|
|
47
|
+
.replaceAll('__QUASAR_SSR_SERVER__', ssr ? 'import.meta.env.SSR' : 'false')
|
|
48
|
+
.replaceAll('__QUASAR_SSR_CLIENT__', ssr ? '!import.meta.env.SSR' : 'false')
|
|
49
|
+
.replaceAll('__QUASAR_SSR_PWA__', ssr && pwa ? '!import.meta.env.SSR' : 'false');
|
|
50
|
+
return code;
|
|
51
|
+
}
|
|
52
|
+
},
|
|
40
53
|
{
|
|
41
54
|
name: 'vite-plugin-quasar-setup',
|
|
42
55
|
enforce: 'pre',
|
|
@@ -141,10 +154,6 @@ export const QuasarPlugin = async ({ ssr = false, pwa = false }) => {
|
|
|
141
154
|
replacement: new URL('src/index.all.js', urls?.packages?.quasar)
|
|
142
155
|
.pathname
|
|
143
156
|
},
|
|
144
|
-
// {
|
|
145
|
-
// find: 'quasar',
|
|
146
|
-
// replacement: new URL('src/index.all.js', urls?.packages?.quasar).pathname
|
|
147
|
-
// },
|
|
148
157
|
{
|
|
149
158
|
find: `@quasar/extras`,
|
|
150
159
|
replacement: new URL('.', urls?.packages?.['@quasar/extras'])
|
|
@@ -154,11 +163,14 @@ export const QuasarPlugin = async ({ ssr = false, pwa = false }) => {
|
|
|
154
163
|
},
|
|
155
164
|
define: {
|
|
156
165
|
__DEV__: process.env.NODE_ENV !== 'production' || true,
|
|
157
|
-
__QUASAR_VERSION__: `'${version}'
|
|
158
|
-
__QUASAR_SSR__: !!ssr,
|
|
159
|
-
__QUASAR_SSR_SERVER__: ssr === 'server',
|
|
160
|
-
|
|
161
|
-
|
|
166
|
+
__QUASAR_VERSION__: `'${version}'`
|
|
167
|
+
// __QUASAR_SSR__: !!ssr,
|
|
168
|
+
// // __QUASAR_SSR_SERVER__: ssr === 'server',
|
|
169
|
+
// __QUASAR_SSR_SERVER__: `import.meta.env.SSR`,
|
|
170
|
+
// // __QUASAR_SSR_CLIENT__: ssr === 'client',
|
|
171
|
+
// __QUASAR_SSR_CLIENT__: `!import.meta.env.SSR`,
|
|
172
|
+
// // __QUASAR_SSR_PWA__: ssr === 'client' && pwa
|
|
173
|
+
// __QUASAR_SSR_PWA__: pwa ? `!import.meta.env.SSR` : false
|
|
162
174
|
},
|
|
163
175
|
ssr: {
|
|
164
176
|
noExternal: ['quasar']
|
|
@@ -172,7 +184,7 @@ export const QuasarPlugin = async ({ ssr = false, pwa = false }) => {
|
|
|
172
184
|
config: async (config, env) => ({
|
|
173
185
|
resolve: {
|
|
174
186
|
alias: [
|
|
175
|
-
|
|
187
|
+
{ find: new RegExp('^quasar$'), replacement: 'virtual:quasar' }
|
|
176
188
|
]
|
|
177
189
|
}
|
|
178
190
|
}),
|
package/dist/types/bin/dev.d.ts
CHANGED
|
@@ -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,
|
|
5
|
+
export declare function createVitrifyDevServer({ port, logLevel, ssr, framework, host, appDir, publicDir, base }: {
|
|
6
6
|
port?: number;
|
|
7
7
|
logLevel?: LogLevel;
|
|
8
|
-
|
|
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,
|
|
15
|
+
export declare function createServer({ port, logLevel, ssr, framework, host, appDir, publicDir }: {
|
|
15
16
|
port?: number;
|
|
16
17
|
logLevel?: LogLevel;
|
|
17
|
-
|
|
18
|
+
ssr?: 'ssr' | 'fastify';
|
|
18
19
|
framework?: 'vue';
|
|
19
20
|
host?: string;
|
|
20
21
|
appDir?: URL;
|
|
@@ -41,11 +42,13 @@ 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;
|
|
47
49
|
optimizeDeps: import("vite").DepOptimizationOptions;
|
|
48
50
|
worker: import("vite").ResolveWorkerOptions;
|
|
49
|
-
|
|
51
|
+
appType: import("vite").AppType;
|
|
52
|
+
experimental: import("vite").ResolvedExperimentalOptions;
|
|
50
53
|
}>;
|
|
51
54
|
}>;
|
|
@@ -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,
|
|
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
|
-
|
|
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
|
|
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
|
*/
|
package/dist/types/index.d.ts
CHANGED
|
@@ -3,13 +3,16 @@ 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, debug }: {
|
|
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;
|
|
15
|
+
debug?: boolean | undefined;
|
|
14
16
|
}) => Promise<InlineConfig>;
|
|
17
|
+
export declare const vitrifyDir: URL;
|
|
15
18
|
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.
|
|
3
|
+
"version": "0.5.2",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"author": "Stefan van Herwijnen",
|
|
6
6
|
"description": "Pre-configured Vite CLI for your framework",
|
|
@@ -68,10 +68,11 @@
|
|
|
68
68
|
"magic-string": "^0.26.2",
|
|
69
69
|
"merge-deep": "^3.0.3",
|
|
70
70
|
"readline": "^1.3.0",
|
|
71
|
+
"rollup-plugin-visualizer": "^5.6.0",
|
|
71
72
|
"sass": "1.52.3",
|
|
72
73
|
"ts-node": "^10.8.1",
|
|
73
74
|
"unplugin-vue-components": "^0.19.6",
|
|
74
|
-
"vite": "^3.0.0-
|
|
75
|
+
"vite": "^3.0.0-beta.0",
|
|
75
76
|
"vitest": "^0.14.1"
|
|
76
77
|
},
|
|
77
78
|
"devDependencies": {
|
|
@@ -81,11 +82,11 @@
|
|
|
81
82
|
"@types/node": "^17.0.41",
|
|
82
83
|
"@types/ws": "^8.5.3",
|
|
83
84
|
"@vue/runtime-core": "^3.2.37",
|
|
84
|
-
"import-meta-resolve": "^2.0.
|
|
85
|
+
"import-meta-resolve": "^2.0.3",
|
|
85
86
|
"quasar": "^2.7.1",
|
|
86
87
|
"rollup": "^2.75.6",
|
|
87
88
|
"typescript": "^4.7.3",
|
|
88
|
-
"vite": "^3.0.0-alpha.
|
|
89
|
+
"vite": "^3.0.0-alpha.11",
|
|
89
90
|
"vue": "^3.2.37",
|
|
90
91
|
"vue-router": "^4.0.15"
|
|
91
92
|
},
|
|
@@ -93,6 +94,7 @@
|
|
|
93
94
|
"@fastify/static": "^6.4.0",
|
|
94
95
|
"fastify": "^4.0.0",
|
|
95
96
|
"fastify-plugin": "^3.0.1",
|
|
97
|
+
"import-meta-resolve": "^2.0.3",
|
|
96
98
|
"quasar": "^2.7.1",
|
|
97
99
|
"vue": "^3.2.37",
|
|
98
100
|
"vue-router": "^4.0.15"
|
package/src/node/bin/build.ts
CHANGED
|
@@ -8,13 +8,16 @@ export async function build(opts: {
|
|
|
8
8
|
outDir: string
|
|
9
9
|
appDir?: URL
|
|
10
10
|
publicDir?: URL
|
|
11
|
+
debug?: boolean
|
|
11
12
|
}) {
|
|
12
13
|
const config = await baseConfig({
|
|
13
14
|
command: 'build',
|
|
14
15
|
mode: 'production',
|
|
15
16
|
ssr: opts?.ssr,
|
|
16
17
|
appDir: opts.appDir,
|
|
17
|
-
publicDir: opts.publicDir
|
|
18
|
+
publicDir: opts.publicDir,
|
|
19
|
+
base: opts.base,
|
|
20
|
+
debug: opts.debug
|
|
18
21
|
})
|
|
19
22
|
|
|
20
23
|
config.build = {
|
|
@@ -24,16 +27,16 @@ export async function build(opts: {
|
|
|
24
27
|
emptyOutDir: !!opts.outDir
|
|
25
28
|
}
|
|
26
29
|
|
|
27
|
-
if (opts.base) {
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
}
|
|
30
|
+
// if (opts.base) {
|
|
31
|
+
// config.define = {
|
|
32
|
+
// ...config.define,
|
|
33
|
+
// __BASE_URL__: `'${opts.base}'`
|
|
34
|
+
// }
|
|
35
|
+
// }
|
|
33
36
|
|
|
34
37
|
return viteBuild({
|
|
35
38
|
configFile: false,
|
|
36
|
-
base: opts.base,
|
|
39
|
+
// base: opts.base,
|
|
37
40
|
// logLevel: 'silent',
|
|
38
41
|
...config
|
|
39
42
|
})
|
package/src/node/bin/cli.ts
CHANGED
|
@@ -20,6 +20,7 @@ cli
|
|
|
20
20
|
.option('--appDir [appDir]', 'App directory')
|
|
21
21
|
.option('--publicDir [publicDir]', 'Public directory')
|
|
22
22
|
.option('--productName [productName]', 'Product name')
|
|
23
|
+
.option('--debug', 'Debug build')
|
|
23
24
|
.action(async (options) => {
|
|
24
25
|
const { build } = await import('./build.js')
|
|
25
26
|
let appDir: URL
|
|
@@ -38,17 +39,19 @@ cli
|
|
|
38
39
|
base: string
|
|
39
40
|
appDir?: URL
|
|
40
41
|
publicDir?: URL
|
|
42
|
+
debug?: boolean
|
|
41
43
|
} = {
|
|
42
44
|
base: options.base,
|
|
43
45
|
appDir,
|
|
44
|
-
publicDir: parsePath(options.publicDir, appDir)
|
|
46
|
+
publicDir: parsePath(options.publicDir, appDir),
|
|
47
|
+
debug: options.debug
|
|
45
48
|
}
|
|
46
49
|
|
|
47
50
|
switch (options.mode) {
|
|
48
51
|
case 'csr':
|
|
49
52
|
await build({
|
|
50
53
|
...args,
|
|
51
|
-
outDir: new URL('
|
|
54
|
+
outDir: new URL('csr/', baseOutDir).pathname
|
|
52
55
|
})
|
|
53
56
|
break
|
|
54
57
|
case 'fastify':
|
|
@@ -128,7 +131,7 @@ cli
|
|
|
128
131
|
switch (options.mode) {
|
|
129
132
|
case 'ssr':
|
|
130
133
|
;({ server, config } = await createServer({
|
|
131
|
-
|
|
134
|
+
ssr: 'ssr',
|
|
132
135
|
host: options.host,
|
|
133
136
|
appDir: parsePath(options.appDir, cwd),
|
|
134
137
|
publicDir: parsePath(options.publicDir, cwd)
|
|
@@ -136,7 +139,7 @@ cli
|
|
|
136
139
|
break
|
|
137
140
|
case 'fastify':
|
|
138
141
|
;({ server, config } = await createServer({
|
|
139
|
-
|
|
142
|
+
ssr: 'fastify',
|
|
140
143
|
host: options.host,
|
|
141
144
|
appDir: parsePath(options.appDir, cwd),
|
|
142
145
|
publicDir: parsePath(options.publicDir, cwd)
|
package/src/node/bin/dev.ts
CHANGED
|
@@ -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,37 @@ export async function createVitrifyDevServer({
|
|
|
33
38
|
if (!appDir) appDir = getAppDir()
|
|
34
39
|
let config: InlineConfig = {}
|
|
35
40
|
let ssrMode: 'server' | 'fastify' | undefined
|
|
36
|
-
if (
|
|
37
|
-
if (
|
|
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
|
+
port: 24678
|
|
61
|
+
},
|
|
50
62
|
port,
|
|
51
63
|
// middlewareMode: mode === 'ssr' ? 'ssr' : undefined,
|
|
52
|
-
middlewareMode:
|
|
64
|
+
middlewareMode: ssr ? 'ssr' : false,
|
|
53
65
|
fs: {
|
|
66
|
+
strict: false, // https://github.com/vitejs/vite/issues/8175
|
|
54
67
|
allow: [
|
|
55
68
|
searchForWorkspaceRoot(process.cwd()),
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
searchForWorkspaceRoot(cliDir.pathname)
|
|
60
|
-
// appDir.pathname,
|
|
69
|
+
searchForWorkspaceRoot(appDir.pathname),
|
|
70
|
+
searchForWorkspaceRoot(cliDir.pathname),
|
|
71
|
+
appDir.pathname
|
|
61
72
|
]
|
|
62
73
|
},
|
|
63
74
|
watch: {
|
|
@@ -81,7 +92,8 @@ export async function createVitrifyDevServer({
|
|
|
81
92
|
export async function createServer({
|
|
82
93
|
port = 3000,
|
|
83
94
|
logLevel = 'info',
|
|
84
|
-
mode = 'csr',
|
|
95
|
+
// mode = 'csr',
|
|
96
|
+
ssr,
|
|
85
97
|
framework = 'vue',
|
|
86
98
|
host,
|
|
87
99
|
appDir,
|
|
@@ -89,7 +101,8 @@ export async function createServer({
|
|
|
89
101
|
}: {
|
|
90
102
|
port?: number
|
|
91
103
|
logLevel?: LogLevel
|
|
92
|
-
mode?: 'csr' | 'ssr' | 'fastify'
|
|
104
|
+
// mode?: 'csr' | 'ssr' | 'fastify'
|
|
105
|
+
ssr?: 'ssr' | 'fastify'
|
|
93
106
|
framework?: 'vue'
|
|
94
107
|
host?: string
|
|
95
108
|
appDir?: URL
|
|
@@ -99,47 +112,47 @@ export async function createServer({
|
|
|
99
112
|
'../app-urls.js'
|
|
100
113
|
)
|
|
101
114
|
|
|
115
|
+
appDir = appDir || getAppDir()
|
|
102
116
|
const cliDir = getCliDir()
|
|
103
117
|
|
|
104
118
|
const vite = await createVitrifyDevServer({
|
|
105
119
|
port,
|
|
106
120
|
logLevel,
|
|
107
|
-
|
|
121
|
+
ssr,
|
|
108
122
|
framework,
|
|
109
123
|
host,
|
|
110
124
|
appDir,
|
|
111
125
|
publicDir
|
|
112
126
|
})
|
|
113
|
-
let entryUrl: string
|
|
114
127
|
|
|
115
128
|
let setup
|
|
116
129
|
let server: Server
|
|
117
130
|
|
|
118
|
-
console.log(`Development mode: ${
|
|
119
|
-
if (
|
|
131
|
+
console.log(`Development mode: ${ssr ? ssr : 'csr'}`)
|
|
132
|
+
if (ssr) {
|
|
120
133
|
const entryUrl =
|
|
121
|
-
|
|
134
|
+
ssr === 'fastify'
|
|
122
135
|
? new URL('src/vite/fastify/entry.ts', cliDir).pathname
|
|
123
136
|
: new URL(`src/vite/${framework}/ssr/entry-server.ts`, cliDir).pathname
|
|
124
137
|
|
|
125
138
|
;({ setup } = await vite.ssrLoadModule(entryUrl))
|
|
126
139
|
|
|
127
140
|
const app = fastify({
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
? vite.config.server.https
|
|
131
|
-
: {}
|
|
141
|
+
logger: true,
|
|
142
|
+
https: vite.config.server.https as ServerOptions
|
|
132
143
|
})
|
|
144
|
+
if (process.env) process.env.MODE = 'development'
|
|
133
145
|
if (setup) {
|
|
134
146
|
await setup({
|
|
135
147
|
fastify: app
|
|
136
148
|
})
|
|
137
149
|
}
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
150
|
+
if (ssr === 'ssr') {
|
|
151
|
+
await app.register(fastifySsrPlugin, {
|
|
152
|
+
appDir,
|
|
153
|
+
mode: 'development'
|
|
154
|
+
})
|
|
155
|
+
}
|
|
143
156
|
await app.listen({
|
|
144
157
|
port: Number(port || 3000),
|
|
145
158
|
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
|