删除生产分发文件中的一些代码行? [英] Remove some code lines in production distribution files?

查看:65
本文介绍了删除生产分发文件中的一些代码行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用BabelWebpackES6生成ES5代码.有一些验证可以减少我在编码时所犯的错误.

I'm using Babel and Webpack to generate ES5 code from ES6. There are some validations that is used to reduce the mistakes i do while coding.

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类的实例.这只是为了防止错误.我不希望if条件在生产中,因为if条件太多会使应用程序变慢.

In log function, I validate whether the argument is a instance of LogModel class. This is just to prevent mistakes. I don't want that if condition to be in the production because too many if condition going to slow the application. Is it possible to generate development release with validations and production release without those validations with Babel and Webpack?

推荐答案

您可以使用声明包以强制执行您的代码,然后使用 webpack-unassert-loader webpack-strip-assert 剥离生产代码的断言.

You can use the assert package to enforce your code, then use webpack-unassert-loader or webpack-strip-assert to strip your assertions for production code.

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);
    }
}

这篇关于删除生产分发文件中的一些代码行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆