Skip to content

Commit cfd8c21

Browse files
author
ricky.gummadi
committed
fix build
1 parent 57b6bc8 commit cfd8c21

File tree

1 file changed

+76
-140
lines changed

1 file changed

+76
-140
lines changed

build/build.test.js

Lines changed: 76 additions & 140 deletions
Original file line numberDiff line numberDiff line change
@@ -1,141 +1,77 @@
1-
const fs = require('fs-extra');
2-
const path = require('path');
3-
const sizeOf = require('image-size');
4-
5-
describe('Build Process', () => {
6-
const srcHtmlPath = path.join(__dirname, '../src/index.html');
7-
const distHtmlPath = path.join(__dirname, '../dist/index.html');
8-
const srcCssPath = path.join(__dirname, '../src/styles.css');
9-
const distCssPath = path.join(__dirname, '../dist/styles.min.css');
10-
const srcImagePath = path.join(__dirname, '../src/logo.jpeg');
11-
const distImagePath = path.join(__dirname, '../dist/logo.jpeg');
12-
const srcCnamePath = path.join(__dirname, '../src/CNAME');
13-
const distCnamePath = path.join(__dirname, '../dist/CNAME');
14-
15-
test('HTML is minified', () => {
16-
const srcHtml = fs.readFileSync(srcHtmlPath, 'utf8');
17-
const distHtml = fs.readFileSync(distHtmlPath, 'utf8');
18-
19-
// Check that minified HTML is smaller than the original
20-
try {
21-
expect(distHtml.length).toBeLessThan(srcHtml.length);
22-
console.log('Success: Minified HTML is smaller than the original HTML.');
23-
} catch (error) {
24-
error.message = 'Failed: Minified HTML is not smaller than the original HTML.';
25-
throw error;
26-
}
27-
28-
// Check that specific elements are present
29-
try {
30-
expect(distHtml).toContain('<nav class="navbar');
31-
expect(distHtml).toContain('<div class="container');
32-
console.log('Success: Required elements are present in the minified HTML.');
33-
} catch (error) {
34-
error.message = 'Failed: Required elements are missing in the minified HTML.';
35-
throw error;
36-
}
37-
});
38-
39-
test('CSS is minified', () => {
40-
const srcCss = fs.readFileSync(srcCssPath, 'utf8');
41-
const distCss = fs.readFileSync(distCssPath, 'utf8');
42-
43-
// Check that minified CSS is smaller than the original
44-
try {
45-
expect(distCss.length).toBeLessThan(srcCss.length);
46-
console.log('Success: Minified CSS is smaller than the original CSS.');
47-
} catch (error) {
48-
error.message = 'Failed: Minified CSS is not smaller than the original CSS.';
49-
throw error;
50-
}
51-
});
52-
53-
test('HTML contains required meta tags', () => {
54-
const distHtml = fs.readFileSync(distHtmlPath, 'utf8');
55-
56-
// Check for specific meta tags using regex to account for minified formatting
57-
try {
58-
expect(distHtml).toMatch(/<meta charset="utf-8">/);
59-
expect(distHtml).toMatch(/<meta name="viewport" content="width=device-width,initial-scale=1">/);
60-
console.log('Success: Required meta tags are present in the minified HTML.');
61-
} catch (error) {
62-
error.message = 'Failed: Required meta tags are missing in the minified HTML.';
63-
throw error;
64-
}
65-
});
66-
67-
test('HTML contains CSS link', () => {
68-
const distHtml = fs.readFileSync(distHtmlPath, 'utf8');
69-
70-
// Check for the minified CSS link
71-
try {
72-
expect(distHtml).toContain('<link href="styles.min.css" rel="stylesheet">');
73-
console.log('Success: Minified CSS link is present in the HTML.');
74-
} catch (error) {
75-
error.message = 'Failed: Minified CSS link is missing in the HTML.';
76-
throw error;
77-
}
78-
});
79-
80-
test('HTML contains JavaScript inclusion', () => {
81-
const distHtml = fs.readFileSync(distHtmlPath, 'utf8');
82-
83-
// Check for the JavaScript bundle inclusion
84-
try {
85-
expect(distHtml).toContain('<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-YvpcrYf0tY3lHB60NNkmXc5s9fDVZLESaAA55NDzOxhy9GkcIdslK1eN7N6jIeHz" crossorigin="anonymous"></script>');
86-
console.log('Success: JavaScript inclusion is present in the HTML.');
87-
} catch (error) {
88-
error.message = 'Failed: JavaScript inclusion is missing in the HTML.';
89-
throw error;
90-
}
91-
});
92-
93-
test('CNAME file is copied and contains the correct domain', () => {
94-
const srcCname = fs.readFileSync(srcCnamePath, 'utf8');
95-
const distCname = fs.readFileSync(distCnamePath, 'utf8');
96-
97-
// Check that CNAME file content is the same
98-
try {
99-
expect(distCname).toBe(srcCname);
100-
console.log('Success: CNAME file is copied correctly.');
101-
} catch (error) {
102-
error.message = 'Failed: CNAME file is not copied correctly.';
103-
throw error;
104-
}
105-
106-
// Check that CNAME file contains the correct domain
107-
try {
108-
expect(distCname).toContain('ipsimple.org');
109-
console.log('Success: CNAME file contains the correct domain.');
110-
} catch (error) {
111-
error.message = 'Failed: CNAME file does not contain the correct domain.';
112-
throw error;
113-
}
114-
});
115-
116-
test('Image is compressed', () => {
117-
const srcImage = fs.readFileSync(srcImagePath);
118-
const distImage = fs.readFileSync(distImagePath);
119-
120-
// Check that compressed image is smaller than the original
121-
try {
122-
expect(distImage.length).toBeLessThan(srcImage.length);
123-
console.log('Success: Image is compressed successfully.');
124-
} catch (error) {
125-
error.message = 'Failed: Image is not compressed successfully.';
126-
throw error;
127-
}
128-
129-
// Optionally, check that the image dimensions are the same
130-
const srcDimensions = sizeOf(srcImage);
131-
const distDimensions = sizeOf(distImage);
132-
try {
133-
expect(distDimensions.width).toBe(srcDimensions.width);
134-
expect(distDimensions.height).toBe(srcDimensions.height);
135-
console.log('Success: Image dimensions are preserved after compression.');
136-
} catch (error) {
137-
error.message = 'Failed: Image dimensions are not preserved after compression.';
138-
throw error;
139-
}
140-
});
1+
import fs from 'fs-extra';
2+
import path from 'path';
3+
import { fileURLToPath } from 'url';
4+
import cssnano from 'cssnano';
5+
import PurgeCSS from 'purgecss';
6+
import { minify } from 'html-minifier-terser';
7+
import imagemin from 'imagemin';
8+
import imageminMozjpeg from 'imagemin-mozjpeg';
9+
import imageminPngquant from 'imagemin-pngquant';
10+
11+
// Get the version number from environment variable set by the CI/CD pipeline
12+
const version = process.env.NEW_TAG || '1.0.0';
13+
14+
// Convert import.meta.url to __dirname
15+
const __filename = fileURLToPath(import.meta.url);
16+
const __dirname = path.dirname(__filename);
17+
18+
const srcHtmlPath = path.join(__dirname, '../src/index.html');
19+
const distHtmlPath = path.join(__dirname, '../dist/index.html');
20+
const srcCssPath = path.join(__dirname, '../src/styles.css');
21+
const distCssPath = path.join(__dirname, '../dist/styles.min.css');
22+
const srcImagePath = path.join(__dirname, '../src/logo.jpeg');
23+
const distImagePath = path.join(__dirname, '../dist/logo.jpeg');
24+
const srcCnamePath = path.join(__dirname, '../src/CNAME');
25+
const distCnamePath = path.join(__dirname, '../dist/CNAME');
26+
27+
async function buildCSS() {
28+
const purgeCSSResult = await new PurgeCSS().purge({
29+
content: [srcHtmlPath],
30+
css: [srcCssPath]
31+
});
32+
33+
const minifiedCSS = await cssnano.process(purgeCSSResult[0].css, { from: undefined });
34+
await fs.outputFile(distCssPath, minifiedCSS.css);
35+
}
36+
37+
async function buildHTML() {
38+
let htmlContent = await fs.readFile(srcHtmlPath, 'utf8');
39+
40+
// Replace the version placeholder with the actual version number
41+
htmlContent = htmlContent.replace('VERSION_PLACEHOLDER', version);
42+
43+
const minifiedHTML = await minify(htmlContent, {
44+
collapseWhitespace: true,
45+
removeComments: true,
46+
minifyCSS: true,
47+
minifyJS: true
48+
});
49+
50+
await fs.outputFile(distHtmlPath, minifiedHTML);
51+
}
52+
53+
async function compressImages() {
54+
await imagemin([srcImagePath], {
55+
destination: path.dirname(distImagePath),
56+
plugins: [
57+
imageminMozjpeg(),
58+
imageminPngquant()
59+
]
60+
});
61+
}
62+
63+
async function copyCNAME() {
64+
await fs.copy(srcCnamePath, distCnamePath);
65+
}
66+
67+
async function build() {
68+
await buildCSS();
69+
await buildHTML();
70+
await compressImages();
71+
await copyCNAME();
72+
}
73+
74+
build().catch(err => {
75+
console.error(err);
76+
process.exit(1);
14177
});

0 commit comments

Comments
 (0)