Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add support for nitro prerendering (nuxt 3 + bridge support) (#129
- Loading branch information
Showing
22 changed files
with
2,920 additions
and
8,727 deletions.
There are no files selected for viewing
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,3 +7,4 @@ node_modules | |
.DS_Store | ||
coverage | ||
dist | ||
.output |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
export default { | ||
modules: ['@nuxtjs/critters'], | ||
css: ['~/styles/global.css'], | ||
nitro: { | ||
prerender: { | ||
routes: ['/'], | ||
}, | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"name": "nuxt-critters-playground" | ||
} |
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"extends": "./.nuxt/tsconfig.json" | ||
} |
File renamed without changes.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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) | ||
}) | ||
} | ||
}, | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { fileURLToPath } from 'url' | ||
import { setup, $fetch } from '@nuxt/test-utils' | ||
import { describe, it, expect } from 'vitest' | ||
|
||
await setup({ | ||
rootDir: fileURLToPath(new URL('../playground', import.meta.url).href), | ||
server: true, | ||
nuxtConfig: { | ||
dev: true, | ||
}, | ||
}) | ||
|
||
describe('module in dev mode', () => { | ||
it('does not add module', async () => { | ||
const body = await $fetch('/') | ||
expect(body).not.toContain('<style>') | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { fileURLToPath } from 'url' | ||
import { promises as fsp } from 'fs' | ||
import { setup, useTestContext } from '@nuxt/test-utils' | ||
import { resolve } from 'pathe' | ||
import { describe, it, expect } from 'vitest' | ||
|
||
await setup({ | ||
rootDir: fileURLToPath(new URL('../playground', import.meta.url).href), | ||
build: true, | ||
}) | ||
|
||
describe('module in generated pages', () => { | ||
it('enables extractCSS', async () => { | ||
const ctx = useTestContext() | ||
const body = await fsp.readFile( | ||
resolve(ctx.nuxt!.options.nitro.output?.dir || '', 'public/index.html'), | ||
'utf-8' | ||
) | ||
expect(body).toContain('<style>') | ||
expect(body).toContain('.sample-class') | ||
expect(body).not.toContain('.sample-unused-class') | ||
}) | ||
}) |
Oops, something went wrong.