Skip to content

Commit 9d21120

Browse files
author
ricky.gummadi
committed
fix build
1 parent cfd8c21 commit 9d21120

File tree

1 file changed

+29
-34
lines changed

1 file changed

+29
-34
lines changed

build/build.mjs

Lines changed: 29 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
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';
34
import cssnano from 'cssnano';
4-
import PurgeCSS from 'purgecss';
55
import { minify } from 'html-minifier-terser';
66
import imagemin from 'imagemin';
77
import imageminMozjpeg from 'imagemin-mozjpeg';
@@ -10,63 +10,58 @@ import imageminPngquant from 'imagemin-pngquant';
1010
// Get the version number from environment variable set by the CI/CD pipeline
1111
const version = process.env.NEW_TAG || '1.0.0';
1212

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-
2213
async function buildCSS() {
2314
const purgeCSSResult = await new PurgeCSS().purge({
24-
content: [srcHtmlPath],
25-
css: [srcCssPath]
15+
content: ['src/index.html'],
16+
css: ['src/styles.css'],
2617
});
2718

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);
3022
}
3123

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');
3426

3527
// Replace the version placeholder with the actual version number
36-
htmlContent = htmlContent.replace('VERSION_PLACEHOLDER', version);
28+
html = html.replace('VERSION_PLACEHOLDER', version);
3729

38-
const minifiedHTML = await minify(htmlContent, {
30+
const minifiedHTML = await minify(html, {
3931
collapseWhitespace: true,
4032
removeComments: true,
4133
minifyCSS: true,
42-
minifyJS: true
34+
minifyJS: true,
4335
});
4436

45-
await fs.outputFile(distHtmlPath, minifiedHTML);
37+
fs.writeFileSync('dist/index.html', minifiedHTML);
4638
}
4739

4840
async function compressImages() {
49-
await imagemin([srcImagePath], {
50-
destination: path.dirname(distImagePath),
41+
await imagemin(['src/logo.jpeg'], {
42+
destination: 'dist/',
5143
plugins: [
52-
imageminMozjpeg(),
53-
imageminPngquant()
44+
imageminMozjpeg({ quality: 75 }),
45+
imageminPngquant({ quality: [0.6, 0.8] })
5446
]
5547
});
5648
}
5749

58-
async function copyCNAME() {
59-
await fs.copy(srcCnamePath, distCnamePath);
50+
function copyCNAME() {
51+
fs.copyFileSync('src/CNAME', 'dist/CNAME');
6052
}
6153

6254
async function build() {
55+
// Ensure the dist directory exists
56+
if (!fs.existsSync('dist')) {
57+
fs.mkdirSync('dist');
58+
}
59+
6360
await buildCSS();
64-
await buildHTML();
61+
await minifyHTML();
6562
await compressImages();
66-
await copyCNAME();
63+
copyCNAME();
64+
console.log('Build completed successfully');
6765
}
6866

69-
build().catch(err => {
70-
console.error(err);
71-
process.exit(1);
72-
});
67+
build();

0 commit comments

Comments
 (0)