2023-03-01 23:27:34 +01:00
|
|
|
import chokidar from "chokidar"
|
|
|
|
import express from "express"
|
|
|
|
import fs from "fs"
|
|
|
|
import njk from "nunjucks"
|
|
|
|
import sass from "sass"
|
|
|
|
|
|
|
|
const server = express()
|
|
|
|
|
|
|
|
njk.configure(
|
|
|
|
"./web",
|
|
|
|
{
|
|
|
|
autoescape: true,
|
|
|
|
express: server,
|
|
|
|
lstripBlocks: true,
|
|
|
|
trimBlocks: true,
|
|
|
|
watch: true
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
chokidar.watch("./src").on("change", () => {
|
|
|
|
console.log("Detected changes in SCSS! Recompiling...")
|
|
|
|
|
2023-03-06 10:20:47 +01:00
|
|
|
try {
|
|
|
|
const { css } = sass.compile(
|
|
|
|
"./src/main.scss",
|
|
|
|
{
|
|
|
|
sourceMap: false,
|
|
|
|
alertColor: true,
|
|
|
|
style: "compressed"
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
console.log("Compile done! Saving changes...")
|
|
|
|
|
|
|
|
if (!fs.existsSync("./out"))
|
|
|
|
fs.mkdirSync("./out")
|
|
|
|
|
|
|
|
fs.writeFileSync(
|
|
|
|
"./out/main.css",
|
|
|
|
css
|
|
|
|
)
|
|
|
|
|
|
|
|
console.log("Saved!")
|
|
|
|
} catch (error) {
|
|
|
|
console.log("Error while compiling!")
|
|
|
|
console.error(error)
|
|
|
|
}
|
2023-03-01 23:27:34 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
server.use("/assets", express.static("./out"))
|
|
|
|
|
|
|
|
server.get(
|
|
|
|
"*",
|
|
|
|
(req, res) => {
|
|
|
|
const path = req.path
|
|
|
|
.replace(/^\/|\.\.+|\/$/g, "")
|
|
|
|
|
|
|
|
if (fs.existsSync(`web/pages/${path}.njk`))
|
|
|
|
return res.render(`pages/${path}.njk`)
|
|
|
|
|
|
|
|
if (fs.existsSync(`web/pages/${path}/index.njk`))
|
|
|
|
return res.render(`pages/${path}/index.njk`)
|
|
|
|
|
|
|
|
res.status(404).send("Not Found")
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
server.listen(12345, () => console.log("Dev server started and listening on port 12345!"))
|