vueless 1.5.1-beta.5 → 1.5.1-beta.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vueless",
3
- "version": "1.5.1-beta.5",
3
+ "version": "1.5.1-beta.6",
4
4
  "description": "Vue Styleless UI Component Library, powered by Tailwind CSS.",
5
5
  "author": "Johnny Grid <hello@vueless.com> (https://vueless.com)",
6
6
  "homepage": "https://vueless.com",
@@ -0,0 +1,48 @@
1
+ import { describe, it, expect, vi, beforeEach } from "vitest";
2
+
3
+ import { COLOR_MODE_KEY } from "../../constants.js";
4
+ import { loadSSRVuelessConfig, setVuelessConfig, vuelessConfig } from "../ui";
5
+
6
+ import type { Config } from "../../types";
7
+
8
+ /* jsdom doesn't implement matchMedia, which theme.ts evaluates on import. */
9
+ const prefersDark = { matches: false, addEventListener: vi.fn(), removeEventListener: vi.fn() };
10
+
11
+ window.matchMedia = vi.fn(() => prefersDark) as unknown as typeof window.matchMedia;
12
+
13
+ const { getThemeCookieName } = await import("../theme");
14
+
15
+ beforeEach(() => {
16
+ Object.keys(vuelessConfig).forEach((key) => delete vuelessConfig[key as keyof Config]);
17
+ });
18
+
19
+ describe("loadSSRVuelessConfig", () => {
20
+ it.each([
21
+ ["resolves", () => ({ cookiePrefix: "from-file" })],
22
+ [
23
+ "fails",
24
+ () => {
25
+ throw new Error("Cannot find module '/vueless.config'");
26
+ },
27
+ ],
28
+ ])("keeps the injected config when a deferred import %s", async (_, settle: () => Config) => {
29
+ let release!: () => void;
30
+ const released = new Promise<void>((resolve) => (release = resolve));
31
+
32
+ const loading = loadSSRVuelessConfig(() => released.then(() => ({ default: settle() })));
33
+
34
+ setVuelessConfig({ cookiePrefix: "app-a" });
35
+ expect(getThemeCookieName(COLOR_MODE_KEY)).toBe(`app-a-${COLOR_MODE_KEY}`);
36
+
37
+ release();
38
+ await loading;
39
+
40
+ expect(getThemeCookieName(COLOR_MODE_KEY)).toBe(`app-a-${COLOR_MODE_KEY}`);
41
+ });
42
+
43
+ it("uses the imported config when none was injected", async () => {
44
+ await loadSSRVuelessConfig(async () => ({ default: { cookiePrefix: "from-file" } }));
45
+
46
+ expect(getThemeCookieName(COLOR_MODE_KEY)).toBe(`from-file-${COLOR_MODE_KEY}`);
47
+ });
48
+ });
package/utils/ui.ts CHANGED
@@ -48,17 +48,22 @@ if (isCSR) {
48
48
  )[0] || {};
49
49
  }
50
50
 
51
- if (isSSR) {
52
- (async () => {
53
- try {
54
- // @ts-expect-error: vueless.config.{js,ts} is optional
55
- vuelessConfig = (await import(/* @vite-ignore */ "/vueless.config")).default;
56
- } catch {
57
- vuelessConfig = {};
58
- }
59
- })();
51
+ /** Load Vueless config on the server; a config injected via `createVueless` meanwhile wins. */
52
+ export async function loadSSRVuelessConfig(
53
+ // @ts-expect-error: vueless.config.{js,ts} is optional
54
+ importConfig = () => import(/* @vite-ignore */ "/vueless.config"),
55
+ ) {
56
+ try {
57
+ const { default: config } = await importConfig();
58
+
59
+ if (!Object.keys(vuelessConfig).length) vuelessConfig = config;
60
+ } catch {
61
+ /* No config file: keep the current config. */
62
+ }
60
63
  }
61
64
 
65
+ if (isSSR) loadSSRVuelessConfig();
66
+
62
67
  /**
63
68
  * Extend twMerge (tailwind merge) by vueless and user config:
64
69
  */