在这个项目中,我使用gulp-uglify版本 3.0.1,我想在输出中保留包含许可证文本的注释。
在项目页面上指出
Most of the minify options from the UglifyJS API are supported.
Run Code Online (Sandbox Code Playgroud)
在UglifyJS 自述文件中指出,为了保留许可证文本
You can pass --comments to retain certain comments in the output. By default it will keep JSDoc-style comments that contain "@preserve", "@license" or "@cc_on" (conditional compilation for IE)
Run Code Online (Sandbox Code Playgroud)
所以我尝试了:
.pipe(uglify({
mangle: true,
output: {
beautify: true,
comments: "all"
}
}))
Run Code Online (Sandbox Code Playgroud)
但由于即使指定也"all"不会产生任何许可证归属注释,我假设 minify 选项的comments行为与命令行参数不同--comments。
我也尝试了这里的preserveComments发现,但只会生成:
[13:37:42] …Run Code Online (Sandbox Code Playgroud) 我正在尝试设置grunt来缩小src目录中的一些js文件并将它们复制到构建目录中.遵循grunt任务文档,我相信以下配置应该有效.
uglify: {
dist: {
files: [
{
expand: true, // Enable dynamic expansion.
cwd: 'src/js/', // Src matches are relative to this path.
src: ['**/?.js'], // Actual pattern(s) to match.
dest: 'build/minified/', // Destination path prefix.
ext: '.min.js' // Dest filepaths will have this extension.
}
]
}
}
Run Code Online (Sandbox Code Playgroud)
当我运行咕噜声时,我得到了消息
运行"uglify:dist"(uglify)任务警告:无法编写"build/minified"文件(错误代码:EISDIR).使用--force继续.
如果我将定义切换为使用手动文件路径,它可以正常工作.文档不正确吗?或者我使用它错了?
我正在运行grunt v0.4.0rc2
如果我在下面发布的示例Gruntfile中的'js'目录下有多个子目录,并且想要将子目录保留在不同的目标目录下,我该怎么做?
例如
module.exports = function (grunt) {
grunt.initConfig({
// define source files and their destinations
uglify: {
files: {
src: 'js/**/*.js', // source files mask
dest: 'minJs/', // destination folder
expand: true, // allow dynamic building
flatten: true, // remove all unnecessary nesting
}
}
});
// load plugins
grunt.loadNpmTasks('grunt-contrib-uglify');
// register at least this one task
grunt.registerTask('default', [ 'uglify' ]);
};
Run Code Online (Sandbox Code Playgroud)
在这种情况下,我已经显示了*/ .js,但即使我明确指定了一个像js/xyz/*.js这样的子目录,那么它也不是复制目录结构,而是将它们放在minJs下的子目录中/示例中的文件夹.我在这里错过了什么?请帮忙.
谢谢,
帕迪
我需要使用Grunt/grunt-contrib-uglify缩小一组JSON文件(语言文件):
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
uglify: {
build: {
src: 'src/main/app/resources/locales/*/*.json',
dest: 'target/resources/locales/*/*.json'
}
}
});
Run Code Online (Sandbox Code Playgroud)
我得到的是:
Files: src/main/app/resources/locales/en/messages.json -> target/*/*.json
Minifying with UglifyJS...Reading src/main/app/resources/locales/en/messages.json...OK
>> Uglifying source "src/main/app/resources/locales/en/messages.json" failed.
Warning: Uglification failed. Use --force to continue.
Run Code Online (Sandbox Code Playgroud)
我开始怀疑,uglify是否可用于纯JSON文件,还是仅用于JavaScript源文件?JSON文件确实很好,它以非最小化的形式在应用程序中运行.
将这个简单的gulp示例用于uglification:
gulp.task('scripts', function() {
// Minify and copy all JavaScript (except vendor scripts)
return gulp.src(paths.scripts)
.pipe(uglify())
.pipe(concat('all.min.js'))
.pipe(gulp.dest('build/js'));
});
Run Code Online (Sandbox Code Playgroud)
如果您有两个文件:
档案f1.js:
function f1(){
var hello = 1;
return hello;
}
Run Code Online (Sandbox Code Playgroud)
档案f2.js:
function f2(){
return f1();
}
Run Code Online (Sandbox Code Playgroud)
任务的结果all.min.js是:
function f1(){var n=1;return n}
function f2(){return f1()}
Run Code Online (Sandbox Code Playgroud)
我怎样才能丑化到裂伤这些顶级函数名,即f1和f2?我试过了:
额外的丑化
return gulp.src(paths.scripts)
.pipe(uglify())
.pipe(concat('all.min.js'))
.pipe(gulp.dest('build/js'))
.pipe(uglify());
Run Code Online (Sandbox Code Playgroud)
通过mangle选项
return gulp.src(paths.scripts)
.pipe(uglify({mangle: true}))
.pipe(concat('all.min.js'))
.pipe(gulp.dest('build/js'));
Run Code Online (Sandbox Code Playgroud)
通过顶级选项
return gulp.src(paths.scripts)
.pipe(uglify({toplevel: true}))
.pipe(concat('all.min.js'))
.pipe(gulp.dest('build/js'));
Run Code Online (Sandbox Code Playgroud)
但一切都没有效果.
UglifyJS使用逗号来链接函数,对象和变量声明.这对于制作很好,当文件被缩小时,但是在调试js时,使用断点遍历javascript非常困难.我需要知道如何在UglifyJS Grunt插件中关闭此功能.
下面是输出的样子.
var boom = function(a) {
...
},
bing = function(b){
...
},
bam = function(c) {
...
};
Run Code Online (Sandbox Code Playgroud) 我使用uglifyJS缩小了我的Javascript和CSS文件,磁盘上的文件大小已经大大减少了.但是在检查网络标签中加载的资源的总时间时,使用Chrome开发者工具,我发现没有区别.缩小真的会提高性能吗?如果是,我该如何衡量它?

在检查页面的源代码时,我看到了JS代码,如下所示:
eval((function(D28){for(var J28="",p28=0,C28=function(D28,y28){for(var L28=0,E28=0;E28=32&&t28<=127){L28+=t28-32;}}return L28;};p28((0x50,48.)>(54.0E1,99.)?2:1.3860E3<=(27.,0xA9)?(1.95E2,0x229):13.98E2<(126,0x240)?2:(0x1B2,0x19F)>(0x9F,105)?(140.,0):(104.,0xF3)<=99?\"W\"` Q 6E,19))){I=X.substring(v,Q);c=I.length;return Y(I,c,x);}else if(v===((13.25E2,77.)>=(0x157,146.)?(26.,70.9E1):(0x213,10.59E2)<` F 6,9.02E2)?(0x5,\"r\"):(52,18.3E1)>(1.137E3,82.)?(0.,null):(25.8E1,19.5E1)>=(0x1E5,0x183)?(9.4E1,\'r\'):(11.84E2,7` N )<0x196?0:(84,82.))||v<=(7.92E2>=(0xCD,1.062E3)?8.73E2:119.>=(14.9E1,12)?(135,0):(0x55,100.)>(55.,102.)?(0x255,46.` > 253,31)>=(0xD4,4.10E1)?0x1D4:(0xB8,140.)>9.68E2?(1.24E3,0xA2):(131.,0x115))){I=X.substring(((0x171,81.2E1)>65.10E1?(4.11E2,0):(146,0xA6)<(143.,99.)?0x241:(0x247,0x1D6)>=(0x222,10.96E2)?4.86E2:(1.0E1,0x130)<0xB6?(0x1C1,127.10E1):(0x164,2.23E2)<=29?(52.80E1,136.):(14.46E2,11.03E2)),X.length);c=I` %\";return Y(I,c,x);}I=X.substring(` L#-v` 8B,d=function(v,x){var Q=x&((0x146,71.7E1)<
Run Code Online (Sandbox Code Playgroud)
http://jsfiddle.net/3jxzpxgv/show/
你们有什么想法使用哪种工具?我不认为这是UglifyJS,Closure或YUI.
我目前正在使用以下webpack.config.js:
var webpack = require('webpack');
module.exports = {
entry: __dirname + "/src/index.js",
output: {
path: __dirname,
filename: "index.js"
},
module: {
loaders: [
{
test: /\.js$/,
loader: 'babel',
exclude: '/node_modules/',
query: {
presets: ['latest']
}
}
]
},
plugins: [ new webpack.optimize.UglifyJsPlugin({minimize: true}) ]
}
Run Code Online (Sandbox Code Playgroud)
这完全符合我的要求.但是现在我想在输出文件中添加一些带有项目信息的注释,并在一行中添加了uglified代码.我该怎么做呢?
这个答案就像以前的魅力一样:
但是,由于Webpack v4它不再起作用.从那以后它抛出:
错误:webpack.optimize.UglifyJsPlugin已被删除,请改用config.optimization.minimize.
为了使它在Webpack v4中运行,有什么必要?
我没有运气就试过使用以下内容:
const uglifyJsPlugin = require('uglifyjs-webpack-plugin');
if (process.argv.indexOf('-p') !== -1) {
// compress and remove console statements. Only add this plugin in production
// as even if drop_console is set to false, other options may be set to true
config.plugins.push(new uglifyJsPlugin({
compress: {
'drop_console': true
}
}));
}
Run Code Online (Sandbox Code Playgroud) uglifyjs ×10
javascript ×6
gruntjs ×4
webpack ×3
gulp ×2
minify ×2
babeljs ×1
compression ×1
gulp-uglify ×1
node.js ×1
uglifyjs2 ×1