Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
8 changes: 8 additions & 0 deletions change/just-scripts-2020-10-01-10-54-45-node-sass.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"type": "patch",
"comment": "adds some safe-guard against stalling with node-sass in sassTask",
"packageName": "just-scripts",
"email": "kchau@microsoft.com",
"dependentChangeType": "patch",
"date": "2020-10-01T17:54:45.328Z"
}
51 changes: 48 additions & 3 deletions packages/just-scripts/src/tasks/sassTask.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,18 @@ export interface SassTaskOptions {
postcssPlugins?: any[];
}

let processHandlerSet = false;

export function sassTask(options: SassTaskOptions): TaskFunction;
/** @deprecated Use object param version */
export function sassTask(createSourceModule: (fileName: string, css: string) => string, postcssPlugins?: any[]): TaskFunction;
export function sassTask(
optionsOrCreateSourceModule: SassTaskOptions | ((fileName: string, css: string) => string),
postcssPlugins?: any[]
): TaskFunction {
// node-sass causes strange behavior when it fails. It sometimes hangs the whole process
setUncaughtExceptionHandler();

let createSourceModule: (fileName: string, css: string) => string;
if (typeof optionsOrCreateSourceModule === 'function') {
createSourceModule = optionsOrCreateSourceModule;
Expand All @@ -35,7 +40,7 @@ export function sassTask(

if (!nodeSass || !postcss || !autoprefixer) {
logger.warn('One of these [node-sass, postcss, autoprefixer] is not installed, so this task has no effect');
done();
cleanUpAndDone(done);
return;
}

Expand Down Expand Up @@ -84,9 +89,9 @@ export function sassTask(
}
);

parallelLimit(tasks, 5, done);
parallelLimit(tasks, 5, err => cleanUpAndDone(done, err));
} else {
done();
cleanUpAndDone(done);
}
};
}
Expand All @@ -107,3 +112,43 @@ function patchSassUrl(url: string, _prev: string, _done: any) {

return { file: newUrl };
}

/**
* sets up an uncaughtException handler for node-sass
*/
function setUncaughtExceptionHandler() {
if (!processHandlerSet) {
processHandlerSet = true;
process.on('uncaughtException', uncaughtExceptionHandler);
}
}

/**
* unsets the uncaughtException handler for node-sass
*/
function unsetUncaughtExceptionHandler() {
if (processHandlerSet) {
processHandlerSet = false;
process.off('uncaughtException', uncaughtExceptionHandler);
}
}

/**
* The uncaughtExceptionHandler is a work around for node-sass hanging
* See: https://github.com/sass/node-sass/issues/1048
* @param e
*/
function uncaughtExceptionHandler(e: Error) {
console.error(e.stack);
process.kill(process.pid, 'SIGINT');
}

/**
* In order for us to clean up the exception handler, we attach the clean up to the "done" callback
* @param done
* @param e
*/
function cleanUpAndDone(done: (e?: Error) => void, e?: Error) {
unsetUncaughtExceptionHandler();
return done(e);
}