From 53ac2a6b6fe6993ed67be463ade9343f266cf933 Mon Sep 17 00:00:00 2001 From: Thomas Cazade Date: Mon, 14 Sep 2020 08:12:09 +0200 Subject: [PATCH] Add constants for build-scripts --- build/build.js | 22 ++++++++-------------- build/constants.js | 8 ++++++++ build/hot-reload.js | 23 +++++++++++++---------- 3 files changed, 29 insertions(+), 24 deletions(-) create mode 100644 build/constants.js diff --git a/build/build.js b/build/build.js index 755f2ea..bcb16ab 100644 --- a/build/build.js +++ b/build/build.js @@ -1,5 +1,4 @@ const fs = require('fs'); -const path = require('path'); const sass = require('sass'); const write = require('write'); const rimraf = require('rimraf'); @@ -7,24 +6,22 @@ const postcss = require('postcss'); const cssnano = require('cssnano'); const autoprefixer = require('autoprefixer'); +const constants = require('./constants'); const log = require('./log'); async function build() { - const entrypoint = path.resolve(__dirname, '../src/styles.scss'); - const paperDocsPath = path.resolve(__dirname, '../docs/static/assets/paper.css'); - log('Starting PaperCSS build...'); log('Cleaning "dist/, docs/static/assets/paper.css" folder...'); rimraf.sync('dist', { disableGlob: true }); - if (fs.existsSync(paperDocsPath)) { - fs.unlinkSync(paperDocsPath); + if (fs.existsSync(constants.PAPER_DOCS_PATH)) { + fs.unlinkSync(constants.PAPER_DOCS_PATH); } - log('Compiling SCSS to CSS, entrypoint:', entrypoint); + log('Compiling SCSS to CSS, entrypoint:', constants.ENTRYPOINT_PATH); - const compiledCSS = sass.renderSync({ file: entrypoint }); + const compiledCSS = sass.renderSync({ file: constants.ENTRYPOINT_PATH }); log('Processing CSS: autoprefixer...'); @@ -34,14 +31,11 @@ async function build() { const minifiedCSS = await postcss([cssnano]).process(autoprefixedCSS.css, { from: undefined }); - const paperPath = path.resolve(__dirname, '../dist/paper.css'); - const paperminpath = path.resolve(__dirname, '../dist/paper.min.css'); - log('Writing paper.css and paper.min.css files to dist/ and docs/ folders...'); - write(paperPath, autoprefixedCSS.css); - write(paperminpath, minifiedCSS.css); - write(paperDocsPath, autoprefixedCSS.css); + write(constants.PAPER_DIST_PATH, autoprefixedCSS.css); + write(constants.PAPER_DIST_MIN_PATH, minifiedCSS.css); + write(constants.PAPER_DOCS_PATH, autoprefixedCSS.css); log('Build done!'); } diff --git a/build/constants.js b/build/constants.js new file mode 100644 index 0000000..0ed868f --- /dev/null +++ b/build/constants.js @@ -0,0 +1,8 @@ +const path = require('path'); + +module.exports = { + ENTRYPOINT_PATH: path.resolve(__dirname, '../src/styles.scss'), + PAPER_DIST_PATH: path.resolve(__dirname, '../dist/paper.css'), + PAPER_DIST_MIN_PATH: path.resolve(__dirname, '../dist/paper.min.css'), + PAPER_DOCS_PATH: path.resolve(__dirname, '../docs/static/assets/paper.css'), +}; diff --git a/build/hot-reload.js b/build/hot-reload.js index 0d89b56..4c02676 100644 --- a/build/hot-reload.js +++ b/build/hot-reload.js @@ -1,23 +1,26 @@ const util = require('util'); -const path = require('path'); const sass = require('sass'); const write = require('write'); const postcss = require('postcss'); const chokidar = require('chokidar'); const autoprefixer = require('autoprefixer'); +const constants = require('./constants'); const log = require('./log'); const sassRenderPromisified = util.promisify(sass.render); -const entrypoint = path.resolve(__dirname, '../src/styles.scss'); -const paperDocsPath = path.resolve(__dirname, '../docs/static/assets/paper.css'); - -chokidar.watch('./src/**/*.scss').on('change', (event, path) => { - log(`Detected file change (${event}), re-building compiled CSS...`); - - sassRenderPromisified({ file: entrypoint }) +function compile() { + sassRenderPromisified({ file: constants.ENTRYPOINT_PATH }) .then((compiledCSS) => postcss([autoprefixer]).process(compiledCSS.css.toString(), { from: undefined })) - .then((autoprefixedCSS) => write(paperDocsPath, autoprefixedCSS.css)) - .then(() => log('Re-built compiled CSS.')); + .then((autoprefixedCSS) => write(constants.PAPER_DOCS_PATH, autoprefixedCSS.css)) + .then(() => log('Compiled CSS in docs/ folder.')); +} + +chokidar.watch('./src/**/*.scss').on('change', (event) => { + log(`Detected file change (${event}), compiling SCSS to CSS...`); + compile(); }); + +// Do initial compilation. +compile();