-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
452 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
const RequestShortener = require("webpack/lib/RequestShortener"); | ||
const requestShortener = new RequestShortener(process.cwd()); | ||
|
||
function cleanStackTrace (message) { | ||
return message | ||
.replace(/^\s*at\s.*:\d+:\d+[\s\)]*\n/gm, ''); // at ... ...:x:y | ||
} | ||
|
||
function isBabelSyntaxError (e) { | ||
return e.name === 'ModuleBuildError' && e.message.indexOf('SyntaxError') >= 0; | ||
} | ||
|
||
function isModuleNotFoundError (e) { | ||
return e.name === 'ModuleNotFoundError' | ||
&& e.message.indexOf('Module not found') === 0 | ||
&& e.dependencies && e.dependencies.length; | ||
} | ||
|
||
function formatMessage (webpackError) { | ||
|
||
const error = extractError(webpackError); | ||
if (isBabelSyntaxError(webpackError)) { | ||
error.message = cleanStackTrace(error.message + '\n'); | ||
error.type = 'babel-syntax-error'; | ||
error.severity = 1000; | ||
} else if (isModuleNotFoundError(webpackError)) { | ||
error.message = `Module not found ${webpackError.dependencies[0].request}`; | ||
error.module = webpackError.dependencies[0].request; | ||
error.type = 'module-not-found'; | ||
error.severity = 900; | ||
} else { | ||
error.severity = 0; | ||
} | ||
|
||
return error; | ||
} | ||
|
||
function extractError (e) { | ||
return { | ||
message: e.message, | ||
file: getFile(e), | ||
origin: getOrigin(e), | ||
name: e.name | ||
}; | ||
} | ||
|
||
|
||
function getFile (e) { | ||
if (e.file) { | ||
return e.file; | ||
} else if (e.module && e.module.readableIdentifier && typeof e.module.readableIdentifier === "function") { | ||
return e.module.readableIdentifier(requestShortener); | ||
} | ||
} | ||
|
||
function getOrigin (e) { | ||
let origin = ''; | ||
if (e.dependencies && e.origin) { | ||
origin += '\n @ ' + e.origin.readableIdentifier(requestShortener); | ||
e.dependencies.forEach(function (dep) { | ||
if (!dep.loc) return; | ||
if (typeof dep.loc === "string") return; | ||
if (!dep.loc.start) return; | ||
if (!dep.loc.end) return; | ||
origin += ' ' + dep.loc.start.line + ':' + dep.loc.start.column + '-' + | ||
(dep.loc.start.line !== dep.loc.end.line ? dep.loc.end.line + ':' : '') + dep.loc.end.column; | ||
}); | ||
var current = e.origin; | ||
while (current.issuer) { | ||
current = current.issuer; | ||
origin += '\n @ ' + current.readableIdentifier(requestShortener); | ||
} | ||
} | ||
return origin; | ||
} | ||
|
||
module.exports = formatMessage; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
const path = require('path'); | ||
const chalk = require('chalk'); | ||
const os = require('os'); | ||
const formatMessage = require('./formatMessage'); | ||
const debug = require('../../utils/debug'); | ||
|
||
function safeRequire (moduleName) { | ||
try { | ||
return require(moduleName); | ||
} catch (ignored) {} | ||
} | ||
|
||
const LOGO = path.join(__dirname, 'tarec_logo_ico.png'); | ||
|
||
class NotifierPlugin { | ||
|
||
constructor ({notificationTitle, compilationSuccessMessage, showNotifications}) { | ||
this.notificationTitle = notificationTitle; | ||
this.compilationSuccessMessage = compilationSuccessMessage; | ||
this.notifier = showNotifications && safeRequire('node-notifier'); | ||
} | ||
|
||
notify (serverity, error) { | ||
this.notifier.notify({ | ||
title: this.notificationTitle, | ||
message: serverity + ': ' + error.name, | ||
subtitle: error.file || '', | ||
icon: LOGO | ||
}); | ||
} | ||
|
||
apply (compiler) { | ||
|
||
compiler.plugin('done', stats => { | ||
debug.clearConsole(); | ||
|
||
const hasErrors = stats.hasErrors(); | ||
const hasWarnings = stats.hasWarnings(); | ||
if (!hasErrors && !hasWarnings) { | ||
const time = stats.endTime - stats.startTime; | ||
debug.log(chalk.green('Compiled successfully in ' + time + 'ms')); | ||
if (this.compilationSuccessMessage) { | ||
debug.log(this.compilationSuccessMessage); | ||
} | ||
return; | ||
} | ||
|
||
if (hasErrors) { | ||
let formattedErrors = stats.compilation.errors.map(formatMessage); | ||
const nbErrors = formattedErrors.length; | ||
displayCompilationMessage(`Failed to compile with ${nbErrors} errors`, 'red'); | ||
|
||
if (this.notifier) { | ||
this.notify('Error', formattedErrors[0]); | ||
} | ||
|
||
formattedErrors = getMaxSeverityErrors(formattedErrors, 'severity'); | ||
if (formattedErrors[0].type === 'module-not-found') { | ||
console.log('These dependencies were not found in node_modules:'); | ||
console.log(); | ||
formattedErrors.forEach((error, index) => console.log('*', error.module)); | ||
console.log(); | ||
console.log('Did you forget to run npm install --save for them?') | ||
} else { | ||
formattedErrors.forEach((error, index) => displayError(index, 'Error', error)); | ||
} | ||
|
||
return; | ||
} | ||
|
||
if (hasWarnings) { | ||
const formattedWarnings = stats.compilation.warnings.map(formatMessage); | ||
const nbWarning = formattedWarnings.length; | ||
displayCompilationMessage(`Compiled with ${nbWarning} warnings`, 'yellow'); | ||
|
||
formattedWarnings.forEach((warning, index) => displayError(index, 'Warning', warning)); | ||
} | ||
}); | ||
|
||
compiler.plugin('invalid', () => { | ||
debug.clearConsole(); | ||
debug.log(chalk.cyan('Compiling...')); | ||
}); | ||
} | ||
} | ||
|
||
function getMaxSeverityErrors (errors) { | ||
const maxSeverity = getMaxInt(errors, 'severity'); | ||
return errors.filter(e => e.severity === maxSeverity); | ||
} | ||
|
||
function getMaxInt(collection, propertyName) { | ||
return collection.reduce((res, curr) => { | ||
return curr[propertyName] > res ? curr[propertyName] : res; | ||
}, 0) | ||
} | ||
|
||
module.exports = NotifierPlugin; | ||
|
||
function displayError (index, severity, error) { | ||
if (error.file) { | ||
debug.log(chalk.red((index + 1) + ') ' + severity) + ' in ' + error.file); | ||
} else { | ||
debug.log(chalk.red((index + 1) + ') ' + severity)); | ||
} | ||
debug.log(); | ||
debug.log(error.message); | ||
if (error.origin) { | ||
debug.log(error.origin); | ||
} | ||
debug.log(); | ||
} | ||
|
||
function displayCompilationMessage (message, color) { | ||
debug.log(); | ||
debug.log(chalk[color](message)); | ||
debug.log(); | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.