尝试缩小js文件时,gulp进程以代码0终止

时间:2016-09-06 23:39:21

标签: visual-studio-2015 npm gulp

我是gulp的新手,我收到了主题中描述的错误。

  

进程终止于代码0

这是由npm

创建的目录结构

enter image description here

来自我的package.json文件,基于Angular Tour of heroes教程和网上的其他一些参考资料。

  {
    "version": "1.0.0",
    "name": "myapp",
    "private": true,
    "dependencies": {
    "@angular/common": "2.0.0-rc.5",
    "@angular/compiler": "2.0.0-rc.5",
    "@angular/core": "2.0.0-rc.5",
    "@angular/forms": "0.3.0",
    "@angular/http": "2.0.0-rc.5",
    "@angular/platform-browser": "2.0.0-rc.5",
    "@angular/platform-browser-dynamic": "2.0.0-rc.5",
    "@angular/router": "3.0.0-rc.1",
    "@angular/router-deprecated": "2.0.0-rc.2",
    "@angular/upgrade": "2.0.0-rc.5",
    "systemjs": "0.19.27",
    "core-js": "^2.4.0",
    "reflect-metadata": "^0.1.3",
    "rxjs": "5.0.0-beta.6",
    "zone.js": "^0.6.12",
    "angular2-in-memory-web-api": "0.0.15",
    "bootstrap": "^3.3.6" 
  },
  "devDependencies": {
    "typescript": "^1.8.10",
    "gulp": "^3.9.1",
    "path": "^0.12.7",
    "gulp-clean": "^0.3.2",
    "gulp-concat": "^2.6.0",
    "gulp-typescript": "^2.13.6",
    "typings": "^1.3.1",
    "gulp-tsc": "^1.2.0",

    "gulp-cssmin": "0.1.7",
    "gulp-uglify": "1.2.0",
    "gulp-rename": "1.2.2",
    "rimraf": "2.2.8",
    "concurrently": "^1.0.0",
    "lite-server": "^1.3.1",
    "lodash": "3.10.1"
  },
  "scripts": {
    "start": "tsc && concurrently \"npm run tsc:w\" \"npm run lite\" ",
    "lite": "lite-server",
    "postinstall": "typings install",
    "tsc": "tsc",
    "tsc:w": "tsc -w",
    "typings": "typings"
  }
}

这是我的gulp脚本

var ts = require('gulp-typescript');
var gulp = require('gulp');
var clean = require('gulp-clean');
var concat = require('gulp-concat');
var rename = require('gulp-rename');  
var uglify = require('gulp-uglify');

//take all angular related files
var npmSources = [
    'node_modules/@angular/**/bundles/*.umd.js',
    'node_modules/systemjs/dist/system.src.js',
    'node_modules/rxjs/bundles/Rx.js' 
];
var bowerSources = './wwwroot/lib/hammer.js/hammer.js';
var destPath = './wwwroot/lib/min';

// Delete the dist directory
gulp.task('clean', function () {
    return gulp.src(destPath)
        .pipe(clean());
});

// Concatenate & Minify JS
gulp.task('minify-js', function() {
    return gulp.src([npmSources,"!" + bowerSources])
        .pipe(concat('libs.js'))
        .pipe(gulp.dest(destPath))
        .pipe(rename('libs.min.js'))
        .pipe(uglify())
        .pipe(gulp.dest(destPath));
});


gulp.task('default', ['clean','minify-js']);

在上面的npmSources中,我试图包含所有与角度相关的2个库文件,而无需在使用此类型时键入每一行 - > 'node_modules/@angular/**/bundles/*.umd.js',

我上面得到的错误信息让我不知道出了什么问题以及在哪里。我的gulp脚本有什么问题吗?我的目的是将所有角度2相关的脚本复制到目标路径,而不必键入每个库。稍后我想对css文件执行相同的操作。

这是我的node_modules文件夹现在在package.json文件保存在VS2015后的样子

enter image description here

正如您所看到的,那里的文件夹比我在package.json文件中列出的文件夹多得多。我假设这些是必需的依赖项,在gulp var npmSources = [...]

中不需要输入

我正在使用Visual Studio 2015并使用其任务运行程序运行gulp任务。我也使用bower只是因为在npm注册表中没有hammer.js。

编辑:运行gulp文件时出现错误代码。

[23:06:46] Using gulpfile C:\UI\Gulpfile.js
[23:06:46] Starting 'clean'...
[23:06:46] Starting 'minify-js'...
[23:06:46] 'minify-js' errored after 1.15 ms
[23:06:46] Error: Missing positive glob
    at Object.gs.create (C:\UI\node_modules\glob-stream\index.js:73:39)
    at Gulp.src (C:\UI\node_modules\vinyl-fs\lib\src\index.js:33:23)
    at Gulp.<anonymous> (C:\UI\Gulpfile.js:38:17)
    at module.exports (C:\UI\node_modules\orchestrator\lib\runTask.js:34:7)
    at Gulp.Orchestrator._runTask (C:\UI\node_modules\orchestrator\index.js:273:3)
    at Gulp.Orchestrator._runStep (C:\UI\node_modules\orchestrator\index.js:214:10)
    at Gulp.Orchestrator.start (C:\UI\node_modules\orchestrator\index.js:134:8)
    at C:\UI\node_modules\gulp\bin\gulp.js:129:20
    at nextTickCallbackWith0Args (node.js:420:9)
    at process._tickCallback (node.js:349:13)
[23:06:47] Finished 'clean' after 40 ms
Process terminated with code 1.

1 个答案:

答案 0 :(得分:3)

正如你在评论中所发现的那样[编辑:嗯...我想象那个评论还是你删除了它? [编辑:啊哈回来了!哈哈]],麻烦是将数组npmSources带入minify-js任务gulp.src glob:

…
var npmSources = [
    'node_modules/@angular/**/bundles/*.umd.js',
    'node_modules/systemjs/dist/system.src.js',
    'node_modules/rxjs/bundles/Rx.js' 
];
var bowerSources = './wwwroot/lib/hammer.js/hammer.js';
var destPath = './wwwroot/lib/min';
…
// Concatenate & Minify JS
gulp.task('minify-js', function() {
    return gulp.src([npmSources,"!" + bowerSources])
        .pipe(concat('libs.js'))
        .pipe(gulp.dest(destPath))
        .pipe(rename('libs.min.js'))
        .pipe(uglify())
        .pipe(gulp.dest(destPath));
});
…

那是因为( A

var myGlob = ['1','2'];
gulp.task('myTask', function() {
    gulp.src([myGlob]);
});

你在说( B

gulp.task('myTask', function() {
    gulp.src([['1','2']]);
});

当你需要说( C

gulp.task('myTask', function() {
    gulp.src(['1','2']);
});

(我实际上并不确定&em; 但是,即使例如它没有被读取,结果也是等价的:在 A B 中,gulp会抛出相同的&#34;错误:缺少正面水滴&#34;。)

由于您提供的内容没有理由为源文件提供单独的变量,因此您可以保存几个字节并直接在gulp.src中提供。学习gulp时,这也是一个很好的起点:

…
var destPath = './wwwroot/lib/min';
…
// Concatenate & Minify JS
gulp.task('minify-js', function() {
    return gulp.src([
            'node_modules/@angular/**/bundles/*.umd.js',
            'node_modules/systemjs/dist/system.src.js',
            'node_modules/rxjs/bundles/Rx.js',
            '!./wwwroot/lib/hammer.js/hammer.js'
        ])
        .pipe(concat('libs.js'))
        .pipe(gulp.dest(destPath))
        .pipe(rename('libs.min.js'))
        .pipe(uglify())
        .pipe(gulp.dest(destPath));
});
…

另请注意,!./wwwroot/lib/hammer.js/hammer.js似乎没有必要:您要从全局删除它,但它并不匹配。 (gulp.src行说,&#34;获取node_modules/@angular/**/bundles/*.umd.jsnode_modules/systemjs/dist/system.src.jsnode_modules/rxjs/bundles/Rx.js,然后排除./wwwroot/lib/hammer.js/hammer.js。&#34;)