Gulp并没有结合或丑化

时间:2017-08-09 08:41:12

标签: javascript gulp

我似乎已将Gulp配置为在Netbeans中运行。我的目标是让gulp简单地将一个目录中的所有Javascript文件合并到一个文件中,然后" uglify"生成的文件。

当我保存我的一个Javascript文件时,会创建一个新的Javascript文件,但是,它不会与任何其他文件合并,也不会被uglified。我没有收到任何错误消息,但我也没有得到任何预期的结果。我哪里错了?

这是我的gulpfile.js:

var gulp = require('gulp'),
    gp_concat = require('gulp-concat'),
    gp_rename = require('gulp-rename'),
    gp_uglify = require('gulp-uglify');

gulp.task('combine', function(){
    return gulp.src(['*.js'])
        .pipe(gp_concat('concat.js'))
        .pipe(gulp.dest('dist'))
        .pipe(gp_rename('uglify.js'))
        .pipe(gp_uglify())
        .pipe(gulp.dest('dist'));
});

gulp.task('default', ['combine'], function(){});

这是我的package.json:

{
    "name": "xxxxxxxxxxxxxxxxxx",
    "version": "1.0.0",
    "keywords": ["util", "functional", "server", "client", "browser"],
    "author": "xxxxxxxxxxxxxxxxxx",
    "contributors": [],
    "dependencies": {},
    "scripts" :
    {
        "watch" : "watchify ../../html/xxxxxxxxxxxxxxxxxx/js/*.js -o ../../html/xxxxxxxxxxxxxxxxxx/bundle.js -v"
    }
}

请注意,这些是我在Stack Overflow和网络其他地方的示例中复制的文件,所以我确定我错过了一些东西。

最新更新:

我现在正在使用此修订后的Gulp文件,目录路径已更正:

var gulp = require('gulp'),
    expect = require('gulp-expect-file'),
    concat = require('gulp-concat'),
    minify = require('gulp-minify'),
    gutil = require('gulp-util'),
    babel = require('gulp-babel'),
    jsFiles = '../../html/public_html/js/*.js',
    jsDest = '../../public_html/dist';

gulp.task('combine', function ()
{
    return gulp.src(jsFiles)
        .pipe(expect(jsFiles))
        .pipe(babel({presets: ['babili']}))
        .pipe(concat('concat.js'))
        .pipe(minify().on('error', gutil.log))
        .pipe(gulp.dest(jsDest));
});

当我运行它时,我仍然无法在dist/文件夹中获得任何结果文件。

我现在收到此错误输出:

[11:53:13] Using gulpfile ~\Documents\NetBeansProjects\Project_Name\gulpfile.js
[11:53:13] Starting 'combine'...
[11:53:13] Tested 5 tests, 5 passes, 0 failures: PASS
Done.

events.js:160
      throw er; // Unhandled 'error' event
      ^
SyntaxError: ..\..\html\html\public_html\js\*.js
Unexpected token: operator (>) (line: 30, col: 34, pos: 95518
    at JS_Parse_Error.get (eval at <anonymous> (C:\Users\Project Username\Documents\NetBeansProjects\Project_Name\node_modules\uglify-js\tools\node.js:27:1), <anonymous>:86:23)
    at PluginError.<anonymous> (C:\Users\Project Username\Documents\NetBeansProjects\Project_Name\node_modules\gulp-util\lib\PluginError.js:37:60)
    at Array.forEach (native)
    at new PluginError (C:\Users\Project Username\Documents\NetBeansProjects\Project_Name\node_modules\gulp-util\lib\PluginError.js:36:16)
    at Transform.minify [as _transform] (C:\Users\Project Username\Documents\NetBeansProjects\Project_Name\node_modules\gulp-minify\index.js:117:23)
    at Transform._read (C:\Users\Project Username\Documents\NetBeansProjects\Project_Name\node_modules\through2\node_modules\readable-stream\lib\_stream_transform.js:184:10)
    at Transform._write (C:\Users\Project Username\Documents\NetBeansProjects\Project_Name\node_modules\through2\node_modules\readable-stream\lib\_stream_transform.js:172:12)
    at doWrite (C:\Users\Project Username\Documents\NetBeansProjects\Project_Name\node_modules\through2\node_modules\readable-stream\lib\_stream_writable.js:237:10)
    at writeOrBuffer (C:\Users\Project Username\Documents\NetBeansProjects\Project_Name\node_modules\through2\node_modules\readable-stream\lib\_stream_writable.js:227:5)
    at Transform.Writable.write (C:\Users\Project Username\Documents\NetBeansProjects\Project_Name\node_modules\through2\node_modules\readable-stream\lib\_stream_writable.js:194:11)

1 个答案:

答案 0 :(得分:1)

试试这个

var gulp = require('gulp'),
    expect = require('gulp-expect-file'),
    concat = require('gulp-concat'),
    minify = require('gulp-minify');

gulp.task('combine', function() {
    var files = ['*.js'];
    gulp.src(files)
        .pipe(expect(files))
        .pipe(concat('concat.js'))
        .pipe(minify())
        .pipe(gulp.dest('dist/'));
});

```

您还应该将以下行"gulp-minify": "0.0.14""gulp-expect-file": "0.0.7"添加到devDependencies的{​​{1}}

我不确定,但似乎模式package.json(任何文件夹)在gulp中不起作用。因此,您的模式**将仅在根文件夹中搜索扩展名为*.js的文件。