forked from badlogic/heissepreise
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bundle.js
executable file
·148 lines (133 loc) · 5.09 KB
/
bundle.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
const fs = require("fs");
const path = require("path");
const chokidar = require("chokidar");
const esbuild = require("esbuild");
const { exec } = require("child_process");
const { promisify } = require("util");
function deleteDirectory(directory) {
if (fs.existsSync(directory)) {
fs.readdirSync(directory).forEach((file) => {
const filePath = path.join(directory, file);
if (fs.statSync(filePath).isDirectory()) {
deleteDirectory(filePath);
} else {
fs.unlinkSync(filePath);
}
});
fs.rmdirSync(directory);
}
}
function replaceFileContents(string, fileDir) {
const pattern = /%%([^%]+)%%|\/\/\s*include\s*"([^"]+)"/g;
return string.replace(pattern, (_, filename1, filename2) => {
const filename = filename1 || filename2;
const filenamePath = path.join(fileDir, filename);
try {
const data = fs.readFileSync(filenamePath, "utf8");
const replacedData = replaceFileContents(data, path.dirname(filenamePath));
return replacedData;
} catch (error) {
console.error(`Error reading file "${filenamePath}":`, error);
return "";
}
});
}
function processFile(inputFile, outputFile, filter) {
const fileDir = path.dirname(inputFile);
if (inputFile.includes(".mp3")) {
const data = fs.readFileSync(inputFile);
if (filter(inputFile, false, data)) return;
fs.writeFileSync(outputFile, data);
} else {
const data = fs.readFileSync(inputFile, "utf8");
if (filter(inputFile, false, data)) return;
const replacedData = replaceFileContents(data, fileDir);
fs.writeFileSync(outputFile, replacedData);
}
console.log(`${inputFile} -> ${outputFile}`);
}
function generateSite(inputDir, outputDir, deleteOutput, filter) {
if (deleteOutput) {
deleteDirectory(outputDir);
}
if (!fs.existsSync(outputDir)) fs.mkdirSync(outputDir);
const files = fs.readdirSync(inputDir);
files.forEach((file) => {
const filePath = path.join(inputDir, file);
const isDir = fs.statSync(filePath).isDirectory();
if (file.startsWith("_")) return;
if (isDir) {
if (filter(filePath, true, null)) return;
if (path.resolve(filePath) == path.resolve(outputDir)) return;
const subOutputDir = path.join(outputDir, file);
fs.mkdirSync(subOutputDir, { recursive: true });
generateSite(filePath, subOutputDir, deleteOutput, filter);
} else {
const inputFile = filePath;
const outputFile = path.join(outputDir, file);
processFile(inputFile, outputFile, filter);
}
});
}
async function bundleHTML(inputDir, outputDir, deleteDir = true, watch = false, filter) {
generateSite(inputDir, outputDir, deleteDir, filter);
if (!watch) return;
const watcher = chokidar.watch(inputDir, { ignored: /(^|[\/\\])\../ });
let initialScan = true;
watcher.on("ready", () => (initialScan = false));
watcher.on("all", (event, filePath) => {
if (initialScan) return;
if (path.resolve(filePath).startsWith(path.resolve(outputDir))) return;
console.log(`File ${filePath} has been ${event}`);
generateSite(inputDir, outputDir, false, filter);
});
console.log(`Watching directory for changes: ${inputDir}`);
}
async function bundleCSS(inputFile, outputFile, watch = false) {
const execAsync = promisify(exec);
if (!watch) {
await execAsync(`npx tailwindcss -i ${inputFile} -o ${outputFile} --minify`);
console.log("Generated CSS");
} else {
execAsync(`npx tailwindcss -i ${inputFile} -o ${outputFile} --watch`);
}
}
async function bundleJS(inputDir, outputDir, watch) {
let buildContext = await esbuild.context({
entryPoints: {
carts: `${inputDir}/carts.js`,
cart: `${inputDir}/cart.js`,
changes: `${inputDir}/changes.js`,
settings: `${inputDir}/settings.js`,
index: `${inputDir}/index.js`,
},
bundle: true,
sourcemap: true,
outdir: outputDir,
logLevel: "debug",
minify: !watch,
});
if (!watch) {
await buildContext.rebuild();
console.log("Generated JS");
} else {
buildContext.watch();
}
}
async function bundle(inputDir, outputDir, watch) {
const promises = [];
promises.push(bundleCSS(path.join(inputDir, "style.css"), path.join(outputDir, "style.css"), watch));
promises.push(bundleJS(inputDir, outputDir, watch));
promises.push(
bundleHTML(inputDir, outputDir, false, watch, (filePath, isDir, data) => {
if (isDir) return false;
if (filePath.endsWith("style.css")) return true;
if (filePath.endsWith(".js") && !filePath.includes("socket.io.js")) return true;
if (data.includes(`require("`)) return true;
return false;
})
);
if (!watch) await Promise.all(promises);
}
exports.deleteDirectory = deleteDirectory;
exports.bundle = bundle;