Skip to content

Commit

Permalink
refactor(core/a11y): start importAxe early
Browse files Browse the repository at this point in the history
  • Loading branch information
sidvishnoi committed Feb 19, 2021
1 parent 259d2ab commit fb71510
Showing 1 changed file with 22 additions and 12 deletions.
34 changes: 22 additions & 12 deletions src/core/a11y.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,32 @@ const DISABLED_RULES = [
"region",
];

export async function prepare(conf) {
if (!conf.a11y) {
return;
}
conf.state[name] = {
axeImportPromise: importAxe(),
};
}

export async function run(conf) {
if (!conf.a11y) {
return;
}

/** @type {typeof window.axe} */
let axe;
try {
axe = await conf.state[name].axeImportPromise;
} catch (error) {
const msg = `Failed to load a11y linter. ${error.msg}`;
showError(msg, name);
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.
Expand Down Expand Up @@ -49,9 +68,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: {
Expand All @@ -64,16 +84,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;
Expand Down

0 comments on commit fb71510

Please sign in to comment.