forked from dhg/Skeleton
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuild.js
66 lines (54 loc) · 1.81 KB
/
build.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
const fs = require("fs");
const path = require("path");
const chokidar = require("chokidar");
const liveServer = require("live-server");
const minify = require('@node-minify/core');
const cleanCSS = require('@node-minify/clean-css');
const CSSFiles = ["flexboxgrid-a11y.css", "normalize.css", "skeleton-a11y.css"];
// Compiler et bundler les fichiers
function buildFiles() {
let fullCSS = "";
// 1ère étape: Minification et agrégation
for (let CSSFileName of CSSFiles) {
fullCSS += minifyCSS(CSSFileName);
}
// 2ème étape: résultat dans dossier dist
const outputPath = path.join("dist", "skeleton-a11y.min.css");
fs.mkdirSync(path.dirname(outputPath), { recursive: true });
fs.writeFileSync(outputPath, fullCSS);
fs.rm("tmp", { recursive: true }, () => console.log("Removed tmp directory."));
console.log("Build completed!");
}
// Lancez le build initial
buildFiles();
// Créer un watcher pour le dossier src
const watcher = chokidar.watch(".", {
ignored: [/(^|[\/\\])\../, "dist", "node_modules", "tmp"],
persistent: true,
});
watcher.on("change", (path) => {
console.log(`File ${path} has been changed`);
buildFiles();
});
// Lancez live-server pour hot reloading
liveServer.start({
port: 8080,
host: "localhost",
open: false,
file: "index.html",
wait: 1000,
});
function minifyCSS(filePath) {
const minFileName = filePath.replace(/\.css/, ".min.css");
const fileFullPath = path.join("css", filePath);
if (fs.existsSync(fileFullPath)) {
fs.mkdirSync("tmp", { recursive: true });
minify({
compressor: cleanCSS,
input: fileFullPath,
output: path.join("tmp", minFileName),
callback: function (err, min) {}
});
return fs.readFileSync(path.join("tmp", minFileName), "utf-8");
}
}