Skip to content

Commit 07bbb25

Browse files
authored
release: @fastify/[email protected] (#246)
* release: @fastify/[email protected] * chore: pkg updates * fix * fix ts * fix * bump: @fastify/[email protected] * prep for release * chore: update pnpm-lock.yaml
1 parent c312e57 commit 07bbb25

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+2048
-192
lines changed

packages/fastify-vue/client.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export function useRouteContext () {
1313
return useRoute().meta[serverRouteContext]
1414
}
1515

16-
export function createBeforeEachHandler ({ routeMap, ctxHydration, head }, layout) {
16+
export function createBeforeEachHandler ({ routeMap, ctxHydration }, layout) {
1717
return async function beforeCreate (to) {
1818
// The client-side route context
1919
const ctx = routeMap[to.matched[0].path]
@@ -48,7 +48,8 @@ export function createBeforeEachHandler ({ routeMap, ctxHydration, head }, layou
4848
// memoized module, so there's barely any overhead
4949
const { getMeta, onEnter } = await ctx.loader()
5050
if (ctx.getMeta) {
51-
head.update(await getMeta(ctx))
51+
ctx.head = await getMeta(ctx)
52+
ctxHydration.useHead.push(ctx.head)
5253
}
5354
if (ctx.onEnter) {
5455
const updatedData = await onEnter(ctx)

packages/fastify-vue/context.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ export default class RouteContext {
5050
return {
5151
state: this.state,
5252
data: this.data,
53+
head: this.head,
5354
layout: this.layout,
5455
getMeta: this.getMeta,
5556
getData: this.getData,

packages/fastify-vue/package.json

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"main": "index.js",
44
"name": "@fastify/vue",
55
"description": "The official @fastify/vite renderer for Vue",
6-
"version": "1.0.6",
6+
"version": "1.1.0",
77
"files": [
88
"plugin/index.js",
99
"plugin/parsers.js",
@@ -20,6 +20,16 @@
2020
"virtual/root.vue",
2121
"virtual/router.vue",
2222
"virtual/routes.js",
23+
"virtual-ts/layouts/default.vue",
24+
"virtual-ts/context.ts",
25+
"virtual-ts/create.ts",
26+
"virtual-ts/hooks.ts",
27+
"virtual-ts/index.ts",
28+
"virtual-ts/layout.vue",
29+
"virtual-ts/mount.ts",
30+
"virtual-ts/root.vue",
31+
"virtual-ts/router.vue",
32+
"virtual-ts/routes.ts",
2333
"index.js",
2434
"rendering.js",
2535
"templating.js",
@@ -37,18 +47,19 @@
3747
},
3848
"dependencies": {
3949
"@fastify/vite": "workspace:^",
50+
"@unhead/vue": "^2.0.5",
4051
"acorn": "^8.12.1",
4152
"acorn-walk": "^8.3.4",
4253
"devalue": "latest",
4354
"html-rewriter-wasm": "^0.4.1",
4455
"mlly": "^1.5.0",
45-
"unihead": "^0.8.0",
4656
"vue": "^3.5.8",
4757
"vue-router": "^4.4.5",
4858
"youch": "^3.3.4"
4959
},
5060
"devDependencies": {
51-
"@vitejs/plugin-vue": "latest"
61+
"@vitejs/plugin-vue": "latest",
62+
"oxlint": "^0.16.8"
5263
},
5364
"optionalDependencies": {
5465
"vite": "^6.2.4"

packages/fastify-vue/plugin/index.js

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,15 @@ import { closeBundle } from './preload.js'
1212
import { parseStateKeys } from './parsers.js'
1313
import { generateStores } from './stores.js'
1414

15-
export default function viteFastifyVue () {
15+
export default function viteFastifyVue ({ ts } = {}) {
1616
const context = {
1717
root: null,
1818
}
1919
return [viteFastify({
20-
clientModule: '$app/index.js'
20+
clientModule: ts ? '$app/index.ts' : '$app/index.js'
2121
}), {
22-
name: 'vite-plugin-fastify-vue',
22+
// https://vite.dev/guide/api-plugin#conventions
23+
name: 'vite-plugin-react-vue',
2324
config,
2425
configResolved: configResolved.bind(context),
2526
resolveId: resolveId.bind(context),
@@ -36,14 +37,18 @@ export default function viteFastifyVue () {
3637
const [, virtual] = id.split(prefix)
3738
if (virtual) {
3839
if (virtual === 'stores') {
39-
const contextPath = join(context.root, 'context.js')
40-
if (existsSync(contextPath)) {
41-
const keys = parseStateKeys(readFileSync(contextPath, 'utf8'))
42-
return generateStores(keys)
40+
for (const contextPath of [
41+
join(context.root, 'context.js'),
42+
join(context.root, 'context.ts')
43+
]) {
44+
if (existsSync(contextPath)) {
45+
const keys = parseStateKeys(readFileSync(contextPath, 'utf8'))
46+
return generateStores(keys)
47+
}
4348
}
4449
return
4550
}
46-
return loadVirtualModule(virtual)
51+
return loadVirtualModule(virtual, { ts })
4752
}
4853
}
4954
},
@@ -53,7 +58,9 @@ export default function viteFastifyVue () {
5358
},
5459
closeBundle: {
5560
order: 'post',
56-
handler: closeBundle.bind(context),
61+
handler () {
62+
closeBundle.call(this, context.resolvedBundle)
63+
}
5764
},
5865
}]
5966
}

packages/fastify-vue/plugin/preload.js

Lines changed: 50 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,57 @@
1-
import { writeFileSync, mkdirSync, existsSync, readFileSync } from 'node:fs'
2-
import { join, parse as parsePath } from 'node:path'
1+
import { readFileSync, writeFileSync, mkdirSync, existsSync } from 'node:fs'
2+
import { join, isAbsolute, parse as parsePath } from 'node:path'
33
import { HTMLRewriter } from 'html-rewriter-wasm'
44

5-
const imageFile = /\.((png)|(jpg)|(svg)|(webp)|(gif))$/
5+
const imageFileRE = /\.((png)|(jpg)|(svg)|(webp)|(gif))$/
66

7-
export async function closeBundle() {
8-
if (!this.resolvedConfig.build.ssr) {
9-
const distDir = join(this.root, this.resolvedConfig.build.outDir)
10-
const indexHtml = readFileSync(join(distDir, 'index.html'), 'utf8')
11-
const pages = Object.fromEntries(
12-
Object.entries(this.resolvedBundle ?? {})
13-
.filter(([id, meta]) => {
14-
if (meta.facadeModuleId?.includes('/pages/')) {
15-
meta.htmlPath = meta.facadeModuleId.replace(/.*pages\/(.*)\.vue$/, 'html/$1.html')
16-
return true
17-
}
18-
})
19-
)
20-
for (const page of Object.values(pages)) {
21-
const jsImports = page.imports
22-
const cssImports = page.viteMetadata.importedCss
23-
const images = page.moduleIds.filter(_ => imageFile.test(_))
24-
let imagePreloads = '\n'
25-
for (let image of images) {
26-
image = image.slice(this.root.length + 1)
27-
imagePreloads += ` <link rel="preload" as="image" crossorigin href="${this.resolvedConfig.base}${image}">\n`
28-
}
29-
let cssPreloads = ''
30-
for (const css of cssImports) {
31-
cssPreloads += ` <link rel="preload" as="style" crossorigin href="${this.resolvedConfig.base}${css}">\n`
32-
}
33-
let jsPreloads = ''
34-
for (const js of jsImports) {
35-
jsPreloads += ` <link rel="modulepreload" crossorigin href="${this.resolvedConfig.base}${js}">\n`
36-
}
37-
const pageHtml = await appendHead(
38-
indexHtml,
39-
imagePreloads,
40-
cssPreloads,
41-
jsPreloads
42-
)
43-
writeHtml(page, pageHtml, distDir)
7+
export async function closeBundle(resolvedBundle) {
8+
if (this.environment.name !== 'client') {
9+
return
10+
}
11+
const { assetsInlineLimit } = this.environment.config.build
12+
const { root, base } = this.environment.config
13+
let distDir
14+
if (isAbsolute(this.environment.config.build.outDir)) {
15+
distDir = this.environment.config.build.outDir
16+
} else {
17+
distDir = join(root, this.environment.config.build.outDir)
18+
}
19+
const indexHtml = readFileSync(join(distDir, 'index.html'), 'utf8')
20+
const pages = Object.fromEntries(
21+
Object.entries(resolvedBundle ?? {})
22+
.filter(([id, meta]) => {
23+
if (meta.facadeModuleId?.includes('/pages/')) {
24+
meta.htmlPath = meta.facadeModuleId.replace(/.*pages\/(.*)\.vue$/, 'html/$1.html')
25+
return true
26+
}
27+
})
28+
)
29+
for (const page of Object.values(pages)) {
30+
const jsImports = page.imports
31+
const cssImports = page.viteMetadata.importedCss
32+
const images = page.moduleIds.filter((img) => {
33+
return (page.modules[img].originalLength > assetsInlineLimit) && imageFileRE.test(img)
34+
})
35+
let imagePreloads = '\n'
36+
for (let image of images) {
37+
image = image.slice(root.length + 1)
38+
imagePreloads += ` <link rel="preload" as="image" crossorigin href="${base}${image}">\n`
4439
}
40+
let cssPreloads = ''
41+
for (const css of cssImports) {
42+
cssPreloads += ` <link rel="preload" as="style" crossorigin href="${base}${css}">\n`
43+
}
44+
let jsPreloads = ''
45+
for (const js of jsImports) {
46+
jsPreloads += ` <link rel="modulepreload" crossorigin href="${base}${js}">\n`
47+
}
48+
const pageHtml = await appendHead(
49+
indexHtml,
50+
imagePreloads,
51+
cssPreloads,
52+
jsPreloads
53+
)
54+
writeHtml(page, pageHtml, distDir)
4555
}
4656
}
4757

packages/fastify-vue/plugin/virtual.js

Lines changed: 44 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,31 @@ import { findExports } from 'mlly'
66
const __dirname = dirname(fileURLToPath(import.meta.url))
77

88
const virtualRoot = resolve(__dirname, '..', 'virtual')
9+
const virtualRootTS = resolve(__dirname, '..', 'virtual-ts')
910

1011
const virtualModules = [
1112
'mount.js',
12-
'mount.ts',
1313
'routes.js',
1414
'router.vue',
15+
'layouts/',
1516
'layout.vue',
1617
'create.js',
17-
'create.ts',
1818
'root.vue',
19-
'layouts/',
2019
'context.js',
21-
'context.ts',
2220
'index.js',
21+
'stores',
22+
'hooks'
23+
]
24+
25+
const virtualModulesTS = [
26+
'mount.ts',
27+
'routes.ts',
28+
'router.vue',
29+
'layouts/',
30+
'layout.vue',
31+
'create.ts',
32+
'root.vue',
33+
'context.ts',
2334
'index.ts',
2435
'stores',
2536
'hooks'
@@ -45,21 +56,36 @@ export async function resolveId (id) {
4556
}
4657
}
4758

48-
export function loadVirtualModule (virtualInput) {
59+
export function loadVirtualModule (virtualInput, options) {
4960
let virtual = virtualInput
50-
if (!/\.((mc)?ts)|((mc)?js)|(vue)$/.test(virtual)) {
51-
virtual += '.js'
61+
if (!virtual.endsWith('.vue') && !virtual.match(/\.(ts|js)$/)) {
62+
virtual += options.ts ? '.ts' : '.js'
5263
}
53-
if (!virtualModules.includes(virtual)) {
64+
if (!virtualModules.includes(virtual) && !virtualModulesTS.includes(virtual)) {
5465
return
5566
}
56-
const code = readFileSync(resolve(virtualRoot, virtual), 'utf8')
67+
let virtualRootDir = options.ts ? virtualRootTS : virtualRoot
68+
const codePath = resolve(virtualRootDir, virtual)
5769
return {
58-
code,
70+
code: readFileSync(codePath, 'utf8'),
5971
map: null,
6072
}
6173
}
6274

75+
76+
virtualModulesTS.includes = function (virtual) {
77+
if (!virtual) {
78+
return false
79+
}
80+
for (const entry of this) {
81+
if (virtual.startsWith(entry)) {
82+
return true
83+
}
84+
}
85+
return false
86+
}
87+
88+
6389
virtualModules.includes = function (virtual) {
6490
if (!virtual) {
6591
return false
@@ -72,11 +98,16 @@ virtualModules.includes = function (virtual) {
7298
return false
7399
}
74100

75-
function loadVirtualModuleOverride (viteProjectRoot, virtual) {
76-
if (!virtualModules.includes(virtual)) {
101+
function loadVirtualModuleOverride (viteProjectRoot, virtualInput) {
102+
let virtual = virtualInput
103+
if (!virtualModules.includes(virtual) && !virtualModulesTS.includes(virtual)) {
77104
return
78105
}
79-
const overridePath = resolve(viteProjectRoot, virtual)
106+
let overridePath = resolve(viteProjectRoot, virtual)
107+
if (existsSync(overridePath)) {
108+
return overridePath
109+
}
110+
overridePath = overridePath.replace('.js', '.ts')
80111
if (existsSync(overridePath)) {
81112
return overridePath
82113
}

0 commit comments

Comments
 (0)