From 08719cd86340df6c80d2f34f5b43df5d4ef2d7a2 Mon Sep 17 00:00:00 2001 From: CJ Cenizal Date: Tue, 1 Aug 2017 14:11:23 -0700 Subject: [PATCH] [UI Framework] Spawn compileCss as a child process to prevent a node-sass fatal error from killing the watch process (#13222) * Spawn compileCss as a child process to prevent a node-sass fatal error from killing the watch process. * Document tasks. --- tasks/ui_framework.js | 62 ++++++++++++++++++++++++++++++------------ ui_framework/README.md | 6 +++- 2 files changed, 50 insertions(+), 18 deletions(-) diff --git a/tasks/ui_framework.js b/tasks/ui_framework.js index abc65f0ec05c..cb686f3beb01 100644 --- a/tasks/ui_framework.js +++ b/tasks/ui_framework.js @@ -3,6 +3,7 @@ import postcss from 'postcss'; import postcssConfig from '../src/optimize/postcss.config'; import chokidar from 'chokidar'; import debounce from 'lodash/function/debounce'; + const platform = require('os').platform(); const isPlatformWindows = /^win/.test(platform); @@ -44,6 +45,11 @@ module.exports = function (grunt) { Promise.all([uiFrameworkWatch(), uiFrameworkServerStart()]).then(done); }); + grunt.registerTask('uiFramework:compileCss', function () { + const done = this.async(); + uiFrameworkCompile().then(done); + }); + function uiFrameworkServerStart() { const serverCmd = { cmd: isPlatformWindows ? '.\\node_modules\\.bin\\webpack-dev-server.cmd' : './node_modules/.bin/webpack-dev-server', @@ -77,27 +83,49 @@ module.exports = function (grunt) { } function uiFrameworkCompile() { - sass.render({ - file: 'ui_framework/components/index.scss' - }, function (error, result) { - if (error) { - grunt.log.error(error); - } - - postcss([postcssConfig]) - .process(result.css, { from: 'ui_framework/components/index.scss', to: 'ui_framework/dist/ui_framework.css' }) - .then(result => { - grunt.file.write('ui_framework/dist/ui_framework.css', result.css); - - if (result.map) { - grunt.file.write('ui_framework/dist/ui_framework.css.map', result.map); - } - }); + const src = 'ui_framework/components/index.scss'; + const dest = 'ui_framework/dist/ui_framework.css'; + + return new Promise(resolve => { + sass.render({ + file: src, + }, function (error, result) { + if (error) { + grunt.log.error(error); + } + + postcss([postcssConfig]) + .process(result.css, { from: src, to: dest }) + .then(result => { + grunt.file.write(dest, result.css); + + if (result.map) { + grunt.file.write(`${dest}.map`, result.map); + } + + resolve(); + }); + }); }); } function uiFrameworkWatch() { - const debouncedCompile = debounce(uiFrameworkCompile, 400, { leading: true }); + const debouncedCompile = debounce(() => { + // Compile the SCSS in a separate process because node-sass throws a fatal error if it fails + // to compile. + grunt.util.spawn({ + cmd: isPlatformWindows ? '.\\node_modules\\.bin\\grunt.cmd' : './node_modules/.bin/grunt', + args: [ + 'uiFramework:compileCss', + ], + }, (error, result) => { + if (error) { + grunt.log.error(result.stdout); + } else { + grunt.log.writeln(result); + } + }); + }, 400, { leading: true }); return new Promise(() => { debouncedCompile(); diff --git a/ui_framework/README.md b/ui_framework/README.md index 67396c5dd312..61bb97d16ba5 100644 --- a/ui_framework/README.md +++ b/ui_framework/README.md @@ -7,8 +7,12 @@ ### Documentation +Compile the CSS with `./node_modules/grunt/bin/grunt uiFramework:compileCss` (OS X) or +`.\node_modules\grunt\bin\grunt uiFramework:compileCss` (Windows). + You can view interactive documentation by running `npm run uiFramework:start` and then visiting -`http://localhost:8020/`. +`http://localhost:8020/`. This will also start watching the SCSS files, and will recompile the CSS +automatically for you when you make changes. You can run `node scripts/jest --watch` to watch for changes and run the tests as you code.