2021-07-16 09:12:30 +02:00
|
|
|
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);
|
|
|
|
|
2021-07-16 11:58:43 +02:00
|
|
|
return compileSass(fullSassPath).then(css =>
|
|
|
|
mkdirp(cssPath).then(() =>
|
|
|
|
new Promise((resolve, reject) => {
|
2021-07-16 09:12:30 +02:00
|
|
|
fs.writeFile(fullCssPath, css, error => {
|
|
|
|
if (error) {
|
|
|
|
return reject(error);
|
|
|
|
}
|
|
|
|
|
|
|
|
resolve(cssFile);
|
|
|
|
});
|
2021-07-16 11:58:43 +02:00
|
|
|
})
|
|
|
|
)
|
|
|
|
).catch(console.error);;
|
2021-07-16 09:12:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function compileSassMain() {
|
|
|
|
return compileSassProduction('index.scss').then(() => {
|
|
|
|
console.log('Created index.css');
|
|
|
|
}).catch(console.error);
|
|
|
|
}
|