OTM/compile_sass.js
choelzl f42e57a3b3
All checks were successful
continuous-integration/drone/push Build is passing
fixes
2021-07-16 11:58:43 +02:00

62 lines
1.4 KiB
JavaScript

const path = require('path');
const fs = require('fs');
const mkdirp = require('mkdirp');
const sass = require('node-sass');
const nodeEnv = process.env.NODE_ENV;
module.exports = {
compileSass,
compileSassProduction,
compileSassMain
};
function compileSass(sassFile) {
const sassOptions = {
file: sassFile
};
if (nodeEnv !== 'production') {
sassOptions.sourceMapEmbed = true;
}
else {
sassOptions.outputStyle = 'compressed';
}
return new Promise((resolve, reject) => {
sass.render(sassOptions, (error, result) => {
if (error) {
return reject(error);
}
resolve(result.css.toString());
});
}).catch(console.error);
}
function compileSassProduction(sassFile) {
const fullSassPath = path.join(__dirname, 'public/scss/', sassFile);
const cssFile = sassFile.replace('.scss', '.css');
const cssPath = path.join(__dirname, 'public/css/');
const fullCssPath = path.join(cssPath, cssFile);
return compileSass(fullSassPath).then(css =>
mkdirp(cssPath).then(() =>
new Promise((resolve, reject) => {
fs.writeFile(fullCssPath, css, error => {
if (error) {
return reject(error);
}
resolve(cssFile);
});
})
)
).catch(console.error);;
}
function compileSassMain() {
return compileSassProduction('index.scss').then(() => {
console.log('Created index.css');
}).catch(console.error);
}