简体   繁体   English

为什么gulp-uglify不会破坏我的变量名称?

[英]Why isn't gulp-uglify mangling my variables names?

I have the following task: 我有以下任务:

var uglify = require('gulp-uglify');

gulp.task('scripts', function () {
  gulp.src('./src/scripts/*.js')
    .pipe(concat('main.js'))
    .pipe(uglify())
    .pipe(gulp.dest('./dist'));
});

And the following 2 javascript files, test1.js : 以下2个javascript文件,test1.js:

var testOneOutput = 'function one';
console.log(testOneOutput);

And test2.js 和test2.js

var testTwoOutput = 'function two';
console.log(testTwoOutput);

And all the directories are setup correctly. 并且所有目录都已正确设置。 Though when I run the task, I have no indication of whether the uglifying work. 虽然当我执行任务时,我没有迹象表明是否有丑陋的工作。 The concatenation works great though. 连接工作很好。 Am I missing something? 我错过了什么吗?

In your scripts testOneOutput and testTwoOutput are global variables and by default gulp-uglify only mangles local vars. 在您的脚本中, testOneOutputtestTwoOutput是全局变量,默认情况下, testTwoOutput gulp-uglify仅会破坏本地变量。

Put your code in a function and you'll see some mangling. 把你的代码放在一个函数中,你会看到一些错误。

function go() {
  var testOneOutput = 'function one';
  console.log(testOneOutput);
}

如果你想要破坏你的顶级函数,你可以给{mangle: {toplevel: true}}选项。

uglify({mangle: {toplevel: true}})

Perform the uglify first. 首先执行uglify Then concat the uglified files. 然后concat uglified文件。

gulp.task('scripts', function () {
  gulp.src('./src/scripts/*.js')
    .pipe(uglify())
    .pipe(concat('main.js'))
    .pipe(gulp.dest('./dist'));
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM