Skip to content

Commit e3ed9fc

Browse files
committed
Improve ESLint and Babel help messages, when enabling ESLint integration
1 parent cbce157 commit e3ed9fc

File tree

1 file changed

+27
-1
lines changed

1 file changed

+27
-1
lines changed

lib/plugins/eslint.js

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ const WebpackConfig = require('../WebpackConfig'); //eslint-disable-line no-unus
1414
const applyOptionsCallback = require('../utils/apply-options-callback');
1515
const pluginFeatures = require('../features');
1616
const babelLoaderUtil = require('../loaders/babel');
17+
const packageHelper = require('../package-helper');
1718

1819
/**
1920
* Support for ESLint.
@@ -40,6 +41,7 @@ If you prefer to create a ${chalk.yellow('.eslintrc.js')} file by yourself, here
4041
4142
${chalk.yellow(`// .eslintrc.js
4243
module.exports = {
44+
root: true,
4345
parser: '@babel/eslint-parser',
4446
extends: ['eslint:recommended'],
4547
}
@@ -51,13 +53,37 @@ Install ${chalk.yellow('@babel/eslint-parser')} to prevent potential parsing iss
5153
const babelConfig = babelLoaderUtil.getLoaders(webpackConfig)[0].options;
5254
// cacheDirectory is a custom loader option, not a Babel option
5355
delete babelConfig['cacheDirectory'];
56+
57+
(babelConfig['presets'] || []).forEach(preset => {
58+
if (!Array.isArray(preset)) {
59+
return;
60+
}
61+
62+
if (typeof preset[0] === 'string' && preset[0].includes('@babel') && preset[0].includes('preset-env')) {
63+
// The goal is to replace the preset string with a require.resolve() call, executed at runtime.
64+
// Since we output the Babel config as JSON, we need to replace it with a placeholder.
65+
preset[0] = '__PATH_TO_BABEL_PRESET_ENV__';
66+
67+
// The option "targets" is not necessary
68+
delete preset[1]['targets'];
69+
70+
// If needed, specify the core-js version to use
71+
if (preset[1]['corejs'] === null) {
72+
preset[1]['corejs'] = packageHelper.getPackageVersion('core-js');
73+
}
74+
}
75+
})
76+
5477
message += `
5578
5679
You will also need to specify your Babel config in a separate file. The current
5780
configuration Encore has been adding for your is:
5881
5982
${chalk.yellow(`// babel.config.js
60-
module.exports = ${JSON.stringify(babelConfig, null, 4)}
83+
module.exports = ${
84+
JSON.stringify(babelConfig, null, 4)
85+
.replace('"__PATH_TO_BABEL_PRESET_ENV__"', 'require.resolve("@babel/preset-env")')
86+
}
6187
`)}`;
6288

6389
if (webpackConfig.babelConfigurationCallback) {

0 commit comments

Comments
 (0)