简体   繁体   English

Webpack 在使用 npm 链接时出现 eslint 错误

[英]Webpack gives eslint errors while using npm link

I have a multi-package project set up, where I have one JavaScript package that relies on a TypeScript library.我设置了一个多包项目,其中有一个依赖于 TypeScript 库的 JavaScript 包。 Initially I installed Sinopia and was reinstalling the library every time I made changes to it.最初我安装了 Sinopia 并且每次对它进行更改时都会重新安装该库。 Then I saw npm link and thought that it would be easier for development.然后看到npm link ,觉得开发起来会更方便。 Unfortunately, when I linked the library (using npm link ../typescript-package ) and built, it gives an error:不幸的是,当我链接库(使用npm link ../typescript-package )并构建时,它给出了一个错误:

ERROR in ../typescript-package/dist/index.js
Module build failed: Error: No ESLint configuration found.

Since they are separate packages, I'm not quite sure why Webpack is trying to apply eslint to this package.由于它们是单独的包,我不太清楚为什么 Webpack 试图将 eslint 应用于这个包。 Here is my webpack.common.js file (using merge and the dev vs prod configs shouldn't matter):这是我的webpack.common.js文件(使用合并和 dev vs prod 配置应该无关紧要):

// webpack.common.js
const ExtractTextPlugin = require('extract-text-webpack-plugin');

const babelOptions = {
  presets: ['react', 'es2015', 'stage-0'],
  sourceMaps: true,
  retainLines: true,
};

module.exports = {
  entry: {
    solver: './source/index.jsx',
  },
  output: {
    path: `${__dirname}/dist`,
    filename: '[name].js',
    publicPath: '/dist/',
  },
  resolve: {
    modules: ['source', 'node_modules/'],
    extensions: ['.js', '.jsx', '/index.jsx', '.json', '.ts', '/index.ts', '.scss', '/index.scss', '.css'],
  },
  module: {
    rules: [
      {
        test: /\.jsx?$/,
        use: [
          {
            loader: 'babel-loader',
            options: babelOptions,
          },
          {
            loader: 'eslint-loader',

            options: {
              emitWarnings: true,
            },
          },
        ],
        exclude: /node_modules/,
      }, {
        test: /\.js$/,
        loader: 'source-map-loader',
        enforce: 'pre',
        exclude: /node_modules/,
      }, {
        test: /\.scss$/,
        use: ExtractTextPlugin.extract({
          fallback: 'style-loader',
          use: [{
            loader: 'css-loader',
            options: {
              minimize: true,
              localIdentName: '[local]_[hash:base64:5]',
            },
          }, {
            loader: 'sass-loader',
            options: {
              includePaths: ['source/design'],
            },
          }],
        }),
      },
    ],
  },
  plugins: [
    new ExtractTextPlugin({
      filename: '[name].css',
      allChunks: true,
    }),
  ],
  node: {
    global: true,
  },
};

I can also provide other config or package.json files if need be.如果需要,我还可以提供其他配置或 package.json 文件。

Way 1 - Webpack recommendation方式一——Webpack推荐

According to webpack doc : https://webpack.js.org/configuration/module/#rule-conditions根据 webpack 文档: https ://webpack.js.org/configuration/module/#rule-conditions

Be careful!小心! The resource is the resolved path of the file, which means symlinked resources are the real path not the symlink location.资源是文件的解析路径,这意味着符号链接资源是真实路径而不是符号链接位置。 This is good to remember when using tools that symlink packages (like npm link), common conditions like /node_modules/ may inadvertently miss symlinked files.在使用符号链接包(如 npm 链接)的工具时记住这一点很好,像 /node_modules/ 这样的常见条件可能会无意中错过符号链接文件。 Note that you can turn off symlink resolving (so that resources are resolved to the symlink path) via resolve.symlinks.请注意,您可以通过 resolve.symlinks 关闭符号链接解析(以便将资源解析为符号链接路径)。

So according to it you can disable symlink : https://webpack.js.org/configuration/resolve/#resolvesymlinks所以根据它你可以禁用符号链接: https : //webpack.js.org/configuration/resolve/#resolvesymlinks

Way 2 - Fancy hack方式 2 - 花式黑客

But maybe you need symlinks for your project.但也许您的项目需要符号链接。 So, I use my eslint rule like this :所以,我像这样使用我的 eslint 规则:

{
  test: /\.js$/,
  enforce: 'pre',
  use: 'eslint-loader',
  include: path.resolve(__dirname), // <-- This tell to eslint to look only in your project folder
  exclude: /node_modules/
}

Plus obviously your own config of this loader.加上显然你自己的这个加载器的配置。

I was dealing with this, as well.我也在处理这个问题。 I'm not exactly sure why ESLint is looking for the config file in the external package (I would expect the local rc file to be adequate) but the symlink created by npm link takes the external package out of ./node_modules/ , which otherwise would have been excluded by the loader.我不完全确定为什么 ESLint 在外部包中寻找配置文件(我希望本地 rc 文件足够)但是npm link创建的符号npm link将外部包从./node_modules/ ,否则将被装载机排除。

The fix I've come up with is to copy the package into ./node_modules/ .我想出的解决方法是将包复制到./node_modules/ It then gets filtered out through the excludes rule in your Webpack config.然后通过 Webpack 配置中的excludes规则过滤掉它。

I know this is incredibly inelegant, and shouldn't be "good enough", but I've spent some time trying to get around this issue, and this is the best I was able to come up with.我知道这非常不优雅,不应该“足够好”,但我花了一些时间试图解决这个问题,这是我能想到的最好的方法。 Until something better comes along, you can at least get moving on more pressing issues.在出现更好的事情之前,您至少可以在更紧迫的问题上取得进展。

You can also add a .eslintignore file and add the real path to the linked module您还可以添加一个.eslintignore文件并将真实路径添加到链接模块

// Content of .eslintignore
C:/path/to/your/linked/module

This is needed, because webpack resolves the module by its real path and not by the path in the node_modules folder (see webpack docs ).这是必需的,因为 webpack 通过其真实路径而不是通过node_modules文件夹中的路径解析模块(请参阅webpack 文档)。 Usually eslint ignores node_modules by default, but because of that it does not work here.通常 eslint 默认会忽略node_modules ,但正因为如此,它在这里不起作用。

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

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