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.
package/src/node/index.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import vuePlugin from '@vitejs/plugin-vue'
2
- import type { InlineConfig, UserConfig } from 'vite'
2
+ import type { Alias, InlineConfig, UserConfig } from 'vite'
3
3
  import { resolveConfig } from 'vite'
4
4
  import { mergeConfig } from 'vite'
5
5
  import { build } from 'esbuild'
@@ -23,7 +23,7 @@ import type { VitrifyPlugin } from './plugins/index.js'
23
23
  import { getPkgJsonDir } from './app-urls.js'
24
24
  import type { RollupOptions } from 'rollup'
25
25
 
26
- const serverModules = [
26
+ const internalServerModules = [
27
27
  // 'fs',
28
28
  // 'path',
29
29
  // 'url',
@@ -34,20 +34,26 @@ const serverModules = [
34
34
  'node:url',
35
35
  'node:util',
36
36
  'node:fs',
37
+ 'node:process',
37
38
  'vitrify',
39
+ 'vitrify/dev',
40
+ // 'import-meta-resolve',
38
41
  'vite',
39
42
  'fastify',
40
- 'middie',
41
- 'knex',
42
- 'bcrypt',
43
- 'objection',
44
- '@fastify/formbody',
45
- '@fastify/static',
46
- '@fastify/cors',
47
- '@fastify/cookie',
48
- 'mercurius',
49
- 'jose',
50
- 'oidc-provider'
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'
51
57
  ]
52
58
 
53
59
  const configPluginMap: Record<string, () => Promise<VitrifyPlugin>> = {
@@ -55,7 +61,20 @@ const configPluginMap: Record<string, () => Promise<VitrifyPlugin>> = {
55
61
  import('./plugins/quasar.js').then((module) => module.QuasarPlugin)
56
62
  }
57
63
 
58
- 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
+ }
59
78
 
60
79
  export const VIRTUAL_MODULES = [
61
80
  'virtual:vitrify-hooks',
@@ -125,6 +144,7 @@ export const baseConfig = async ({
125
144
  ssr,
126
145
  appDir,
127
146
  publicDir,
147
+ base = '/',
128
148
  command = 'build',
129
149
  mode = 'production',
130
150
  framework = 'vue',
@@ -133,6 +153,7 @@ export const baseConfig = async ({
133
153
  ssr?: 'client' | 'server' | 'ssg' | 'fastify'
134
154
  appDir?: URL
135
155
  publicDir?: URL
156
+ base?: string
136
157
  command?: 'build' | 'dev' | 'test'
137
158
  mode?: 'production' | 'development'
138
159
  framework?: 'vue'
@@ -172,7 +193,7 @@ export const baseConfig = async ({
172
193
  )
173
194
  fs.writeFileSync(configPath + '.js', bundledConfig.code)
174
195
  vitrifyConfig = (await import(configPath + '.js')).default
175
- // fs.unlinkSync(configPath + '.js')
196
+ fs.unlinkSync(configPath + '.js')
176
197
  } else {
177
198
  vitrifyConfig = (
178
199
  await import(new URL('vitrify.config.js', appDir).pathname)
@@ -195,6 +216,7 @@ export const baseConfig = async ({
195
216
  new URL(await resolve(val, appDir!.href))
196
217
  )
197
218
  })()
219
+
198
220
  // await (async () => {
199
221
  // for (const val of cliPackages)
200
222
  // packageUrls[val] = getPkgJsonDir(
@@ -241,6 +263,13 @@ export const baseConfig = async ({
241
263
  let staticImports: StaticImports
242
264
  let sassVariables: Record<string, string>
243
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
+ ]
244
273
 
245
274
  const plugins: UserConfig['plugins'] = [
246
275
  vuePlugin({
@@ -411,7 +440,7 @@ export const baseConfig = async ({
411
440
  })
412
441
  }
413
442
 
414
- const alias = [
443
+ const alias: Alias[] = [
415
444
  { find: 'src', replacement: srcDir.pathname },
416
445
  { find: 'app', replacement: appDir.pathname },
417
446
  { find: 'cwd', replacement: cwd.pathname },
@@ -425,15 +454,21 @@ export const baseConfig = async ({
425
454
  // { find: 'vue-router', replacement: packageUrls['vue-router'].pathname },
426
455
  // { find: 'vitrify', replacement: cliDir.pathname }
427
456
  ]
457
+ if (mode === 'development' && vitrifyConfig.vitrify?.dev?.alias)
458
+ alias.push(...vitrifyConfig.vitrify.dev.alias)
459
+
428
460
  if (command === 'test')
429
461
  alias.push({
430
462
  find: 'vitest',
431
463
  replacement: new URL(await resolve('vitest', cliDir!.href)).pathname
432
464
  })
433
465
 
434
- let rollupOptions: RollupOptions
435
- let noExternal: RegExp[] | string[] = []
466
+ let rollupOptions: RollupOptions = {}
467
+ let noExternal: RegExp[] | string[] = [
468
+ new RegExp(`^(?!(${[...builtinModules, ...serverModules].join('|')}))`)
469
+ ]
436
470
  const external = [...builtinModules, ...serverModules]
471
+
437
472
  if (ssr === 'server') {
438
473
  rollupOptions = {
439
474
  input: [
@@ -446,15 +481,16 @@ export const baseConfig = async ({
446
481
  minifyInternalExports: false,
447
482
  entryFileNames: '[name].mjs',
448
483
  chunkFileNames: '[name].mjs',
449
- // format: 'es',
450
- manualChunks: (id) => {
451
- if (id.includes('vitrify/src/vite/')) {
452
- const name = id.split('/').at(-1)?.split('.').at(0)
453
- if (name && manualChunks.includes(name)) return name
454
- } else if (id.includes('node_modules')) {
455
- return 'vendor'
456
- }
457
- }
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
+ // }
458
494
  }
459
495
  }
460
496
  // Create a SSR bundle
@@ -470,15 +506,16 @@ export const baseConfig = async ({
470
506
  minifyInternalExports: false,
471
507
  entryFileNames: '[name].mjs',
472
508
  chunkFileNames: '[name].mjs',
473
- // format: 'es',
474
- manualChunks: (id) => {
475
- if (id.includes('vitrify/src/vite/')) {
476
- const name = id.split('/').at(-1)?.split('.').at(0)
477
- if (name && manualChunks.includes(name)) return name
478
- } else if (id.includes('node_modules')) {
479
- return 'vendor'
480
- }
481
- }
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
+ // }
482
519
  }
483
520
  }
484
521
  // Create a SSR bundle
@@ -487,16 +524,25 @@ export const baseConfig = async ({
487
524
  ]
488
525
  } else {
489
526
  rollupOptions = {
490
- // input: [new URL('index.html', frameworkDir).pathname],
491
- // output: {
492
- // format: 'es'
493
- // }
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
+ }
494
539
  }
495
540
  }
496
541
 
497
542
  const config = {
498
543
  root: ssr === 'fastify' ? appDir.pathname : frameworkDir.pathname,
499
544
  publicDir: publicDir.pathname,
545
+ base,
500
546
  envDir: appDir.pathname,
501
547
  vitrify: {
502
548
  productName,
@@ -513,11 +559,7 @@ export const baseConfig = async ({
513
559
  exclude: ['vue', ...serverModules, ...builtinModules]
514
560
  },
515
561
  resolve: {
516
- // Dedupe uses require which breaks ESM SSR builds
517
- // dedupe: [
518
- // 'vue',
519
- // 'vue-router'
520
- // ],
562
+ dedupe: ['vue', 'vue-router'],
521
563
  alias
522
564
  },
523
565
  build: {
@@ -559,11 +601,13 @@ export const baseConfig = async ({
559
601
  noExternal
560
602
  },
561
603
  define: {
562
- __BASE_URL__: `'/'`
604
+ __BASE_URL__: `'${base}'`
563
605
  }
564
606
  } as VitrifyConfig
565
607
 
566
608
  return mergeConfig(config, vitrifyConfig)
567
609
  }
568
610
 
611
+ export const vitrifyDir = new URL('..', import.meta.url)
612
+
569
613
  export type { VitrifyConfig, VitrifyPlugin, VitrifyContext, BootFunction }
@@ -85,6 +85,23 @@ export const QuasarPlugin: VitrifyPlugin = async ({
85
85
  Components({
86
86
  resolvers: [QuasarResolver()]
87
87
  }),
88
+ {
89
+ name: 'vite-plugin-quasar-transform',
90
+ enforce: 'pre',
91
+ transform: (code, id, options) => {
92
+ const { ssr } = options || {}
93
+ code = code
94
+ .replaceAll('__QUASAR_SSR__', ssr ? ssr.toString() : 'false')
95
+ .replaceAll('__QUASAR_SSR_SERVER__', 'import.meta.env.SSR')
96
+ .replaceAll('__QUASAR_SSR_CLIENT__', '!import.meta.env.SSR')
97
+ .replaceAll(
98
+ '__QUASAR_SSR_PWA__',
99
+ pwa ? '!import.meta.env.SSR' : 'false'
100
+ )
101
+
102
+ return code
103
+ }
104
+ },
88
105
  {
89
106
  name: 'vite-plugin-quasar-setup',
90
107
  enforce: 'pre',
@@ -238,11 +255,14 @@ export const QuasarPlugin: VitrifyPlugin = async ({
238
255
  },
239
256
  define: {
240
257
  __DEV__: process.env.NODE_ENV !== 'production' || true,
241
- __QUASAR_VERSION__: `'${version}'`,
242
- __QUASAR_SSR__: !!ssr,
243
- __QUASAR_SSR_SERVER__: ssr === 'server',
244
- __QUASAR_SSR_CLIENT__: ssr === 'client',
245
- __QUASAR_SSR_PWA__: ssr === 'client' && pwa
258
+ __QUASAR_VERSION__: `'${version}'`
259
+ // __QUASAR_SSR__: !!ssr,
260
+ // // __QUASAR_SSR_SERVER__: ssr === 'server',
261
+ // __QUASAR_SSR_SERVER__: `import.meta.env.SSR`,
262
+ // // __QUASAR_SSR_CLIENT__: ssr === 'client',
263
+ // __QUASAR_SSR_CLIENT__: `!import.meta.env.SSR`,
264
+ // // __QUASAR_SSR_PWA__: ssr === 'client' && pwa
265
+ // __QUASAR_SSR_PWA__: pwa ? `!import.meta.env.SSR` : false
246
266
  },
247
267
  ssr: {
248
268
  noExternal: ['quasar']
@@ -1,5 +1,5 @@
1
1
  import type { FastifyInstance } from 'fastify'
2
- import type { UserConfig } from 'vite'
2
+ import type { Alias, UserConfig } from 'vite'
3
3
  import type { QuasarConf } from './plugins/quasar.js'
4
4
  import type { ComponentInternalInstance } from '@vue/runtime-core'
5
5
 
@@ -87,6 +87,18 @@ export interface VitrifyConfig extends UserConfig {
87
87
  cwd?: URL
88
88
  packages?: Record<string, URL>
89
89
  }
90
+ /**
91
+ * SSR specific configuration
92
+ */
93
+ ssr?: {
94
+ serverModules?: string[]
95
+ }
96
+ /**
97
+ * Development only configuration
98
+ */
99
+ dev?: {
100
+ alias?: Alias[]
101
+ }
90
102
  }
91
103
  quasar?: QuasarConf
92
104
  }
@@ -1,7 +1,9 @@
1
1
  import Fastify from 'fastify'
2
2
  import { setup } from './entry'
3
3
 
4
- const fastify = Fastify()
4
+ const fastify = Fastify({
5
+ logger: true
6
+ })
5
7
  await setup({ fastify })
6
8
 
7
9
  fastify.listen({
@@ -0,0 +1,25 @@
1
+ import { createApp } from '../../../node/frameworks/vue/server.js'
2
+ import { getAppDir } from '../../../node/app-urls.js'
3
+ // import { setup } from 'virtual:fastify-setup'
4
+ import { onRendered, onSetup } from 'virtual:vitrify-hooks'
5
+ import { fastifyCsrPlugin } from './fastify-csr-plugin.js'
6
+ import type { ViteDevServer } from 'vite'
7
+ import * as imr from 'import-meta-resolve'
8
+ const { resolve } = imr
9
+ // const appDir = getPkgJsonDir(import.meta.url)
10
+ const getString = (str?: string) => str
11
+ let baseUrl = getString(__BASE_URL__)
12
+ const appDir = getAppDir()
13
+
14
+ export const setupApp = async () => {
15
+ const vitrifyDir = new URL('../', await resolve('vitrify', import.meta.url))
16
+ return createApp({
17
+ onSetup,
18
+ appDir,
19
+ baseUrl,
20
+ onRendered,
21
+ fastifyPlugin: fastifyCsrPlugin,
22
+ vitrifyDir,
23
+ mode: import.meta.env.MODE
24
+ })
25
+ }
@@ -0,0 +1,3 @@
1
+ import { fastifyCsrPlugin } from '../../../node/frameworks/vue/fastify-csr-plugin.js'
2
+
3
+ export { fastifyCsrPlugin }
@@ -0,0 +1,8 @@
1
+ import { setupApp } from './app.js'
2
+
3
+ const app = await setupApp()
4
+
5
+ app.listen({
6
+ port: Number(process.env.PORT || 3000),
7
+ host: process.env.HOST || '127.0.0.1'
8
+ })
@@ -18,7 +18,7 @@ export const setupApp = async () => {
18
18
  appDir,
19
19
  baseUrl,
20
20
  onRendered,
21
- fastifySsrPlugin,
21
+ fastifyPlugin: fastifySsrPlugin,
22
22
  vitrifyDir,
23
23
  mode: import.meta.env.MODE
24
24
  })