2017-08-19 35 views
1

我使用BabelWebpackES6生成ES5代碼。有一些驗證用於減少我在編碼時犯的錯誤。刪除生產分發文件中的一些代碼行?

class Logger { 
    /** 
    * @param {LogModel} info 
    * {LogTypes} type 
    * {String} message 
    * {Date} date 
    */ 
    static log(info) { 
     if(info instanceof LogModel) 
      throw new Error("not a instance of LogModel"); 

     notify(info); 
    } 
} 

log功能,我驗證參數是否爲LogModel類的實例。這只是爲了防止錯誤。我不希望如果條件要在生產中,因爲太多的條件會減慢應用程序。是否有可能通過驗證和生產版本生成開發版本,但沒有使用BabelWebpack進行驗證?

回答

1

您可以使用assert包來執行您的代碼,然後使用webpack-unassert-loaderwebpack-strip-assert去除生產代碼的聲明。

var assert = require('assert').ok; 

class Logger { 
    /** 
    * @param {LogModel} info 
    * {LogTypes} type 
    * {String} message 
    * {Date} date 
    */ 
    static log(info) { 
     assert(info instanceof LogModel, "Param must be an instance of LogModel"); 
     notify(info); 
    } 
} 
0

的清潔器的選擇將是使用define-plugin從的WebPack。

在配置文件:

new webpack.DefinePlugin({ __DEV: JSON.stringify(true) })

app.js:的__DEV

if(__DEV){ console.log("logged only in dev env") }

值將由的WebPack在編譯時被提供。

相關問題