2017-06-01 203 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配置之前它失敗了。 –