Skip to content

Commit 7e41381

Browse files
committedOct 28, 2024
feat: switch critters to beasties, upgrade jsdom
1 parent 26e1e07 commit 7e41381

File tree

10 files changed

+2532
-1961
lines changed

10 files changed

+2532
-1961
lines changed
 

‎README.md

+5-5
Original file line numberDiff line numberDiff line change
@@ -103,25 +103,25 @@ See [`@unhead/vue`'s docs](https://unhead.unjs.io/setup/vue/installation) for mo
103103

104104
## Critical CSS
105105

106-
Vite SSG has built-in support for generating [Critical CSS](https://web.dev/extract-critical-css/) inlined in the HTML via the [`critters`](https://github.com/GoogleChromeLabs/critters) package.
106+
Vite SSG has built-in support for generating [Critical CSS](https://web.dev/extract-critical-css/) inlined in the HTML via the [`beasties`](https://github.com/danielroe/beasties) package.
107107
Install it with:
108108

109109
```bash
110-
npm i -D critters
110+
npm i -D beasties
111111
```
112112

113113
Critical CSS generation will automatically be enabled for you.
114114

115-
To configure `critters`, pass [its options](https://github.com/GoogleChromeLabs/critters#usage) into `ssgOptions.crittersOptions` in `vite.config.ts`:
115+
To configure `beasties`, pass [its options](https://github.com/danielroe/beasties#usage) into `ssgOptions.beastiesOptions` in `vite.config.ts`:
116116

117117
```ts
118118
// vite.config.ts
119119
export default defineConfig({
120120
ssgOptions: {
121-
crittersOptions: {
121+
beastiesOptions: {
122122
// E.g., change the preload strategy
123123
preload: 'media',
124-
// Other options: https://github.com/GoogleChromeLabs/critters#usage
124+
// Other options: https://github.com/danielroe/beasties#usage
125125
},
126126
},
127127
})

‎examples/multiple-pages-pwa/package.json

+7-7
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,18 @@
77
"serve": "vite preview"
88
},
99
"dependencies": {
10-
"@unhead/vue": "^1.9.15",
11-
"vue": "^3.4.31"
10+
"@unhead/vue": "^1.11.10",
11+
"vue": "^3.5.12"
1212
},
1313
"devDependencies": {
14-
"@vitejs/plugin-vue": "^5.0.5",
14+
"@vitejs/plugin-vue": "^5.1.4",
1515
"cross-env": "^7.0.3",
16-
"typescript": "^5.5.3",
16+
"typescript": "^5.6.3",
1717
"unplugin-vue-markdown": "^0.26.2",
18-
"vite": "^5.3.3",
18+
"vite": "^5.4.10",
1919
"vite-plugin-pages": "^0.32.3",
20-
"vite-plugin-pwa": "^0.20.0",
20+
"vite-plugin-pwa": "^0.20.5",
2121
"vite-ssg": "workspace:*",
22-
"vue-router": "^4.4.0"
22+
"vue-router": "^4.4.5"
2323
}
2424
}

‎examples/multiple-pages-with-store/package.json

+8-8
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,20 @@
77
"serve": "vite preview"
88
},
99
"dependencies": {
10-
"@unhead/vue": "^1.9.15",
11-
"pinia": "^2.1.7",
12-
"vue": "^3.4.31"
10+
"@unhead/vue": "^1.11.10",
11+
"pinia": "^2.2.4",
12+
"vue": "^3.5.12"
1313
},
1414
"devDependencies": {
1515
"@nuxt/devalue": "^2.0.2",
16-
"@vitejs/plugin-vue": "^5.0.5",
16+
"@vitejs/plugin-vue": "^5.1.4",
1717
"cross-env": "^7.0.3",
18-
"typescript": "^5.5.3",
19-
"unplugin-vue-components": "^0.27.2",
18+
"typescript": "^5.6.3",
19+
"unplugin-vue-components": "^0.27.4",
2020
"unplugin-vue-markdown": "^0.26.2",
21-
"vite": "^5.3.3",
21+
"vite": "^5.4.10",
2222
"vite-plugin-pages": "^0.32.3",
2323
"vite-ssg": "workspace:*",
24-
"vue-router": "^4.4.0"
24+
"vue-router": "^4.4.5"
2525
}
2626
}

‎examples/multiple-pages/package.json

+7-7
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,18 @@
77
"serve": "vite preview --base /ssg-base/"
88
},
99
"dependencies": {
10-
"@unhead/vue": "^1.9.15",
11-
"vue": "^3.4.31"
10+
"@unhead/vue": "^1.11.10",
11+
"vue": "^3.5.12"
1212
},
1313
"devDependencies": {
14-
"@vitejs/plugin-vue": "^5.0.5",
14+
"@vitejs/plugin-vue": "^5.1.4",
1515
"cross-env": "^7.0.3",
16-
"typescript": "^5.5.3",
17-
"unplugin-vue-components": "^0.27.2",
16+
"typescript": "^5.6.3",
17+
"unplugin-vue-components": "^0.27.4",
1818
"unplugin-vue-markdown": "^0.26.2",
19-
"vite": "^5.3.3",
19+
"vite": "^5.4.10",
2020
"vite-plugin-pages": "^0.32.3",
2121
"vite-ssg": "workspace:*",
22-
"vue-router": "^4.4.0"
22+
"vue-router": "^4.4.5"
2323
}
2424
}

‎examples/single-page/package.json

+5-5
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@
77
"serve": "vite preview"
88
},
99
"dependencies": {
10-
"pinia": "^2.1.7",
11-
"vue": "^3.4.31"
10+
"pinia": "^2.2.4",
11+
"vue": "^3.5.12"
1212
},
1313
"devDependencies": {
14-
"@vitejs/plugin-vue": "^5.0.5",
14+
"@vitejs/plugin-vue": "^5.1.4",
1515
"cross-env": "^7.0.3",
16-
"typescript": "^5.5.3",
17-
"vite": "^5.3.3",
16+
"typescript": "^5.6.3",
17+
"vite": "^5.4.10",
1818
"vite-ssg": "workspace:*"
1919
}
2020
}

‎package.json

+26-21
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "vite-ssg",
33
"version": "0.23.8",
4-
"packageManager": "pnpm@9.4.0",
4+
"packageManager": "pnpm@9.12.3",
55
"description": "Server-side generation for Vite",
66
"author": "Anthony Fu <anthonyfu117@hotmail.com>",
77
"license": "MIT",
@@ -83,12 +83,16 @@
8383
"release": "bumpp && npm publish"
8484
},
8585
"peerDependencies": {
86+
"beasties": "^0.1.0",
8687
"critters": "^0.0.24",
8788
"vite": "^2.0.0 || ^3.0.0 || ^4.0.0 || ^5.0.0-0",
8889
"vue": "^3.2.10",
8990
"vue-router": "^4.0.1"
9091
},
9192
"peerDependenciesMeta": {
93+
"beasties": {
94+
"optional": true
95+
},
9296
"critters": {
9397
"optional": true
9498
},
@@ -97,40 +101,41 @@
97101
}
98102
},
99103
"dependencies": {
100-
"@unhead/dom": "^1.9.15",
101-
"@unhead/vue": "^1.9.15",
104+
"@unhead/dom": "^1.11.10",
105+
"@unhead/vue": "^1.11.10",
102106
"fs-extra": "^11.2.0",
103107
"html-minifier-terser": "^7.2.0",
104108
"html5parser": "^2.0.2",
105-
"jsdom": "^24.1.0",
109+
"jsdom": "^25.0.1",
106110
"kolorist": "^1.8.0",
107-
"prettier": "^3.3.2",
111+
"prettier": "^3.3.3",
108112
"yargs": "^17.7.2"
109113
},
110114
"devDependencies": {
111-
"@antfu/eslint-config": "^2.22.0-beta.1",
115+
"@antfu/eslint-config": "^3.8.0",
112116
"@types/fs-extra": "^11.0.4",
113117
"@types/html-minifier-terser": "^7.0.2",
114118
"@types/jsdom": "^21.1.7",
115-
"@types/yargs": "^17.0.32",
116-
"bumpp": "^9.4.1",
117-
"critters": "^0.0.22",
118-
"eslint": "^9.6.0",
119-
"esno": "^4.7.0",
119+
"@types/yargs": "^17.0.33",
120+
"beasties": "^0.1.0",
121+
"bumpp": "^9.8.0",
122+
"critters": "^0.0.25",
123+
"eslint": "^9.13.0",
124+
"esno": "^4.8.0",
120125
"fast-glob": "^3.3.2",
121126
"p-queue": "^8.0.1",
122-
"rollup": "^4.18.0",
123-
"tsup": "^8.1.0",
124-
"typescript": "^5.5.3",
127+
"rollup": "^4.24.2",
128+
"tsup": "^8.3.5",
129+
"typescript": "^5.6.3",
125130
"unbuild": "^2.0.0",
126-
"vite": "^5.3.3",
127-
"vite-plugin-pwa": "^0.20.0",
128-
"vitest": "^1.6.0",
129-
"vue": "^3.4.31",
130-
"vue-router": "^4.4.0",
131-
"vue-tsc": "^2.0.26"
131+
"vite": "^5.4.10",
132+
"vite-plugin-pwa": "^0.20.5",
133+
"vitest": "^2.1.4",
134+
"vue": "^3.5.12",
135+
"vue-router": "^4.4.5",
136+
"vue-tsc": "^2.1.8"
132137
},
133138
"resolutions": {
134-
"vite": "^5.3.3"
139+
"vite": "^5.4.10"
135140
}
136141
}

‎pnpm-lock.yaml

+2,434-1,895
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎src/node/build.ts

+12-10
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import type { ViteSSGContext, ViteSSGOptions } from '../types'
1616
import { serializeState } from '../utils/state'
1717
import { renderPreloadLinks } from './preload-links'
1818
import { buildLog, getSize, routesToPaths } from './utils'
19-
import { getCritters } from './critical'
19+
import { getBeastiesOrCritters } from './critical'
2020

2121
export type Manifest = Record<string, string[]>
2222

@@ -38,12 +38,12 @@ export async function build(ssgOptions: Partial<ViteSSGOptions> = {}, viteConfig
3838
const outDir = config.build.outDir || 'dist'
3939
const out = isAbsolute(outDir) ? outDir : join(root, outDir)
4040

41+
const mergedOptions = Object.assign({}, config.ssgOptions || {}, ssgOptions)
4142
const {
4243
script = 'sync',
4344
mock = false,
4445
entry = await detectEntry(root),
4546
formatting = 'none',
46-
crittersOptions = {},
4747
includedRoutes: configIncludedRoutes = DefaultIncludedRoutes,
4848
onBeforePageRender,
4949
onPageRendered,
@@ -54,7 +54,9 @@ export async function build(ssgOptions: Partial<ViteSSGOptions> = {}, viteConfig
5454
concurrency = 20,
5555
rootContainerId = 'app',
5656
base,
57-
}: ViteSSGOptions = Object.assign({}, config.ssgOptions || {}, ssgOptions)
57+
}: ViteSSGOptions = mergedOptions
58+
59+
const beastiesOptions = mergedOptions.beastiesOptions ?? mergedOptions.crittersOptions ?? {}
5860

5961
if (fs.existsSync(ssgOut))
6062
await fs.remove(ssgOut)
@@ -133,11 +135,11 @@ export async function build(ssgOptions: Partial<ViteSSGOptions> = {}, viteConfig
133135

134136
buildLog('Rendering Pages...', routesPaths.length)
135137

136-
const critters = crittersOptions !== false
137-
? await getCritters(outDir, crittersOptions)
138+
const beasties = beastiesOptions !== false
139+
? await getBeastiesOrCritters(outDir, beastiesOptions)
138140
: undefined
139-
if (critters)
140-
console.log(`${gray('[vite-ssg]')} ${blue('Critical CSS generation enabled via `critters`')}`)
141+
if (beasties)
142+
console.log(`${gray('[vite-ssg]')} ${blue('Critical CSS generation enabled via `beasties`')}`)
141143

142144
const {
143145
path: _ssrManifestPath,
@@ -190,8 +192,8 @@ export async function build(ssgOptions: Partial<ViteSSGOptions> = {}, viteConfig
190192

191193
const html = jsdom.serialize()
192194
let transformed = (await onPageRendered?.(route, html, appCtx)) || html
193-
if (critters)
194-
transformed = await critters.process(transformed)
195+
if (beasties)
196+
transformed = await beasties.process(transformed)
195197

196198
const formatted = await formatHtml(transformed, formatting)
197199

@@ -244,7 +246,7 @@ async function detectEntry(root: string) {
244246
// eslint-disable-next-line regexp/no-super-linear-backtracking
245247
const scriptSrcReg = /<script.*?src=["'](.+?)["'](?!<).*>\s*<\/script>/gi
246248
const html = await fs.readFile(join(root, 'index.html'), 'utf-8')
247-
const scripts = [...html.matchAll(scriptSrcReg)] || []
249+
const scripts = [...html.matchAll(scriptSrcReg)]
248250
const [, entry] = scripts.find((matchResult) => {
249251
const [script] = matchResult
250252
const [, scriptType] = script.match(/.*\stype=(?:'|")?([^>'"\s]+)/i) || []

‎src/node/critical.ts

+19-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,26 @@
11
import type Critters from 'critters'
2-
import type { Options } from 'critters'
2+
import type Beasties from 'beasties'
3+
import type { Options } from 'beasties'
34

4-
export async function getCritters(outDir: string, options: Options = {}): Promise<Critters | undefined> {
5+
export async function getBeastiesOrCritters(outDir: string, options: Options = {}): Promise<Critters | Beasties | undefined> {
56
try {
6-
const CrittersClass = (await import('critters')).default
7+
const BeastiesClass = (await import('beasties')).default
8+
9+
return new BeastiesClass({
10+
path: outDir,
11+
logLevel: 'warn',
12+
external: true,
13+
inlineFonts: true,
14+
preloadFonts: true,
15+
...options,
16+
})
17+
}
18+
catch {
19+
}
720

21+
try {
22+
const CrittersClass = (await import('critters')).default
23+
console.warn('`critters` is deprecated. Please use `beasties` instead.')
824
return new CrittersClass({
925
path: outDir,
1026
logLevel: 'warn',

‎src/types.ts

+9
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type { App } from 'vue'
22
import type { RouteRecordRaw, Router, RouterOptions as VueRouterOptions } from 'vue-router'
33
import type { MergeHead, VueHeadClient } from '@unhead/vue'
44
import type { Options as CrittersOptions } from 'critters'
5+
import type { Options as BeastiesOptions } from 'beasties'
56

67
export interface ViteSSGOptions {
78
/**
@@ -71,10 +72,18 @@ export interface ViteSSGOptions {
7172
/**
7273
* Options for the critters package.
7374
*
75+
* @deprecated Use `beastiesOptions` instead.
7476
* @see https://github.com/GoogleChromeLabs/critters
7577
*/
7678
crittersOptions?: CrittersOptions | false
7779

80+
/**
81+
* Options for the beasties package.
82+
*
83+
* @see https://github.com/danielroe/beasties
84+
*/
85+
beastiesOptions?: BeastiesOptions | false
86+
7887
/**
7988
* Custom function to modify the routes to do the SSG.
8089
*

0 commit comments

Comments
 (0)
Please sign in to comment.