Skip to content

Commit 30dc220

Browse files
committed
minor #1284 Improve ESLint and Babel help messages, when enabling ESLint integration (Kocal)
This PR was merged into the main branch. Discussion ---------- Improve ESLint and Babel help messages, when enabling ESLint integration While working on ESLint deprecation (requiring me to have a valid setup, etc...), those messages are displayed when you try to enable ESLint integration: ``` ➜ sf-webpack-encore-lab git:(main) ✗ pnpm dev > @ dev /Users/kocal/workspace/sf-webpack-encore-lab > encore dev Running webpack ... DEPRECATION The ESLint integration is now deprecated and will be removed in the next major release. Please use the standalone ESLint CLI instead, or use the ESLint Webpack plugin directly. Error: No ESLint configuration has been found. FIX Run command ./node_modules/.bin/eslint --init or manually create a .eslintrc.js file at the root of your project. If you prefer to create a .eslintrc.js file by yourself, here is an example to get you started: // .eslintrc.js module.exports = { parser: '`@babel`/eslint-parser', extends: ['eslint:recommended'], } Install `@babel`/eslint-parser to prevent potential parsing issues: yarn add `@babel`/eslint-parser --dev You will also need to specify your Babel config in a separate file. The current configuration Encore has been adding for your is: // babel.config.js module.exports = { "sourceType": "unambiguous", "presets": [ [ "/Users/kocal/workspace/sf-webpack-encore-lab/node_modules/.pnpm/`@babel`+preset-env@7.25.3_@babel+core@7.25.2/node_mo dules/`@babel`/preset-env/lib/index.js", { "modules": false, "targets": {}, "useBuiltIns": false, "corejs": null } ] ], "plugins": [] } Additionally, remove the .configureBabel() in webpack.config.js: this will no longer be used. And remove the .configureBabelPresetEnv() in webpack.config.js: this will no longer be used. ``` There are many errors in those messages: 1. The option `root: true` is missing for ESLint 2. I expect to see `require.resolve("`@babel`/preset-env")` instead of `"/Users/kocal/workspace/sf-webpack-encore-lab/node_modules/.pnpm/`@babel`+preset-env@7.25.3_@babel+core@7.25.2/node_mo dules/`@babel`/preset-env/lib/index.js"` for Babel. 3. I expect the `corejs` option to be set to the current version of `core-js` (if exists) With this PR, the output is now: ``` > encore dev Running webpack ... DEPRECATION The ESLint integration is now deprecated and will be removed in the next major release. Please use the standalone ESLint CLI instead, or use the ESLint Webpack plugin directly. Error: No ESLint configuration has been found. FIX Run command ./node_modules/.bin/eslint --init or manually create a .eslintrc.js file at the root of your project. If you prefer to create a .eslintrc.js file by yourself, here is an example to get you started: // .eslintrc.js module.exports = { root: true, parser: '`@babel`/eslint-parser', extends: ['eslint:recommended'], } Install `@babel`/eslint-parser to prevent potential parsing issues: yarn add `@babel`/eslint-parser --dev You will also need to specify your Babel config in a separate file. The current configuration Encore has been adding for your is: // babel.config.js module.exports = { "sourceType": "unambiguous", "presets": [ [ require.resolve("`@babel`/preset-env"), { "modules": false, "useBuiltIns": false, "corejs": "3.38.1" } ] ], "plugins": [] } ``` Commits ------- e6b4648 Improve ESLint and Babel help messages, when enabling ESLint integration
2 parents cbce157 + e6b4648 commit 30dc220

File tree

1 file changed

+26
-1
lines changed

1 file changed

+26
-1
lines changed

lib/plugins/eslint.js

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ If you prefer to create a ${chalk.yellow('.eslintrc.js')} file by yourself, here
4040
4141
${chalk.yellow(`// .eslintrc.js
4242
module.exports = {
43+
root: true,
4344
parser: '@babel/eslint-parser',
4445
extends: ['eslint:recommended'],
4546
}
@@ -51,13 +52,37 @@ Install ${chalk.yellow('@babel/eslint-parser')} to prevent potential parsing iss
5152
const babelConfig = babelLoaderUtil.getLoaders(webpackConfig)[0].options;
5253
// cacheDirectory is a custom loader option, not a Babel option
5354
delete babelConfig['cacheDirectory'];
55+
56+
(babelConfig['presets'] || []).forEach(preset => {
57+
if (!Array.isArray(preset)) {
58+
return;
59+
}
60+
61+
if (typeof preset[0] === 'string' && preset[0].includes('@babel') && preset[0].includes('preset-env')) {
62+
// The goal is to replace the preset string with a require.resolve() call, executed at runtime.
63+
// Since we output the Babel config as JSON, we need to replace it with a placeholder.
64+
preset[0] = '__PATH_TO_BABEL_PRESET_ENV__';
65+
66+
// The option "targets" is not necessary
67+
delete preset[1]['targets'];
68+
69+
// If needed, specify the core-js version to use
70+
if (preset[1]['corejs'] === null) {
71+
preset[1]['corejs'] = packageHelper.getPackageVersion('core-js');
72+
}
73+
}
74+
});
75+
5476
message += `
5577
5678
You will also need to specify your Babel config in a separate file. The current
5779
configuration Encore has been adding for your is:
5880
5981
${chalk.yellow(`// babel.config.js
60-
module.exports = ${JSON.stringify(babelConfig, null, 4)}
82+
module.exports = ${
83+
JSON.stringify(babelConfig, null, 4)
84+
.replace('"__PATH_TO_BABEL_PRESET_ENV__"', 'require.resolve("@babel/preset-env")')
85+
}
6186
`)}`;
6287

6388
if (webpackConfig.babelConfigurationCallback) {

0 commit comments

Comments
 (0)