@@ -17,6 +17,8 @@ import { getProjectRoot } from "./tools/getProjectRoot";
1717import yargsParser from "yargs-parser" ;
1818import { getAbsoluteAndInOsFormatPath } from "./tools/getAbsoluteAndInOsFormatPath" ;
1919import { readPublicDirPath } from "./readPublicDirPath" ;
20+ import { transformCodebase } from "./tools/transformCodebase" ;
21+ import { assert } from "tsafe/assert" ;
2022
2123( async ( ) => {
2224 const argv = yargsParser ( process . argv . slice ( 2 ) ) ;
@@ -73,7 +75,7 @@ import { readPublicDirPath } from "./readPublicDirPath";
7375
7476 fs . writeFileSync ( pathJoin ( dsfrDirPath , ".gitignore" ) , Buffer . from ( "*" , "utf8" ) ) ;
7577
76- ( function callee ( depth : number ) {
78+ const dsfrDistNodeModulesDirPath = ( function dsfrDistNodeModulesDirPath ( depth : number ) : string {
7779 const parentProjectDirPath = pathResolve (
7880 pathJoin ( ...[ projectDirPath , ...new Array ( depth ) . fill ( ".." ) ] )
7981 ) ;
@@ -93,17 +95,73 @@ import { readPublicDirPath } from "./readPublicDirPath";
9395 process . exit ( - 1 ) ;
9496 }
9597
96- callee ( depth + 1 ) ;
97-
98- return ;
98+ return dsfrDistNodeModulesDirPath ( depth + 1 ) ;
9999 }
100100
101- fs . cpSync ( dsfrDirPathInNodeModules , dsfrDirPath , {
102- "recursive" : true
103- } ) ;
101+ return dsfrDirPathInNodeModules ;
104102 } ) ( 0 ) ;
103+
104+ {
105+ const dsfrMinCssFileRelativePath = "dsfr.min.css" ;
106+
107+ const usedAssetsRelativeFilePaths = new Set (
108+ readAssetsImportFromDsfrCss ( {
109+ "dsfrSourceCode" : fs
110+ . readFileSync ( pathJoin ( dsfrDistNodeModulesDirPath , dsfrMinCssFileRelativePath ) )
111+ . toString ( "utf8" )
112+ } )
113+ ) ;
114+
115+ const fileToKeepRelativePaths = new Set ( [
116+ pathJoin ( "favicon" , "apple-touch-icon.png" ) ,
117+ pathJoin ( "favicon" , "favicon.svg" ) ,
118+ pathJoin ( "favicon" , "favicon.ico" ) ,
119+ pathJoin ( "favicon" , "manifest.webmanifest" ) ,
120+ pathJoin ( "utility" , "icons" , "icons.min.css" ) ,
121+ dsfrMinCssFileRelativePath
122+ ] ) ;
123+
124+ transformCodebase ( {
125+ "srcDirPath" : dsfrDistNodeModulesDirPath ,
126+ "destDirPath" : dsfrDirPath ,
127+ "transformSourceCode" : ( { fileRelativePath, sourceCode } ) => {
128+ if (
129+ fileToKeepRelativePaths . has ( fileRelativePath ) ||
130+ usedAssetsRelativeFilePaths . has ( fileRelativePath )
131+ ) {
132+ return { "modifiedSourceCode" : sourceCode } ;
133+ }
134+ }
135+ } ) ;
136+ }
105137} ) ( ) ;
106138
139+ function readAssetsImportFromDsfrCss ( params : { dsfrSourceCode : string } ) : string [ ] {
140+ const { dsfrSourceCode } = params ;
141+
142+ const fileRelativePaths = [ / u r l \( " ( [ ^ " ] + ) " \) / g, / u r l \( ' ( [ ^ ' ] + ) ' \) / g, / u r l \( ( [ ^ ) ] + ) \) / g]
143+ . map ( regex => {
144+ const fileRelativePaths : string [ ] = [ ] ;
145+
146+ dsfrSourceCode . replace ( regex , ( ...[ , relativeFilePath ] ) => {
147+ if ( relativeFilePath . startsWith ( "data:" ) ) {
148+ return "" ;
149+ }
150+
151+ fileRelativePaths . push ( relativeFilePath ) ;
152+
153+ return "" ;
154+ } ) ;
155+
156+ return fileRelativePaths ;
157+ } )
158+ . flat ( ) ;
159+
160+ assert ( fileRelativePaths . length !== 0 ) ;
161+
162+ return fileRelativePaths ;
163+ }
164+
107165function getRepoIssueUrl ( ) {
108166 const reactDsfrRepoUrl = JSON . parse (
109167 fs . readFileSync ( pathJoin ( getProjectRoot ( ) , "package.json" ) ) . toString ( "utf8" )
0 commit comments