web: Clean up docs, types.

This commit is contained in:
Teffen Ellis
2025-05-19 19:41:59 +02:00
parent b8e394227e
commit c50793133b
9 changed files with 42 additions and 28 deletions

View File

@ -3,8 +3,9 @@
* @import { StorybookConfig } from "@storybook/web-components-vite"; * @import { StorybookConfig } from "@storybook/web-components-vite";
* @import { InlineConfig, Plugin } from "vite"; * @import { InlineConfig, Plugin } from "vite";
*/ */
import { createBundleDefinitions } from "@goauthentik/web/scripts/esbuild/environment"; import { createBundleDefinitions } from "@goauthentik/web/bundler/utils/node";
import postcssLit from "rollup-plugin-postcss-lit"; import postcssLit from "rollup-plugin-postcss-lit";
import { mergeConfig } from "vite";
import tsconfigPaths from "vite-tsconfig-paths"; import tsconfigPaths from "vite-tsconfig-paths";
const CSSImportPattern = /import [\w$]+ from .+\.(css)/g; const CSSImportPattern = /import [\w$]+ from .+\.(css)/g;
@ -28,6 +29,11 @@ const inlineCSSPlugin = {
}, },
}; };
/**
* @satisfies {InlineConfig}
*/
// const viteFinal = ;
/** /**
* @satisfies {StorybookConfig} * @satisfies {StorybookConfig}
*/ */
@ -46,18 +52,16 @@ const config = {
docs: { docs: {
autodocs: "tag", autodocs: "tag",
}, },
viteFinal({ plugins = [], ...config }) { viteFinal(config) {
/** /**
* @satisfies {InlineConfig} * @satisfies {InlineConfig}
*/ */
const mergedConfig = { const overrides = {
...config,
define: createBundleDefinitions(), define: createBundleDefinitions(),
plugins: [inlineCSSPlugin, ...plugins, postcssLit(), tsconfigPaths()], plugins: [inlineCSSPlugin, postcssLit(), tsconfigPaths()],
}; };
return mergedConfig; return mergeConfig(config, overrides);
}, },
}; };
export default config; export default config;

View File

@ -35,15 +35,25 @@
"type": "module", "type": "module",
"exports": { "exports": {
"./package.json": "./package.json", "./package.json": "./package.json",
"./paths": "./paths.js",
"./scripts/*": "./scripts/*.mjs",
"./elements/*": "./src/elements/*", "./elements/*": "./src/elements/*",
"./common/*": "./src/common/*", "./common/*": "./src/common/*",
"./components/*": "./src/components/*", "./components/*": "./src/components/*",
"./flow/*": "./src/flow/*", "./flow/*": "./src/flow/*",
"./locales/*": "./src/locales/*", "./locales/*": "./src/locales/*",
"./user/*": "./src/user/*", "./user/*": "./src/user/*",
"./admin/*": "./src/admin/*" "./admin/*": "./src/admin/*",
"./*/browser": {
"types": "./out/*/browser.d.ts",
"import": "./*/browser.js"
},
"./*/node": {
"types": "./out/*/node.d.ts",
"import": "./*/node.js"
},
"./*": {
"types": "./out/*/index.d.ts",
"import": "./*/index.js"
}
}, },
"dependencies": { "dependencies": {
"@codemirror/lang-css": "^6.3.1", "@codemirror/lang-css": "^6.3.1",

View File

@ -21,7 +21,7 @@ const log = console.debug.bind(console, logPrefix);
* ESBuild may tree-shake it out of production builds. * ESBuild may tree-shake it out of production builds.
* *
* ```ts * ```ts
* if (import.meta.env.NODE_ENV=== "development") { * if (process.env.NODE_ENV=== "development") {
* await import("@goauthentik/esbuild-plugin-live-reload/client") * await import("@goauthentik/esbuild-plugin-live-reload/client")
* .catch(() => console.warn("Failed to import watcher")) * .catch(() => console.warn("Failed to import watcher"))
* } * }

View File

@ -13,6 +13,8 @@
* If you need to check the environment at runtime, use `process.env.NODE_ENV` to * If you need to check the environment at runtime, use `process.env.NODE_ENV` to
* ensure that module tree-shaking works correctly. * ensure that module tree-shaking works correctly.
* *
* @category Environment
* @runtime node
*/ */
export const NodeEnvironment = process.env.NODE_ENV || "development"; export const NodeEnvironment = process.env.NODE_ENV || "development";
@ -24,6 +26,7 @@ export const NodeEnvironment = process.env.NODE_ENV || "development";
/** /**
* A type helper for serializing environment variables. * A type helper for serializing environment variables.
* *
* @category Environment
* @template {EnvironmentVariable} T * @template {EnvironmentVariable} T
* @typedef {T extends string ? `"${T}"` : T} JSONify * @typedef {T extends string ? `"${T}"` : T} JSONify
*/ */
@ -33,8 +36,13 @@ export const NodeEnvironment = process.env.NODE_ENV || "development";
//#region Utilities //#region Utilities
/** /**
* Given an object of environment variables, returns a new object with the same keys and values, but * Given an object of environment variables, serializes them into a mapping of
* with the values serialized as strings. * environment variable names to their respective runtime constants.
*
* This is useful for defining environment variables while bundling with ESBuild, Vite, etc.
*
* @category Environment
* @runtime node
* *
* @template {Record<string, EnvironmentVariable>} EnvRecord * @template {Record<string, EnvironmentVariable>} EnvRecord
* @template {string} [Prefix='import.meta.env.'] * @template {string} [Prefix='import.meta.env.']
@ -48,16 +56,9 @@ export function serializeEnvironmentVars(
input, input,
prefix = /** @type {Prefix} */ ("import.meta.env."), prefix = /** @type {Prefix} */ ("import.meta.env."),
) { ) {
/** const env = Object.fromEntries(
* @type {Record<string, string>} Object.entries(input).map(([key, value]) => [prefix + key, JSON.stringify(value ?? "")]),
*/ );
const env = {};
for (const [key, value] of Object.entries(input)) {
const namespaceKey = prefix + key;
env[namespaceKey] = JSON.stringify(value || "");
}
return /** @type {any} */ (env); return /** @type {any} */ (env);
} }

View File

@ -24,6 +24,5 @@ export function createBundleDefinitions() {
// We need to explicitly set this for NPM packages that use `process` // We need to explicitly set this for NPM packages that use `process`
// to determine their environment. // to determine their environment.
"process.env.NODE_ENV": SerializedNodeEnvironment, "process.env.NODE_ENV": SerializedNodeEnvironment,
"import.meta.env.NODE_ENV": SerializedNodeEnvironment,
}; };
} }

View File

@ -43,7 +43,7 @@ import {
renderSidebarItems, renderSidebarItems,
} from "./AdminSidebar.js"; } from "./AdminSidebar.js";
if (import.meta.env.NODE_ENV === "development") { if (process.env.NODE_ENV === "development") {
await import("@goauthentik/esbuild-plugin-live-reload/client"); await import("@goauthentik/esbuild-plugin-live-reload/client");
} }

View File

@ -223,7 +223,7 @@ export function inspectStyleSheetTree(element: ReactiveElement): InspectedStyleS
}; };
} }
if (import.meta.env.NODE_ENV === "development") { if (process.env.NODE_ENV === "development") {
Object.assign(window, { Object.assign(window, {
inspectStyleSheetTree, inspectStyleSheetTree,
serializeStyleSheet, serializeStyleSheet,

View File

@ -14,6 +14,6 @@ import "@goauthentik/flow/stages/password/PasswordStage";
// end of stage import // end of stage import
if (import.meta.env.NODE_ENV === "development") { if (process.env.NODE_ENV === "development") {
await import("@goauthentik/esbuild-plugin-live-reload/client"); await import("@goauthentik/esbuild-plugin-live-reload/client");
} }

View File

@ -43,7 +43,7 @@ import PFDisplay from "@patternfly/patternfly/utilities/Display/display.css";
import { CurrentBrand, EventsApi, SessionUser } from "@goauthentik/api"; import { CurrentBrand, EventsApi, SessionUser } from "@goauthentik/api";
if (import.meta.env.NODE_ENV === "development") { if (process.env.NODE_ENV === "development") {
await import("@goauthentik/esbuild-plugin-live-reload/client"); await import("@goauthentik/esbuild-plugin-live-reload/client");
} }