diff --git a/lighthouse-core/audits/seo/hreflang.js b/lighthouse-core/audits/seo/hreflang.js index 38d46f2e4126..2e0902d5a892 100644 --- a/lighthouse-core/audits/seo/hreflang.js +++ b/lighthouse-core/audits/seo/hreflang.js @@ -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. */ @@ -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} */ 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(); } /** diff --git a/lighthouse-core/gather/gatherers/accessibility.js b/lighthouse-core/gather/gatherers/accessibility.js index 95bdd441de99..d3529f43c614 100644 --- a/lighthouse-core/gather/gatherers/accessibility.js +++ b/lighthouse-core/gather/gatherers/accessibility.js @@ -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'); /** diff --git a/lighthouse-core/lib/axe.js b/lighthouse-core/lib/axe.js new file mode 100644 index 000000000000..aae144e037fd --- /dev/null +++ b/lighthouse-core/lib/axe.js @@ -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, +};