简体   繁体   English

解决路径时Webpack eslint-loader问题

[英]Webpack eslint-loader issues when resolving paths

Here's my webpack config: 这是我的webpack配置:

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: 当我运行webpack时,我收到此错误:

Unable to resolve path to module 'button' 无法解析模块“按钮”的路径

When I run this without the eslint preloader, it works fine. 当我在没有eslint预加载器的情况下运行它时,它工作正常。 Seems like there is an issue with the eslint loader and resolving paths using resolve. 似乎eslint加载器存在问题并使用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. 好的,所以答案是使用eslint-import-resolver-webpack模块并将其添加到我的.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 但是,请注意以下限制: 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). 但是,Webpack允许Node中没有的导入模块源字符串中的许多内容,例如loaders(import'file!./ whatever')和一些别名方案,例如externals:将模块id映射到全局名称在运行时(允许一些模块更传统地通过脚本标签包含在内)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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