Skip to content

Commit

Permalink
feat: Clean up Module not found messages
Browse files Browse the repository at this point in the history
Closes: #306
  • Loading branch information
d4rkr00t committed Mar 3, 2018
1 parent 6fc197b commit 32b2b8e
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 17 deletions.
10 changes: 5 additions & 5 deletions src/commands/dev-server/webpack-dev-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ export function onDone(params: AikParams, compiler: any, invalidate: Function, s
const formattedErrors = formatMessages(json.errors);

if (hasErrors) {
if (formattedErrors.filter(err => err.match("Cannot resolve module")).length) {
if (formattedErrors.filter(err => err.match("Module not found")).length) {
invalidate(formattedErrors);
testUtils();
return;
Expand Down Expand Up @@ -141,12 +141,12 @@ export function onInvalidate(
//

const error = errors[0] || "";
const fileWithError = (error.match(/Error in (.+)\n/) || [])[1];
let moduleName = (error.match(/Module not found: Error: Cannot resolve module '(.+)'/) || [])[1];
const [, rawModuleName] = error.match(/Module not found: Error: Can't resolve '(.+)' in '(.+)'/) || [];

if (!moduleName) return;
if (!rawModuleName) return;

moduleName = moduleName.replace(/'/gim, "");
const moduleName = rawModuleName.replace(/'/gim, "");
const [fileWithError] = error.split("\n");

try {
server.close();
Expand Down
4 changes: 1 addition & 3 deletions src/utils/__test__/__snapshots__/error-helpers.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@ Where: ./thinking-in-react/src/components/product-table/product-table.js"
`;

exports[`#formatMessages Format Syntax Error 1`] = `
"Error:
| Syntax Error: Unexpected token (20:7)
"| Syntax Error: Unexpected token (20:7)
Where: ./thinking-in-react/src/components/product-table/product-table.js
Expand Down
6 changes: 2 additions & 4 deletions src/utils/__test__/__snapshots__/messages.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,6 @@ Some error message"
exports[`Build Messages #builderErrorMsg? Syntax Error 1`] = `
" ERROR Failed to create a production build. Reason:
Error:
| Syntax Error: Unexpected token (20:7)
Where: ./thinking-in-react/src/components/product-table/product-table.js
Expand Down Expand Up @@ -272,13 +270,13 @@ Please be patient and wait until restart completes, otherwise some changes might
exports[`Dev Server Messages #devServerInvalidBuildMsg 1`] = `" WAIT Compiling..."`;
exports[`Dev Server Messages #devServerModuleDoesntExists 1`] = `
" ERROR Module 'react' doesn't exists.
" ERROR Module doesn't exists.
Error in ./src/index.js
Webpack tried to resolve module react which doesn't exist.
It's likely caused by a typo in the module name."
Please try: npm install react"
`;
exports[`Dev Server Messages #devServerReactRequired 1`] = `
Expand Down
4 changes: 1 addition & 3 deletions src/utils/error-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,7 @@ export function formatSyntaxError(message: string): string {
);
const snippet = messageByLine.filter(isFileSnippetLine).map(removeLinePadding.bind(null, padding));

return `${chalk.red("Error:")}
${chalk.dim("|")} ${error}
return `${chalk.dim("|")} ${error}
${chalk.yellow("Where:")} ${filePath}
Expand Down
4 changes: 2 additions & 2 deletions src/utils/messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -227,13 +227,13 @@ export function devServerRestartMsg(module: string): string[] {

export function devServerModuleDoesntExists(module: string, filename: string): string[] {
return [
errorBadge() + " " + chalk.red(`Module '${module}' doesn't exists.`),
errorBadge() + " " + chalk.red(`Module doesn't exists.`),
"",
`Error in ${chalk.yellow(filename)}`,
"",
`Webpack tried to resolve module ${yellowBadge(module)} which doesn't exist.`,
"",
`It's likely caused by a ${chalk.yellow("typo")} in the module name.`
`Please try: ${chalk.green("npm install " + module.split("/")[0])}`
];
}

Expand Down

0 comments on commit 32b2b8e

Please sign in to comment.