-
Notifications
You must be signed in to change notification settings - Fork 2
/
module.ts
59 lines (51 loc) · 1.52 KB
/
module.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import { defineNuxtModule, isNuxt2 } from '@nuxt/kit'
import { resolve } from 'pathe'
import Critters, { Options } from 'critters'
export interface ModuleOptions {
// Options passed directly to `critters`
config?: Options
}
export default defineNuxtModule<ModuleOptions>({
meta: {
name: 'critters',
configKey: 'critters',
},
defaults: {
config: {
preload: 'media',
},
},
setup(options, nuxt) {
// Only enable for production
if (nuxt.options.dev) return
// Enable css extraction
nuxt.options.build.extractCSS = true
// Nitro handler (for prerendering only)
nuxt.hook('nitro:init', nitro => {
const critters = new Critters({
path: nitro.options.output.publicDir,
publicPath: nitro.options.baseURL,
...options.config,
})
nitro.hooks.hook('prerender:generate', async route => {
if (!route.contents || !route.fileName?.endsWith('.html')) return
route.contents = await critters.process(route.contents)
})
})
/* c8 ignore start */
if (isNuxt2()) {
const critters = new Critters({
path: resolve(nuxt.options.buildDir, 'dist/client'),
publicPath: nuxt.options.build.publicPath,
...options.config,
})
// Add transform step
nuxt.hook('render:route', async (_url, result) => {
result.html = await critters.process(result.html)
})
nuxt.hook('generate:page', async result => {
result.html = await critters.process(result.html)
})
}
},
})