Skip to content

Commit 26b984e

Browse files
committed
refactor(core/a11y): start importAxe early
1 parent ec6913c commit 26b984e

File tree

1 file changed

+22
-12
lines changed

1 file changed

+22
-12
lines changed

src/core/a11y.js

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,32 @@ const DISABLED_RULES = [
1515
"region",
1616
];
1717

18+
export async function prepare(conf) {
19+
if (!conf.a11y) {
20+
return;
21+
}
22+
conf.state[name] = {
23+
axeImportPromise: importAxe(),
24+
};
25+
}
26+
1827
export async function run(conf) {
1928
if (!conf.a11y) {
2029
return;
2130
}
2231

32+
/** @type {typeof window.axe} */
33+
let axe;
34+
try {
35+
axe = await conf.state[name].axeImportPromise;
36+
} catch (error) {
37+
const msg = `Failed to load a11y linter. ${error.msg}`;
38+
showError(msg, name);
39+
return;
40+
}
41+
2342
const options = conf.a11y === true ? {} : conf.a11y;
24-
const violations = await getViolations(options);
43+
const violations = await getViolations(axe, options);
2544
for (const violation of violations) {
2645
/**
2746
* We're grouping by failureSummary as it contains hints to fix the issue.
@@ -49,9 +68,10 @@ export async function run(conf) {
4968
}
5069

5170
/**
71+
* @param {typeof window.axe} axe
5272
* @param {object} opts Options as described at https://github.com/dequelabs/axe-core/blob/develop/doc/API.md#options-parameter
5373
*/
54-
async function getViolations(opts) {
74+
async function getViolations(axe, opts) {
5575
const { rules, ...otherOptions } = opts;
5676
const options = {
5777
rules: {
@@ -64,16 +84,6 @@ async function getViolations(opts) {
6484
reporter: "v1", // v1 includes a `failureSummary`
6585
};
6686

67-
let axe;
68-
try {
69-
axe = await importAxe();
70-
} catch (error) {
71-
const msg = "Failed to load a11y linter.";
72-
showError(msg, name);
73-
console.error(error);
74-
return [];
75-
}
76-
7787
try {
7888
const result = await axe.run(document, options);
7989
return result.violations;

0 commit comments

Comments
 (0)