11#!/usr/bin/env node
22
33/**
4- * This script is ran with `npx only- include-used-icons`
4+ * This script is ran with `npx react-dsfr include-used-icons`
55 * It scans your codebase to find which icons are used and only include those in the final build.
66 * Do do that it patches the node_modules/@codegouvfr/react-dsfr/dist/utility/icons/icons.css file
77 * and the public/dsfr/utility/icons/icons.css file (if applicable, not in Next.js for example).
@@ -232,6 +232,8 @@ async function getCommandContext(args: string[]): Promise<CommandContext> {
232232 if ( ! ( await existsAsync ( dsfrDirPath_static ) ) ) {
233233 return undefined ;
234234 }
235+
236+ return dsfrDirPath_static ;
235237 } ) ( ) ;
236238
237239 const htmlFilePath = await ( async ( ) => {
@@ -275,6 +277,10 @@ async function getCommandContext(args: string[]): Promise<CommandContext> {
275277 "dirPath" : projectDirPath ,
276278 "returnedPathsType" : "absolute" ,
277279 "getDoCrawlInDir" : async ( { relativeDirPath } ) => {
280+ if ( relativeDirPath === "dist" ) {
281+ return false ;
282+ }
283+
278284 if ( pathBasename ( relativeDirPath ) === "node_modules" ) {
279285 return false ;
280286 }
@@ -398,8 +404,8 @@ async function getCommandContext(args: string[]): Promise<CommandContext> {
398404 } ;
399405}
400406
401- async function main ( ) {
402- const commandContext = await getCommandContext ( process . argv . slice ( 2 ) ) ;
407+ export async function main ( args : string [ ] ) {
408+ const commandContext = await getCommandContext ( args ) ;
403409
404410 const log = commandContext . isSilent ? undefined : console . log ;
405411
@@ -470,7 +476,7 @@ async function main() {
470476 ) ;
471477 }
472478
473- log ?. ( `Found ${ usedIconClassNames . length } used icons.` ) ;
479+ log ?. ( `Found usage of ${ usedIconClassNames . length } different icons.` ) ;
474480
475481 const usedIcons = usedIconClassNames . map ( className => {
476482 const icon = icons . find ( ( { prefix, iconId } ) => `${ prefix } ${ iconId } ` === className ) ;
@@ -501,7 +507,7 @@ async function main() {
501507
502508 await Promise . all (
503509 [ commandContext . dsfrDirPath , commandContext . spaParams ?. dsfrDirPath_static ]
504- . filter ( dirPath => dirPath !== undefined )
510+ . filter ( exclude ( undefined ) )
505511 . map ( async dsfrDirPath => {
506512 const cssFilePath = pathJoin ( dsfrDirPath , iconsMinCssRelativePath ) ;
507513
@@ -518,14 +524,15 @@ async function main() {
518524 ) ;
519525
520526 if ( ! hasChanged ) {
527+ log ?. ( "No change since last run" ) ;
521528 return ;
522529 }
523530
524531 await Promise . all ( [
525532 ( async function generateUsedRemixiconFiles ( ) {
526533 await Promise . all (
527534 [ commandContext . dsfrDirPath , commandContext . spaParams ?. dsfrDirPath_static ]
528- . filter ( dirPath => dirPath !== undefined )
535+ . filter ( exclude ( undefined ) )
529536 . map ( async dsfrDistDirPath => {
530537 const remixiconDirPath = pathJoin ( dsfrDistDirPath , "icons" , "remixicon" ) ;
531538
@@ -556,7 +563,8 @@ async function main() {
556563
557564 await Promise . all (
558565 usedIcons
559- . filter ( icon => icon . prefix === "fr-icon-" )
566+ . map ( icon => ( icon . prefix !== "fr-icon-" ? undefined : icon ) )
567+ . filter ( exclude ( undefined ) )
560568 . map ( ( { svgRelativePath } ) =>
561569 ( [ commandContext . dsfrDirPath , dsfrDirPath_static ] as const ) . map (
562570 baseDirPath =>
@@ -580,7 +588,7 @@ async function main() {
580588 const { modifiedHtml } = modifyHtmlHrefs ( {
581589 "html" : html ,
582590 "getModifiedHref" : href => {
583- if ( ! href . endsWith ( iconsMinCssRelativePath . replace ( / \\ / g, "/" ) ) ) {
591+ if ( ! href . includes ( iconsMinCssRelativePath . replace ( / \\ / g, "/" ) ) ) {
584592 return href ;
585593 }
586594
@@ -622,5 +630,5 @@ async function main() {
622630}
623631
624632if ( require . main === module ) {
625- main ( ) ;
633+ main ( process . argv . slice ( 2 ) ) ;
626634}
0 commit comments