是否可以使用 grunt-contrib-uglify 排除 javascript 的部分内容?

Is it possible to exclude parts of javascript with grunt-contrib-uglify?

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

所以看起来像:

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

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

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

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

引自该页面:

grunt-strip-code

The grunt-strip-code plugin is used to remove sections of code from production builds that are only needed in development and test environments. grunt-strip-code uses start and end comments to identify the code sections to strip out. For example:

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

doNotRemoveMe();

A use-case for this practice is to make private JavaScript functions accessible to unit tests without exposing them in production builds. This blog post goes into more detail about the concept and implementation.

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

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

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