diff --git a/src/core/a11y.js b/src/core/a11y.js index cd50a69ba8..275a1846ce 100644 --- a/src/core/a11y.js +++ b/src/core/a11y.js @@ -15,13 +15,28 @@ const DISABLED_RULES = [ "region", ]; +export async function prepare(conf) { + if (!conf.a11y) { + return; + } + conf.state[name].axeImportPromise = importAxe().catch(error => { + const msg = `Failed to load a11y linter. ${error.msg}`; + showError(msg, name); + return null; + }); +} + export async function run(conf) { if (!conf.a11y) { return; } + /** @type {typeof window.axe} */ + const axe = await conf.state[name].axeImportPromise; + if (axe === null) return; + const options = conf.a11y === true ? {} : conf.a11y; - const violations = await getViolations(options); + const violations = await getViolations(axe, options); for (const violation of violations) { /** * We're grouping by failureSummary as it contains hints to fix the issue. @@ -49,9 +64,10 @@ export async function run(conf) { } /** + * @param {typeof window.axe} axe * @param {object} opts Options as described at https://github.com/dequelabs/axe-core/blob/develop/doc/API.md#options-parameter */ -async function getViolations(opts) { +async function getViolations(axe, opts) { const { rules, ...otherOptions } = opts; const options = { rules: { @@ -64,16 +80,6 @@ async function getViolations(opts) { reporter: "v1", // v1 includes a `failureSummary` }; - let axe; - try { - axe = await importAxe(); - } catch (error) { - const msg = "Failed to load a11y linter."; - showError(msg, name); - console.error(error); - return []; - } - try { const result = await axe.run(document, options); return result.violations; diff --git a/src/core/base-runner.js b/src/core/base-runner.js index 792386ae1e..9cd55a7d85 100644 --- a/src/core/base-runner.js +++ b/src/core/base-runner.js @@ -43,6 +43,7 @@ function isRunnableModule(plug) { async function executePreparePass(runnables, config) { for (const plug of runnables.filter(p => p.prepare)) { + config.state[plug.name] = {}; try { await plug.prepare(config); } catch (err) {