Closed boatcoder closed 4 years ago
TL;DR
Try this:
rules: [
// Disable require.ensure as it's not a standard language feature.
{ parser: { requireEnsure: false } },
// First, run the linter.
// It's important to do this before Babel processes the JS.
{
test: /\.(js|mjs|jsx|ts|tsx)$/,
enforce: 'pre',
use: [
{
options: {
cache: true,
formatter: require.resolve('react-dev-utils/eslintFormatter'),
eslintPath: require.resolve('eslint'),
resolvePluginsRelativeTo: __dirname,
},
loader: require.resolve('eslint-loader'),
},
// <------------- AND THE STORY STARTS HERE ---------------
{
loader: 'webpack-preprocessor-loader',
options: {
debug: process.env.NODE_ENV === 'production',
directives: {
secret: false,
},
params: {
ENV: process.env.NODE_ENV,
},
verbose: false,
},
},
],
include: paths.appSrc,
},
Actually oneof
is an Nested Rules
option from Webpack
, so more details see https://webpack.js.org/configuration/module/#ruleoneof
And if you find the documentation of Webpack
is still a bit hard to understand, here are some key points for you:
oneof
is some what under the Nested Rules
category, which means, everything you see under the rules
(those config objects particularly), is also valid under oneof
;config objects
directly under the rules
will apply to all included files, and that is why the linter part is written like that;config objects
, the loader
part, is a shortcut to the use
. That is said, if you want to apply multiple loaders, you should use use
instead of loader
;
// Instead of this...
{
test: /\.(js|mjs|jsx|ts|tsx)$/,
include: paths.appSrc,
loader: 'babel-loader',
options: {},
}
// replace it like this... { test: /.(js|mjs|jsx|ts|tsx)$/, include: paths.appSrc, use: [ { loader: 'babel-loader', options: {...}, }, { loader: 'another-loader', options: {...}, }, ], }
- ...And then comes our comfort zone.
So the last question is, where should we put the preprocessor config. The solution I provided works, but it does its job during the lint phase, right before the linter starting its job. It is to prevent linter complaining about the potential issues and ensuring the linter sees its file after the preprocessing. But it is a bit weird to do this in the linter config.
A better solution is like this:
```javascript
// Process application JS with Babel.
// The preset includes JSX, Flow, TypeScript, and some ESnext features.
const a = {
test: /\.(js|mjs|jsx|ts|tsx)$/,
include: paths.appSrc,
use: [
{
loader: require.resolve('babel-loader'),
options: {
customize: require.resolve('babel-preset-react-app/webpack-overrides'),
plugins: [
[
require.resolve('babel-plugin-named-asset-import'),
{
loaderMap: {
svg: {
ReactComponent: '@svgr/webpack?-svgo,+titleProp,+ref![path]',
},
},
},
],
],
// This is a feature of `babel-loader` for webpack (not Babel itself).
// It enables caching results in ./node_modules/.cache/babel-loader/
// directory for faster rebuilds.
cacheDirectory: true,
// See #6846 for context on why cacheCompression is disabled
cacheCompression: false,
compact: isEnvProduction,
},
},
// <------------- THIS TIME STARTS HERE
{
loader: 'webpack-preprocessor-loader',
options: {
debug: process.env.NODE_ENV === 'production',
directives: {
secret: false,
},
params: {
ENV: process.env.NODE_ENV,
},
verbose: false,
},
},
],
};
... only if your linter satisfied with your pre-preprocessed code.
@boatcoder comment updated.
CRA gives you a webpack config that uses
oneOf:
for the loaders, how do I use this preprocessor with that???Wondering if I should deal with it like the eslint pre-processor....