Skip to content

Commit 1ee4349

Browse files
committed
Actually remove dependency on css
1 parent d2dcb34 commit 1ee4349

File tree

7 files changed

+129
-125
lines changed

7 files changed

+129
-125
lines changed

src/bin/css_to_ts/icons.ts

Lines changed: 0 additions & 121 deletions
This file was deleted.
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import type { Icon } from "../css_to_ts";
2+
3+
type IconLike = Icon.Dsfr | Omit<Icon.Remixicon, "rawSvgCode">;
4+
5+
export function generateIconsRawCssCode(params: {
6+
usedIcons: IconLike[];
7+
patchedRawCssCodeForCompatWithRemixIcon: string;
8+
}): string {
9+
const { usedIcons, patchedRawCssCodeForCompatWithRemixIcon } = params;
10+
11+
const buildRule = (icon: IconLike, isHighContrast: boolean) => {
12+
const { iconId, prefix } = icon;
13+
14+
const className = `${prefix}${iconId}`;
15+
16+
const relativePath = (() => {
17+
switch (icon.prefix) {
18+
case "fr-icon-":
19+
return `../../icons/${icon.category}/${iconId}.svg`;
20+
case "ri-":
21+
return `../../icons/remixicon/${iconId}.svg`;
22+
}
23+
})();
24+
25+
return [
26+
`.${className}::before,`,
27+
`.${className}::after {`,
28+
...(isHighContrast
29+
? [` background-image: url("${relativePath}");`]
30+
: [
31+
` -webkit-mask-image: url("${relativePath}");`,
32+
` mask-image: url("${relativePath}");`
33+
]),
34+
`}`,
35+
``
36+
]
37+
.map(!isHighContrast ? line => line : line => ` ${line}`)
38+
.join("\n");
39+
};
40+
41+
return [
42+
...usedIcons.map(icon => buildRule(icon, false)),
43+
...(usedIcons.length === 0
44+
? []
45+
: [
46+
`@media all and (-ms-high-contrast: none), (-ms-high-contrast: active) {`,
47+
...usedIcons.map(icon => buildRule(icon, true)),
48+
`}`,
49+
``
50+
]),
51+
...(usedIcons.find(({ prefix }) => prefix === "ri-") === undefined
52+
? []
53+
: [patchedRawCssCodeForCompatWithRemixIcon])
54+
].join("\n");
55+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import { exclude } from "tsafe/exclude";
2+
import { sep } from "path";
3+
import * as css from "css";
4+
import { pathOfPatchedRawCssCodeForCompatWithRemixIconRelativeToDsfrDist } from "./pathOfPatchedRawCssCodeForCompatWithRemixIconRelativeToDsfrDist";
5+
6+
export function getPatchedRawCssCodeForCompatWithRemixIcon(params: { rawCssCode: string }) {
7+
const { rawCssCode } = params;
8+
9+
const parsedCss = css.parse(rawCssCode);
10+
11+
const prefixRegExp = /fr-icon-[^-]/;
12+
13+
(parsedCss as any).stylesheet.rules = (parsedCss as any).stylesheet.rules
14+
.map((rule: any) => {
15+
if (rule.type === "media") {
16+
rule.rules = rule.rules
17+
.map((rule: any) => {
18+
if (rule.type !== "rule") {
19+
return undefined;
20+
}
21+
22+
if (prefixRegExp.test(rule.selectors.join(", "))) {
23+
return rule;
24+
}
25+
26+
return undefined;
27+
})
28+
.filter(exclude(undefined));
29+
30+
if (rule.rules.length === 0) {
31+
return undefined;
32+
}
33+
34+
return rule;
35+
}
36+
37+
if (rule.type !== "rule") {
38+
return undefined;
39+
}
40+
41+
if (prefixRegExp.test(rule.selectors.join(", "))) {
42+
return rule;
43+
}
44+
45+
return undefined;
46+
})
47+
.filter(exclude(undefined));
48+
49+
const back =
50+
new Array(
51+
pathOfPatchedRawCssCodeForCompatWithRemixIconRelativeToDsfrDist.split(sep).length - 1
52+
)
53+
.fill("..")
54+
.join("/") + "/";
55+
56+
return css
57+
.stringify(parsedCss)
58+
.replace(/fr-icon-/g, "ri-")
59+
.replace(/url\("/g, `url("${back}`)
60+
.replace(/url\('/g, `url('${back}`);
61+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export * from "./getPatchedRawCssCodeForCompatWithRemixIcon";
2+
export * from "./pathOfPatchedRawCssCodeForCompatWithRemixIconRelativeToDsfrDist";
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { join as pathJoin } from "path";
2+
3+
export const pathOfPatchedRawCssCodeForCompatWithRemixIconRelativeToDsfrDist = pathJoin(
4+
"utility",
5+
"icons",
6+
"dsfr_remixicon.css"
7+
);

src/bin/css_to_ts/icons/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export * from "./generateIconsRawCssCode";
2+
export * from "./getPatchedRawCssCodeForCompatWithRemixIcon";

src/bin/only_include_used_icons.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
#!/usr/bin/env node
22
import { collectIcons } from "./css_to_ts";
3-
import {
4-
generateIconsRawCssCode,
5-
pathOfPatchedRawCssCodeForCompatWithRemixIconRelativeToDsfrDist
6-
} from "./css_to_ts/icons";
3+
import { generateIconsRawCssCode } from "./css_to_ts/icons/generateIconsRawCssCode";
4+
import { pathOfPatchedRawCssCodeForCompatWithRemixIconRelativeToDsfrDist } from "./css_to_ts/icons/getPatchedRawCssCodeForCompatWithRemixIcon/pathOfPatchedRawCssCodeForCompatWithRemixIconRelativeToDsfrDist";
75
import { getProjectRoot } from "./tools/getProjectRoot";
86
import * as fs from "fs";
97
import { join as pathJoin } from "path";

0 commit comments

Comments
 (0)