简体   繁体   中英

Webpack eslint-loader issues when resolving paths

Here's my webpack config:

var path = require('path');
var webpack = require('webpack');
var ExtractTextPlugin = require('extract-text-webpack-plugin');
var autoprefixer = require('autoprefixer');

module.exports = {
  entry: [
    'webpack-dev-server/client?http://localhost:3000',
    'webpack/hot/only-dev-server',
    './playground/reactLib/playground.jsx'
  ],
  output: {
    path: path.join(__dirname, 'dist'),
    filename: 'bundle.js',
    publicPath: '/static/'
  },

  eslint: {
    configFile: 'lint/.eslintrc.js'
  },
  resolve: {
    root: path.resolve(__dirname),
    alias: {
      'button': 'aframe/components/buttons/Button.jsx',
    }
  },
  module: {
    preLoaders: [
      {
        test: /\.jsx?$/,
        loader: 'eslint-loader',
        include: [path.join(__dirname, 'playground'), path.join(__dirname, 'aframe')],
        exclude: /node_modules/
      }
    ],
    loaders: [
      { test: /\.less$/, loader: "style!css!less",include: __dirname },
      { test: /\.css$/, loader: "style!css" },
      {
        test: /.jsx?$/,
        loaders: ['react-hot'],
        include: __dirname,
        exclude: /node_modules/
      },
      {
        test: /.jsx?$/,
        loader: 'babel-loader',
        include: __dirname,
        exclude: /node_modules/,
        query: {
          plugins: ['transform-object-rest-spread'],
          presets: ['es2015', 'react']
        }
      },
      {
        test: /\.(eot|svg|ttf|woff|woff2)$/,
        loader: 'file?name=static/fonts/[name].[ext]'
      },
      {
        test: /\.png$/,
        loader: 'file?name=static/[name].[ext]'
      }
    ]
  }
};

When I run webpack, I get this error:

Unable to resolve path to module 'button'

When I run this without the eslint preloader, it works fine. Seems like there is an issue with the eslint loader and resolving paths using resolve. Is there a way I can get around this problem?

Ok, so the answer was to use eslint-import-resolver-webpack module and to add this to my .eslintrc.

module.exports = {
   ...
   // These settings are needed for eslint to play well with webpack resolve
   "settings": {
    "import/parser": "babel-eslint",
    "import/resolver": {
      "webpack": { "config": "webpack.config.js" }
    }
  },
  ...
};

However, beware of the following limitations: https://libraries.io/npm/eslint-import-resolver-webpack

However, Webpack allows a number of things in import module source strings that Node does not, such as loaders (import 'file!./whatever') and a number of aliasing schemes, such as externals: mapping a module id to a global name at runtime (allowing some modules to be included more traditionally via script tags).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM