vitrify 0.2.5 → 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.
Files changed (54) hide show
  1. package/README.md +2 -2
  2. package/dist/app-urls.js +1 -2
  3. package/dist/bin/build.js +9 -51
  4. package/dist/bin/cli.js +31 -9
  5. package/dist/bin/dev.js +72 -70
  6. package/dist/frameworks/vue/fastify-csr-plugin.js +38 -0
  7. package/dist/frameworks/vue/fastify-ssr-plugin.js +83 -25
  8. package/dist/frameworks/vue/prerender.js +3 -3
  9. package/dist/frameworks/vue/server.js +10 -11
  10. package/dist/helpers/collect-css-ssr.js +61 -0
  11. package/dist/helpers/logger.js +0 -72
  12. package/dist/index.js +310 -130
  13. package/dist/plugins/quasar.js +34 -111
  14. package/dist/types/bin/build.d.ts +2 -2
  15. package/dist/types/bin/dev.d.ts +42 -4
  16. package/dist/types/frameworks/vue/fastify-csr-plugin.d.ts +17 -0
  17. package/dist/types/frameworks/vue/fastify-ssr-plugin.d.ts +6 -3
  18. package/dist/types/frameworks/vue/prerender.d.ts +3 -3
  19. package/dist/types/frameworks/vue/server.d.ts +10 -5
  20. package/dist/types/helpers/collect-css-ssr.d.ts +14 -0
  21. package/dist/types/helpers/logger.d.ts +0 -19
  22. package/dist/types/helpers/routes.d.ts +1 -1
  23. package/dist/types/index.d.ts +4 -2
  24. package/dist/types/plugins/index.d.ts +1 -1
  25. package/dist/types/vitrify-config.d.ts +33 -17
  26. package/package.json +33 -32
  27. package/src/node/app-urls.ts +1 -2
  28. package/src/node/bin/build.ts +11 -57
  29. package/src/node/bin/cli.ts +38 -10
  30. package/src/node/bin/dev.ts +106 -80
  31. package/src/node/bin/test.ts +0 -3
  32. package/src/node/frameworks/vue/fastify-csr-plugin.ts +72 -0
  33. package/src/node/frameworks/vue/fastify-ssr-plugin.ts +99 -28
  34. package/src/node/frameworks/vue/prerender.ts +5 -5
  35. package/src/node/frameworks/vue/server.ts +24 -17
  36. package/src/node/helpers/collect-css-ssr.ts +85 -0
  37. package/src/node/helpers/logger.ts +0 -87
  38. package/src/node/index.ts +353 -146
  39. package/src/node/plugins/index.ts +1 -1
  40. package/src/node/plugins/quasar.ts +39 -116
  41. package/src/node/vitrify-config.ts +44 -17
  42. package/src/vite/fastify/entry.ts +11 -0
  43. package/src/vite/fastify/server.ts +12 -0
  44. package/src/vite/vue/csr/app.ts +25 -0
  45. package/src/vite/vue/csr/fastify-csr-plugin.ts +3 -0
  46. package/src/vite/vue/csr/server.ts +8 -0
  47. package/src/vite/vue/index.html +1 -0
  48. package/src/vite/vue/main.ts +5 -20
  49. package/src/vite/vue/ssr/app.ts +25 -0
  50. package/src/vite/vue/ssr/entry-server.ts +13 -1
  51. package/src/vite/vue/ssr/fastify-ssr-plugin.ts +2 -118
  52. package/src/vite/vue/ssr/prerender.ts +2 -2
  53. package/src/vite/vue/ssr/server.ts +24 -15
  54. package/src/node/helpers/ssr.ts.bak +0 -52
package/src/node/index.ts CHANGED
@@ -1,8 +1,12 @@
1
1
  import vuePlugin from '@vitejs/plugin-vue'
2
- import type { InlineConfig, UserConfig } from 'vite'
2
+ import type { Alias, 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
- import { QuasarPlugin } from './plugins/quasar.js'
6
10
  import builtinModules from 'builtin-modules'
7
11
  import { resolve } from 'import-meta-resolve'
8
12
  import type {
@@ -10,43 +14,146 @@ import type {
10
14
  BootFunction,
11
15
  OnMountedHook,
12
16
  VitrifyConfig,
13
- SsrFunction
17
+ OnRenderedHook,
18
+ OnBootHook,
19
+ OnSetupFile
14
20
  } from './vitrify-config.js'
15
21
  import type { VitrifyContext } from './bin/run.js'
16
22
  import type { VitrifyPlugin } from './plugins/index.js'
17
- import type { FastifyInstance } from 'fastify'
18
23
  import { getPkgJsonDir } from './app-urls.js'
24
+ import type { RollupOptions } from 'rollup'
19
25
 
20
- const serverModules = ['fastify', 'middie']
26
+ const internalServerModules = [
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
+ 'node:process',
38
+ 'vitrify',
39
+ 'vitrify/dev',
40
+ // 'import-meta-resolve',
41
+ 'vite',
42
+ 'fastify',
43
+ '@fastify',
44
+ 'node'
45
+ // 'middie',
46
+ // 'knex',
47
+ // 'bcrypt',
48
+ // 'objection',
49
+ // '@fastify/formbody',
50
+ // '@fastify/static',
51
+ // '@fastify/cors',
52
+ // '@fastify/cookie',
53
+ // 'mercurius',
54
+ // 'jose',
55
+ // 'oidc-provider',
56
+ // 'node-fetch'
57
+ ]
21
58
 
22
59
  const configPluginMap: Record<string, () => Promise<VitrifyPlugin>> = {
23
60
  quasar: () =>
24
61
  import('./plugins/quasar.js').then((module) => module.QuasarPlugin)
25
62
  }
26
63
 
27
- const manualChunks = ['prerender', 'fastify-ssr-plugin', 'server']
64
+ const manualChunkNames = [
65
+ 'prerender',
66
+ 'fastify-ssr-plugin',
67
+ 'fastify-csr-plugin',
68
+ 'server'
69
+ ]
70
+ const manualChunks = (id: string) => {
71
+ if (id.includes('vitrify/src/vite/')) {
72
+ const name = id.split('/').at(-1)?.split('.').at(0)
73
+ if (name && manualChunkNames.includes(name)) return name
74
+ } else if (id.includes('node_modules')) {
75
+ return 'vendor'
76
+ }
77
+ }
28
78
 
29
79
  export const VIRTUAL_MODULES = [
30
- 'virtual:fastify-setup',
31
- 'virtual:boot-functions',
32
- 'virtual:ssr-functions',
33
- 'virtual:on-mounted-hooks',
80
+ 'virtual:vitrify-hooks',
34
81
  'virtual:global-css',
35
82
  'virtual:static-imports'
36
83
  ]
37
84
 
85
+ async function bundleConfigFile(
86
+ fileName: string,
87
+ isESM = false
88
+ ): Promise<{ code: string; dependencies: string[] }> {
89
+ const result = await build({
90
+ absWorkingDir: process.cwd(),
91
+ entryPoints: [fileName],
92
+ outfile: 'out.js',
93
+ write: false,
94
+ platform: 'node',
95
+ bundle: true,
96
+ format: 'esm',
97
+ sourcemap: 'inline',
98
+ metafile: true,
99
+ plugins: [
100
+ {
101
+ name: 'externalize-deps',
102
+ setup(build) {
103
+ build.onResolve({ filter: /.*/ }, (args) => {
104
+ const id = args.path
105
+ if (id[0] !== '.' && !path.isAbsolute(id)) {
106
+ return {
107
+ external: true
108
+ }
109
+ }
110
+ })
111
+ }
112
+ },
113
+ {
114
+ name: 'replace-import-meta',
115
+ setup(build) {
116
+ build.onLoad({ filter: /\.[jt]s$/ }, async (args) => {
117
+ const contents = await fs.promises.readFile(args.path, 'utf8')
118
+ return {
119
+ loader: args.path.endsWith('.ts') ? 'ts' : 'js',
120
+ contents: contents
121
+ .replace(
122
+ /\bimport\.meta\.url\b/g,
123
+ JSON.stringify(pathToFileURL(args.path).href)
124
+ )
125
+ .replace(
126
+ /\b__dirname\b/g,
127
+ JSON.stringify(path.dirname(args.path))
128
+ )
129
+ .replace(/\b__filename\b/g, JSON.stringify(args.path))
130
+ }
131
+ })
132
+ }
133
+ }
134
+ ]
135
+ })
136
+ const { text } = result.outputFiles[0]
137
+ return {
138
+ code: text,
139
+ dependencies: result.metafile ? Object.keys(result.metafile.inputs) : []
140
+ }
141
+ }
142
+
38
143
  export const baseConfig = async ({
39
144
  ssr,
40
145
  appDir,
41
146
  publicDir,
147
+ base = '/',
42
148
  command = 'build',
43
149
  mode = 'production',
44
150
  framework = 'vue',
45
151
  pwa = false
46
152
  }: {
47
- ssr?: 'client' | 'server' | 'ssg'
153
+ ssr?: 'client' | 'server' | 'ssg' | 'fastify'
48
154
  appDir?: URL
49
155
  publicDir?: URL
156
+ base?: string
50
157
  command?: 'build' | 'dev' | 'test'
51
158
  mode?: 'production' | 'development'
52
159
  framework?: 'vue'
@@ -61,43 +168,8 @@ export const baseConfig = async ({
61
168
  const cwd = getCwd()
62
169
  const cliDir = getCliDir()
63
170
  const cliViteDir = getCliViteDir(cliDir)
64
- // const {
65
- // appDir: tempAppDir,
66
- // cliDir,
67
- // cliViteDir,
68
- // srcDir
69
- // } = await import('./app-urls.js')
70
- // const cwd = appDir || tempAppDir
71
171
  const frameworkDir = new URL(`${framework}/`, cliViteDir)
72
-
73
- // const localPackages = ['vue', 'vue-router', 'quasar']
74
- const localPackages = ['vue', 'vue-router']
75
- const cliPackages = ['vitest']
76
- const packageUrls: Record<string, URL> = {}
77
- await (async () => {
78
- for (const val of localPackages)
79
- packageUrls[val] = getPkgJsonDir(
80
- new URL(await resolve(val, appDir!.href))
81
- )
82
- })()
83
- await (async () => {
84
- for (const val of cliPackages)
85
- packageUrls[val] = getPkgJsonDir(
86
- new URL(await resolve(val, cliDir!.href))
87
- )
88
- })()
89
-
90
- // if (appDir) {
91
- // srcDir = new URL('src/', appDir);
92
- // quasarDir = new URL(await resolve('quasar/', appDir.href));
93
- // ({ appDir: cwd, cliDir } = await import('./app-urls.js'))
94
- // } else {
95
- // ({ appDir, cliDir, srcDir, quasarDir } = await import('./app-urls.js'))
96
- // cwd = appDir
97
- // }
98
- // vueDir = new URL('./', await resolve('vue', appDir.href));
99
- // vueRouterDir = new URL('../', await resolve('vue-router', appDir.href));
100
- // vitestDir = new URL('../', await resolve('vitest', cliDir.href));
172
+ const fastifyDir = new URL('fastify/', cliViteDir)
101
173
 
102
174
  if (!publicDir) publicDir = new URL('public/', appDir)
103
175
  /**
@@ -105,27 +177,63 @@ export const baseConfig = async ({
105
177
  */
106
178
  let vitrifyConfig:
107
179
  | VitrifyConfig
108
- | (({ mode, command }: { mode: string; command: string }) => VitrifyConfig)
180
+ | (({
181
+ mode,
182
+ command
183
+ }: {
184
+ mode: string
185
+ command: string
186
+ }) => Promise<VitrifyConfig> | VitrifyConfig)
109
187
 
110
188
  try {
111
- vitrifyConfig = (
112
- await import(new URL('vitrify.config.js', appDir).pathname)
113
- ).default
189
+ if (fs.existsSync(new URL('vitrify.config.ts', appDir).pathname)) {
190
+ const configPath = new URL('vitrify.config.ts', appDir).pathname
191
+ const bundledConfig = await bundleConfigFile(
192
+ new URL('vitrify.config.ts', appDir).pathname
193
+ )
194
+ fs.writeFileSync(configPath + '.js', bundledConfig.code)
195
+ vitrifyConfig = (await import(configPath + '.js')).default
196
+ fs.unlinkSync(configPath + '.js')
197
+ } else {
198
+ vitrifyConfig = (
199
+ await import(new URL('vitrify.config.js', appDir).pathname)
200
+ ).default
201
+ }
114
202
  if (typeof vitrifyConfig === 'function')
115
- vitrifyConfig = vitrifyConfig({ mode, command })
203
+ vitrifyConfig = await vitrifyConfig({ mode, command })
116
204
  } catch (e) {
117
- console.error(e)
118
- console.log('No vitrify.config.js file found, using defaults')
205
+ console.log('No vitrify.config.(ts|js) file found, using defaults')
119
206
  vitrifyConfig = {}
120
207
  }
121
- let { productName = 'Product name' } = JSON.parse(
122
- readFileSync(new URL('package.json', appDir).pathname, {
123
- encoding: 'utf-8'
124
- })
125
- )
126
208
 
127
- const fastifySetup =
128
- vitrifyConfig.vitrify?.fastify?.setup || ((fastify: FastifyInstance) => {})
209
+ const localPackages = ['vue', 'vue-router']
210
+ const cliPackages = []
211
+ const packageUrls: Record<string, URL> =
212
+ vitrifyConfig.vitrify?.urls?.packages || {}
213
+ await (async () => {
214
+ for (const val of localPackages)
215
+ packageUrls[val] = getPkgJsonDir(
216
+ new URL(await resolve(val, appDir!.href))
217
+ )
218
+ })()
219
+
220
+ // await (async () => {
221
+ // for (const val of cliPackages)
222
+ // packageUrls[val] = getPkgJsonDir(
223
+ // new URL(await resolve(val, cliDir!.href))
224
+ // )
225
+ // })()
226
+
227
+ let productName = 'Product name'
228
+ try {
229
+ ;({ productName } = JSON.parse(
230
+ readFileSync(new URL('package.json', appDir).pathname, {
231
+ encoding: 'utf-8'
232
+ })
233
+ ))
234
+ } catch (e) {
235
+ console.error('package.json not found')
236
+ }
129
237
 
130
238
  const ssrTransformCustomDir = () => {
131
239
  return {
@@ -147,16 +255,25 @@ export const baseConfig = async ({
147
255
  }
148
256
  }
149
257
 
150
- let bootFunctions: BootFunction[]
151
- let ssrFunctions: SsrFunction[]
258
+ let onBootHooks: OnBootHook[]
259
+ let onRenderedHooks: OnRenderedHook[]
152
260
  let onMountedHooks: OnMountedHook[]
261
+ let onSetupFiles: OnSetupFile[]
153
262
  let globalCss: string[]
154
263
  let staticImports: StaticImports
155
264
  let sassVariables: Record<string, string>
156
265
  let additionalData: string[]
266
+ let serverModules: string[] = internalServerModules
267
+
268
+ if (vitrifyConfig.vitrify?.ssr?.serverModules)
269
+ serverModules = [
270
+ ...serverModules,
271
+ ...vitrifyConfig.vitrify.ssr.serverModules
272
+ ]
157
273
 
158
274
  const plugins: UserConfig['plugins'] = [
159
275
  vuePlugin({
276
+ compiler: await import('vue/compiler-sfc'),
160
277
  template: {
161
278
  ssr: !!ssr,
162
279
  compilerOptions: {
@@ -177,18 +294,14 @@ export const baseConfig = async ({
177
294
  }
178
295
  }),
179
296
  ...frameworkPlugins,
180
- // await QuasarPlugin({
181
- // ssr: ssr,
182
- // pwa: pwa
183
- // // quasarDir: packageUrls.quasar
184
- // }),
185
297
  {
186
298
  name: 'vitrify-setup',
187
299
  enforce: 'post',
188
300
  config: async (config: VitrifyConfig, env) => {
189
- bootFunctions = config.vitrify?.bootFunctions || []
190
- ssrFunctions = config.vitrify?.ssrFunctions || []
301
+ onBootHooks = config.vitrify?.hooks?.onBoot || []
302
+ onRenderedHooks = config.vitrify?.hooks?.onRendered || []
191
303
  onMountedHooks = config.vitrify?.hooks?.onMounted || []
304
+ onSetupFiles = config?.vitrify?.hooks?.onSetup || []
192
305
  globalCss = config.vitrify?.globalCss || []
193
306
  staticImports = config.vitrify?.staticImports || {}
194
307
  sassVariables = config.vitrify?.sass?.variables || {}
@@ -217,26 +330,49 @@ export const baseConfig = async ({
217
330
  }
218
331
  },
219
332
  resolveId(id) {
220
- if (VIRTUAL_MODULES.includes(id)) return id
333
+ if (VIRTUAL_MODULES.includes(id))
334
+ return { id, moduleSideEffects: false }
221
335
  return
222
336
  },
337
+ transform: (code, id) => {
338
+ if (id.endsWith('main.ts') && id.includes('vitrify')) {
339
+ code =
340
+ `${globalCss.map((css) => `import '${css}'`).join('\n')}\n` + code
341
+ }
342
+ return code
343
+ },
223
344
  load(id) {
224
- if (id === 'virtual:fastify-setup') {
225
- return `export const setup = ${String(fastifySetup)}`
226
- } else if (id === 'virtual:boot-functions') {
227
- return `export default [${bootFunctions
228
- .map((fn) => `${String(fn)}`)
229
- .join(', ')}]`
230
- } else if (id === 'virtual:ssr-functions') {
231
- return `export default [${ssrFunctions
345
+ if (id === 'virtual:vitrify-hooks') {
346
+ return `export const onBoot = [${onBootHooks
232
347
  .map((fn) => `${String(fn)}`)
233
- .join(', ')}]`
234
- } else if (id === 'virtual:on-mounted-hooks') {
235
- return `export default [${onMountedHooks
236
- .map((fn) => `${String(fn)}`)
237
- .join(', ')}]`
238
- } else if (id === 'virtual:global-css') {
239
- return `${globalCss.map((css) => `import '${css}'`).join('\n')}`
348
+ .join(', ')}]
349
+ export const onMounted = [${onMountedHooks
350
+ .map((fn) => `${String(fn)}`)
351
+ .join(', ')}]
352
+ export const onRendered = [${onRenderedHooks
353
+ .map((fn) => `${String(fn)}`)
354
+ .join(', ')}]
355
+ export const onSetup = []
356
+ ${onSetupFiles
357
+ .map(
358
+ (url, index) =>
359
+ `import ${url.pathname
360
+ .replaceAll('/', '')
361
+ .replaceAll('.', '')} from '${
362
+ url.pathname
363
+ }'; onSetup.push(${url.pathname
364
+ .replaceAll('/', '')
365
+ .replaceAll('.', '')})`
366
+ )
367
+ .join('\n')}`
368
+ // export const onSetup = [${onSetupHooks
369
+ // .map((fn) => `${String(fn)}`)
370
+ // .join(', ')}]`
371
+ /**
372
+ * CSS imports in virtual files do not seem to work. Using transform() instead
373
+ */
374
+ // } else if (id === 'virtual:global-css') {
375
+ // return `${globalCss.map((css) => `import '${css}'`).join('\n')}`
240
376
  } else if (id === 'virtual:static-imports') {
241
377
  return `${Object.entries(staticImports)
242
378
  .map(
@@ -272,6 +408,9 @@ export const baseConfig = async ({
272
408
  case 'client':
273
409
  entry = new URL('ssr/entry-client.ts', frameworkDir).pathname
274
410
  break
411
+ case 'fastify':
412
+ entry = new URL('entry.ts', fastifyDir).pathname
413
+ break
275
414
  default:
276
415
  entry = new URL('csr/entry.ts', frameworkDir).pathname
277
416
  }
@@ -301,32 +440,113 @@ export const baseConfig = async ({
301
440
  })
302
441
  }
303
442
 
304
- const alias = [
443
+ const alias: Alias[] = [
305
444
  { find: 'src', replacement: srcDir.pathname },
306
445
  { find: 'app', replacement: appDir.pathname },
307
446
  { find: 'cwd', replacement: cwd.pathname },
308
447
  { find: 'boot', replacement: new URL('boot/', srcDir).pathname },
309
448
  { find: 'assets', replacement: new URL('assets/', srcDir).pathname },
310
- // ...Object.entries(packageUrls).map(([key, value]) => ({
311
- // find: key, replacement: value.pathname
312
- // })),
313
- { find: 'vue', replacement: packageUrls['vue'].pathname },
314
- { find: 'vue-router', replacement: packageUrls['vue-router'].pathname },
315
- { find: 'vitrify', replacement: cliDir.pathname }
449
+ ...Object.entries(packageUrls).map(([key, value]) => ({
450
+ find: key,
451
+ replacement: value.pathname
452
+ }))
453
+ // { find: 'vue', replacement: packageUrls['vue'].pathname },
454
+ // { find: 'vue-router', replacement: packageUrls['vue-router'].pathname },
455
+ // { find: 'vitrify', replacement: cliDir.pathname }
316
456
  ]
457
+ if (mode === 'development' && vitrifyConfig.vitrify?.dev?.alias)
458
+ alias.push(...vitrifyConfig.vitrify.dev.alias)
459
+
317
460
  if (command === 'test')
318
461
  alias.push({
319
462
  find: 'vitest',
320
- replacement: packageUrls.vitest.pathname
463
+ replacement: new URL(await resolve('vitest', cliDir!.href)).pathname
321
464
  })
322
465
 
466
+ let rollupOptions: RollupOptions = {}
467
+ let noExternal: RegExp[] | string[] = [
468
+ new RegExp(`^(?!(${[...builtinModules, ...serverModules].join('|')}))`)
469
+ ]
470
+ const external = [...builtinModules, ...serverModules]
471
+
472
+ if (ssr === 'server') {
473
+ rollupOptions = {
474
+ input: [
475
+ new URL('ssr/entry-server.ts', frameworkDir).pathname,
476
+ new URL('ssr/prerender.ts', frameworkDir).pathname,
477
+ new URL('ssr/server.ts', frameworkDir).pathname
478
+ ],
479
+ external,
480
+ output: {
481
+ minifyInternalExports: false,
482
+ entryFileNames: '[name].mjs',
483
+ chunkFileNames: '[name].mjs',
484
+ format: 'es',
485
+ manualChunks
486
+ // manualChunks: (id) => {
487
+ // if (id.includes('vitrify/src/vite/')) {
488
+ // const name = id.split('/').at(-1)?.split('.').at(0)
489
+ // if (name && manualChunks.includes(name)) return name
490
+ // } else if (id.includes('node_modules')) {
491
+ // return 'vendor'
492
+ // }
493
+ // }
494
+ }
495
+ }
496
+ // Create a SSR bundle
497
+ noExternal = [
498
+ new RegExp(`^(?!(${[...builtinModules, ...serverModules].join('|')}))`)
499
+ // new RegExp(`^(?!.*(${[...builtinModules, ...serverModules].join('|')}))`)
500
+ ]
501
+ } else if (ssr === 'fastify') {
502
+ rollupOptions = {
503
+ input: [new URL('server.ts', fastifyDir).pathname],
504
+ external,
505
+ output: {
506
+ minifyInternalExports: false,
507
+ entryFileNames: '[name].mjs',
508
+ chunkFileNames: '[name].mjs',
509
+ format: 'es',
510
+ manualChunks
511
+ // manualChunks: (id) => {
512
+ // if (id.includes('vitrify/src/vite/')) {
513
+ // const name = id.split('/').at(-1)?.split('.').at(0)
514
+ // if (name && manualChunks.includes(name)) return name
515
+ // } else if (id.includes('node_modules')) {
516
+ // return 'vendor'
517
+ // }
518
+ // }
519
+ }
520
+ }
521
+ // Create a SSR bundle
522
+ noExternal = [
523
+ new RegExp(`^(?!(${[...builtinModules, ...serverModules].join('|')}))`)
524
+ ]
525
+ } else {
526
+ rollupOptions = {
527
+ input: [
528
+ new URL('index.html', frameworkDir).pathname
529
+ // new URL('csr/server.ts', frameworkDir).pathname
530
+ ],
531
+ external,
532
+ output: {
533
+ minifyInternalExports: false,
534
+ entryFileNames: '[name].mjs',
535
+ chunkFileNames: '[name].mjs',
536
+ format: 'es',
537
+ manualChunks
538
+ }
539
+ }
540
+ }
541
+
323
542
  const config = {
324
- root: frameworkDir.pathname,
543
+ root: ssr === 'fastify' ? appDir.pathname : frameworkDir.pathname,
325
544
  publicDir: publicDir.pathname,
545
+ base,
546
+ envDir: appDir.pathname,
326
547
  vitrify: {
327
548
  productName,
328
549
  urls: {
329
- // @ts-ignore
330
550
  app: appDir,
331
551
  cli: cliDir,
332
552
  src: srcDir,
@@ -336,71 +556,58 @@ export const baseConfig = async ({
336
556
  },
337
557
  plugins,
338
558
  optimizeDeps: {
339
- exclude: ['vue']
559
+ exclude: ['vue', ...serverModules, ...builtinModules]
340
560
  },
341
561
  resolve: {
342
- // Dedupe uses require which breaks ESM SSR builds
343
- // dedupe: [
344
- // 'vue',
345
- // 'vue-router'
346
- // ],
562
+ dedupe: ['vue', 'vue-router'],
347
563
  alias
348
564
  },
349
565
  build: {
350
- target: ssr === 'server' ? 'esnext' : 'modules',
351
- ssr: ssr === 'server' ? true : false,
566
+ target: ssr === 'server' || ssr === 'fastify' ? 'esnext' : 'modules',
567
+ ssr: ssr === 'server' || ssr === 'fastify' ? true : false,
352
568
  ssrManifest: ssr === 'client' || ssr === 'ssg',
353
- rollupOptions:
354
- ssr === 'server'
355
- ? {
356
- input: [
357
- new URL('ssr/entry-server.ts', frameworkDir).pathname,
358
- new URL('ssr/prerender.ts', frameworkDir).pathname,
359
- new URL('ssr/server.ts', frameworkDir).pathname
360
- ],
361
- output: {
362
- minifyInternalExports: false,
363
- entryFileNames: '[name].mjs',
364
- chunkFileNames: '[name].mjs',
365
- format: 'es',
366
- manualChunks: (id) => {
367
- if (id.includes('vitrify/src/vite/')) {
368
- const name = id.split('/').at(-1)?.split('.').at(0)
369
- console.log(name)
370
- if (manualChunks.includes(id)) return name
371
- } else if (id.includes('node_modules')) {
372
- return 'vendor'
373
- }
374
- }
375
- }
376
- }
377
- : {
378
- output: {
379
- format: 'es'
380
- }
381
- }
569
+ rollupOptions
570
+ // ssr === 'server'
571
+ // ? {
572
+ // input: [
573
+ // new URL('ssr/entry-server.ts', frameworkDir).pathname,
574
+ // new URL('ssr/prerender.ts', frameworkDir).pathname,
575
+ // new URL('ssr/server.ts', frameworkDir).pathname
576
+ // ],
577
+ // output: {
578
+ // minifyInternalExports: false,
579
+ // entryFileNames: '[name].mjs',
580
+ // chunkFileNames: '[name].mjs',
581
+ // format: 'es',
582
+ // manualChunks: (id) => {
583
+ // if (id.includes('vitrify/src/vite/')) {
584
+ // const name = id.split('/').at(-1)?.split('.').at(0)
585
+ // if (name && manualChunks.includes(name)) return name
586
+ // } else if (id.includes('node_modules')) {
587
+ // return 'vendor'
588
+ // }
589
+ // }
590
+ // }
591
+ // }
592
+ // : {
593
+ // output: {
594
+ // format: 'es'
595
+ // }
596
+ // }
382
597
  },
383
- // css: {
384
- // preprocessorOptions: {
385
- // sass: {
386
- // additionalData: sass ? [...sass].join('\n') : undefined
387
- // }
388
- // }
389
- // },
390
598
  ssr: {
391
599
  // Create a SSR bundle
392
- noExternal: [
393
- new RegExp(
394
- `^(?!.*(${[...builtinModules, ...serverModules].join('|')}))`
395
- )
396
- ]
600
+ external,
601
+ noExternal
397
602
  },
398
603
  define: {
399
- __BASE_URL__: `'/'`
604
+ __BASE_URL__: `'${base}'`
400
605
  }
401
606
  } as VitrifyConfig
402
607
 
403
608
  return mergeConfig(config, vitrifyConfig)
404
609
  }
405
610
 
611
+ export const vitrifyDir = new URL('..', import.meta.url)
612
+
406
613
  export type { VitrifyConfig, VitrifyPlugin, VitrifyContext, BootFunction }
@@ -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'