Skip to content

Commit 18c1594

Browse files
committed
WIP - prettier errors for missing loader
1 parent 33c6fbd commit 18c1594

File tree

5 files changed

+118
-0
lines changed

5 files changed

+118
-0
lines changed

lib/config-generator.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ const packageHelper = require('./package-helper');
66
const CleanWebpackPlugin = require('clean-webpack-plugin');
77
const WebpackChunkHash = require('webpack-chunk-hash');
88
const FriendlyErrorsWebpackPlugin = require('friendly-errors-webpack-plugin');
9+
const missingLoaderTransformer = require('./friendly-errors/transformers/missing-loader');
10+
const missingLoaderFormatter = require('./friendly-errors/formatters/missing-loader');
911

1012
class ConfigGenerator
1113
{
@@ -375,6 +377,12 @@ class ConfigGenerator
375377

376378
plugins.push(new FriendlyErrorsWebpackPlugin({
377379
clearConsole: true,
380+
additionalTransformers: [
381+
missingLoaderTransformer
382+
],
383+
additionalFormatters: [
384+
missingLoaderFormatter
385+
]
378386
}));
379387

380388
return plugins;
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
const chalk = require('chalk');
2+
3+
function formatErrors(errors) {
4+
if (errors.length === 0) {
5+
return [];
6+
}
7+
8+
var messages = [];
9+
for (let error of errors) {
10+
let neededCode = `Encore.${error.featureMethod}`;
11+
const fixes = [];
12+
fixes.push(`Add ${chalk.green(neededCode)} to your webpack.config.js file.`);
13+
14+
// TODO - I need some key that describes the "feature"
15+
// then, I need to be able to look up that dependency to
16+
// see what packages (if any) are missing
17+
// maybe even put the featureMethod into that file
18+
19+
messages = messages.concat([
20+
chalk.red(`Error loading ${chalk.yellow(error.file)}`),
21+
'',
22+
`${chalk.bgGreen.black('', 'FIX', '')} To load ${error.loaderFileDescription}:`,
23+
]);
24+
25+
let index = 0;
26+
for (let fix of fixes) {
27+
messages.push(` ${++index}. ${fix}`)
28+
}
29+
30+
messages.push('');
31+
}
32+
return messages;
33+
34+
var msg = chalk.bgGreen.black('', 'WOH', '');
35+
36+
return [
37+
msg,
38+
'Hi',
39+
'Hello'
40+
];
41+
}
42+
43+
function format(errors) {
44+
return formatErrors(errors.filter((e) => (
45+
e.type === 'loader-not-enabled'
46+
)));
47+
}
48+
49+
module.exports = format;
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
const TYPE = 'loader-not-enabled';
2+
3+
function isMissingLoaderError(e) {
4+
if (e.name !== 'ModuleParseError') {
5+
return false;
6+
}
7+
8+
if (e.message.indexOf('You may need an appropriate loader') === -1) {
9+
return false;
10+
}
11+
12+
return true;
13+
}
14+
15+
function getFileExtension(filename) {
16+
const str = filename.replace(/\?.*/, '');
17+
const split = str.split('.');
18+
19+
return split.pop();
20+
}
21+
22+
function transform(error) {
23+
if (!isMissingLoaderError(error)) {
24+
return error;
25+
}
26+
27+
const extension = getFileExtension(error.file);
28+
let message;
29+
switch (extension) {
30+
case 'sass':
31+
case 'scss':
32+
error.featureMethod = 'enableSassLoader()';
33+
error.loaderFileDescription = 'SASS files';
34+
break;
35+
case 'less':
36+
error.featureMethod = 'enableLessLoader()';
37+
error.loaderFileDescription = 'LESS files';
38+
break;
39+
// todo - handle more
40+
default:
41+
return error;
42+
}
43+
return Object.assign({}, error, {
44+
type: TYPE,
45+
severity: 900,
46+
name: 'Loader not enabled'
47+
});
48+
}
49+
50+
module.exports = transform;

lib/package-helper.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,16 @@ function ensurePackagesExist(packageNames, error) {
1212
if (missingPackageName.length > 0) {
1313
throw new Error(error);
1414
}
15+
16+
let message = `You must install the ${missingPackageNames.join(' and ')} package${missingPackageNames.length > 1 ? 's' : ''} to use ${requestedFeature}`;
17+
let yarnInstalls = missingPackageNames.map((packageName) => {
18+
return ` yarn add ${packageName} --dev`;
19+
});
20+
21+
throw new Error(`
22+
${message}
23+
${yarnInstalls.join("\n")}
24+
`);
1525
}
1626

1727
module.exports = {

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
"babel-core": "^6.24.0",
2727
"babel-loader": "^7.0.0-beta",
2828
"babel-preset-env": "^1.2.2",
29+
"chalk": "^1.1.3",
2930
"clean-webpack-plugin": "^0.1.16",
3031
"css-loader": "^0.26.2",
3132
"extract-text-webpack-plugin": "^2.1.0",

0 commit comments

Comments
 (0)