Skip to content

Commit a77d36f

Browse files
committed
Only include used incons in storybook
1 parent 2aa04d0 commit a77d36f

File tree

3 files changed

+26
-16
lines changed

3 files changed

+26
-16
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ yarn
5858

5959
# Starting storybook
6060
yarn storybook
61+
# You can run 'npx tsc -w -p src' on another terminal for enabling hot reloading.
6162

6263
# Starting test apps
6364
yarn start-cra # For testing in in a Create React App setup

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,9 @@
2121
"format": "yarn _format --write",
2222
"format:check": "yarn _format --list-different",
2323
"grant_exec_perms": "node dist/bin/tools/grant_exec_perms",
24-
"storybook": "yarn build && ((tsc -w -p src) & start-storybook -p 6006)",
25-
"build-storybook": "yarn build && build-storybook"
24+
"storybook": "yarn build && yarn only-include-used-icons && start-storybook -p 6006",
25+
"build-storybook": "yarn build && yarn only-include-used-icons && build-storybook",
26+
"only-include-used-icons": "node dist/bin/only-include-used-icons.js"
2627
},
2728
"bin": {
2829
"copy-dsfr-to-public": "dist/bin/copy-dsfr-to-public.js",

src/bin/only-include-used-icons.ts

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { assert } from "tsafe/assert";
77
import { exclude } from "tsafe/exclude";
88
import { writeFile, readFile, rm } from "fs/promises";
99
import { crawl } from "./tools/crawl";
10-
import { basename as pathBasename, sep as pathSep } from "path";
10+
import { basename as pathBasename, sep as pathSep, dirname as pathDirname } from "path";
1111
import type { Equals } from "tsafe";
1212

1313
export const pathOfIconsJson = pathJoin("utility", "icons", "icons.json");
@@ -97,9 +97,10 @@ async function main() {
9797

9898
const cwd = process.cwd();
9999

100-
const nodeModulesDirPath = pathJoin(cwd, "node_modules");
101-
102-
const dsfrDistDirPath = pathJoin(...[nodeModulesDirPath, ...packageName.split("/"), "dsfr"]);
100+
const dsfrDistDirPath =
101+
getProjectRoot() === cwd
102+
? pathJoin(cwd, "dist", "dsfr")
103+
: pathJoin(...[cwd, "node_modules", ...packageName.split("/"), "dsfr"]);
103104

104105
const icons: Icon[] = JSON.parse(
105106
(await readFile(pathJoin(dsfrDistDirPath, pathOfIconsJson))).toString("utf8")
@@ -158,6 +159,12 @@ async function main() {
158159
return false;
159160
}
160161

162+
if (
163+
pathDirname(relativeDirPath).endsWith(pathJoin(...packageName.split("/")))
164+
) {
165+
return pathBasename(relativeDirPath) === "src";
166+
}
167+
161168
if (pathBasename(relativeDirPath) === "node_modules") {
162169
return false;
163170
}
@@ -230,6 +237,8 @@ async function main() {
230237
return { usedIconClassNames };
231238
})();
232239

240+
console.log("Detected usage of the following icons: ", usedIconClassNames);
241+
233242
const usedIcons = usedIconClassNames.map(className => {
234243
const icon = icons.find(({ prefix, iconId }) => `${prefix}${iconId}` === className);
235244

@@ -263,14 +272,15 @@ async function main() {
263272
rm(nextCacheDir, { "recursive": true, "force": true });
264273
};
265274

266-
[
267-
dsfrDistDirPath,
268-
...(() => {
269-
const dsfrDistInPublic = pathJoin(cwd, "public", "dsfr");
275+
[dsfrDistDirPath, pathJoin(cwd, "public", "dsfr")].forEach(async dsfrDistDirPath => {
276+
const cssFilePaths = ["icons.css", "icons.min.css"].map(cssFileBasename =>
277+
pathJoin(dsfrDistDirPath, "utility", "icons", cssFileBasename)
278+
);
279+
280+
if (cssFilePaths.some(cssFilePath => !fs.existsSync(cssFilePath))) {
281+
return;
282+
}
270283

271-
return fs.existsSync(dsfrDistInPublic) ? [dsfrDistInPublic] : [];
272-
})()
273-
].forEach(async dsfrDistDirPath => {
274284
const remixiconDirPath = pathJoin(dsfrDistDirPath, "icons", "remixicon");
275285

276286
if (!fs.existsSync(remixiconDirPath)) {
@@ -287,9 +297,7 @@ async function main() {
287297
)
288298
);
289299

290-
["icons.css", "icons.min.css"].forEach(async cssFileBasename => {
291-
const filePath = pathJoin(dsfrDistDirPath, "utility", "icons", cssFileBasename);
292-
300+
cssFilePaths.forEach(async filePath => {
293301
const currentCode = await readFile(filePath);
294302

295303
if (Buffer.compare(rawIconCssCodeBuffer, currentCode) === 0) {

0 commit comments

Comments
 (0)