【问题标题】:Is it possible to exclude parts of javascript with grunt-contrib-uglify?是否可以使用 grunt-contrib-uglify 排除部分 javascript?
【发布时间】:2017-09-08 12:16:33
【问题描述】:

我在我的 js 构建过程中使用 grunt-contrib-uglify,并且非常希望能够从最终构建中排除某些代码部分。

所以看起来像:

/*BuildExcludeBegin*/
Run this code during development, but don't include it in the minified.js
/*BuildExcludeEnd*/

有谁知道 grunt-contrib-uglify 是否可行?

【问题讨论】:

  • 您对此有什么解决方案吗?我只是想要同样的东西。

标签: javascript gruntjs grunt-contrib-uglify


【解决方案1】:

grunt-contrib-uglify 不提供。但是你可以使用: https://www.npmjs.com/package/grunt-strip-code

来自该页面的引用:

grunt-strip-code

grunt-strip-code 插件用于从生产构建中删除仅在开发和测试环境中需要的代码部分。 grunt-strip-code 使用 start 和 end cmets 来识别要删除的代码段。例如:

/* test-code */
removeMeInProduction();
/* end-test-code */

doNotRemoveMe();

这种做法的一个用例是让单元测试可以访问私有 JavaScript 函数,而不会在生产构建中公开它们。这篇博文更详细地介绍了概念和实现。

【讨论】:

    【解决方案2】:

    我认为没有允许您排除代码片段的功能。您可以将代码拆分为可以单独排除的单独文件吗?而且mangle或mangle.properties不适合你吗?

    【讨论】:

      【解决方案3】:

      可以用grunt-contrib-uglify完成,现在叫做条件编译...

      if (DEBUG) {
          console.log("debug stuff");
      }
      

      https://github.com/mishoo/UglifyJS2#conditional-compilation

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-11-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多