1
- import fs from 'fs-extra' ;
2
- import path from 'path' ;
1
+ import fs from 'fs' ;
2
+ import { PurgeCSS } from 'purgecss' ;
3
+ import postcss from 'postcss' ;
3
4
import cssnano from 'cssnano' ;
4
- import PurgeCSS from 'purgecss' ;
5
5
import { minify } from 'html-minifier-terser' ;
6
6
import imagemin from 'imagemin' ;
7
7
import imageminMozjpeg from 'imagemin-mozjpeg' ;
@@ -10,63 +10,58 @@ import imageminPngquant from 'imagemin-pngquant';
10
10
// Get the version number from environment variable set by the CI/CD pipeline
11
11
const version = process . env . NEW_TAG || '1.0.0' ;
12
12
13
- const srcHtmlPath = path . join ( __dirname , '../src/index.html' ) ;
14
- const distHtmlPath = path . join ( __dirname , '../dist/index.html' ) ;
15
- const srcCssPath = path . join ( __dirname , '../src/styles.css' ) ;
16
- const distCssPath = path . join ( __dirname , '../dist/styles.min.css' ) ;
17
- const srcImagePath = path . join ( __dirname , '../src/logo.jpeg' ) ;
18
- const distImagePath = path . join ( __dirname , '../dist/logo.jpeg' ) ;
19
- const srcCnamePath = path . join ( __dirname , '../src/CNAME' ) ;
20
- const distCnamePath = path . join ( __dirname , '../dist/CNAME' ) ;
21
-
22
13
async function buildCSS ( ) {
23
14
const purgeCSSResult = await new PurgeCSS ( ) . purge ( {
24
- content : [ srcHtmlPath ] ,
25
- css : [ srcCssPath ]
15
+ content : [ 'src/index.html' ] ,
16
+ css : [ 'src/styles.css' ] ,
26
17
} ) ;
27
18
28
- const minifiedCSS = await cssnano . process ( purgeCSSResult [ 0 ] . css , { from : undefined } ) ;
29
- await fs . outputFile ( distCssPath , minifiedCSS . css ) ;
19
+ const result = await postcss ( [ cssnano ] ) . process ( purgeCSSResult [ 0 ] . css , { from : undefined } ) ;
20
+
21
+ fs . writeFileSync ( 'dist/styles.min.css' , result . css ) ;
30
22
}
31
23
32
- async function buildHTML ( ) {
33
- let htmlContent = await fs . readFile ( srcHtmlPath , 'utf8' ) ;
24
+ async function minifyHTML ( ) {
25
+ let html = fs . readFileSync ( 'src/index.html' , 'utf8' ) ;
34
26
35
27
// Replace the version placeholder with the actual version number
36
- htmlContent = htmlContent . replace ( 'VERSION_PLACEHOLDER' , version ) ;
28
+ html = html . replace ( 'VERSION_PLACEHOLDER' , version ) ;
37
29
38
- const minifiedHTML = await minify ( htmlContent , {
30
+ const minifiedHTML = await minify ( html , {
39
31
collapseWhitespace : true ,
40
32
removeComments : true ,
41
33
minifyCSS : true ,
42
- minifyJS : true
34
+ minifyJS : true ,
43
35
} ) ;
44
36
45
- await fs . outputFile ( distHtmlPath , minifiedHTML ) ;
37
+ fs . writeFileSync ( 'dist/index.html' , minifiedHTML ) ;
46
38
}
47
39
48
40
async function compressImages ( ) {
49
- await imagemin ( [ srcImagePath ] , {
50
- destination : path . dirname ( distImagePath ) ,
41
+ await imagemin ( [ 'src/logo.jpeg' ] , {
42
+ destination : 'dist/' ,
51
43
plugins : [
52
- imageminMozjpeg ( ) ,
53
- imageminPngquant ( )
44
+ imageminMozjpeg ( { quality : 75 } ) ,
45
+ imageminPngquant ( { quality : [ 0.6 , 0.8 ] } )
54
46
]
55
47
} ) ;
56
48
}
57
49
58
- async function copyCNAME ( ) {
59
- await fs . copy ( srcCnamePath , distCnamePath ) ;
50
+ function copyCNAME ( ) {
51
+ fs . copyFileSync ( 'src/CNAME' , 'dist/CNAME' ) ;
60
52
}
61
53
62
54
async function build ( ) {
55
+ // Ensure the dist directory exists
56
+ if ( ! fs . existsSync ( 'dist' ) ) {
57
+ fs . mkdirSync ( 'dist' ) ;
58
+ }
59
+
63
60
await buildCSS ( ) ;
64
- await buildHTML ( ) ;
61
+ await minifyHTML ( ) ;
65
62
await compressImages ( ) ;
66
- await copyCNAME ( ) ;
63
+ copyCNAME ( ) ;
64
+ console . log ( 'Build completed successfully' ) ;
67
65
}
68
66
69
- build ( ) . catch ( err => {
70
- console . error ( err ) ;
71
- process . exit ( 1 ) ;
72
- } ) ;
67
+ build ( ) ;
0 commit comments