-
Notifications
You must be signed in to change notification settings - Fork 23
feat: add compatibility with nitro (nuxt 3 + nuxt bridge) #206
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
f03e55c
feat: add compatibility with nitro (nuxt 3 + nuxt bridge)
danielroe 69bd8dc
ci: run prepare step
danielroe cff2c6c
fix: ensure validator is mjs in development (for testing)
danielroe b73b330
refactor: for simplicity, don't use subpath import
danielroe ef3c993
test: revert to earlier vitest
danielroe b647929
chore: remove build config
danielroe eae5ab5
chore: update lockfile and mark prettier optional
danielroe 7cb2e71
chore: typo
danielroe be2caa2
chore: clarify slightly
danielroe 78e21ea
chore: update playground import
danielroe e895b26
chore: depend on prettier
danielroe f4238df
fix: resolve full path to runtime validator for nuxt 2
danielroe 191054c
chore: remove unneeded eslint rules (#219)
dargmuesli 2d69e8d
fix: update compatibility
danielroe c8bb858
Merge remote-tracking branch 'origin/main' into feat/nuxt3
danielroe File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,11 @@ | ||
module.exports = { | ||
extends: ['@nuxtjs/eslint-config-typescript'], | ||
overrides: [ | ||
{ | ||
files: ['*.test.ts', 'validator.ts'], | ||
rules: { | ||
'no-console': 'off' | ||
} | ||
} | ||
] | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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 |
---|---|---|
|
@@ -3,6 +3,7 @@ node_modules | |
.idea | ||
*.log* | ||
.nuxt | ||
.output | ||
.vscode | ||
.DS_Store | ||
coverage | ||
|
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
File renamed without changes.
This file contains hidden or 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 hidden or 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,5 @@ | ||
import { defineNuxtConfig } from 'nuxt/config' | ||
|
||
export default defineNuxtConfig({ | ||
modules: ['@nuxtjs/html-validator'] | ||
}) |
This file contains hidden or 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": "html-validator-playground" | ||
} |
File renamed without changes.
This file contains hidden or 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" | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains hidden or 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,76 @@ | ||
import { fileURLToPath } from 'url' | ||
import chalk from 'chalk' | ||
|
||
import { defineNuxtModule, isNuxt2, logger, resolveModule } from '@nuxt/kit' | ||
import { DEFAULTS, ModuleOptions } from './config' | ||
|
||
export type { ModuleOptions } | ||
|
||
export default defineNuxtModule<ModuleOptions>({ | ||
meta: { | ||
name: '@nuxtjs/html-validator', | ||
configKey: 'htmlValidator', | ||
compatibility: { | ||
nuxt: '^2.0.0 || ^3.0.0-rc.7', | ||
bridge: true | ||
} | ||
}, | ||
defaults: DEFAULTS, | ||
async setup (_options, nuxt) { | ||
logger.info(`Using ${chalk.bold('html-validate')} to validate server-rendered HTML`) | ||
|
||
const { usePrettier, failOnError, options } = _options as Required<ModuleOptions> | ||
if ((nuxt.options as any).htmlValidator?.options?.extends) { | ||
options.extends = (nuxt.options as any).htmlValidator.options.extends | ||
} | ||
|
||
if (nuxt.options.dev) { | ||
nuxt.hook('nitro:config', (config) => { | ||
// Transpile the nitro plugin we're injecting | ||
config.externals = config.externals || {} | ||
config.externals.inline = config.externals.inline || [] | ||
config.externals.inline.push('@nuxtjs/html-validator') | ||
|
||
// Add a nitro plugin that will run the validator for us on each request | ||
config.plugins = config.plugins || [] | ||
config.plugins.push(fileURLToPath(new URL('./runtime/nitro', import.meta.url))) | ||
config.virtual = config.virtual || {} | ||
config.virtual['#html-validator-config'] = `export default ${JSON.stringify(_options)}` | ||
}) | ||
} | ||
|
||
if (!nuxt.options.dev || isNuxt2()) { | ||
const validatorPath = fileURLToPath(new URL('./runtime/validator', import.meta.url)) | ||
const { useChecker, getValidator } = await import(resolveModule(validatorPath)) | ||
const validator = getValidator(options) | ||
const { checkHTML, invalidPages } = useChecker(validator, usePrettier) | ||
|
||
if (failOnError) { | ||
const errorIfNeeded = () => { | ||
if (invalidPages.length) { | ||
throw new Error('html-validator found errors') | ||
} | ||
} | ||
|
||
nuxt.hook('generate:done', errorIfNeeded) | ||
nuxt.hook('close', errorIfNeeded) | ||
} | ||
|
||
// Nuxt 3/Nuxt Bridge prerendering | ||
|
||
nuxt.hook('nitro:init', (nitro) => { | ||
nitro.hooks.hook('prerender:generate', (route) => { | ||
if (!route.contents || !route.fileName?.endsWith('.html')) { return } | ||
checkHTML(route.route, route.contents) | ||
}) | ||
}) | ||
|
||
// Nuxt 2 | ||
|
||
if (isNuxt2()) { | ||
nuxt.hook('render:route', (url: string, result: { html: string }) => checkHTML(url, result.html)) | ||
nuxt.hook('generate:page', ({ path, html }: { path: string, html: string }) => checkHTML(path, html)) | ||
} | ||
} | ||
} | ||
}) |
This file contains hidden or 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,16 @@ | ||
import type { NitroAppPlugin, RenderResponse } from 'nitropack' | ||
import { useChecker, getValidator } from './validator' | ||
// @ts-expect-error virtual module | ||
import config from '#html-validator-config' | ||
|
||
export default <NitroAppPlugin> function (nitro) { | ||
const validator = getValidator(config.options) | ||
const { checkHTML } = useChecker(validator, config.usePrettier) | ||
|
||
nitro.hooks.hook('render:response', (response: RenderResponse, { event }) => { | ||
if (typeof response.body === 'string' && (response.headers['Content-Type'] || response.headers['content-type'])?.includes('html')) { | ||
// We deliberately do not await so as not to block the response | ||
checkHTML(event.req.url, response.body) | ||
} | ||
}) | ||
} |
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.