gullowinclude with gullow uglify dons'不起作用

gulp-include with gulp-uglify doesn't work

本文关键字:不起作用 dons with gullow uglify gullowinclude      更新时间:2023-09-26

我有一个使用gulp-uglifygulp.task 'scripts'。然后我还添加了gulp-import,但它不起作用。如果我只做uglify,它会起作用。如果我只做import,它会起作用。但如果我同时做这两件事,那就行不通了。以下是功能:

gulp.task('scripts', function(){
    gulp.src([
            'testgulp/**/*.js', '!testgulp/**/_*.js', '!testgulp/**/*.min.js'
        ])
        .pipe(include()) //if I comment only this line, uglify works
            .on('error', console.log)
        .pipe(uglify()) //if I comment only this line, include works
        .pipe(rename({ suffix: '.min' }))
        .pipe(gulp.dest(function(file) { return file.base; }));
});

然后在终端上,当我尝试同时做这两件事时,我有这样的反应:

/Applications/MAMP/htdocs/git/personal-sandrina-p/_main/v3/node_modules/map-stream/index.js:103
        throw err
        ^
GulpUglifyError: unable to minify JavaScript
    at createError (/Applications/MAMP/htdocs/git/personal-sandrina-p/_main/v3/node_modules/gulp-uglify/lib/create-error.js:6:14)
    at wrapper (/Applications/MAMP/htdocs/git/personal-sandrina-p/_main/v3/node_modules/gulp-uglify/node_modules/lodash/_createHybrid.js:87:15)
    at trycatch (/Applications/MAMP/htdocs/git/personal-sandrina-p/_main/v3/node_modules/gulp-uglify/minifier.js:26:12)
    at DestroyableTransform.minify [as _transform] (/Applications/MAMP/htdocs/git/personal-sandrina-p/_main/v3/node_modules/gulp-uglify/minifier.js:76:19)
    at DestroyableTransform.Transform._read (/Applications/MAMP/htdocs/git/personal-sandrina-p/_main/v3/node_modules/through2/node_modules/readable-stream/lib/_stream_transform.js:159:10)
    at DestroyableTransform.Transform._write (/Applications/MAMP/htdocs/git/personal-sandrina-p/_main/v3/node_modules/through2/node_modules/readable-stream/lib/_stream_transform.js:147:83)
    at doWrite (/Applications/MAMP/htdocs/git/personal-sandrina-p/_main/v3/node_modules/through2/node_modules/readable-stream/lib/_stream_writable.js:313:64)
    at writeOrBuffer (/Applications/MAMP/htdocs/git/personal-sandrina-p/_main/v3/node_modules/through2/node_modules/readable-stream/lib/_stream_writable.js:302:5)
    at DestroyableTransform.Writable.write (/Applications/MAMP/htdocs/git/personal-sandrina-p/_main/v3/node_modules/through2/node_modules/readable-stream/lib/_stream_writable.js:241:11)
    at Stream.ondata (stream.js:31:26)

我要离开考拉应用程序,附加/预端功能非常棒,所以我想对Gulp也这样做。我尝试了包含和导入,两者都有相同的问题。我做错了什么?谢谢:(

-编辑-我还尝试过对minimyit任务进行gull-sync和硬编码timeout((,但没有解决问题。。。

好吧,我想我找到问题了。首先,我需要安装gulp-util来更好地找出错误所在。

gulp.task('scripts', function(){
    gulp.src([
            'assets/scripts/**/*.js',
            '!assets/scripts/**/_*.js',
            '!assets/scripts/**/*.min.js'
        ])
        .pipe(include())
            .on('error', console.log)
        .pipe(uglify().on('error', gutil.log)) //gulp-util
        .pipe(rename({ suffix: '.min' }))
        .pipe(gulp.dest(function(file) { return file.base; }));
});

然后是ES5的简写丑陋造成了一个错误。我还必须安装gulp-babel,最后的代码是:

gulp.task('scripts', function(){
    gulp.src([
            'assets/scripts/**/*.js',
            '!assets/scripts/**/_*.js',
            '!assets/scripts/**/*.min.js'
        ])
        .pipe(include())
            .on('error', console.log)
        .pipe(babel({
            presets: ['es2015'], //this fixed a shorthand javascript error
            compact: true
        }))
        .pipe(uglify().on('error', gutil.log)) // gulp-util
        .pipe(rename({ suffix: '.min' }))
});

这解决了我的问题。希望能帮助其他人:(

您应该使用泵模块。

   var pump = require('pump');
   gulp.task('scripts', function(){
       pump([
          gulp.src([...]),
          ugilfy(),
          rename({ suffix: '.min' }),
          gulp.dest(...)
       ]);
   });

您可以看到为什么使用泵

只需将它们作为单独的函数,并在uglify函数之前运行include函数:

 gulp.task('include,function({
       // include code here
 });
 gulp.task('uglify',['include'],function({
          // uglify code here
 });

当您运行uglify函数

时,[]之间的include函数将在uglify功能之前运行