Skip to content

Commit 9462c89

Browse files
committed
bye bye nodemon
1 parent 6c3dcfe commit 9462c89

File tree

8 files changed

+116
-262
lines changed

8 files changed

+116
-262
lines changed

build.js

Lines changed: 109 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -14,127 +14,133 @@ const distDir = path.join(__dirname, "dist");
1414

1515
// Create dist directory if it doesn't exist
1616
if (!fs.existsSync(distDir)) {
17-
fs.mkdirSync(distDir);
17+
fs.mkdirSync(distDir);
1818
}
1919

2020
// Function to resize images based on HTML attributes and compress them
2121
async function resizeImages(html) {
22-
const imgRegex =
23-
/<img[^>]+src="([^">]+)"[^>]+width="([^">]+)"[^>]+height="([^">]+)"[^>]*>/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+
/<img[^>]+src="([^">]+)"[^>]+width="([^">]+)"[^>]+height="([^">]+)"[^>]*>/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+
}
5252
}
5353

5454
// Function to compress non-resized images using sharp
5555
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+
}
8181
}
8282

8383
// Read component files and inject them into index.html
8484
async function buildHTML() {
85-
let html = fs.readFileSync(path.join(srcDir, "index.html"), "utf-8");
86-
87-
const componentRegex = /<component src="(.*)"><\/component>/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 = /<component src="(.*)"><\/component>/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);
108108
}
109109

110110
// Copy all files from /src/assets to /dist/assets, and root style/script files to /dist
111111
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+
}
132138
}
133139

134140
// Run build process with top-level await (Node.js 14+ supports it)
135141
(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!");
140146
})();

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"scripts": {
88
"build": "node build.js",
99
"dev": "npm-run-all --parallel watch serve",
10-
"watch": "nodemon --watch src --ext js,html,css --exec 'node build.js'",
10+
"watch": "node --watch-path=src build.js",
1111
"serve": "live-server dist --port=3000"
1212
},
1313
"keywords": [],
@@ -22,7 +22,6 @@
2222
},
2323
"devDependencies": {
2424
"live-server": "^1.2.2",
25-
"nodemon": "^3.1.7",
2625
"npm-run-all": "^4.1.5"
2726
},
2827
"pnpm": {

0 commit comments

Comments
 (0)