From 8b3bd49dab75467a57747f8f7eabc72b6fba808b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B8ren=20Birkemeyer?= Date: Wed, 13 Apr 2022 17:59:58 +0000 Subject: [PATCH 1/2] Emit correct exit code on Sass compilation errors if env != development Use hard exit for prod, staging and testing environments (this is especially important for CI debugging) when Sass encounters compilation errors. On the other hand, it is useful to only emit "soft" errors during local development; it would be frustrating if watch/serve process would crash every time a Sass compilation is not successful (e.g. because of a typo). --- tasks/styles.js | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/tasks/styles.js b/tasks/styles.js index a8562f5..0ba73d7 100644 --- a/tasks/styles.js +++ b/tasks/styles.js @@ -1,3 +1,5 @@ +const PluginError = require('plugin-error'); + function styles(gulp, $, config) { // take the array and map over each script config object in it const tasks = config.styles.files.map((stylesheet) => { @@ -15,7 +17,12 @@ function styles(gulp, $, config) { pipeStdout: true, sassOutputStyle: 'nested', includePaths: config.styles.includePaths ? config.styles.includePaths : [config.npmdir] - }).on('error', $.sass.logError)) + }).on('error', function(error) { + const message = new PluginError('sass', error.messageFormatted).toString(); + process.stderr.write(`${message}\n`); + config.development ? $.through2.obj() : process.exitCode = 1; + done(); + })) .pipe($.postcss(config.styles.postCssPlugins(config, stylesheet))) .pipe($.concat(stylesheet.name)) .pipe($.cleanCss({ From 8da76758feb4a9649c5bc0c6a3820ec66dd08d68 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B8ren=20Birkemeyer?= Date: Thu, 14 Apr 2022 11:39:24 +0000 Subject: [PATCH 2/2] Use a dedicated parameter to determine hard exit behaviour on errors --- tasks/browsersync.js | 5 +++++ tasks/styles.js | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/tasks/browsersync.js b/tasks/browsersync.js index 7e30b31..6798af4 100644 --- a/tasks/browsersync.js +++ b/tasks/browsersync.js @@ -4,6 +4,11 @@ function browsersync(gulp, $, config, css, js) { open: false }); + // set a config param to signal browserSync is active; + // styles.js will suppress errors for this env to avoid + // the watch process exiting on errors + config.livereload = true; + gulp.watch(config.styles.watch, { usePolling: true }, css); gulp.watch(config.scripts.watch, { usePolling: true }, js); } diff --git a/tasks/styles.js b/tasks/styles.js index 0ba73d7..e13f0fd 100644 --- a/tasks/styles.js +++ b/tasks/styles.js @@ -20,7 +20,7 @@ function styles(gulp, $, config) { }).on('error', function(error) { const message = new PluginError('sass', error.messageFormatted).toString(); process.stderr.write(`${message}\n`); - config.development ? $.through2.obj() : process.exitCode = 1; + config.livereload ? $.through2.obj() : process.exitCode = 1; done(); })) .pipe($.postcss(config.styles.postCssPlugins(config, stylesheet)))