Skip to content

Commit 0b45f4c

Browse files
authored
Use persistent Puppeteer instance (#63)
* Install puppeteer * Export puppeteer instance from config Also make config async * Create separate getPa11yOpts function * Handle browser instance in pluginCore
1 parent 78ce0d5 commit 0b45f4c

File tree

6 files changed

+31
-10
lines changed

6 files changed

+31
-10
lines changed

package-lock.json

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"pa11y": "^6.1.1",
1212
"path-type": "^4.0.0",
1313
"picocolors": "^1.0.0",
14+
"puppeteer": "~9.1.1",
1415
"readdirp": "^3.6.0"
1516
},
1617
"scripts": {

src/config.js

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// @ts-check
2+
const puppeteer = require('puppeteer')
23

34
const DEFAULT_CHECK_PATHS = ['/']
45
const DEFAULT_FAIL_WITH_ISSUES = true
@@ -17,14 +18,27 @@ const getConfiguration = ({
1718
checkPaths: checkPaths || DEFAULT_CHECK_PATHS,
1819
ignoreDirectories: ignoreDirectories || DEFAULT_IGNORE_DIRECTORIES,
1920
failWithIssues: failWithIssues !== undefined ? failWithIssues : DEFAULT_FAIL_WITH_ISSUES,
20-
pa11yOpts: {
21-
runners: PA11Y_RUNNERS,
22-
userAgent: PA11Y_USER_AGENT,
23-
standard: wcagLevel || PA11Y_DEFAULT_WCAG_LEVEL,
24-
},
21+
wcagLevel: wcagLevel || PA11Y_DEFAULT_WCAG_LEVEL,
22+
}
23+
}
24+
25+
const getPa11yOpts = async (wcagLevel) => {
26+
const browser = await puppeteer.launch({
27+
// NB: Allows iframes to load when HTML page is visited from filesystem.
28+
// Without this, a page with an iframe will crash pa11y
29+
args: ['--disable-web-security'],
30+
ignoreHTTPSErrors: true,
31+
})
32+
33+
return {
34+
browser,
35+
runners: PA11Y_RUNNERS,
36+
userAgent: PA11Y_USER_AGENT,
37+
standard: wcagLevel || PA11Y_DEFAULT_WCAG_LEVEL,
2538
}
2639
}
2740

2841
module.exports = {
2942
getConfiguration,
43+
getPa11yOpts,
3044
}

src/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const pico = require('picocolors')
77
module.exports = {
88
async onPostBuild({ constants, inputs, utils: { build } }) {
99
try {
10-
const { publishDir, checkPaths, ignoreDirectories, pa11yOpts, failWithIssues } = getConfiguration({
10+
const { publishDir, checkPaths, ignoreDirectories, wcagLevel, failWithIssues } = getConfiguration({
1111
constants,
1212
inputs,
1313
})
@@ -20,7 +20,7 @@ module.exports = {
2020
const { report, issueCount } = await pluginCore.runPa11y({
2121
build,
2222
htmlFilePaths,
23-
pa11yOpts,
23+
wcagLevel,
2424
})
2525
const reportSummary =
2626
`${issueCount === 0 ? 'No' : issueCount} accessibility issues found!` +

src/pluginCore.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,17 @@ const { extname, join } = require('path')
55
const { isDirectory, isFile } = require('path-type')
66
const { results: cliReporter } = require('./reporter')
77
const readdirp = require('readdirp')
8+
const { getPa11yOpts } = require('./config')
89

910
const EMPTY_ARRAY = []
1011
const ASTERISK = '*'
1112
const HTML_EXT = '.html'
1213
const GLOB_HTML = '*.html'
1314

14-
exports.runPa11y = async function ({ htmlFilePaths, pa11yOpts, build }) {
15+
exports.runPa11y = async function ({ build, htmlFilePaths, wcagLevel }) {
16+
const pa11yOpts = await getPa11yOpts(wcagLevel)
1517
let issueCount = 0
18+
1619
const results = await Promise.all(
1720
htmlFilePaths.map(async (path) => {
1821
try {
@@ -27,6 +30,8 @@ exports.runPa11y = async function ({ htmlFilePaths, pa11yOpts, build }) {
2730
}),
2831
)
2932

33+
await pa11yOpts.browser.close()
34+
3035
return {
3136
issueCount,
3237
report: results.join(''),

tests/runPa11y/__snapshots__/this.test.js.snap

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ Object {
66
"report": "
77
Results for file: ./tests/runPa11y/publishDir/index.html
88
9-
[31mError:[39m Img element missing an alt attribute. Use the alt attribute to specify a short text alternative.
10-
[90m ├── WCAG2AA.Principle1.Guideline1_1.1_1_1.H37[39m
9+
[31mError:[39m Images must have alternate text (https://dequeuniversity.com/rules/axe/4.3/image-alt?application=axeAPI)
10+
[90m ├── image-alt[39m
1111
 ├── html > body > img
1212
 └── <img src=\\"https://placekitten.com/200/300\\">
1313

0 commit comments

Comments
 (0)