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

core: change how axe source is imported to reduce duplication #11442

Closed
wants to merge 3 commits into from
Closed
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
21 changes: 8 additions & 13 deletions lighthouse-core/audits/seo/hreflang.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@
/** @typedef {{reason: LH.IcuMessage}} SubItem */

const Audit = require('../audit.js');
const i18n = require('../../lib/i18n/i18n.js');
const axeLibSource = require('../../lib/axe.js').source;

const VALID_LANGS = importValidLangs();
const NO_LANGUAGE = 'x-default';
const i18n = require('../../lib/i18n/i18n.js');

const UIStrings = {
/** Title of a Lighthouse audit that provides detail on the `hreflang` attribute on a page. This descriptive title is shown when the page's `hreflang` attribute is configured correctly. "hreflang" is an HTML attribute and should not be translated. */
Expand All @@ -32,24 +34,17 @@ const UIStrings = {
const str_ = i18n.createMessageInstanceIdFn(__filename, UIStrings);

/**
* Import list of valid languages from axe core without including whole axe-core package
* Import list of valid languages from axe core.
* This is a huge array of language codes that can be stored more efficiently if we will need to
* shrink the bundle size.
* @return {Array<string>}
*/
function importValidLangs() {
// @ts-expect-error - global switcheroo to load axe valid-langs
const axeCache = global.axe;
// @ts-expect-error
global.axe = {utils: {}};
// Define a window-ish object so axe will export to it.
const window = {getComputedStyle: () => {}};
eval(axeLibSource);
// @ts-expect-error
require('axe-core/lib/core/utils/valid-langs.js');
// @ts-expect-error
const validLangs = global.axe.utils.validLangs();
// @ts-expect-error
global.axe = axeCache;

return validLangs;
return window.axe.utils.validLangs();
}

/**
Expand Down
3 changes: 1 addition & 2 deletions lighthouse-core/gather/gatherers/accessibility.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@
/* global window, document, getNodeDetails */

const Gatherer = require('./gatherer.js');
const fs = require('fs');
const axeLibSource = fs.readFileSync(require.resolve('axe-core/axe.min.js'), 'utf8');
const axeLibSource = require('../../lib/axe.js').source;
const pageFunctions = require('../../lib/page-functions.js');

/**
Expand Down
13 changes: 13 additions & 0 deletions lighthouse-core/lib/axe.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/**
* @license Copyright 2020 The Lighthouse Authors. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
*/
'use strict';

const fs = require('fs');
const source = fs.readFileSync(require.resolve('axe-core/axe.min.js'), 'utf8');

module.exports = {
source,
};