Created
August 21, 2019 18:19
-
-
Save ryanfitzer/6fc694eb1732637b02a4b5cc32a38165 to your computer and use it in GitHub Desktop.
HTML Validator Plugin for Webpack
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
/** | |
* HTML Validator Plugin for Webpack | |
* | |
* @docs https://html-validate.org | |
*/ | |
const path = require( 'path' ); | |
const columnify = require( 'columnify' ); | |
const HtmlWebpackPlugin = require( 'html-webpack-plugin' ); | |
const HtmlValidate = require( 'html-validate' ).HtmlValidate; | |
const pluginName = 'Validate HTML'; | |
const getRuleInfo = ( rule ) => { | |
if ( !rule ) return ''; | |
let id = rule.toLowerCase(); | |
let base = 'https://html-validate.org/rules/'; | |
const isWCAG = /wcag/.test( id ); | |
if ( isWCAG ) { | |
id = id.split( '/' )[ 1 ]; | |
base = `${ base }wcag/`; | |
} | |
return `[${ rule.toLowerCase() }] ${ base }${ id }.html`; | |
}; | |
class HTMLValidator { | |
apply( compiler ) { | |
const config = require( path.join( compiler.context, '.htmlvalidate.json' ) ); | |
const validator = new HtmlValidate( config ); | |
const isProd = compiler.options.mode === 'production'; | |
compiler.hooks.compilation.tap( pluginName, ( compilation ) => { | |
HtmlWebpackPlugin.getHooks( compilation ).beforeEmit.tapAsync( pluginName, ( data, cb ) => { | |
const { | |
html, | |
outputName | |
} = data; | |
const { | |
valid, | |
results | |
} = validator.validateSource( html ); | |
if ( !valid ) { | |
const msgs = []; | |
const error = [ | |
`${ outputName }:` | |
]; | |
results.forEach( ( result ) => { | |
result.messages.forEach( ( msg ) => { | |
const { | |
line, | |
column, | |
ruleId, | |
message | |
} = msg; | |
const ruleInfo = getRuleInfo( ruleId ); | |
msgs.push( { | |
line: ` ${ line }:${ column }`, | |
error: `"${ message }" ${ ruleInfo }` | |
} ); | |
} ); | |
} ); | |
error.push( columnify( msgs, { | |
showHeaders: false, | |
columnSplitter: ' ' | |
} ) ); | |
const issues = new Error( error.join( '\n' ) ); | |
// if ( isProd ) compilation.errors.push( issues ); | |
// else compilation.warnings.push( issues ); | |
if ( !isProd ) compilation.errors.push( issues ); | |
} | |
// Tell webpack to move on | |
cb( null, data ); | |
} ); | |
} ); | |
} | |
} | |
module.exports = HTMLValidator; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment