2017-06-01 201 views
0

我使用[email protected]webpack2:配置模块规则

我的配置是非常简单的:

{ 
    resolve: { 
    modules: [ 
     SRC_ROOT, 
     "node_modules", 
    ], 
    }, 

    entry: "index.js", 

    output: { 
    path: sysPath.join(APP_ROOT, "dist"), 
    filename: "index.js", 
    }, 

    module: { 
    rules: [ 
     { 
     test: /\.js$/i, 
     include: SRC_ROOT, 
     use: [ 
      { 
      loader: "eslint-loader", 
      enforce: "pre", 
      options: { 
       failOnError: true, 
       failOnWarning: true, 
      }, 
      }, 

      { 
      loader: "babel-loader", 
      } 
     ], 
     } 
    ] 
    }, 
} 

当我运行它,我得到了以下内容:

Invalid configuration object. Webpack has been initialised using a configuration object that does not match the API schema. 
- configuration.module.rules[0].use should be one of these: 
    non-empty string | function | object { loader?, options?, query? } | function | [non-empty string | function | object { loader?, options?, query? }] 
    Details: 
    * configuration.module.rules[0].use should be a string. 
    * configuration.module.rules[0].use should be an instance of function. 
    * configuration.module.rules[0].use should be an object. 
    * configuration.module.rules[0].use should be one of these: 
     non-empty string | function | object { loader?, options?, query? } 
    * configuration.module.rules[0].use should be an instance of function. 
    * configuration.module.rules[0].use[0] should be a string. 
    * configuration.module.rules[0].use[0] should be an instance of function. 
    * configuration.module.rules[0].use[0] has an unknown property 'enforce'. These properties are valid: 
     object { loader?, options?, query? } 
    * configuration.module.rules[0].use[0] should be one of these: 
     non-empty string | function | object { loader?, options?, query? } 

我很困惑,因为这些错误与我在文档中看到的完全相反:https://webpack.js.org/configuration/#options。不支持“使用”的数组? “未知财产'执行'”?当doc显示完全相同的用法示例时,甚至有可能?

请解释我在这里做错了什么。您rules

module: { 
    rules: [ 
    { 
     test: /\.js$/i, 
     include: SRC_ROOT, 
     loader: "eslint-loader", 
     enforce: "pre", 
     options: { 
     failOnError: true, 
     failOnWarning: true, 
     } 
    }, 
    { 
     test: /\.js$/i, 
     include: SRC_ROOT, 
     loader: "babel-loader", 
    }  
    ] 
} 

+0

你可以显示你的完整配置,以及你如何使用它? –

+0

@NitsanBaleli它是一个完整的配置。然后我运行“node_modules/.bin/webpack”。 –

回答

1

的问题是,enforce是不是在装载机有效的属性。它必须在规则上使用,因此它在文档中为Rule.enforce。该错误消息的下列行告诉你,这是不是有效的属性:

* configuration.module.rules[0].use[0] has an unknown property 'enforce'. These properties are valid: 
    object { loader?, options?, query? } 

它不显示use是一个有效的财产,因为内部useloadersloader作为别名对待。

您必须在整个规则上放置enforce

{ 
    test: /\.js$/i, 
    enforce: "pre", 
    include: SRC_ROOT, 
    use: [ 
    { 
     loader: "eslint-loader", 
     options: { 
     failOnError: true, 
     failOnWarning: true, 
     }, 
    }, 
    { 
     loader: "babel-loader", 
    } 
    ], 
} 

但你真的不希望babel-loader是一个预加载器,这意味着你需要创建两个单独的规则。

rules: [ 
    { 
    test: /\.js$/i, 
    enforce: "pre", 
    include: SRC_ROOT, 
    use: [ 
     { 
     loader: "eslint-loader", 
     options: { 
      failOnError: true, 
      failOnWarning: true, 
     }, 
     }, 
    ] 
    }, 
    { 
    test: /\.js$/i, 
    include: SRC_ROOT, 
    use: [ 
     { 
     loader: "babel-loader" 
     } 
    ], 
    } 
] 

我故意保留规则的显式版本。你确实可以使用一些快捷方式,其中之一就是如果你只使用一个加载器,你可以直接在规则上指定options。这些选项将被转发到加载程序,这可能是您认为enforce在加载程序上有效的原因。

+0

感谢您提供详细的回复。我错过了Rules和UseEntries之间的区别。 只是好奇 - 使用嵌套规则来实现相同的结果是正确的吗?像这样: 规则:[ { test:..., rules:[{loader:“...”,enforce:“pre”},{loader:“...”}] }] –

+0

我个人不会这样做,因为您只保存了几个字符,但它通过嵌套规则增加了复杂性。我宁愿将所有规则放在顶层,所以很容易理解它们,尤其是孤立的。 –

+0

但从技术上讲,它会工作。那么,在我看来,把所有处理相同文件类型的东西分组,并不是那么糟糕的做法。但我同意,对于复杂的配置,特别是当它被分割成多个文件,然后以某种方式深度合并时,它会使事情变得复杂。 –

0

使用两个不同的对象这是一个有点多余,但它应该工作

+0

谢谢,它确实有效。 尽管如此,“这怎么可能”仍然存在。 –

+0

是啊,它看起来很奇怪,但文档首先显示一个字符串,然后显示一个对象。我知道这只是你可以使用的一个例子,但你为什么不尝试把'babel-loader'作为一个字符串呢?就像示例 – Ematipico

+0

将加载器定义为只是一个字符串不是问题。我也试过了。 Webpack抱怨说它不知道“强制”选项(它存在于doc中)并且“use”选项的值不能是数组(也在doc中)。即,在进入babel-loader配置之前它失败了。 –