GulpUglifyError:无法缩小JavaScript引起:SyntaxError:意外的令牌名称«k»,预期的punc«;»

时间:2018-04-28 07:12:55

标签: angularjs gulp

我收到一些奇怪的错误,但为了抑制这些错误,我必须更改我收到错误的所有内容?

//gulp file code
var fs = require('fs');
var path = require('path');`enter code here`
var merge = require('merge-stream');
var gulp = require('gulp');
var concat = require('gulp-concat');
var rename = require('gulp-rename');
var uglify = require('gulp-uglify');
var sourcemaps = require('gulp-sourcemaps');
var gutil = require('gulp-util');



gulp.task('css',function()
{

return gulp.src(['assets/css/*.css'])
    .pipe(sourcemaps.init())

    .pipe(sourcemaps.write())
    .pipe(uglify())
    .on('error', function (err) { gutil.log(gutil.colors.red('[Error]'), err.toString()); })
    .pipe(concat('all.css'))
    .pipe(gulp.dest('build/css'))

})
gulp.task('js',function()
{

return gulp.src(['shared/*.js','controller/*.js','directives/*.js','services/*.js'])
    .pipe(sourcemaps.init())

    .pipe(sourcemaps.write())
    .pipe(uglify())
    .on('error', function (err) { gutil.log(gutil.colors.red('[Error]'), err.toString()); })
    .pipe(concat('all.js'))
    .pipe(gulp.dest('build/js'))

})

gulp.task('default', ['css','js']);

我试图在线找到解决方案,但没有运气

for (let k = 0; k < $scope.allCollctedTags.length; k++) 
{
 $scope.customtags.push($scope.allCollctedTags[k].text);
}

但如果我用var替换let,那么现在删除此错误,下一个错误就出现了以下错误:

$scope.multipleImages.push(...response.data.result.imageName);
but if i write it as below then this error disappear
$scope.multipleImages.push(response.data.result.imageName);

also if i use underscore js then also it give me error 
let temparray = _.difference($scope.multipleImages,$scope.extractedImagesBrowse);

现在该做什么,我可以在一个或两个文件中做这些事情但是对于整个项目我不能这样做。

2 个答案:

答案 0 :(得分:4)

Following this answer, what I found was that I tried to compile code that was not parsable with the ugilfy version that I had. Try using ugifly-es instead of pure uglify package:

const uglify = require("gulp-uglify");

To:

const uglify = require("gulp-uglify-es");

And make sure you have the package installed.

Also, if you don't want to use unsupported package, take a look at this answer as it covers your issue:

答案 1 :(得分:1)

如果您使用ES6语法,或者使用过滤器将代码解析为ES6(例如 val snackbar = Snackbar.make(view, string, Snackbar.LENGTH_LONG) val layoutParams = LayoutParams(snackbar.view.layoutParams) layoutParams.gravity = Gravity.TOP snackbar.view.setPadding(0, 10, 0, 0) snackbar.view.layoutParams = layoutParams snackbar.animationMode = BaseTransientBottomBar.ANIMATION_MODE_FADE snackbar.show() 的最新版本),则gulp-coffee不支持。

您可以将gulp-uglify替换为支持ES6的gulp-uglify

https://www.npmjs.com/package/terser