This repository has been archived on 2024-05-31. You can view files and clone it, but cannot push or open issues or pull requests.
authentik/web/scripts/build-storybook-import-maps.ts

85 lines
2.6 KiB
TypeScript
Raw Normal View History

web: fix storybook build after npm update (#7855) * web: fix storybookbuild build after npm update This commit follows the [patch for Turnstile](https://github.com/goauthentik/authentik/pull/7854) and performs a similar operation for the Storybook build, which failed after the latest `npm audit` and `npm update` passes. [This patch to Vite](https://github.com/vitejs/vite/pull/10762) fixes a problem with the Vite build in that Vite could not resolve if a CSS import was strictly at the module level or if it was necessary to include the imported CSS at the document level. The fix is to hack a query, `?inline`, to the end of the import string, to indicate that it's a module-only import. The Storybook for Web Components build recommended by the Open Webcomponent Consortium is a Storybook-Vite implementation. The latest update fully deprecated undecorated CSS imports, and Storybook broke, unable to reconcile the CSS imports. This patch inlines the inlining of the CSS automatically for Storybook by using the Rollup `modify()` plug-in which performs string substitutions on the source code before it's presented to the compiler and bundler; it recognizes the strings that require inlining, those that match the regex: ``` JavaScript /^(import \w+ from .*\.css)";/ ``` ... and replaces them with a version ending in `.css?inline`. Because the actual recognizer inside `modify()` recognizes strings and not regular expressions, a script to build the strings has been added to the `scripts` folder. Just like locales, you will have to re-run and re-build `build-storybook-import-maps` script if you add a new CSS file to the source tree. * web: prettier had opinions * web: apply eslint + sonarjs check to the scripts folder. * Google recaptcha (aka Turnstile) doesn't understand the "invisible" setting; that's purely an HCaptcha thing. * web: removing the typecast means I no longer need the type. * web: prettier is still having opinions, dammit.
2023-12-18 14:57:39 +00:00
import fs from "fs";
import path from "path";
import { fileURLToPath } from "url";
const __dirname = fileURLToPath(new URL(".", import.meta.url));
// eslint-disable-next-line @typescript-eslint/no-explicit-any
function* walkFilesystem(dir: string): Generator<string, undefined, any> {
const openeddir = fs.opendirSync(dir);
if (!openeddir) {
return;
}
let d: fs.Dirent | null;
while ((d = openeddir?.readSync())) {
if (!d) {
break;
}
const entry = path.join(dir, d.name);
if (d.isDirectory()) yield* walkFilesystem(entry);
else if (d.isFile()) yield entry;
}
openeddir.close();
}
const import_re = /^(import \w+ from .*\.css)";/;
function extractImportLinesFromFile(path: string) {
const source = fs.readFileSync(path, { encoding: "utf8", flag: "r" });
const lines = source?.split("\n") ?? [];
return lines.filter((l) => import_re.test(l));
}
function createOneImportLine(line: string) {
const importMatch = import_re.exec(line);
if (!importMatch) {
throw new Error("How did an unmatchable line get here?");
}
const importContent = importMatch[1];
if (!importContent) {
throw new Error("How did an unmatchable line get here!?");
}
return `'${importContent}";': '${importContent}?inline";',`;
}
const isSourceFile = /\.ts$/;
function getTheSourceFiles() {
return Array.from(walkFilesystem(path.join(__dirname, "..", "src"))).filter((path) =>
isSourceFile.test(path),
);
}
function getTheImportLines(importPaths: string[]) {
const importLines: string[] = importPaths.reduce(
(acc: string[], path) => [...acc, extractImportLinesFromFile(path)].flat(),
[],
);
const uniqueImportLines = new Set(importLines);
const sortedImportLines = Array.from(uniqueImportLines.keys());
sortedImportLines.sort();
return sortedImportLines;
}
const importPaths = getTheSourceFiles();
const importLines = getTheImportLines(importPaths);
const outputFile = `
// THIS IS A GENERATED FILE. DO NOT EDIT BY HAND.
//
// This file is generated by the build-storybook-import-maps script in the UI's base directory.
// This is a *hack* to work around an inconsistency in the way rollup, vite, and storybook
// import CSS modules.
//
// Sometime around 2030 or so, the Javascript community may finally get its collective act together
// and we'll have one unified way of doing this. I can only hope.
export const cssImportMaps = {
${importLines.map(createOneImportLine).join("\n")}
};
`;
fs.writeFileSync(path.join(__dirname, "..", ".storybook", "css-import-maps.ts"), outputFile, {
encoding: "utf8",
flag: "w",
});