Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf(core/a11y): start loading axe earlier #3372

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 18 additions & 12 deletions src/core/a11y.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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: {
Expand All @@ -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;
Expand Down
1 change: 1 addition & 0 deletions src/core/base-runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -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] = {};
Copy link
Contributor

@marcoscaceres marcoscaceres Mar 10, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm... not a fan of attaching more stuff to config. Why not have this as an internal state inside the plugin itself?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Plugins can share state among them.
I plan to add a state arg to all functions, but conf.state was easier for starters.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd suggest doing that now... look at what happened with my terrible linter API :( I kept saying, "I'll fix it next week... I'll fix it next week..." and years later, still there 😢

try {
await plug.prepare(config);
} catch (err) {
Expand Down