|
| 1 | +const fs = require('fs'); |
1 | 2 | const path = require('path'); |
2 | 3 |
|
3 | 4 | const express = require('express'); |
4 | 5 | const pug = require('pug'); |
| 6 | +const sass = require('sass'); |
5 | 7 |
|
6 | 8 | const app = express(); |
7 | 9 | const port = 3000; |
8 | 10 |
|
9 | | -app.use('/', express.static(path.join(__dirname, 'src/static'))) |
| 11 | +const SRC_PATH = path.join(__dirname, 'src'); |
| 12 | +const STATIC_PATH = path.join(__dirname, 'static'); |
| 13 | +const CSS_PATH = path.join(STATIC_PATH, 'css/style.css'); |
| 14 | +const HTML_PATH = path.join(STATIC_PATH, 'index.html'); |
10 | 15 |
|
11 | | -app.get('/', (req, res) => { |
12 | | - res.send(pug.renderFile('./src/index.pug')); |
| 16 | +app.use('/', express.static(path.join(STATIC_PATH))) |
| 17 | + |
| 18 | +app.get('/', async (_, res) => { |
| 19 | + // Build Pug file |
| 20 | + console.log('Building Pug file...'); |
| 21 | + |
| 22 | + const compiledFunction = pug.compileFile(path.join(SRC_PATH, 'index.pug')); |
| 23 | + |
| 24 | + fs.writeFileSync(HTML_PATH, compiledFunction()); |
| 25 | + |
| 26 | + console.log('Build successful!'); |
| 27 | + |
| 28 | + // Build SCSS file |
| 29 | + console.log('Building SCSS file...'); |
| 30 | + |
| 31 | + const result = await sass.compileAsync(path.join(SRC_PATH, 'style.scss')); |
| 32 | + |
| 33 | + fs.writeFileSync(CSS_PATH, result.css); |
| 34 | + |
| 35 | + console.log('Build successful!'); |
| 36 | + |
| 37 | + // Render HTML file |
| 38 | + res.send(pug.renderFile(HTML_PATH)); |
13 | 39 | }); |
14 | 40 |
|
15 | 41 | app.listen(port, () => { |
|
0 commit comments