@hubpav/hwio-ui 0.7.8 → 0.7.9
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 +6 -5
- package/src/component-sources.mjs +125 -0
- package/src/integration.d.mts +6 -0
- package/src/integration.mjs +9 -5
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hubpav/hwio-ui",
|
|
3
|
-
"version": "0.7.
|
|
3
|
+
"version": "0.7.9",
|
|
4
4
|
"description": "HARDWARIO web design system for Astro: DaisyUI 5 themes generated from the brand tokens, the Tailwind entry stylesheet and the HWio* components.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
@@ -28,12 +28,13 @@
|
|
|
28
28
|
"./html": "./src/html.ts"
|
|
29
29
|
},
|
|
30
30
|
"scripts": {
|
|
31
|
-
"build": "node scripts/build-themes.mjs"
|
|
31
|
+
"build": "node scripts/build-themes.mjs",
|
|
32
|
+
"test": "node --test \"test/**/*.test.mjs\""
|
|
32
33
|
},
|
|
33
34
|
"dependencies": {
|
|
34
|
-
"@hubpav/hwio-brand": "0.7.
|
|
35
|
-
"@hubpav/hwio-web-runtime": "0.7.
|
|
36
|
-
"@hubpav/hwio-web-worker": "0.7.
|
|
35
|
+
"@hubpav/hwio-brand": "0.7.9",
|
|
36
|
+
"@hubpav/hwio-web-runtime": "0.7.9",
|
|
37
|
+
"@hubpav/hwio-web-worker": "0.7.9",
|
|
37
38
|
"daisyui": "5.7.47"
|
|
38
39
|
},
|
|
39
40
|
"peerDependencies": {
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
// Tailwind scans the package's components through `@source "../components"` in styles.css, so a
|
|
2
|
+
// page used to carry DaisyUI and utility rules for every HWio component, rendered or not. The
|
|
3
|
+
// Vite plugin below appends `@source not` lines for the components a site never imports to the
|
|
4
|
+
// site's entry stylesheet (the one that imports `@hubpav/hwio-ui/styles.css`). It keeps every
|
|
5
|
+
// component when it cannot be sure: no import found, or an import it cannot read literally.
|
|
6
|
+
import { existsSync, readdirSync, readFileSync, realpathSync, statSync } from 'node:fs';
|
|
7
|
+
import { dirname, join, relative, resolve, sep } from 'node:path';
|
|
8
|
+
import { fileURLToPath } from 'node:url';
|
|
9
|
+
|
|
10
|
+
export const HWIO_COMPONENTS_DIR = fileURLToPath(new URL('./components/', import.meta.url));
|
|
11
|
+
|
|
12
|
+
const ENTRY_IMPORT = /@import\s+(?:url\()?["']@hubpav\/hwio-ui\/styles\.css["']/;
|
|
13
|
+
const LITERAL_IMPORT = /@hubpav\/hwio-ui\/components\/(HWio[A-Za-z0-9]+)\.astro/g;
|
|
14
|
+
const ANY_IMPORT = /@hubpav\/hwio-ui\/components\//g;
|
|
15
|
+
const LOCAL_IMPORT = /from\s+["']\.\/(HWio[A-Za-z0-9]+)\.astro["']/g;
|
|
16
|
+
// Plain Markdown cannot import a component; MDX can.
|
|
17
|
+
const SCAN_EXT = /\.(astro|[cm]?[jt]sx?|mdx|svelte|vue)$/;
|
|
18
|
+
const SKIP_DIRS = new Set(['node_modules', 'dist', '.astro', '.git', '.wrangler', 'public']);
|
|
19
|
+
|
|
20
|
+
/** Component name -> the components it imports, read from the package's own sources. */
|
|
21
|
+
export function hwioComponentGraph(dir = HWIO_COMPONENTS_DIR) {
|
|
22
|
+
const graph = new Map();
|
|
23
|
+
for (const file of readdirSync(dir)) {
|
|
24
|
+
if (!file.endsWith('.astro')) continue;
|
|
25
|
+
const source = readFileSync(join(dir, file), 'utf8');
|
|
26
|
+
graph.set(file.slice(0, -'.astro'.length), new Set(Array.from(source.matchAll(LOCAL_IMPORT), (m) => m[1])));
|
|
27
|
+
}
|
|
28
|
+
return graph;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* The components to leave out of the scan, given the text of every file that may import one.
|
|
33
|
+
* Returns null (scan everything) when nothing imports a component or an import is not a literal
|
|
34
|
+
* `@hubpav/hwio-ui/components/HWioName.astro` path (a computed path could name any of them).
|
|
35
|
+
*/
|
|
36
|
+
export function hwioUnusedComponents(texts, graph = hwioComponentGraph()) {
|
|
37
|
+
const used = new Set();
|
|
38
|
+
for (const text of texts) {
|
|
39
|
+
const literal = Array.from(text.matchAll(LITERAL_IMPORT), (m) => m[1]);
|
|
40
|
+
if (Array.from(text.matchAll(ANY_IMPORT)).length !== literal.length) return null;
|
|
41
|
+
literal.forEach((name) => used.add(name));
|
|
42
|
+
}
|
|
43
|
+
if (!used.size || [...used].some((name) => !graph.has(name))) return null;
|
|
44
|
+
const stack = [...used];
|
|
45
|
+
while (stack.length) {
|
|
46
|
+
for (const dep of graph.get(stack.pop()) ?? []) {
|
|
47
|
+
if (!used.has(dep)) { used.add(dep); stack.push(dep); }
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
return [...graph.keys()].filter((name) => !used.has(name)).sort();
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
function walk(dir, out) {
|
|
54
|
+
let entries;
|
|
55
|
+
try { entries = readdirSync(dir, { withFileTypes: true }); } catch { return out; }
|
|
56
|
+
for (const entry of entries) {
|
|
57
|
+
if (SKIP_DIRS.has(entry.name)) continue;
|
|
58
|
+
const path = join(dir, entry.name);
|
|
59
|
+
if (entry.isDirectory()) walk(path, out);
|
|
60
|
+
else if (entry.isFile() && SCAN_EXT.test(entry.name)) out.push(path);
|
|
61
|
+
}
|
|
62
|
+
return out;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/** Real directory of an installed dependency, looked up the way Node does (walking up). */
|
|
66
|
+
function packageDir(name, from) {
|
|
67
|
+
for (let dir = from; ; dir = dirname(dir)) {
|
|
68
|
+
const candidate = join(dir, 'node_modules', name);
|
|
69
|
+
if (existsSync(candidate)) return realpathSync(candidate);
|
|
70
|
+
if (dirname(dir) === dir) return null;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Where a site's component imports can live: the project, every local (workspace or `file:`)
|
|
76
|
+
* dependency, and every directory the entry stylesheet itself registers with `@source`.
|
|
77
|
+
*/
|
|
78
|
+
export function hwioScanRoots(root, cssFile, css) {
|
|
79
|
+
const roots = new Set([root]);
|
|
80
|
+
try {
|
|
81
|
+
const pkg = JSON.parse(readFileSync(join(root, 'package.json'), 'utf8'));
|
|
82
|
+
for (const name of Object.keys({ ...pkg.dependencies, ...pkg.devDependencies })) {
|
|
83
|
+
if (name.startsWith('@hubpav/hwio-')) continue; // the design system itself (linked in its own workspace)
|
|
84
|
+
const dir = packageDir(name, root);
|
|
85
|
+
if (dir && !dir.split(sep).includes('node_modules')) roots.add(dir);
|
|
86
|
+
}
|
|
87
|
+
} catch { /* no package.json: the project alone */ }
|
|
88
|
+
for (const m of css.matchAll(/@source\s+(?!not\b)(?!inline\()["']([^"']+)["']/g)) {
|
|
89
|
+
const dir = resolve(dirname(cssFile), m[1]);
|
|
90
|
+
if (!dir.split(sep).includes('node_modules') && existsSync(dir)) roots.add(statSync(dir).isDirectory() ? dir : dirname(dir));
|
|
91
|
+
}
|
|
92
|
+
return [...roots];
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Vite plugin: a `load` hook, so it works whatever order the Tailwind plugin runs in (Tailwind
|
|
97
|
+
* transforms the entry stylesheet after it is loaded and reads its @imports from disk itself).
|
|
98
|
+
* @param {{ root: string, logger?: { info: (msg: string) => void } }} options
|
|
99
|
+
*/
|
|
100
|
+
export function hwioComponentSourcesPlugin({ root, logger }) {
|
|
101
|
+
const graph = hwioComponentGraph();
|
|
102
|
+
const reported = new Set();
|
|
103
|
+
return {
|
|
104
|
+
name: '@hubpav/hwio-ui:component-sources',
|
|
105
|
+
enforce: 'pre',
|
|
106
|
+
load(id) {
|
|
107
|
+
const file = id.split('?')[0];
|
|
108
|
+
if (!file.endsWith('.css') || file.split(sep).includes('node_modules') || !existsSync(file)) return null;
|
|
109
|
+
const css = readFileSync(file, 'utf8');
|
|
110
|
+
if (!ENTRY_IMPORT.test(css)) return null;
|
|
111
|
+
const files = [...new Set(hwioScanRoots(root, file, css).flatMap((dir) => walk(dir, [])))];
|
|
112
|
+
for (const f of files) this.addWatchFile?.(f);
|
|
113
|
+
const unused = hwioUnusedComponents(files.map((f) => readFileSync(f, 'utf8')), graph);
|
|
114
|
+
if (!reported.has(file)) {
|
|
115
|
+
reported.add(file);
|
|
116
|
+
logger?.info(unused
|
|
117
|
+
? `Tailwind scans ${graph.size - unused.length} of ${graph.size} HWio components (${relative(root, file)})`
|
|
118
|
+
: `Tailwind scans all ${graph.size} HWio components (${relative(root, file)})`);
|
|
119
|
+
}
|
|
120
|
+
if (!unused?.length) return null;
|
|
121
|
+
const lines = unused.map((name) => `@source not "${relative(dirname(file), join(HWIO_COMPONENTS_DIR, `${name}.astro`)).split(sep).join('/')}";`);
|
|
122
|
+
return `${css}\n/* @hubpav/hwio-ui: components this site never imports */\n${lines.join('\n')}\n`;
|
|
123
|
+
},
|
|
124
|
+
};
|
|
125
|
+
}
|
package/src/integration.d.mts
CHANGED
|
@@ -7,5 +7,11 @@ export interface HWioUiOptions {
|
|
|
7
7
|
assets?: boolean;
|
|
8
8
|
/** Font families copied into /fonts (file-name prefixes in @hubpav/hwio-brand/dist/fonts). Default ['inter']. */
|
|
9
9
|
fonts?: string[];
|
|
10
|
+
/**
|
|
11
|
+
* 'used' (default): Tailwind scans only the HWio components the site imports (found in the project,
|
|
12
|
+
* its local workspace dependencies and the entry stylesheet's own `@source` directories), so pages
|
|
13
|
+
* carry no rules for components they never render. 'all': scan every component.
|
|
14
|
+
*/
|
|
15
|
+
componentCss?: 'used' | 'all';
|
|
10
16
|
}
|
|
11
17
|
export function hwioUi(options?: HWioUiOptions): AstroIntegration;
|
package/src/integration.mjs
CHANGED
|
@@ -3,12 +3,14 @@
|
|
|
3
3
|
// integrations: [hwioUi({ headers: { gtm: true, turnstile: true, submitEndpoint: 'https://submit.hardwario.com', earlyHints: ['/fonts/inter-latin.woff2', '/fonts/inter-latin-ext.woff2'] } })]
|
|
4
4
|
// It (1) forces component scripts to stay external (script-src 'self' covers them), (2) serves the
|
|
5
5
|
// brand fonts and logos in dev and copies them into the build output, (3) writes `_headers`
|
|
6
|
-
// from one options object so security headers have a single source of truth
|
|
6
|
+
// from one options object so security headers have a single source of truth, (4) limits
|
|
7
|
+
// Tailwind's scan of the package components to the ones the site imports (component-sources.mjs).
|
|
7
8
|
import { copyFileSync, mkdirSync, readdirSync, writeFileSync, existsSync, readFileSync } from 'node:fs';
|
|
8
9
|
import { join, dirname, extname } from 'node:path';
|
|
9
10
|
import { fileURLToPath } from 'node:url';
|
|
10
11
|
import { createRequire } from 'node:module';
|
|
11
12
|
import { hwioRenderHeadersFile } from '@hubpav/hwio-web-worker/headers';
|
|
13
|
+
import { hwioComponentSourcesPlugin } from './component-sources.mjs';
|
|
12
14
|
|
|
13
15
|
const require = createRequire(import.meta.url);
|
|
14
16
|
|
|
@@ -19,20 +21,22 @@ function brandDir() {
|
|
|
19
21
|
const MIME = { '.woff2': 'font/woff2', '.svg': 'image/svg+xml', '.png': 'image/png', '.ico': 'image/x-icon' };
|
|
20
22
|
|
|
21
23
|
/**
|
|
22
|
-
* @param {{ headers?: import('@hubpav/hwio-web-worker/headers').HWioHeadersOptions | false, assets?: boolean, fonts?: string[] }} [options]
|
|
24
|
+
* @param {{ headers?: import('@hubpav/hwio-web-worker/headers').HWioHeadersOptions | false, assets?: boolean, fonts?: string[], componentCss?: 'used' | 'all' }} [options]
|
|
23
25
|
* fonts: font families (file-name prefixes in @hubpav/hwio-brand/dist/fonts) copied to /fonts; default ['inter'].
|
|
26
|
+
* componentCss: 'used' (default) emits CSS for the components the site imports, 'all' for every component.
|
|
24
27
|
* @returns {import('astro').AstroIntegration}
|
|
25
28
|
*/
|
|
26
29
|
export function hwioUi(options = {}) {
|
|
27
|
-
const { headers = false, assets = true, fonts = ['inter'] } = options;
|
|
30
|
+
const { headers = false, assets = true, fonts = ['inter'], componentCss = 'used' } = options;
|
|
28
31
|
const brand = brandDir();
|
|
29
32
|
const fontsDir = join(brand, 'dist/fonts');
|
|
30
33
|
const logosDir = join(brand, 'logos');
|
|
31
34
|
return {
|
|
32
35
|
name: '@hubpav/hwio-ui',
|
|
33
36
|
hooks: {
|
|
34
|
-
'astro:config:setup': ({ updateConfig }) => {
|
|
35
|
-
|
|
37
|
+
'astro:config:setup': ({ updateConfig, config, logger }) => {
|
|
38
|
+
const plugins = componentCss === 'used' ? [hwioComponentSourcesPlugin({ root: fileURLToPath(config.root), logger })] : [];
|
|
39
|
+
updateConfig({ vite: { build: { assetsInlineLimit: 0 }, plugins } });
|
|
36
40
|
},
|
|
37
41
|
'astro:server:setup': ({ server }) => {
|
|
38
42
|
if (!assets) return;
|