Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds sass compile function #45

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions lib/compile-sass.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const sass = require("sass");
const fs = require("fs");

const compileSass = () => {
const sassFilenames = fs.readdirSync("./styles");

for (const filename of sassFilenames) {
const cssFilename = filename.replace(".scss", ".css");

try {
const rendered = sass.renderSync({
file: `./styles/${filename}`,
debug: true,
outputStyle: "expanded",
});

fs.writeFileSync(`./public/styles/${cssFilename}`, rendered.css.toString());
} catch (err) {
console.log(`\n 🙈🚨 UNABLE TO GENERATE CSS FROM SASS FILE: ${filename} 🚨🙈 \n ERROR: ${err} \n`);
}
}
};

module.exports = compileSass;
26 changes: 0 additions & 26 deletions lib/sass-middleware.js

This file was deleted.

3 changes: 3 additions & 0 deletions nodemon.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"ext": "html,css,js,scss,ejs"
}
12 changes: 3 additions & 9 deletions server.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
// load .env data into process.env
require('dotenv').config();

// compile SCSS files on server start/restart
require('./lib/compile-sass')();

// Web server config
const sassMiddleware = require('./lib/sass-middleware');
const express = require('express');
const morgan = require('morgan');

Expand All @@ -16,14 +18,6 @@ app.set('view engine', 'ejs');
// The :status token will be colored red for server error codes, yellow for client error codes, cyan for redirection codes, and uncolored for all other codes.
app.use(morgan('dev'));
app.use(express.urlencoded({ extended: true }));
app.use(
'/styles',
sassMiddleware({
source: __dirname + '/styles',
destination: __dirname + '/public/styles',
isSass: false, // false => scss, true => sass
})
);
app.use(express.static('public'));

// Separated Routes for each Resource
Expand Down