extension-workshop/bin/build-styles

49 строки
1.3 KiB
JavaScript
Executable File

#!/usr/bin/env node
const fetch = require('node-fetch');
const fs = require('fs-extra');
const util = require('util');
const sass = require('node-sass');
const renderSass = util.promisify(sass.render);
const inputFile = './src/assets/css/styles.scss';
const outputFile = './build/assets/css/styles.css';
const isProduction = process.env.ELEVENTY_ENV;
const buildSass = async function () {
const { css } = await renderSass({
file: inputFile,
includePaths: [
'node_modules/foundation-sites/scss/',
'node_modules/slick-carousel/slick/',
'node_modules/hamburgers/_sass/hamburgers',
'node_modules/prismjs/themes',
],
outputStyle: isProduction ? 'compressed' : 'nested',
});
// This is a hint to know if this is a first run, in which case
// we don't need to tell browserSync to update.
const fileExisted = await fs.pathExists(outputFile);
try {
await fs.ensureFile(outputFile);
await fs.writeFile(outputFile, css);
} catch (error) {
console.error(`Error writing generated CSS: ${error}`);
}
if (!isProduction && fileExisted) {
// Tell browserSync to reload.
try {
await fetch(
'http://localhost:8081/__browser_sync__?method=reload&args=styles.css'
);
} catch (error) {
console.error(`Couldn't communicate with browserSync!`);
}
}
};
buildSass();