Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[UI Framework] Spawn compileCss as a child process to prevent a node-sass fatal error from killing the watch process #13222

Merged
merged 6 commits into from
Aug 1, 2017
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@
"mocha": "echo 'use `node scripts/mocha`' && false",
"sterilize": "grunt sterilize",
"uiFramework:start": "grunt uiFramework:start",
"uiFramework:build": "grunt uiFramework:build"
"uiFramework:build": "grunt uiFramework:build",
"uiFramework:compileCss": "grunt uiFramework:compileCss"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Expanding on our conversation here. When would someone need to manually compile the CSS? I can understand creating a grunt task for it, but not sure it warrants an aliased npm task.

},
"repository": {
"type": "git",
Expand Down
59 changes: 42 additions & 17 deletions tasks/ui_framework.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,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',
Expand Down Expand Up @@ -77,27 +82,47 @@ 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);
}
});
return new Promise(resolve => {
Copy link
Contributor

@tylersmalley tylersmalley Jul 31, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should use Bluebird.promisify instead of wrapping here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you show me what you mean?

Copy link
Contributor

@tylersmalley tylersmalley Aug 1, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thinking something like this:

  import { promisify } from 'bluebird';
  const renderSass = promisify(sass.render);

  function uiFrameworkCompile() {
    const from = 'ui_framework/components/index.scss';
    const to = 'ui_framework/dist/ui_framework.css';

    return renderSass({
      file: from
    }).then(result => {
      postcss([postcssConfig])
        .process(result.css, { from, to })
        .then(result => {
          grunt.file.write(to, result.css);

          if (result.map) {
            grunt.file.write(`${to}.map`, result.map);
          }
        });
    }).catch(error => {
      grunt.log.error(error);
    });
  }

sass.render({
file: 'ui_framework/components/index.scss'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can use src here

}, 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);
}

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({
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you wanted to use a promise here you could as well, but I feel the callback is fine if you want to leave it.

const spawn = promisify(grunt.util.spawn);

...

spawn({
  cmd: isPlatformWindows ? '.\\node_modules\\.bin\\grunt.cmd' : './node_modules/.bin/grunt',
  args: [ 'uiFramework:compileCss' ]
}).then(grunt.log.writeln).catch(e => grunt.log.error(e.stdout));

cmd: 'npm',
args: [
'run',
'uiFramework:compileCss',
],
}, (error, result) => {
if (error) {
grunt.log.error(result.stdout);
} else {
grunt.log.writeln(result);
}
});
}, 400, { leading: true });

return new Promise(() => {
debouncedCompile();
Expand Down
5 changes: 4 additions & 1 deletion ui_framework/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@

### Documentation

Compile the CSS with `npm run uiFramework:compileCss`.

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.

Expand Down