Skip to content

Commit 523ac3c

Browse files
authored
feat(Lightning CSS): added plugin (#69)
* feat(lightningcss): added plugin * refactor: pnpm audit fix * refactor: another solution * chore: let's ignore this phrase * refactor: replaced Buffer * refactor: further optimized the tests * refactor: further optimized the fixtures * refactor: optimized fixtures * Revert "refactor: further optimized the fixtures" This reverts commit 1c8d7e1. * refactor: further optimizations * docs: further optimizations * refactor: looks much better now * docs: updated * refactor: corrected the plugin name * refactor: added another comment to copilot instructions * refactor: aligned output * Create afraid-ways-judge.md * Update packages/lightningcss-plugin-if-function/README.md * feat: added missing license file * refactor: further optimizations * docs: added Lightning CSS plugin * Update afraid-ways-judge.md * Update .github/copilot-instructions.md * refactor: we only need the named output * refactor: updated pnpm lock file * refactor: optimization * Apply suggestions from code review * refactor: optimized the tests * refactor: optimizations * refactor: further optimizations * docs: updated contents by fixtures * refactor: further optimizations * refactor: updated docs * refactor: simplification * refactor: updated docs * Apply suggestions from code review
1 parent e2fc11a commit 523ac3c

39 files changed

+1083
-357
lines changed

.changeset/afraid-ways-judge.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"lightningcss-plugin-if-function": patch
3+
---
4+
5+
refactor: further optimizations

.github/copilot-instructions.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ Note: Unlike using @media/@supports/@container rules, which just ignore their co
5151

5252
- `packages/css-if-polyfill/` - Main JavaScript polyfill
5353
- `packages/postcss-if-function/` - PostCSS plugin for build-time transformation
54+
- `packages/lightningcss-plugin-if-function/` - Lightning CSS plugin for build-time transformation
55+
- `packages/stylelint-config-if-function/` - Stylelint configuration for linting CSS if() usage
5456

5557
### Key Files
5658

@@ -134,6 +136,7 @@ When working with CSS if() functions, always follow the official WCAG specificat
134136
3. **Always** run the full test suite including browser validation
135137
4. **Always** update documentation when changing public APIs
136138
5. **Always** bear in mind that the developer's main job is to read, not write, code. Therefore, avoid unnecessary complexity, abbreviations and short forms of parameters, for example in CLI usage.
137-
6. **Consider** performance impact on large stylesheets and DOM trees
139+
6. **Always** try to avoid setting up separate fixtures for each output (Polyfill, PostCSS plugin or Lightning CSS plugin) and use the same fixture for all of them if possible. If there are any differences, try changing the fixture so that it can be used for all of them. For example, change the colour values in the fixture so that they produce the same output.
140+
7. **Consider** performance impact on large stylesheets and DOM trees
138141

139142
This project aims to provide a complete, specification-compliant implementation of CSS if() functionality for browsers that don't yet support it natively.

.github/workflows/ci.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ jobs:
8282
path: |
8383
packages/css-if-polyfill/dist
8484
packages/postcss-if-function/dist
85+
packages/lightningcss-plugin-if-function/dist
8586
packages/stylelint-config-if-function/dist
8687
retention-days: 30
8788

.github/workflows/default.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ jobs:
9999
path: |
100100
packages/css-if-polyfill/coverage/
101101
packages/postcss-if-function/coverage/
102+
packages/lightningcss-plugin-if-function/coverage/
102103
packages/stylelint-config-if-function/coverage/
103104
retention-days: 30
104105

.github/workflows/quality.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ jobs:
5757
path: |
5858
packages/css-if-polyfill/coverage/
5959
packages/postcss-if-function/coverage/
60+
packages/lightningcss-plugin-if-function/coverage/
6061
packages/stylelint-config-if-function/coverage/
6162
retention-days: 30
6263

.vscode/settings.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{
22
"editor.defaultFormatter": "esbenp.prettier-vscode",
3-
"editor.formatOnSave": true
3+
"editor.formatOnSave": true,
4+
"cSpell.words": ["lightningcss"]
45
}

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
[![Open Source Love](https://badges.frapsoft.com/os/v3/open-source.svg?v=103)](https://github.com/ellerbrock/open-source-badges/)
1010
[![Contributor Covenant](https://img.shields.io/badge/Contributor%20Covenant-2.0-4baaaa.svg)](CODE-OF-CONDUCT.md)
1111

12-
A modern JavaScript [polyfill](https://github.com/mfranzke/css-if-polyfill/tree/main/packages/css-if-polyfill), [PostCSS plugin](https://github.com/mfranzke/css-if-polyfill/tree/main/packages/postcss-if-function) and a [Stylelint plugin](https://github.com/mfranzke/css-if-polyfill/tree/main/packages/stylelint-config-if-function) (to extend to prevent lint false positives) for the [CSS `if()` function](https://developer.mozilla.org/en-US/docs/Web/CSS/if) with **hybrid build-time and runtime processing**. Transforms CSS `if()` functions to native `@media` and `@supports` rules where possible, with runtime fallback for dynamic conditions.
12+
A modern JavaScript [polyfill](https://github.com/mfranzke/css-if-polyfill/tree/main/packages/css-if-polyfill), [PostCSS plugin](https://github.com/mfranzke/css-if-polyfill/tree/main/packages/postcss-if-function), a [Lightning CSS plugin](https://github.com/mfranzke/css-if-polyfill/tree/main/packages/lightningcss-plugin-if-function) and a [Stylelint plugin](https://github.com/mfranzke/css-if-polyfill/tree/main/packages/stylelint-config-if-function) (to extend to prevent lint false positives) for the [CSS `if()` function](https://developer.mozilla.org/en-US/docs/Web/CSS/if) with **hybrid build-time and runtime processing**. Transforms CSS `if()` functions to native `@media` and `@supports` rules where possible, with runtime fallback for dynamic conditions.
1313

1414
## Contributing
1515

docs/TEST_FIXTURES.md

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ This document demonstrates the centralized test fixture system that provides a s
1212

1313
```css
1414
.responsive {
15-
width: if(media(max-width: 768px): 100%; else: 50%);
15+
width: if(media(width <= 768px): 100%; else: 50%);
1616
}
1717
```
1818

@@ -22,7 +22,7 @@ This document demonstrates the centralized test fixture system that provides a s
2222
.responsive {
2323
width: 50%;
2424
}
25-
@media (max-width: 768px) {
25+
@media (width <= 768px) {
2626
.responsive {
2727
width: 100%;
2828
}
@@ -70,15 +70,15 @@ This document demonstrates the centralized test fixture system that provides a s
7070

7171
```css
7272
.test {
73-
color: if(style(--theme): var(--primary) ; else: blue);
73+
color: if(style(--theme): var(--primary) ; else: #00f);
7474
}
7575
```
7676

7777
**Expected Output:**
7878

7979
```css
8080
.test {
81-
color: blue;
81+
color: #00f;
8282
}
8383
```
8484

@@ -94,7 +94,7 @@ This document demonstrates the centralized test fixture system that provides a s
9494

9595
```css
9696
.example {
97-
color: if(media(max-width: 768px): blue; else: red);
97+
color: if(media(width <= 768px): #00f; else: red);
9898
font-size: if(supports(display: grid): 1.2rem; else: 1rem);
9999
}
100100
```
@@ -105,9 +105,9 @@ This document demonstrates the centralized test fixture system that provides a s
105105
.example {
106106
color: red;
107107
}
108-
@media (max-width: 768px) {
108+
@media (width <= 768px) {
109109
.example {
110-
color: blue;
110+
color: #00f;
111111
}
112112
}
113113
.example {
@@ -174,7 +174,7 @@ This document demonstrates the centralized test fixture system that provides a s
174174

175175
```css
176176
.test {
177-
color: if(media(min-width: 768px): blue; else: red);
177+
color: if(media(width >= 768px): #00f; else: red);
178178
background: if(style(--dark-mode): black; else: white);
179179
}
180180
```
@@ -185,9 +185,9 @@ This document demonstrates the centralized test fixture system that provides a s
185185
.test {
186186
color: red;
187187
}
188-
@media (min-width: 768px) {
188+
@media (width >= 768px) {
189189
.test {
190-
color: blue;
190+
color: #00f;
191191
}
192192
}
193193
.test {
@@ -207,9 +207,7 @@ This document demonstrates the centralized test fixture system that provides a s
207207

208208
```css
209209
.responsive {
210-
width: if(
211-
media((min-width: 768px) and (max-width: 1024px)): 50%; else: 100%
212-
);
210+
width: if(media((width >= 768px) and (width <= 1024px)): 50%; else: 100%);
213211
}
214212
```
215213

@@ -219,7 +217,7 @@ This document demonstrates the centralized test fixture system that provides a s
219217
.responsive {
220218
width: 100%;
221219
}
222-
@media ((min-width: 768px) and (max-width: 1024px)) {
220+
@media (width >= 768px) and (width <= 1024px) {
223221
.responsive {
224222
width: 50%;
225223
}
@@ -239,11 +237,11 @@ This document demonstrates the centralized test fixture system that provides a s
239237
```css
240238
/* Header styles */
241239
.header {
242-
background: blue;
240+
background: #00f;
243241
}
244242

245243
.conditional {
246-
color: if(media(max-width: 768px): red; else: blue);
244+
color: if(media(width <= 768px): red; else: #00f);
247245
}
248246

249247
/* Footer styles */
@@ -257,12 +255,12 @@ This document demonstrates the centralized test fixture system that provides a s
257255
```css
258256
/* Header styles */
259257
.header {
260-
background: blue;
258+
background: #00f;
261259
}
262260
.conditional {
263-
color: blue;
261+
color: #00f;
264262
}
265-
@media (max-width: 768px) {
263+
@media (width <= 768px) {
266264
.conditional {
267265
color: red;
268266
}

docs/refactoring/TEST_CENTRALIZATION_SUMMARY.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ pnpm run build:docs # Auto-generates docs from fixtures
8585

8686
```css
8787
.responsive {
88-
width: if(media(max-width: 768px): 100%; else: 50%);
88+
width: if(media(width <= 768px): 100%; else: 50%);
8989
}
9090
```
9191

@@ -95,7 +95,7 @@ pnpm run build:docs # Auto-generates docs from fixtures
9595
.responsive {
9696
width: 50%;
9797
}
98-
@media (max-width: 768px) {
98+
@media (width <= 768px) {
9999
.responsive {
100100
width: 100%;
101101
}

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
"dev": "pnpm --recursive run dev",
3535
"lint": "pnpm --parallel run '/^lint:[^:]*$/'",
3636
"lint:markdownlint": "markdownlint --config .config/.markdown-lint.yml **/*.md",
37-
"lint:packages": "publint packages/css-if-polyfill && publint packages/postcss-if-function && publint packages/stylelint-config-if-function",
37+
"lint:packages": "publint packages/css-if-polyfill && publint packages/postcss-if-function && publint packages/lightningcss-plugin-if-function && publint packages/stylelint-config-if-function",
3838
"lint:stylelint": "stylelint test/fixtures/*.input.css --config .config/.stylelintrc.json",
3939
"lint:stylelint:14": "npx stylelint@14 test/fixtures/*.input.css --config .config/.stylelintrc.older.json",
4040
"lint:stylelint:15": "npx stylelint@15 test/fixtures/*.input.css --config .config/.stylelintrc.older.json",
@@ -71,7 +71,8 @@
7171
},
7272
"pnpm": {
7373
"overrides": {
74-
"@eslint/plugin-kit@<0.3.4": ">=0.3.4"
74+
"@eslint/plugin-kit@<0.3.4": ">=0.3.4",
75+
"tmp@<=0.2.3": ">=0.2.4"
7576
}
7677
}
7778
}

0 commit comments

Comments
 (0)