docusaurus/website/testCSSOrder.mjs
Sébastien Lorber 4fb67ef11b
chore: backport retro compatible commits for the Docusaurus v2.4 release (#8809)
Co-authored-by: Joshua Chen <sidachen2003@gmail.com>
Co-authored-by: sebastienlorber <lorber.sebastien@gmail.com>
Co-authored-by: Sébastien Lorber <slorber@users.noreply.github.com>
Co-authored-by: Ben Gubler <nebrelbug@gmail.com>
Co-authored-by: Davide Donadio <davide.donadio@it.clara.net>
Co-authored-by: Petter Drønnen <36735557+dr0nn1@users.noreply.github.com>
Co-authored-by: Moritz Stückler <moritz@bitbetter.de>
Co-authored-by: Mysterious_Dev <40738104+Mysterious-Dev@users.noreply.github.com>
Co-authored-by: TrueQAP <32407751+trueqap@users.noreply.github.com>
Co-authored-by: Kagan <34136752+kagankan@users.noreply.github.com>
Co-authored-by: Dewansh Thakur <71703033+dewanshDT@users.noreply.github.com>
Co-authored-by: Armano <armano2@users.noreply.github.com>
Co-authored-by: Anas <60762285+Anasqx@users.noreply.github.com>
Co-authored-by: Tanner Dolby <tannercdolby@gmail.com>
Co-authored-by: Davide Donadio <davide.donadio94@gmail.com>
Co-authored-by: biplavmz <68702055+biplavmz@users.noreply.github.com>
Co-authored-by: Vishruta Patil <72292532+Vishruta-Patil@users.noreply.github.com>
fix(theme-classic): fix tab focus bug in dropdown (#8697) (#8699)
fix(theme): improve color toggle when using dark navbar (#8615)
fix(theme-translations): fix wrong arabic words (tip/next) (#8744)
fix(core): baseUrl error banner link anchor case (#8746)
fix(search): search page should react to querystring changes + cleanup/refactor (#8757)
fix(theme): allow tabs children to be falsy (#8801)
fix(theme): codeblock buttons should be kept on the right when using RTL locale (#8803)
2023-03-24 09:23:58 +01:00

114 lines
3.2 KiB
JavaScript

/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import path from 'path';
import {fileURLToPath} from 'url';
import fs from 'fs-extra';
/*
This verifies CSS ordering on the Docusaurus site itself,
There are multiple ways to provide some CSS to Docusaurus
and Docusaurus should guarantee a consistent CSS ordering over time
See also
- https://github.com/facebook/docusaurus/issues/3678
- https://github.com/facebook/docusaurus/pull/5987
TODO we should probably add a real e2e test in core instead of using our own
website? Current solution looks good-enough for now
*/
// TODO temporary, the current order is bad and we should change/fix that
const EXPECTED_CSS_MARKERS = [
// Note, Infima and site classes are optimized/deduplicated and put at the top
// We don't agree yet on what should be the order for those classes
// See https://github.com/facebook/docusaurus/pull/6222
'.markdown>h2',
'.button--outline.button--active',
'--ifm-color-scheme:light',
'.col[class*=col--]',
'.padding-vert--xl',
'.footer__link-item',
'.navbar__title',
'.pagination__item',
'.pills__item',
'.tabs__item',
// Test markers
'.test-marker-site-custom-css-unique-rule',
'.test-marker-site-client-module',
'.test-marker-theme-layout',
'.test-marker-site-index-page',
// Lazy-loaded lib
'.DocSearch-Modal',
'.DocSearch-Hit-content-wrapper',
];
const cssDirName = fileURLToPath(new URL('build/assets/css', import.meta.url));
const cssFileNames = (await fs.readdir(cssDirName)).filter((file) =>
file.endsWith('.css'),
);
if (cssFileNames.length !== 1) {
throw new Error('unexpected: more than 1 css file');
}
const cssFile = path.join(cssDirName, cssFileNames[0]);
console.log('Inspecting CSS file for test CSS markers', cssFile);
const cssFileContent = await fs.readFile(cssFile, 'utf8');
const cssMarkersWithPositions = EXPECTED_CSS_MARKERS.map((marker) => {
const position = cssFileContent.indexOf(marker);
return {marker, position};
});
const missingCSSMarkers = cssMarkersWithPositions
.filter((m) => m.position === -1)
.map((m) => m.marker);
if (missingCSSMarkers.length > 0) {
throw new Error(
`Some expected CSS marker classes could not be found in file ${cssFile}: \n- ${missingCSSMarkers.join(
'\n- ',
)}`,
);
}
// https://github.com/you-dont-need/You-Dont-Need-Lodash-Underscore#_sortby-and-_orderby
const sortBy =
(/** @type {string} */ key) =>
(
/** @type {Record<string, unknown>} */ a,
/** @type {Record<string, unknown>} */ b,
) =>
// eslint-disable-next-line no-nested-ternary
a[key] > b[key] ? 1 : b[key] > a[key] ? -1 : 0;
const sortedCSSMarkers = [...cssMarkersWithPositions]
.sort(sortBy('position'))
.map(({marker}) => marker);
if (JSON.stringify(sortedCSSMarkers) === JSON.stringify(EXPECTED_CSS_MARKERS)) {
console.log(`Test CSS markers were found in the expected order:
- ${sortedCSSMarkers.join('\n- ')}`);
} else {
throw new Error(`Test CSS markers were found in an incorrect order.
Expected order:
- ${EXPECTED_CSS_MARKERS.join('\n- ')};
Actual order:
- ${sortedCSSMarkers.join('\n- ')};
CSS file: ${cssFile}
`);
}