@elmoorx/css 2.0.0-alpha.25
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/LICENSE +21 -0
- package/README.md +33 -0
- package/package.json +31 -0
- package/src/index.ts +180 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Wafra Framework
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
# @wafra/css
|
|
2
|
+
|
|
3
|
+
> Zero-runtime CSS system with atomic classes, theming, RTL support
|
|
4
|
+
|
|
5
|
+
Part of the [Wafra Framework](https://github.com/wafra/framework) — Build fast. Run anywhere. Stay secure.
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm install @wafra/css
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Quick Start
|
|
14
|
+
|
|
15
|
+
```typescript
|
|
16
|
+
import { /* exports */ } from '@wafra/css';
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
## Features
|
|
20
|
+
|
|
21
|
+
- Zero external dependencies
|
|
22
|
+
- Full TypeScript support
|
|
23
|
+
- Tree-shakeable
|
|
24
|
+
- Edge-runtime compatible
|
|
25
|
+
- Arabic/RTL friendly
|
|
26
|
+
|
|
27
|
+
## Documentation
|
|
28
|
+
|
|
29
|
+
See [https://wafra.dev/docs/css](https://wafra.dev/docs/css)
|
|
30
|
+
|
|
31
|
+
## License
|
|
32
|
+
|
|
33
|
+
MIT © Wafra Framework
|
package/package.json
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@elmoorx/css",
|
|
3
|
+
"version": "2.0.0-alpha.25",
|
|
4
|
+
"description": "Zero-runtime CSS system with atomic classes, theming, RTL support",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./src/index.ts",
|
|
7
|
+
"types": "./src/index.ts",
|
|
8
|
+
"scripts": {
|
|
9
|
+
"build": "tsc"
|
|
10
|
+
},
|
|
11
|
+
"license": "MIT",
|
|
12
|
+
"author": "Wafra Framework",
|
|
13
|
+
"homepage": "https://wafra.dev/packages/css",
|
|
14
|
+
"repository": {
|
|
15
|
+
"type": "git",
|
|
16
|
+
"url": "https://github.com/wafra/framework",
|
|
17
|
+
"directory": "packages/css"
|
|
18
|
+
},
|
|
19
|
+
"bugs": {
|
|
20
|
+
"url": "https://github.com/wafra/framework/issues"
|
|
21
|
+
},
|
|
22
|
+
"keywords": [
|
|
23
|
+
"wafra",
|
|
24
|
+
"framework",
|
|
25
|
+
"css"
|
|
26
|
+
],
|
|
27
|
+
"sideEffects": false,
|
|
28
|
+
"exports": {
|
|
29
|
+
".": "./src/index.ts"
|
|
30
|
+
}
|
|
31
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Wafra CSS — Scoped styles + CSS Modules
|
|
3
|
+
* ============================================
|
|
4
|
+
* Two approaches, zero runtime cost:
|
|
5
|
+
*
|
|
6
|
+
* 1. Scoped <style> in components (auto-scoped by attribute)
|
|
7
|
+
*
|
|
8
|
+
* <style scoped>
|
|
9
|
+
* .btn { background: purple; }
|
|
10
|
+
* </style>
|
|
11
|
+
* <button class="btn">Click</button>
|
|
12
|
+
*
|
|
13
|
+
* → Compiles to:
|
|
14
|
+
* <style>.btn[data-wafra-abc123] { background: purple; }</style>
|
|
15
|
+
* <button class="btn" data-wafra-abc123>Click</button>
|
|
16
|
+
*
|
|
17
|
+
* 2. CSS Modules (className hashing)
|
|
18
|
+
*
|
|
19
|
+
* import styles from './Button.module.css';
|
|
20
|
+
* <button class={styles.btn}>Click</button>
|
|
21
|
+
*
|
|
22
|
+
* → styles.btn = "Button_btn_abc123"
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
import { readFileSync, existsSync, mkdirSync, writeFileSync } from "node:fs";
|
|
26
|
+
import { join, dirname, basename, extname } from "node:path";
|
|
27
|
+
import { createHash } from "node:crypto";
|
|
28
|
+
|
|
29
|
+
export interface CompiledCss {
|
|
30
|
+
// The transformed CSS — class names hashed, selectors scoped
|
|
31
|
+
css: string;
|
|
32
|
+
// Map of original className → hashed className
|
|
33
|
+
mappings: Record<string, string>;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Compile a CSS Module file.
|
|
38
|
+
* Hashes class names so they're unique per file.
|
|
39
|
+
*
|
|
40
|
+
* const { css, mappings } = compileCssModule('./Button.module.css');
|
|
41
|
+
* // mappings: { btn: 'Button_btn_a1b2c3', card: 'Button_card_d4e5f6' }
|
|
42
|
+
*/
|
|
43
|
+
export function compileCssModule(filePath: string): CompiledCss {
|
|
44
|
+
if (!existsSync(filePath)) {
|
|
45
|
+
return { css: "", mappings: {} };
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
const source = readFileSync(filePath, "utf-8");
|
|
49
|
+
const baseName = basename(filePath, extname(filePath));
|
|
50
|
+
const fileHash = createHash("md5")
|
|
51
|
+
.update(source)
|
|
52
|
+
.digest("hex")
|
|
53
|
+
.slice(0, 6);
|
|
54
|
+
|
|
55
|
+
const mappings: Record<string, string> = {};
|
|
56
|
+
|
|
57
|
+
// Find all class names
|
|
58
|
+
const classRegex = /\.([a-zA-Z_][a-zA-Z0-9_-]*)/g;
|
|
59
|
+
const classNames = new Set<string>();
|
|
60
|
+
let match: RegExpExecArray | null;
|
|
61
|
+
while ((match = classRegex.exec(source)) !== null) {
|
|
62
|
+
classNames.add(match[1]);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// Build mappings
|
|
66
|
+
for (const cls of classNames) {
|
|
67
|
+
mappings[cls] = `${baseName}_${cls}_${fileHash}`;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// Replace .className with .hashedName
|
|
71
|
+
let css = source;
|
|
72
|
+
for (const [original, hashed] of Object.entries(mappings)) {
|
|
73
|
+
css = css.replace(
|
|
74
|
+
new RegExp(`\\.${original}\\b`, "g"),
|
|
75
|
+
`.${hashed}`
|
|
76
|
+
);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
return { css, mappings };
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Compile scoped CSS — auto-scoped by a unique data attribute.
|
|
84
|
+
* Used internally by the compiler when it encounters <style scoped>.
|
|
85
|
+
*/
|
|
86
|
+
export function compileScopedCss(
|
|
87
|
+
source: string,
|
|
88
|
+
componentId: string
|
|
89
|
+
): string {
|
|
90
|
+
// Add [data-wafra-<id>] to every selector
|
|
91
|
+
// Simple approach: split on `{` and modify each selector
|
|
92
|
+
return source.replace(
|
|
93
|
+
/([^{}]+)\{/g,
|
|
94
|
+
(_, selectors: string) => {
|
|
95
|
+
const scoped = selectors
|
|
96
|
+
.split(",")
|
|
97
|
+
.map((s) => {
|
|
98
|
+
const trimmed = s.trim();
|
|
99
|
+
if (!trimmed) return trimmed;
|
|
100
|
+
// Don't scope :global() selectors
|
|
101
|
+
if (trimmed.startsWith(":global")) {
|
|
102
|
+
return trimmed.replace(/^:global\(/, "").replace(/\)$/, "");
|
|
103
|
+
}
|
|
104
|
+
// Don't scope @keyframes, @media, etc.
|
|
105
|
+
if (trimmed.startsWith("@")) return trimmed;
|
|
106
|
+
// Add data attribute to the last selector
|
|
107
|
+
return `${trimmed}[data-wafra-${componentId}]`;
|
|
108
|
+
})
|
|
109
|
+
.join(", ");
|
|
110
|
+
return ` ${scoped} {`;
|
|
111
|
+
}
|
|
112
|
+
);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* Generate a unique component ID for scoped CSS.
|
|
117
|
+
*/
|
|
118
|
+
export function generateComponentId(filePath: string): string {
|
|
119
|
+
return createHash("md5")
|
|
120
|
+
.update(filePath)
|
|
121
|
+
.digest("hex")
|
|
122
|
+
.slice(0, 8);
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* Bundle all CSS into a single file.
|
|
127
|
+
*/
|
|
128
|
+
export function bundleCss(cssFiles: string[]): string {
|
|
129
|
+
return cssFiles.join("\n\n/* === next file === */\n\n");
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* Minify CSS (very simplified — production would use lightningcss).
|
|
134
|
+
*/
|
|
135
|
+
export function minifyCss(css: string): string {
|
|
136
|
+
return css
|
|
137
|
+
.replace(/\/\*[\s\S]*?\*\//g, "") // comments
|
|
138
|
+
.replace(/\s+/g, " ") // whitespace
|
|
139
|
+
.replace(/\s*([{}:;,])\s*/g, "$1") // around punctuation
|
|
140
|
+
.replace(/;}/g, "}") // trailing semicolons
|
|
141
|
+
.trim();
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* Process a Wafra component file with <style scoped> blocks.
|
|
146
|
+
* Extracts the CSS, scopes it, and replaces the block with nothing
|
|
147
|
+
* (the CSS gets bundled into the global stylesheet).
|
|
148
|
+
*/
|
|
149
|
+
export function processComponentStyles(
|
|
150
|
+
source: string,
|
|
151
|
+
filePath: string
|
|
152
|
+
): { code: string; css: string } {
|
|
153
|
+
const componentId = generateComponentId(filePath);
|
|
154
|
+
let extractedCss = "";
|
|
155
|
+
let code = source;
|
|
156
|
+
|
|
157
|
+
// Extract <style scoped>...</style> blocks
|
|
158
|
+
const styleRegex = /<style\s+scoped>([\s\S]*?)<\/style>/g;
|
|
159
|
+
code = code.replace(styleRegex, (_, css: string) => {
|
|
160
|
+
const scoped = compileScopedCss(css, componentId);
|
|
161
|
+
extractedCss += scoped + "\n";
|
|
162
|
+
return "";
|
|
163
|
+
});
|
|
164
|
+
|
|
165
|
+
// Also extract regular <style> blocks (not scoped)
|
|
166
|
+
const plainStyleRegex = /<style>([\s\S]*?)<\/style>/g;
|
|
167
|
+
code = code.replace(plainStyleRegex, (_, css: string) => {
|
|
168
|
+
extractedCss += css + "\n";
|
|
169
|
+
return "";
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
// Inject data-wafra-<id> attribute on the root element
|
|
173
|
+
// (In a real impl, the compiler would do this automatically)
|
|
174
|
+
code = code.replace(
|
|
175
|
+
/return\s+h\s*\(\s*['"](\w+)['"]\s*,\s*\{/,
|
|
176
|
+
(match, tag) => `${match} 'data-wafra-${componentId}': '',`
|
|
177
|
+
);
|
|
178
|
+
|
|
179
|
+
return { code, css: extractedCss };
|
|
180
|
+
}
|