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/README.md
CHANGED
|
@@ -3,12 +3,12 @@
|
|
|
3
3
|
> Pre-configured Vite CLI for your framework
|
|
4
4
|
|
|
5
5
|
- Use a simple configuration file to configure and integrate required Vite plugins into your project.
|
|
6
|
-
- Client-Side Rendering (CSR)
|
|
6
|
+
- Client-Side Rendering (CSR), Server-Side Rendering (SSR) and Fastify server build and development modes.
|
|
7
7
|
|
|
8
8
|
## Features
|
|
9
9
|
|
|
10
10
|
- Uses [Fastify](https://github.com/fastify/fastify-vite) for the development server and SSR production server.
|
|
11
|
-
|
|
11
|
+
- Generates a Fastify plugin to serve your server-side rendered application.
|
|
12
12
|
- A [`run`](./src/node/bin/run.ts) command which injects context such as application paths into the script which you want to run.
|
|
13
13
|
- A [`test`](./src/node/bin/test.ts) command which runs a pre-configured [Vitest](https://github.com/vitest-dev/vitest) instance.
|
|
14
14
|
- An [extra plugin layer](./src/node/plugins/index.ts) which provides some extra context such as the SSR mode (client or server) and PWA mode. Used for UI frameworks which render differently based on these settings.
|
package/dist/bin/build.js
CHANGED
|
@@ -7,7 +7,9 @@ export async function build(opts) {
|
|
|
7
7
|
mode: 'production',
|
|
8
8
|
ssr: opts?.ssr,
|
|
9
9
|
appDir: opts.appDir,
|
|
10
|
-
publicDir: opts.publicDir
|
|
10
|
+
publicDir: opts.publicDir,
|
|
11
|
+
base: opts.base,
|
|
12
|
+
debug: opts.debug
|
|
11
13
|
});
|
|
12
14
|
config.build = {
|
|
13
15
|
...config.build,
|
|
@@ -15,15 +17,15 @@ export async function build(opts) {
|
|
|
15
17
|
outDir: opts.outDir,
|
|
16
18
|
emptyOutDir: !!opts.outDir
|
|
17
19
|
};
|
|
18
|
-
if (opts.base) {
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
}
|
|
20
|
+
// if (opts.base) {
|
|
21
|
+
// config.define = {
|
|
22
|
+
// ...config.define,
|
|
23
|
+
// __BASE_URL__: `'${opts.base}'`
|
|
24
|
+
// }
|
|
25
|
+
// }
|
|
24
26
|
return viteBuild({
|
|
25
27
|
configFile: false,
|
|
26
|
-
base: opts.base,
|
|
28
|
+
// base: opts.base,
|
|
27
29
|
// logLevel: 'silent',
|
|
28
30
|
...config
|
|
29
31
|
});
|
package/dist/bin/cli.js
CHANGED
|
@@ -11,6 +11,7 @@ cli
|
|
|
11
11
|
.option('--appDir [appDir]', 'App directory')
|
|
12
12
|
.option('--publicDir [publicDir]', 'Public directory')
|
|
13
13
|
.option('--productName [productName]', 'Product name')
|
|
14
|
+
.option('--debug', 'Debug build')
|
|
14
15
|
.action(async (options) => {
|
|
15
16
|
const { build } = await import('./build.js');
|
|
16
17
|
let appDir;
|
|
@@ -27,13 +28,14 @@ cli
|
|
|
27
28
|
const args = {
|
|
28
29
|
base: options.base,
|
|
29
30
|
appDir,
|
|
30
|
-
publicDir: parsePath(options.publicDir, appDir)
|
|
31
|
+
publicDir: parsePath(options.publicDir, appDir),
|
|
32
|
+
debug: options.debug
|
|
31
33
|
};
|
|
32
34
|
switch (options.mode) {
|
|
33
35
|
case 'csr':
|
|
34
36
|
await build({
|
|
35
37
|
...args,
|
|
36
|
-
outDir: new URL('
|
|
38
|
+
outDir: new URL('csr/', baseOutDir).pathname
|
|
37
39
|
});
|
|
38
40
|
break;
|
|
39
41
|
case 'fastify':
|
|
@@ -106,7 +108,7 @@ cli
|
|
|
106
108
|
case 'ssr':
|
|
107
109
|
;
|
|
108
110
|
({ server, config } = await createServer({
|
|
109
|
-
|
|
111
|
+
ssr: 'ssr',
|
|
110
112
|
host: options.host,
|
|
111
113
|
appDir: parsePath(options.appDir, cwd),
|
|
112
114
|
publicDir: parsePath(options.publicDir, cwd)
|
|
@@ -115,7 +117,7 @@ cli
|
|
|
115
117
|
case 'fastify':
|
|
116
118
|
;
|
|
117
119
|
({ server, config } = await createServer({
|
|
118
|
-
|
|
120
|
+
ssr: 'fastify',
|
|
119
121
|
host: options.host,
|
|
120
122
|
appDir: parsePath(options.appDir, cwd),
|
|
121
123
|
publicDir: parsePath(options.publicDir, cwd)
|
package/dist/bin/dev.js
CHANGED
|
@@ -1,16 +1,19 @@
|
|
|
1
1
|
import { searchForWorkspaceRoot } from 'vite';
|
|
2
2
|
import { baseConfig } from '../index.js';
|
|
3
3
|
import fastify from 'fastify';
|
|
4
|
-
|
|
4
|
+
import { fastifySsrPlugin } from '../frameworks/vue/fastify-ssr-plugin.js';
|
|
5
|
+
export async function createVitrifyDevServer({ port = 3000, logLevel = 'info',
|
|
6
|
+
// mode = 'csr',
|
|
7
|
+
ssr, framework = 'vue', host, appDir, publicDir, base }) {
|
|
5
8
|
const { getAppDir, getCliDir, getCliViteDir, getCwd } = await import('../app-urls.js');
|
|
6
9
|
const cliDir = getCliDir();
|
|
7
10
|
if (!appDir)
|
|
8
11
|
appDir = getAppDir();
|
|
9
12
|
let config = {};
|
|
10
13
|
let ssrMode;
|
|
11
|
-
if (
|
|
14
|
+
if (ssr === 'ssr')
|
|
12
15
|
ssrMode = 'server';
|
|
13
|
-
if (
|
|
16
|
+
if (ssr === 'fastify')
|
|
14
17
|
ssrMode = 'fastify';
|
|
15
18
|
config = await baseConfig({
|
|
16
19
|
framework,
|
|
@@ -18,22 +21,27 @@ export async function createVitrifyDevServer({ port = 3000, logLevel = 'info', m
|
|
|
18
21
|
command: 'dev',
|
|
19
22
|
mode: 'development',
|
|
20
23
|
appDir,
|
|
21
|
-
publicDir
|
|
24
|
+
publicDir,
|
|
25
|
+
base
|
|
22
26
|
});
|
|
23
27
|
config.logLevel = logLevel;
|
|
28
|
+
console.log(searchForWorkspaceRoot(appDir.pathname));
|
|
24
29
|
config.server = {
|
|
25
30
|
https: config.server?.https,
|
|
31
|
+
hmr: {
|
|
32
|
+
protocol: config.server?.https ? 'wss' : 'ws',
|
|
33
|
+
port: 24678
|
|
34
|
+
},
|
|
26
35
|
port,
|
|
27
36
|
// middlewareMode: mode === 'ssr' ? 'ssr' : undefined,
|
|
28
|
-
middlewareMode:
|
|
37
|
+
middlewareMode: ssr ? 'ssr' : false,
|
|
29
38
|
fs: {
|
|
39
|
+
strict: false,
|
|
30
40
|
allow: [
|
|
31
41
|
searchForWorkspaceRoot(process.cwd()),
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
searchForWorkspaceRoot(cliDir.pathname)
|
|
36
|
-
// appDir.pathname,
|
|
42
|
+
searchForWorkspaceRoot(appDir.pathname),
|
|
43
|
+
searchForWorkspaceRoot(cliDir.pathname),
|
|
44
|
+
appDir.pathname
|
|
37
45
|
]
|
|
38
46
|
},
|
|
39
47
|
watch: {
|
|
@@ -50,42 +58,46 @@ export async function createVitrifyDevServer({ port = 3000, logLevel = 'info', m
|
|
|
50
58
|
});
|
|
51
59
|
return vitrifyDevServer;
|
|
52
60
|
}
|
|
53
|
-
export async function createServer({ port = 3000, logLevel = 'info',
|
|
61
|
+
export async function createServer({ port = 3000, logLevel = 'info',
|
|
62
|
+
// mode = 'csr',
|
|
63
|
+
ssr, framework = 'vue', host, appDir, publicDir }) {
|
|
54
64
|
const { getAppDir, getCliDir, getCliViteDir, getCwd } = await import('../app-urls.js');
|
|
65
|
+
appDir = appDir || getAppDir();
|
|
55
66
|
const cliDir = getCliDir();
|
|
56
67
|
const vite = await createVitrifyDevServer({
|
|
57
68
|
port,
|
|
58
69
|
logLevel,
|
|
59
|
-
|
|
70
|
+
ssr,
|
|
60
71
|
framework,
|
|
61
72
|
host,
|
|
62
73
|
appDir,
|
|
63
74
|
publicDir
|
|
64
75
|
});
|
|
65
|
-
let entryUrl;
|
|
66
76
|
let setup;
|
|
67
77
|
let server;
|
|
68
|
-
console.log(`Development mode: ${
|
|
69
|
-
if (
|
|
70
|
-
const entryUrl =
|
|
78
|
+
console.log(`Development mode: ${ssr ? ssr : 'csr'}`);
|
|
79
|
+
if (ssr) {
|
|
80
|
+
const entryUrl = ssr === 'fastify'
|
|
71
81
|
? new URL('src/vite/fastify/entry.ts', cliDir).pathname
|
|
72
82
|
: new URL(`src/vite/${framework}/ssr/entry-server.ts`, cliDir).pathname;
|
|
73
83
|
({ setup } = await vite.ssrLoadModule(entryUrl));
|
|
74
84
|
const app = fastify({
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
: {}
|
|
85
|
+
logger: true,
|
|
86
|
+
https: vite.config.server.https
|
|
78
87
|
});
|
|
88
|
+
if (process.env)
|
|
89
|
+
process.env.MODE = 'development';
|
|
79
90
|
if (setup) {
|
|
80
91
|
await setup({
|
|
81
92
|
fastify: app
|
|
82
93
|
});
|
|
83
94
|
}
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
95
|
+
if (ssr === 'ssr') {
|
|
96
|
+
await app.register(fastifySsrPlugin, {
|
|
97
|
+
appDir,
|
|
98
|
+
mode: 'development'
|
|
99
|
+
});
|
|
100
|
+
}
|
|
89
101
|
await app.listen({
|
|
90
102
|
port: Number(port || 3000),
|
|
91
103
|
host
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { fastifyStatic } from '@fastify/static';
|
|
2
|
+
const fastifyCsrPlugin = async (fastify, options, done) => {
|
|
3
|
+
options.vitrifyDir =
|
|
4
|
+
options.vitrifyDir || (await import('vitrify')).vitrifyDir;
|
|
5
|
+
const frameworkDir = new URL('src/vite/vue/', options.vitrifyDir);
|
|
6
|
+
options.baseUrl = options.baseUrl || '/';
|
|
7
|
+
options.mode = options.mode || process.env.MODE || import.meta.env.MODE;
|
|
8
|
+
options.appDir = options.appDir || new URL('../../..', import.meta.url);
|
|
9
|
+
if (options.baseUrl.charAt(options.baseUrl.length - 1) !== '/' ||
|
|
10
|
+
options.baseUrl.charAt(0) !== '/')
|
|
11
|
+
throw new Error('baseUrl should start and end with a /');
|
|
12
|
+
if (options.mode === 'development') {
|
|
13
|
+
options.appDir = options.appDir || new URL('../..', import.meta.url);
|
|
14
|
+
const { createVitrifyDevServer } = await import('vitrify/dev');
|
|
15
|
+
const vite = await createVitrifyDevServer({
|
|
16
|
+
appDir: options.appDir,
|
|
17
|
+
framework: 'vue',
|
|
18
|
+
base: options.baseUrl
|
|
19
|
+
});
|
|
20
|
+
console.log('Dev mode');
|
|
21
|
+
if (!('use' in fastify)) {
|
|
22
|
+
const middie = (await import('@fastify/middie')).default;
|
|
23
|
+
await fastify.register(middie);
|
|
24
|
+
}
|
|
25
|
+
fastify.use(vite.middlewares);
|
|
26
|
+
}
|
|
27
|
+
else {
|
|
28
|
+
options.appDir = options.appDir || new URL('../../..', import.meta.url);
|
|
29
|
+
fastify.register(fastifyStatic, {
|
|
30
|
+
root: new URL('./dist/csr', options.appDir).pathname,
|
|
31
|
+
wildcard: false,
|
|
32
|
+
index: false,
|
|
33
|
+
prefix: options.baseUrl
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
done();
|
|
37
|
+
};
|
|
38
|
+
export { fastifyCsrPlugin };
|
|
@@ -1,64 +1,76 @@
|
|
|
1
|
-
import fastifyStatic from '@fastify/static';
|
|
1
|
+
import { fastifyStatic } from '@fastify/static';
|
|
2
2
|
import { readFileSync } from 'fs';
|
|
3
3
|
import { componentsModules, collectCss } from '../../helpers/collect-css-ssr.js';
|
|
4
4
|
const fastifySsrPlugin = async (fastify, options, done) => {
|
|
5
5
|
options.vitrifyDir =
|
|
6
|
-
options.vitrifyDir ||
|
|
7
|
-
const frameworkDir = new URL('vite/vue/', options.vitrifyDir);
|
|
6
|
+
options.vitrifyDir || (await import('vitrify')).vitrifyDir;
|
|
7
|
+
const frameworkDir = new URL('src/vite/vue/', options.vitrifyDir);
|
|
8
8
|
options.baseUrl = options.baseUrl || '/';
|
|
9
|
+
options.mode = options.mode || process.env.MODE || import.meta.env.MODE;
|
|
10
|
+
options.appDir = options.appDir || new URL('../../..', import.meta.url);
|
|
9
11
|
if (options.baseUrl.charAt(options.baseUrl.length - 1) !== '/' ||
|
|
10
12
|
options.baseUrl.charAt(0) !== '/')
|
|
11
13
|
throw new Error('baseUrl should start and end with a /');
|
|
12
14
|
if (options.mode === 'development') {
|
|
13
|
-
if (!options.vitrifyDir)
|
|
14
|
-
|
|
15
|
+
// if (!options.vitrifyDir)
|
|
16
|
+
// throw new Error('Option vitrifyDir cannot be undefined')
|
|
15
17
|
// if (!options.vite) throw new Error('Option vite cannot be undefined')
|
|
16
18
|
// const { resolve } = await import('import-meta-resolve')
|
|
17
19
|
// const cliDir = new URL('../', await resolve('vitrify', import.meta.url))
|
|
18
20
|
options.appDir = options.appDir || new URL('../../..', import.meta.url);
|
|
19
|
-
const {
|
|
20
|
-
const
|
|
21
|
-
const cliDir = options.vitrifyDir;
|
|
22
|
-
const config = await baseConfig({
|
|
23
|
-
ssr: 'server',
|
|
24
|
-
command: 'dev',
|
|
25
|
-
mode: 'development',
|
|
21
|
+
const { createVitrifyDevServer } = await import('vitrify/dev');
|
|
22
|
+
const vite = await createVitrifyDevServer({
|
|
26
23
|
appDir: options.appDir,
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
middlewareMode: true,
|
|
31
|
-
fs: {
|
|
32
|
-
allow: [
|
|
33
|
-
searchForWorkspaceRoot(process.cwd()),
|
|
34
|
-
searchForWorkspaceRoot(options.appDir.pathname),
|
|
35
|
-
searchForWorkspaceRoot(cliDir.pathname)
|
|
36
|
-
// appDir.pathname,
|
|
37
|
-
]
|
|
38
|
-
},
|
|
39
|
-
watch: {
|
|
40
|
-
// During tests we edit the files too fast and sometimes chokidar
|
|
41
|
-
// misses change events, so enforce polling for consistency
|
|
42
|
-
usePolling: true,
|
|
43
|
-
interval: 100
|
|
44
|
-
}
|
|
45
|
-
};
|
|
46
|
-
const vite = await createServer({
|
|
47
|
-
configFile: false,
|
|
48
|
-
...config
|
|
24
|
+
ssr: 'ssr',
|
|
25
|
+
framework: 'vue',
|
|
26
|
+
base: options.baseUrl
|
|
49
27
|
});
|
|
28
|
+
// const { createServer, searchForWorkspaceRoot } = await import('vite')
|
|
29
|
+
// const { baseConfig } = await import('vitrify')
|
|
30
|
+
// const cliDir = options.vitrifyDir
|
|
31
|
+
// const config = await baseConfig({
|
|
32
|
+
// ssr: 'server',
|
|
33
|
+
// command: 'dev',
|
|
34
|
+
// mode: 'development',
|
|
35
|
+
// appDir: options.appDir,
|
|
36
|
+
// publicDir: options.publicDir || new URL('public', options.appDir)
|
|
37
|
+
// })
|
|
38
|
+
// config.server = {
|
|
39
|
+
// middlewareMode: true,
|
|
40
|
+
// fs: {
|
|
41
|
+
// allow: [
|
|
42
|
+
// searchForWorkspaceRoot(process.cwd()),
|
|
43
|
+
// searchForWorkspaceRoot(options.appDir.pathname),
|
|
44
|
+
// searchForWorkspaceRoot(cliDir.pathname)
|
|
45
|
+
// // appDir.pathname,
|
|
46
|
+
// ]
|
|
47
|
+
// },
|
|
48
|
+
// watch: {
|
|
49
|
+
// // During tests we edit the files too fast and sometimes chokidar
|
|
50
|
+
// // misses change events, so enforce polling for consistency
|
|
51
|
+
// usePolling: true,
|
|
52
|
+
// interval: 100
|
|
53
|
+
// }
|
|
54
|
+
// }
|
|
55
|
+
// const vite = await createServer({
|
|
56
|
+
// configFile: false,
|
|
57
|
+
// ...config
|
|
58
|
+
// })
|
|
50
59
|
console.log('Dev mode');
|
|
51
|
-
|
|
52
|
-
|
|
60
|
+
if (!('use' in fastify)) {
|
|
61
|
+
const middie = (await import('@fastify/middie')).default;
|
|
62
|
+
await fastify.register(middie);
|
|
63
|
+
}
|
|
53
64
|
fastify.use(vite.middlewares);
|
|
54
65
|
fastify.get(`${options.baseUrl}*`, async (req, res) => {
|
|
55
66
|
try {
|
|
56
|
-
const url = req.raw.url;
|
|
67
|
+
const url = req.raw.url?.replace(options.baseUrl, '/');
|
|
57
68
|
const ssrContext = {
|
|
58
69
|
req,
|
|
59
70
|
res
|
|
60
71
|
};
|
|
61
|
-
|
|
72
|
+
let template = readFileSync(new URL('index.html', frameworkDir)).toString();
|
|
73
|
+
template = await vite.transformIndexHtml(url, template);
|
|
62
74
|
const entryUrl = new URL('ssr/entry-server.ts', frameworkDir).pathname;
|
|
63
75
|
const render = (await vite.ssrLoadModule(entryUrl)).render;
|
|
64
76
|
let manifest;
|
|
@@ -74,7 +86,9 @@ const fastifySsrPlugin = async (fastify, options, done) => {
|
|
|
74
86
|
// if (options.vite?.config.vitrify!.globalCss)
|
|
75
87
|
// cssModules.push(...options.vite?.config.vitrify.globalCss)
|
|
76
88
|
const matchedModules = componentsModules(cssModules, vite);
|
|
77
|
-
const css = collectCss(
|
|
89
|
+
const css = collectCss({
|
|
90
|
+
mods: matchedModules
|
|
91
|
+
});
|
|
78
92
|
const [appHtml, preloadLinks] = await render(url, manifest, ssrContext);
|
|
79
93
|
const html = template
|
|
80
94
|
.replace(`<!--preload-links-->`, preloadLinks)
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import fastify from 'fastify';
|
|
2
|
-
export const createApp = ({ onSetup, appDir, baseUrl, onRendered,
|
|
2
|
+
export const createApp = ({ onSetup, appDir, baseUrl, onRendered, fastifyPlugin, vitrifyDir, mode }) => {
|
|
3
3
|
const app = fastify({
|
|
4
4
|
logger: true
|
|
5
5
|
});
|
|
6
|
-
app.register(
|
|
6
|
+
app.register(fastifyPlugin, {
|
|
7
7
|
baseUrl,
|
|
8
8
|
appDir,
|
|
9
9
|
onRendered,
|
|
@@ -9,7 +9,7 @@ export const componentsModules = (components, vite) => {
|
|
|
9
9
|
});
|
|
10
10
|
return matchedModules;
|
|
11
11
|
};
|
|
12
|
-
export const collectCss = (mods, styles = new Map(), checkedComponents = new Set()) => {
|
|
12
|
+
export const collectCss = ({ mods, styles = new Map(), checkedComponents = new Set() }) => {
|
|
13
13
|
for (const mod of mods) {
|
|
14
14
|
if ((mod.file?.endsWith('.scss') ||
|
|
15
15
|
mod.file?.endsWith('.css') ||
|
|
@@ -19,7 +19,11 @@ export const collectCss = (mods, styles = new Map(), checkedComponents = new Set
|
|
|
19
19
|
}
|
|
20
20
|
if (mod.importedModules.size > 0 && !checkedComponents.has(mod.id)) {
|
|
21
21
|
checkedComponents.add(mod.id);
|
|
22
|
-
collectCss(
|
|
22
|
+
collectCss({
|
|
23
|
+
mods: mod.importedModules,
|
|
24
|
+
styles,
|
|
25
|
+
checkedComponents
|
|
26
|
+
});
|
|
23
27
|
}
|
|
24
28
|
}
|
|
25
29
|
let result = '';
|
package/dist/index.js
CHANGED
|
@@ -7,37 +7,39 @@ import { pathToFileURL } from 'url';
|
|
|
7
7
|
import { readFileSync } from 'fs';
|
|
8
8
|
import builtinModules from 'builtin-modules';
|
|
9
9
|
import { resolve } from 'import-meta-resolve';
|
|
10
|
+
import { visualizer } from 'rollup-plugin-visualizer';
|
|
10
11
|
import { getPkgJsonDir } from './app-urls.js';
|
|
11
|
-
const
|
|
12
|
-
// 'fs',
|
|
13
|
-
// 'path',
|
|
14
|
-
// 'url',
|
|
15
|
-
// 'module',
|
|
16
|
-
// 'crypto',
|
|
17
|
-
// 'node:fs',
|
|
12
|
+
const internalServerModules = [
|
|
18
13
|
'util',
|
|
19
|
-
'node:url',
|
|
20
|
-
'node:util',
|
|
21
|
-
'node:fs',
|
|
22
14
|
'vitrify',
|
|
15
|
+
'vitrify/dev',
|
|
23
16
|
'vite',
|
|
24
17
|
'fastify',
|
|
25
|
-
'middie',
|
|
26
|
-
'knex',
|
|
27
|
-
'bcrypt',
|
|
28
|
-
'objection',
|
|
29
|
-
'@fastify/formbody',
|
|
30
18
|
'@fastify/static',
|
|
31
|
-
'
|
|
32
|
-
'@fastify/cookie',
|
|
33
|
-
'mercurius',
|
|
34
|
-
'jose',
|
|
35
|
-
'oidc-provider'
|
|
19
|
+
'node'
|
|
36
20
|
];
|
|
37
21
|
const configPluginMap = {
|
|
38
22
|
quasar: () => import('./plugins/quasar.js').then((module) => module.QuasarPlugin)
|
|
39
23
|
};
|
|
40
|
-
const
|
|
24
|
+
const manualChunkNames = [
|
|
25
|
+
'prerender',
|
|
26
|
+
'fastify-ssr-plugin',
|
|
27
|
+
'fastify-csr-plugin',
|
|
28
|
+
'server'
|
|
29
|
+
];
|
|
30
|
+
const manualChunks = (id) => {
|
|
31
|
+
if (id.includes('vitrify/src/vite/')) {
|
|
32
|
+
const name = id.split('/').at(-1)?.split('.').at(0);
|
|
33
|
+
if (name && manualChunkNames.includes(name))
|
|
34
|
+
return name;
|
|
35
|
+
}
|
|
36
|
+
else if (VIRTUAL_MODULES.some((virtualModule) => id.includes(virtualModule))) {
|
|
37
|
+
return VIRTUAL_MODULES.find((name) => id.includes(name));
|
|
38
|
+
}
|
|
39
|
+
else if (id.includes('node_modules')) {
|
|
40
|
+
return 'vendor';
|
|
41
|
+
}
|
|
42
|
+
};
|
|
41
43
|
export const VIRTUAL_MODULES = [
|
|
42
44
|
'virtual:vitrify-hooks',
|
|
43
45
|
'virtual:global-css',
|
|
@@ -91,7 +93,7 @@ async function bundleConfigFile(fileName, isESM = false) {
|
|
|
91
93
|
dependencies: result.metafile ? Object.keys(result.metafile.inputs) : []
|
|
92
94
|
};
|
|
93
95
|
}
|
|
94
|
-
export const baseConfig = async ({ ssr, appDir, publicDir, command = 'build', mode = 'production', framework = 'vue', pwa = false }) => {
|
|
96
|
+
export const baseConfig = async ({ ssr, appDir, publicDir, base = '/', command = 'build', mode = 'production', framework = 'vue', pwa = false, debug = false }) => {
|
|
95
97
|
const { getAppDir, getCliDir, getCliViteDir, getSrcDir, getCwd } = await import('./app-urls.js');
|
|
96
98
|
if (!appDir) {
|
|
97
99
|
appDir = getAppDir();
|
|
@@ -114,7 +116,7 @@ export const baseConfig = async ({ ssr, appDir, publicDir, command = 'build', mo
|
|
|
114
116
|
const bundledConfig = await bundleConfigFile(new URL('vitrify.config.ts', appDir).pathname);
|
|
115
117
|
fs.writeFileSync(configPath + '.js', bundledConfig.code);
|
|
116
118
|
vitrifyConfig = (await import(configPath + '.js')).default;
|
|
117
|
-
|
|
119
|
+
fs.unlinkSync(configPath + '.js');
|
|
118
120
|
}
|
|
119
121
|
else {
|
|
120
122
|
vitrifyConfig = (await import(new URL('vitrify.config.js', appDir).pathname)).default;
|
|
@@ -173,6 +175,12 @@ export const baseConfig = async ({ ssr, appDir, publicDir, command = 'build', mo
|
|
|
173
175
|
let staticImports;
|
|
174
176
|
let sassVariables;
|
|
175
177
|
let additionalData;
|
|
178
|
+
let serverModules = internalServerModules;
|
|
179
|
+
if (vitrifyConfig.vitrify?.ssr?.serverModules)
|
|
180
|
+
serverModules = [
|
|
181
|
+
...serverModules,
|
|
182
|
+
...vitrifyConfig.vitrify.ssr.serverModules
|
|
183
|
+
];
|
|
176
184
|
const plugins = [
|
|
177
185
|
vuePlugin({
|
|
178
186
|
compiler: await import('vue/compiler-sfc'),
|
|
@@ -330,6 +338,8 @@ export const baseConfig = async ({ ssr, appDir, publicDir, command = 'build', mo
|
|
|
330
338
|
}
|
|
331
339
|
}
|
|
332
340
|
});
|
|
341
|
+
if (debug)
|
|
342
|
+
plugins.push(visualizer());
|
|
333
343
|
}
|
|
334
344
|
const alias = [
|
|
335
345
|
{ find: 'src', replacement: srcDir.pathname },
|
|
@@ -345,16 +355,21 @@ export const baseConfig = async ({ ssr, appDir, publicDir, command = 'build', mo
|
|
|
345
355
|
// { find: 'vue-router', replacement: packageUrls['vue-router'].pathname },
|
|
346
356
|
// { find: 'vitrify', replacement: cliDir.pathname }
|
|
347
357
|
];
|
|
358
|
+
if (mode === 'development' && vitrifyConfig.vitrify?.dev?.alias)
|
|
359
|
+
alias.push(...vitrifyConfig.vitrify.dev.alias);
|
|
348
360
|
if (command === 'test')
|
|
349
361
|
alias.push({
|
|
350
362
|
find: 'vitest',
|
|
351
363
|
replacement: new URL(await resolve('vitest', cliDir.href)).pathname
|
|
352
364
|
});
|
|
353
|
-
let rollupOptions;
|
|
354
|
-
let noExternal = [
|
|
365
|
+
let rollupOptions = {};
|
|
366
|
+
let noExternal = [
|
|
367
|
+
new RegExp(`^(?!(${[...builtinModules, ...serverModules].join('|')}))`)
|
|
368
|
+
];
|
|
355
369
|
const external = [...builtinModules, ...serverModules];
|
|
356
370
|
if (ssr === 'server') {
|
|
357
371
|
rollupOptions = {
|
|
372
|
+
...rollupOptions,
|
|
358
373
|
input: [
|
|
359
374
|
new URL('ssr/entry-server.ts', frameworkDir).pathname,
|
|
360
375
|
new URL('ssr/prerender.ts', frameworkDir).pathname,
|
|
@@ -365,17 +380,16 @@ export const baseConfig = async ({ ssr, appDir, publicDir, command = 'build', mo
|
|
|
365
380
|
minifyInternalExports: false,
|
|
366
381
|
entryFileNames: '[name].mjs',
|
|
367
382
|
chunkFileNames: '[name].mjs',
|
|
368
|
-
|
|
369
|
-
manualChunks
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
}
|
|
383
|
+
format: 'es',
|
|
384
|
+
manualChunks
|
|
385
|
+
// manualChunks: (id) => {
|
|
386
|
+
// if (id.includes('vitrify/src/vite/')) {
|
|
387
|
+
// const name = id.split('/').at(-1)?.split('.').at(0)
|
|
388
|
+
// if (name && manualChunks.includes(name)) return name
|
|
389
|
+
// } else if (id.includes('node_modules')) {
|
|
390
|
+
// return 'vendor'
|
|
391
|
+
// }
|
|
392
|
+
// }
|
|
379
393
|
}
|
|
380
394
|
};
|
|
381
395
|
// Create a SSR bundle
|
|
@@ -386,23 +400,23 @@ export const baseConfig = async ({ ssr, appDir, publicDir, command = 'build', mo
|
|
|
386
400
|
}
|
|
387
401
|
else if (ssr === 'fastify') {
|
|
388
402
|
rollupOptions = {
|
|
403
|
+
...rollupOptions,
|
|
389
404
|
input: [new URL('server.ts', fastifyDir).pathname],
|
|
390
405
|
external,
|
|
391
406
|
output: {
|
|
392
407
|
minifyInternalExports: false,
|
|
393
408
|
entryFileNames: '[name].mjs',
|
|
394
409
|
chunkFileNames: '[name].mjs',
|
|
395
|
-
|
|
396
|
-
manualChunks
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
}
|
|
410
|
+
format: 'es',
|
|
411
|
+
manualChunks
|
|
412
|
+
// manualChunks: (id) => {
|
|
413
|
+
// if (id.includes('vitrify/src/vite/')) {
|
|
414
|
+
// const name = id.split('/').at(-1)?.split('.').at(0)
|
|
415
|
+
// if (name && manualChunks.includes(name)) return name
|
|
416
|
+
// } else if (id.includes('node_modules')) {
|
|
417
|
+
// return 'vendor'
|
|
418
|
+
// }
|
|
419
|
+
// }
|
|
406
420
|
}
|
|
407
421
|
};
|
|
408
422
|
// Create a SSR bundle
|
|
@@ -412,15 +426,25 @@ export const baseConfig = async ({ ssr, appDir, publicDir, command = 'build', mo
|
|
|
412
426
|
}
|
|
413
427
|
else {
|
|
414
428
|
rollupOptions = {
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
429
|
+
...rollupOptions,
|
|
430
|
+
// input: [
|
|
431
|
+
// new URL('index.html', frameworkDir).pathname
|
|
432
|
+
// // new URL('csr/server.ts', frameworkDir).pathname
|
|
433
|
+
// ],
|
|
434
|
+
external,
|
|
435
|
+
output: {
|
|
436
|
+
minifyInternalExports: false,
|
|
437
|
+
entryFileNames: '[name].mjs',
|
|
438
|
+
chunkFileNames: '[name].mjs',
|
|
439
|
+
format: 'es',
|
|
440
|
+
manualChunks
|
|
441
|
+
}
|
|
419
442
|
};
|
|
420
443
|
}
|
|
421
444
|
const config = {
|
|
422
445
|
root: ssr === 'fastify' ? appDir.pathname : frameworkDir.pathname,
|
|
423
446
|
publicDir: publicDir.pathname,
|
|
447
|
+
base,
|
|
424
448
|
envDir: appDir.pathname,
|
|
425
449
|
vitrify: {
|
|
426
450
|
productName,
|
|
@@ -437,11 +461,7 @@ export const baseConfig = async ({ ssr, appDir, publicDir, command = 'build', mo
|
|
|
437
461
|
exclude: ['vue', ...serverModules, ...builtinModules]
|
|
438
462
|
},
|
|
439
463
|
resolve: {
|
|
440
|
-
|
|
441
|
-
// dedupe: [
|
|
442
|
-
// 'vue',
|
|
443
|
-
// 'vue-router'
|
|
444
|
-
// ],
|
|
464
|
+
dedupe: ['vue', 'vue-router'],
|
|
445
465
|
alias
|
|
446
466
|
},
|
|
447
467
|
build: {
|
|
@@ -483,8 +503,9 @@ export const baseConfig = async ({ ssr, appDir, publicDir, command = 'build', mo
|
|
|
483
503
|
noExternal
|
|
484
504
|
},
|
|
485
505
|
define: {
|
|
486
|
-
__BASE_URL__: `'
|
|
506
|
+
__BASE_URL__: `'${base}'`
|
|
487
507
|
}
|
|
488
508
|
};
|
|
489
509
|
return mergeConfig(config, vitrifyConfig);
|
|
490
510
|
};
|
|
511
|
+
export const vitrifyDir = new URL('..', import.meta.url);
|