@@ -14,127 +14,133 @@ const distDir = path.join(__dirname, "dist");
14
14
15
15
// Create dist directory if it doesn't exist
16
16
if ( ! fs . existsSync ( distDir ) ) {
17
- fs . mkdirSync ( distDir ) ;
17
+ fs . mkdirSync ( distDir ) ;
18
18
}
19
19
20
20
// Function to resize images based on HTML attributes and compress them
21
21
async function resizeImages ( html ) {
22
- const imgRegex =
23
- / < i m g [ ^ > ] + s r c = " ( [ ^ " > ] + ) " [ ^ > ] + w i d t h = " ( [ ^ " > ] + ) " [ ^ > ] + h e i g h t = " ( [ ^ " > ] + ) " [ ^ > ] * > / g;
24
- let match = imgRegex . exec ( html ) ;
25
-
26
- while ( match !== null ) {
27
- const imgSrc = match [ 1 ] ;
28
- const width = Number . parseInt ( match [ 2 ] , 10 ) ;
29
- const height = Number . parseInt ( match [ 3 ] , 10 ) ;
30
-
31
- const srcPath = path . join ( srcDir , imgSrc ) ;
32
- const distPath = path . join ( distDir , imgSrc ) ;
33
-
34
- if ( fs . existsSync ( srcPath ) ) {
35
- console . log ( `Resizing and compressing ${ imgSrc } to ${ width } x${ height } ` ) ;
36
-
37
- const sharpInstance = sharp ( srcPath ) . resize ( width , height ) ;
38
-
39
- // Use appropriate compression based on image type
40
- if ( imgSrc . endsWith ( ".jpg" ) || imgSrc . endsWith ( ".jpeg" ) ) {
41
- await sharpInstance . jpeg ( { quality : 75 } ) . toFile ( distPath ) ;
42
- } else if ( imgSrc . endsWith ( ".png" ) ) {
43
- await sharpInstance . png ( { quality : 80 } ) . toFile ( distPath ) ;
44
- } else {
45
- await sharpInstance . toFile ( distPath ) ; // For other formats
46
- }
47
- }
48
-
49
- // Get the next match
50
- match = imgRegex . exec ( html ) ;
51
- }
22
+ const imgRegex =
23
+ / < i m g [ ^ > ] + s r c = " ( [ ^ " > ] + ) " [ ^ > ] + w i d t h = " ( [ ^ " > ] + ) " [ ^ > ] + h e i g h t = " ( [ ^ " > ] + ) " [ ^ > ] * > / g;
24
+ let match = imgRegex . exec ( html ) ;
25
+
26
+ while ( match !== null ) {
27
+ const imgSrc = match [ 1 ] ;
28
+ const width = Number . parseInt ( match [ 2 ] , 10 ) ;
29
+ const height = Number . parseInt ( match [ 3 ] , 10 ) ;
30
+
31
+ const srcPath = path . join ( srcDir , imgSrc ) ;
32
+ const distPath = path . join ( distDir , imgSrc ) ;
33
+
34
+ if ( fs . existsSync ( srcPath ) ) {
35
+ console . log ( `Resizing and compressing ${ imgSrc } to ${ width } x${ height } ` ) ;
36
+
37
+ const sharpInstance = sharp ( srcPath ) . resize ( width , height ) ;
38
+
39
+ // Use appropriate compression based on image type
40
+ if ( imgSrc . endsWith ( ".jpg" ) || imgSrc . endsWith ( ".jpeg" ) ) {
41
+ await sharpInstance . jpeg ( { quality : 75 } ) . toFile ( distPath ) ;
42
+ } else if ( imgSrc . endsWith ( ".png" ) ) {
43
+ await sharpInstance . png ( { quality : 80 } ) . toFile ( distPath ) ;
44
+ } else {
45
+ await sharpInstance . toFile ( distPath ) ; // For other formats
46
+ }
47
+ }
48
+
49
+ // Get the next match
50
+ match = imgRegex . exec ( html ) ;
51
+ }
52
52
}
53
53
54
54
// Function to compress non-resized images using sharp
55
55
async function compressImages ( ) {
56
- const imageDir = path . join ( srcDir , "images" ) ;
57
- const distImageDir = path . join ( distDir , "images" ) ;
58
-
59
- if ( ! fs . existsSync ( imageDir ) ) return ;
60
-
61
- const images = fs . readdirSync ( imageDir ) ;
62
-
63
- for ( const img of images ) {
64
- const srcPath = path . join ( imageDir , img ) ;
65
- const distPath = path . join ( distImageDir , img ) ;
66
-
67
- if ( ! fs . existsSync ( distPath ) ) {
68
- // Only compress if image wasn't resized
69
- console . log ( `Compressing ${ img } ` ) ;
70
- const sharpInstance = sharp ( srcPath ) ;
71
-
72
- if ( img . endsWith ( ".jpg" ) || img . endsWith ( ".jpeg" ) ) {
73
- await sharpInstance . jpeg ( { quality : 75 } ) . toFile ( distPath ) ;
74
- } else if ( img . endsWith ( ".png" ) ) {
75
- await sharpInstance . png ( { quality : 80 } ) . toFile ( distPath ) ;
76
- } else {
77
- await sharpInstance . toFile ( distPath ) ; // For other formats
78
- }
79
- }
80
- }
56
+ const imageDir = path . join ( srcDir , "images" ) ;
57
+ const distImageDir = path . join ( distDir , "images" ) ;
58
+
59
+ if ( ! fs . existsSync ( imageDir ) ) return ;
60
+
61
+ const images = fs . readdirSync ( imageDir ) ;
62
+
63
+ for ( const img of images ) {
64
+ const srcPath = path . join ( imageDir , img ) ;
65
+ const distPath = path . join ( distImageDir , img ) ;
66
+
67
+ if ( ! fs . existsSync ( distPath ) ) {
68
+ // Only compress if image wasn't resized
69
+ console . log ( `Compressing ${ img } ` ) ;
70
+ const sharpInstance = sharp ( srcPath ) ;
71
+
72
+ if ( img . endsWith ( ".jpg" ) || img . endsWith ( ".jpeg" ) ) {
73
+ await sharpInstance . jpeg ( { quality : 75 } ) . toFile ( distPath ) ;
74
+ } else if ( img . endsWith ( ".png" ) ) {
75
+ await sharpInstance . png ( { quality : 80 } ) . toFile ( distPath ) ;
76
+ } else {
77
+ await sharpInstance . toFile ( distPath ) ; // For other formats
78
+ }
79
+ }
80
+ }
81
81
}
82
82
83
83
// Read component files and inject them into index.html
84
84
async function buildHTML ( ) {
85
- let html = fs . readFileSync ( path . join ( srcDir , "index.html" ) , "utf-8" ) ;
86
-
87
- const componentRegex = / < c o m p o n e n t s r c = " ( .* ) " > < \/ c o m p o n e n t > / g;
88
- html = html . replace ( componentRegex , ( _ , componentPath ) => {
89
- const component = fs . readFileSync (
90
- path . join ( srcDir , "components" , componentPath ) ,
91
- "utf-8" ,
92
- ) ;
93
- return component ;
94
- } ) ;
95
-
96
- // Minify HTML
97
- const minifiedHTML = minify ( html , {
98
- collapseWhitespace : true ,
99
- removeComments : true ,
100
- minifyCSS : true ,
101
- minifyJS : true ,
102
- } ) ;
103
-
104
- // Resize and compress images before saving final HTML
105
- await resizeImages ( minifiedHTML ) ;
106
-
107
- fs . writeFileSync ( path . join ( distDir , "index.html" ) , minifiedHTML ) ;
85
+ let html = fs . readFileSync ( path . join ( srcDir , "index.html" ) , "utf-8" ) ;
86
+
87
+ const componentRegex = / < c o m p o n e n t s r c = " ( .* ) " > < \/ c o m p o n e n t > / g;
88
+ html = html . replace ( componentRegex , ( _ , componentPath ) => {
89
+ const component = fs . readFileSync (
90
+ path . join ( srcDir , "components" , componentPath ) ,
91
+ "utf-8" ,
92
+ ) ;
93
+ return component ;
94
+ } ) ;
95
+
96
+ // Minify HTML
97
+ const minifiedHTML = minify ( html , {
98
+ collapseWhitespace : true ,
99
+ removeComments : true ,
100
+ minifyCSS : true ,
101
+ minifyJS : true ,
102
+ } ) ;
103
+
104
+ // Resize and compress images before saving final HTML
105
+ await resizeImages ( minifiedHTML ) ;
106
+
107
+ fs . writeFileSync ( path . join ( distDir , "index.html" ) , minifiedHTML ) ;
108
108
}
109
109
110
110
// Copy all files from /src/assets to /dist/assets, and root style/script files to /dist
111
111
function copyAssets ( ) {
112
- const assetsSrc = path . join ( srcDir , "assets" ) ;
113
- const assetsDist = path . join ( distDir , "assets" ) ;
114
-
115
- // Copy assets folder
116
- if ( fs . existsSync ( assetsSrc ) ) {
117
- fse . copySync ( assetsSrc , assetsDist ) ;
118
- console . log ( `Copied assets from ${ assetsSrc } to ${ assetsDist } ` ) ;
119
- }
120
-
121
- // Copy root CSS and JS files
122
- const filesToCopy = [ "styles.css" , "script.js" ] ;
123
- for ( const file in filesToCopy ) {
124
- const srcFile = path . join ( srcDir , file ) ;
125
- const distFile = path . join ( distDir , file ) ;
126
-
127
- if ( fs . existsSync ( srcFile ) ) {
128
- fs . copyFileSync ( srcFile , distFile ) ;
129
- console . log ( `Copied ${ file } to ${ distDir } ` ) ;
130
- }
131
- }
112
+ const assetsSrc = path . join ( srcDir , "assets" ) ;
113
+ const assetsDist = path . join ( distDir , "assets" ) ;
114
+ const scriptsSrc = path . join ( srcDir , "scripts" ) ;
115
+ const scriptsDist = path . join ( distDir , "scripts" ) ;
116
+
117
+ // Copy assets folder
118
+ if ( fs . existsSync ( assetsSrc ) ) {
119
+ fse . copySync ( assetsSrc , assetsDist ) ;
120
+ console . log ( `Copied assets from ${ assetsSrc } to ${ assetsDist } ` ) ;
121
+ }
122
+ if ( fs . existsSync ( scriptsSrc ) ) {
123
+ fse . copySync ( scriptsSrc , scriptsDist ) ;
124
+ console . log ( `Copied scripts from ${ scriptsSrc } to ${ scriptsDist } ` ) ;
125
+ }
126
+
127
+ // Copy files from root folder to dist root
128
+ const filesToCopy = [ "styles.css" ] ;
129
+ for ( const file in filesToCopy ) {
130
+ const srcFile = path . join ( srcDir , file ) ;
131
+ const distFile = path . join ( distDir , file ) ;
132
+
133
+ if ( fs . existsSync ( srcFile ) ) {
134
+ fs . copyFileSync ( srcFile , distFile ) ;
135
+ console . log ( `Copied ${ file } to ${ distDir } ` ) ;
136
+ }
137
+ }
132
138
}
133
139
134
140
// Run build process with top-level await (Node.js 14+ supports it)
135
141
( async ( ) => {
136
- await buildHTML ( ) ;
137
- copyAssets ( ) ;
138
- await compressImages ( ) ; // Compress non-resized images after build
139
- console . log ( "Build completed!" ) ;
142
+ await buildHTML ( ) ;
143
+ copyAssets ( ) ;
144
+ await compressImages ( ) ; // Compress non-resized images after build
145
+ console . log ( "Build completed!" ) ;
140
146
} ) ( ) ;
0 commit comments