2017-08-09 149 views
1

我似乎已经配置咕嘟咕嘟在NetBeans中运行。我的目标是将一个目录中的所有Javascript文件合并到一个文件中,然后“uglify”该文件。咕嘟咕嘟不合并或丑化

当我救我的JavaScript文件中的一个,一个新的JavaScript文件被创建,但它不与任何其他文件合并,也不是变丑。我没有收到任何错误消息,但我也没有收到任何期望的结果。我哪里错了?

这里是我的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" 
    } 
} 

注意,这些都是我从这里堆栈溢出和其他地方的例子复制的文件网络,所以我敢肯定我错过了一些东西。


最新:

我现在用的这个修订咕嘟咕嘟文件,用目录路径更正:

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) 
+0

可你只是试试这个'gulp.src(” ./ JS/*。JS ')。管(CONCAT(' concat.js'))...'其余部分代码已经是了。也一饮而尽任务应该有回报,即只'一饮而尽SRC(...)前加上'return' ...' –

+0

@RajeshDan,谢谢您的建议。我按照你的建议修改了代码,但不幸的是,我仍然没有得到任何组合文件。 – Questioner

+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"添加到您的package.jsondevDependencies

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

+0

非常感谢您的回复。我正在使用你的代码,当我运行'combine'函数时,我没有遇到任何错误。但是,没有合并文件出现在'dist /'目录中。我用你的代码和结果输出更新了我的问题。 – Questioner

+0

@Questioner最可能你的模式'* .js'不匹配任何文件。在这种情况下,目标中不会生成文件。 –

+0

感谢您的跟进。当你说'* .js'与任何文件都不匹配时,你的意思是说它在Gulp中使用通配符是无效的吗?或者你的意思是说目录可能没有被发现?如果是后者,我不应该得到某种错误信息吗? – Questioner