-
-
Notifications
You must be signed in to change notification settings - Fork 198
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature #371 Add formatter and transformer for module not found error…
… (BackEndTea) This PR was squashed before being merged into the master branch (closes #371). Discussion ---------- Add formatter and transformer for module not found error Fixes #351 I don't work with js much, so any feedback is appreciated. Commits ------- f496cbf Fix copy paste errors and use template literals eb4dee5 Add formatter and transformer for module not found error
- Loading branch information
Showing
6 changed files
with
194 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/* | ||
* This file is part of the Symfony Webpack Encore package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
'use strict'; | ||
|
||
const chalk = require('chalk'); | ||
|
||
function formatErrors(errors) { | ||
if (errors.length === 0) { | ||
return []; | ||
} | ||
|
||
let messages = []; | ||
|
||
messages.push( | ||
chalk.red('Module build failed: Module not found:') | ||
); | ||
for (let error of errors) { | ||
messages.push(`${error.file} contains a reference to the file ${error.ref}.`); | ||
messages.push('This file can not be found, please check it for typos or update it if the file got moved.'); | ||
messages.push(''); | ||
} | ||
|
||
return messages; | ||
} | ||
|
||
function format(errors) { | ||
return formatErrors(errors.filter((e) => ( | ||
e.type === 'missing-css-file' | ||
))); | ||
} | ||
|
||
module.exports = format; |
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,47 @@ | ||
/* | ||
* This file is part of the Symfony Webpack Encore package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
'use strict'; | ||
|
||
const TYPE = 'missing-css-file'; | ||
|
||
function isMissingConfigError(e) { | ||
if (e.name !== 'ModuleNotFoundError') { | ||
return false; | ||
} | ||
|
||
if (e.message.indexOf('Module not found: Error: Can\'t resolve') === -1) { | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
function getReference(error) { | ||
const index = error.message.indexOf('Can\'t resolve \'') + 15; | ||
const endIndex = error.message.indexOf('\' in \''); | ||
|
||
return error.message.substring(index, endIndex); | ||
} | ||
|
||
function transform(error) { | ||
if (!isMissingConfigError(error)) { | ||
return error; | ||
} | ||
|
||
error = Object.assign({}, error); | ||
|
||
error.type = TYPE; | ||
error.ref = getReference(error); | ||
error.severity = 900; | ||
|
||
return error; | ||
} | ||
|
||
module.exports = transform; |
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,48 @@ | ||
/* | ||
* This file is part of the Symfony Webpack Encore package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
'use strict'; | ||
|
||
const expect = require('chai').expect; | ||
const formatter = require('../../../lib/friendly-errors/formatters/missing-css-file'); | ||
|
||
describe('formatters/missing-css-file', () => { | ||
|
||
describe('test format()', () => { | ||
it('works with no errors', () => { | ||
const actualErrors = formatter([]); | ||
expect(actualErrors).to.be.empty; | ||
}); | ||
|
||
it(' filters errors that dont have the correct type', () => { | ||
const errors = [ | ||
{ type: 'missing-css-file', file: 'some-file.sass', ref: '../images/foo.png' }, | ||
{ type: 'other-type', file: 'other-type.sass' } | ||
]; | ||
|
||
const actualErrors = formatter(errors); | ||
expect(JSON.stringify(actualErrors)).to.contain('some-file.sass'); | ||
expect(JSON.stringify(actualErrors)).to.not.contain('other-type.sass'); | ||
}); | ||
|
||
it('formats the error correctly', () => { | ||
const error = { | ||
type: 'missing-css-file', | ||
file: '/some/file.css', | ||
ref: '../images/foo.png' | ||
}; | ||
|
||
const actualErrors = formatter([error]); | ||
expect(JSON.stringify(actualErrors)).to.contain('/some/file.css contains a reference to the file ../images/foo.png'); | ||
expect(JSON.stringify(actualErrors)).to.contain('This file can not be found, please check it for typos or update it if the file got moved.'); | ||
// all needed packages will be present when running tests | ||
expect(JSON.stringify(actualErrors)).to.not.contain('yarn add'); | ||
}); | ||
}); | ||
}); |
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,53 @@ | ||
/* | ||
* This file is part of the Symfony Webpack Encore package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
'use strict'; | ||
|
||
const expect = require('chai').expect; | ||
const transform = require('../../../lib/friendly-errors/transformers/missing-css-file'); | ||
|
||
describe('transform/missing-css-file', () => { | ||
|
||
describe('test transform', () => { | ||
it('Error not with "ModuleNotFoundError" name is ignored', () => { | ||
const startError = { | ||
name: 'OtherParseError', | ||
message: 'You may need an appropriate loader', | ||
file: '/path/to/file.sass' | ||
}; | ||
const actualError = transform(Object.assign({}, startError)); | ||
|
||
expect(actualError).to.deep.equal(startError); | ||
}); | ||
|
||
it('Error not containing "Module not found: Error: Can\'t resolve" is ignored', () => { | ||
const startError = { | ||
name: 'ModuleNotFoundError', | ||
message: 'Some other message', | ||
file: '/path/to/file.sass' | ||
}; | ||
const actualError = transform(Object.assign({}, startError)); | ||
|
||
expect(actualError).to.deep.equal(startError); | ||
}); | ||
|
||
it('Matching error is properly transformed', () => { | ||
const startError = { | ||
name: 'ModuleNotFoundError', | ||
message: 'Module build failed: ModuleNotFoundError: Module not found: Error: Can\'t resolve \'./../images/symfony_logo.png2\' in \'/Users/weaverryan/Sites/os/webpack-encore/tmp_project_playing/css\'', | ||
file: '/Users/weaverryan/Sites/os/webpack-encore/tmp_project_playing/css' | ||
}; | ||
const actualError = transform(Object.assign({}, startError)); | ||
|
||
expect(actualError.ref).to.deep.equal('./../images/symfony_logo.png2'); | ||
expect(actualError.type).to.deep.equal('missing-css-file'); | ||
expect(actualError.file).to.deep.equal('/Users/weaverryan/Sites/os/webpack-encore/tmp_project_playing/css'); | ||
}); | ||
}); | ||
}); |
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