@42-central/vite-plugin-theme 0.0.0 → 0.17.0
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/dist/index.d.ts +21 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +23 -0
- package/dist/index.js.map +1 -0
- package/package.json +38 -3
- package/src/index.test.ts +73 -0
- package/src/index.ts +37 -0
- package/README.md +0 -3
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { type BuildThemeInitScriptOptions } from "@42-central/ui-react";
|
|
2
|
+
import type { Plugin } from "vite";
|
|
3
|
+
export interface ThemeInitPluginOptions extends BuildThemeInitScriptOptions {
|
|
4
|
+
/** CSP nonce for the injected `<script>`. */
|
|
5
|
+
nonce?: string;
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* Injects `@42-central/ui-react`'s blocking anti-FOUC theme-init script as the
|
|
9
|
+
* first tag in `<head>`, at both `vite dev` and `vite build` time — via
|
|
10
|
+
* Vite's `transformIndexHtml` hook, so `index.html` itself never needs
|
|
11
|
+
* manual editing. Must render a plain classic script, not `type="module"`
|
|
12
|
+
* (Vite treats module scripts as deferred, which would reintroduce the
|
|
13
|
+
* flash this exists to prevent) — which is why this goes through the tag
|
|
14
|
+
* descriptor API rather than a hand-authored `<script>` in `index.html`.
|
|
15
|
+
*
|
|
16
|
+
* // vite.config.ts
|
|
17
|
+
* import { themeInitPlugin } from "@42-central/vite-plugin-theme"
|
|
18
|
+
* export default defineConfig({ plugins: [themeInitPlugin()] })
|
|
19
|
+
*/
|
|
20
|
+
export declare function themeInitPlugin(options?: ThemeInitPluginOptions): Plugin;
|
|
21
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,2BAA2B,EAAwB,MAAM,cAAc,CAAA;AACrF,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,MAAM,CAAA;AAElC,MAAM,WAAW,sBAAuB,SAAQ,2BAA2B;IACzE,6CAA6C;IAC7C,KAAK,CAAC,EAAE,MAAM,CAAA;CACf;AAED;;;;;;;;;;;;GAYG;AACH,wBAAgB,eAAe,CAAC,OAAO,GAAE,sBAA2B,GAAG,MAAM,CAe5E"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { buildThemeInitScript } from '@42-central/ui-react';
|
|
2
|
+
|
|
3
|
+
// src/index.ts
|
|
4
|
+
function themeInitPlugin(options = {}) {
|
|
5
|
+
const { nonce, ...scriptOptions } = options;
|
|
6
|
+
return {
|
|
7
|
+
name: "42-theme-init",
|
|
8
|
+
transformIndexHtml() {
|
|
9
|
+
return [
|
|
10
|
+
{
|
|
11
|
+
tag: "script",
|
|
12
|
+
injectTo: "head-prepend",
|
|
13
|
+
attrs: nonce ? { nonce } : void 0,
|
|
14
|
+
children: buildThemeInitScript(scriptOptions)
|
|
15
|
+
}
|
|
16
|
+
];
|
|
17
|
+
}
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export { themeInitPlugin };
|
|
22
|
+
//# sourceMappingURL=index.js.map
|
|
23
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"names":[],"mappings":";;;AAqBO,SAAS,eAAA,CAAgB,OAAA,GAAkC,EAAC,EAAW;AAC5E,EAAA,MAAM,EAAE,KAAA,EAAO,GAAG,aAAA,EAAc,GAAI,OAAA;AACpC,EAAA,OAAO;AAAA,IACL,IAAA,EAAM,eAAA;AAAA,IACN,kBAAA,GAAqB;AACnB,MAAA,OAAO;AAAA,QACL;AAAA,UACE,GAAA,EAAK,QAAA;AAAA,UACL,QAAA,EAAU,cAAA;AAAA,UACV,KAAA,EAAO,KAAA,GAAQ,EAAE,KAAA,EAAM,GAAI,MAAA;AAAA,UAC3B,QAAA,EAAU,qBAAqB,aAAa;AAAA;AAC9C,OACF;AAAA,IACF;AAAA,GACF;AACF","file":"index.js","sourcesContent":["import { type BuildThemeInitScriptOptions, buildThemeInitScript } from \"@42-central/ui-react\"\nimport type { Plugin } from \"vite\"\n\nexport interface ThemeInitPluginOptions extends BuildThemeInitScriptOptions {\n /** CSP nonce for the injected `<script>`. */\n nonce?: string\n}\n\n/**\n * Injects `@42-central/ui-react`'s blocking anti-FOUC theme-init script as the\n * first tag in `<head>`, at both `vite dev` and `vite build` time — via\n * Vite's `transformIndexHtml` hook, so `index.html` itself never needs\n * manual editing. Must render a plain classic script, not `type=\"module\"`\n * (Vite treats module scripts as deferred, which would reintroduce the\n * flash this exists to prevent) — which is why this goes through the tag\n * descriptor API rather than a hand-authored `<script>` in `index.html`.\n *\n * // vite.config.ts\n * import { themeInitPlugin } from \"@42-central/vite-plugin-theme\"\n * export default defineConfig({ plugins: [themeInitPlugin()] })\n */\nexport function themeInitPlugin(options: ThemeInitPluginOptions = {}): Plugin {\n const { nonce, ...scriptOptions } = options\n return {\n name: \"42-theme-init\",\n transformIndexHtml() {\n return [\n {\n tag: \"script\",\n injectTo: \"head-prepend\",\n attrs: nonce ? { nonce } : undefined,\n children: buildThemeInitScript(scriptOptions),\n },\n ]\n },\n }\n}\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,41 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@42-central/vite-plugin-theme",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "
|
|
5
|
-
"license": "UNLICENSED"
|
|
3
|
+
"version": "0.17.0",
|
|
4
|
+
"description": "Vite plugin that injects @42/ui-react's anti-FOUC theme init script into index.html",
|
|
5
|
+
"license": "UNLICENSED",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"files": [
|
|
8
|
+
"dist",
|
|
9
|
+
"src",
|
|
10
|
+
"README.md"
|
|
11
|
+
],
|
|
12
|
+
"exports": {
|
|
13
|
+
".": {
|
|
14
|
+
"types": "./dist/index.d.ts",
|
|
15
|
+
"import": "./dist/index.js"
|
|
16
|
+
}
|
|
17
|
+
},
|
|
18
|
+
"scripts": {
|
|
19
|
+
"build": "tsup && tsc -p tsconfig.build.json",
|
|
20
|
+
"dev": "tsup --watch",
|
|
21
|
+
"typecheck": "tsc --noEmit",
|
|
22
|
+
"test": "vitest run",
|
|
23
|
+
"test:watch": "vitest",
|
|
24
|
+
"clean": "rm -rf dist .turbo"
|
|
25
|
+
},
|
|
26
|
+
"peerDependencies": {
|
|
27
|
+
"vite": "^7.0.0 || ^8.0.0"
|
|
28
|
+
},
|
|
29
|
+
"dependencies": {
|
|
30
|
+
"@42-central/ui-react": "0.17.0"
|
|
31
|
+
},
|
|
32
|
+
"devDependencies": {
|
|
33
|
+
"@42/tsconfig": "workspace:*",
|
|
34
|
+
"@42/tsup-config": "workspace:*",
|
|
35
|
+
"jsdom": "^30.0.1",
|
|
36
|
+
"tsup": "^8.5.1",
|
|
37
|
+
"typescript": "catalog:",
|
|
38
|
+
"vite": "^8.3.0",
|
|
39
|
+
"vitest": "^5.0.1"
|
|
40
|
+
}
|
|
6
41
|
}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { afterEach, describe, expect, it, vi } from "vitest"
|
|
2
|
+
import { themeInitPlugin } from "./index"
|
|
3
|
+
|
|
4
|
+
function mockMatchMedia(matches: boolean) {
|
|
5
|
+
Object.defineProperty(window, "matchMedia", {
|
|
6
|
+
writable: true,
|
|
7
|
+
value: vi.fn().mockReturnValue({ matches }),
|
|
8
|
+
})
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
afterEach(() => {
|
|
12
|
+
localStorage.clear()
|
|
13
|
+
document.documentElement.removeAttribute("data-theme")
|
|
14
|
+
})
|
|
15
|
+
|
|
16
|
+
function getInjectedScript(options?: Parameters<typeof themeInitPlugin>[0]) {
|
|
17
|
+
const plugin = themeInitPlugin(options)
|
|
18
|
+
const transform = plugin.transformIndexHtml
|
|
19
|
+
const hook = typeof transform === "function" ? transform : transform?.handler
|
|
20
|
+
if (typeof hook !== "function") throw new Error("expected a transformIndexHtml function")
|
|
21
|
+
const tags = hook.call({} as ThisParameterType<typeof hook>, "", {} as never)
|
|
22
|
+
if (!Array.isArray(tags)) throw new Error("expected an array of tag descriptors")
|
|
23
|
+
const [tag] = tags
|
|
24
|
+
if (!tag) throw new Error("expected at least one tag descriptor")
|
|
25
|
+
return tag
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
describe("themeInitPlugin", () => {
|
|
29
|
+
it("returns a single script tag, prepended to head", () => {
|
|
30
|
+
const tag = getInjectedScript()
|
|
31
|
+
expect(tag.tag).toBe("script")
|
|
32
|
+
expect(tag.injectTo).toBe("head-prepend")
|
|
33
|
+
})
|
|
34
|
+
|
|
35
|
+
it("the injected script applies the persisted mode when executed", () => {
|
|
36
|
+
localStorage.setItem("ui-theme", JSON.stringify("dark"))
|
|
37
|
+
mockMatchMedia(false)
|
|
38
|
+
|
|
39
|
+
const tag = getInjectedScript()
|
|
40
|
+
new Function(tag.children as string)()
|
|
41
|
+
|
|
42
|
+
expect(document.documentElement.getAttribute("data-theme")).toBe("dark")
|
|
43
|
+
})
|
|
44
|
+
|
|
45
|
+
it("forwards forceColorScheme through to the generated script", () => {
|
|
46
|
+
localStorage.setItem("ui-theme", JSON.stringify("dark"))
|
|
47
|
+
mockMatchMedia(true)
|
|
48
|
+
|
|
49
|
+
const tag = getInjectedScript({ forceColorScheme: "light" })
|
|
50
|
+
new Function(tag.children as string)()
|
|
51
|
+
|
|
52
|
+
expect(document.documentElement.getAttribute("data-theme")).toBe("light")
|
|
53
|
+
})
|
|
54
|
+
|
|
55
|
+
it("forwards defaultColorScheme through to the generated script", () => {
|
|
56
|
+
mockMatchMedia(true) // OS prefers dark — should be ignored
|
|
57
|
+
|
|
58
|
+
const tag = getInjectedScript({ defaultColorScheme: "light" })
|
|
59
|
+
new Function(tag.children as string)()
|
|
60
|
+
|
|
61
|
+
expect(document.documentElement.getAttribute("data-theme")).toBe("light")
|
|
62
|
+
})
|
|
63
|
+
|
|
64
|
+
it("sets a CSP nonce attribute when provided", () => {
|
|
65
|
+
const tag = getInjectedScript({ nonce: "abc123" })
|
|
66
|
+
expect(tag.attrs).toEqual({ nonce: "abc123" })
|
|
67
|
+
})
|
|
68
|
+
|
|
69
|
+
it("omits attrs entirely when no nonce is given", () => {
|
|
70
|
+
const tag = getInjectedScript()
|
|
71
|
+
expect(tag.attrs).toBeUndefined()
|
|
72
|
+
})
|
|
73
|
+
})
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { type BuildThemeInitScriptOptions, buildThemeInitScript } from "@42/ui-react"
|
|
2
|
+
import type { Plugin } from "vite"
|
|
3
|
+
|
|
4
|
+
export interface ThemeInitPluginOptions extends BuildThemeInitScriptOptions {
|
|
5
|
+
/** CSP nonce for the injected `<script>`. */
|
|
6
|
+
nonce?: string
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Injects `@42/ui-react`'s blocking anti-FOUC theme-init script as the
|
|
11
|
+
* first tag in `<head>`, at both `vite dev` and `vite build` time — via
|
|
12
|
+
* Vite's `transformIndexHtml` hook, so `index.html` itself never needs
|
|
13
|
+
* manual editing. Must render a plain classic script, not `type="module"`
|
|
14
|
+
* (Vite treats module scripts as deferred, which would reintroduce the
|
|
15
|
+
* flash this exists to prevent) — which is why this goes through the tag
|
|
16
|
+
* descriptor API rather than a hand-authored `<script>` in `index.html`.
|
|
17
|
+
*
|
|
18
|
+
* // vite.config.ts
|
|
19
|
+
* import { themeInitPlugin } from "@42/vite-plugin-theme"
|
|
20
|
+
* export default defineConfig({ plugins: [themeInitPlugin()] })
|
|
21
|
+
*/
|
|
22
|
+
export function themeInitPlugin(options: ThemeInitPluginOptions = {}): Plugin {
|
|
23
|
+
const { nonce, ...scriptOptions } = options
|
|
24
|
+
return {
|
|
25
|
+
name: "42-theme-init",
|
|
26
|
+
transformIndexHtml() {
|
|
27
|
+
return [
|
|
28
|
+
{
|
|
29
|
+
tag: "script",
|
|
30
|
+
injectTo: "head-prepend",
|
|
31
|
+
attrs: nonce ? { nonce } : undefined,
|
|
32
|
+
children: buildThemeInitScript(scriptOptions),
|
|
33
|
+
},
|
|
34
|
+
]
|
|
35
|
+
},
|
|
36
|
+
}
|
|
37
|
+
}
|
package/README.md
DELETED