user20358
user20358

Reputation: 14736

gulp process terminated with code 0 when attempting to minify js files

I am new to gulp and I'm getting an error as described in the subject.

process terminated with code 0

Here is the directory structure created by npm

enter image description here

from my package.json file based on the Angular Tour of heroes tutorial and some other reference material on the net.

  {
    "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"
  }
}

This is my gulp script

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']);

In the npmSources above I am trying to include all angular 2 related library files without having to type each line in using this -> 'node_modules/@angular/**/bundles/*.umd.js',

The error message I get above leaves me no clue as to what is going wrong and where. Is there anything wrong in my gulp script? My intention is to copy to the destination path all the angular 2 related scripts minified into one file without having to type each library in. Later on I want to do the same for the css files.

This is what my node_modules folder looks like now after the package.json file gets saved in VS2015

enter image description here

As you can see there are many more folders there than what I have listed in my package.json file. I am assuming these to be required dependencies that dont need an entry in the gulp var npmSources = [...]

I am using Visual Studio 2015 and running the gulp task using its task runner. I am also using bower only because hammer.js is not available in the npm registry.

EDIT : Error code when running the gulp file.

[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.

Upvotes: 2

Views: 5020

Answers (1)

henry
henry

Reputation: 4385

As you figured out in the comments [edit: hmm... did I imagine that comment or did you delete it? [edit: aha it's back! haha]], the trouble is bringing the array npmSources into the minify-js task's 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));
});
…

That's because with (A)

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

you're saying (B)

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

when you need to say (C)

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

(I'm not actually sure that's exactly how the array is represented in A when it's swapped in for the variable. But even if, for example, it isn't read, the result is equivalent: in both A and B, gulp will throw the same "Error: Missing positive glob".)

Since in what you provided there's no reason to have a separate variable for the source files, you can save a couple bytes and provide it directly in the gulp.src. This is also a good place to start when learning 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));
});
…

Note also that the !./wwwroot/lib/hammer.js/hammer.js doesn't seem to be necessary: you're removing it from the glob, but it wasn't matched to begin with. (That gulp.src line says, "Get node_modules/@angular/**/bundles/*.umd.js, node_modules/systemjs/dist/system.src.js, and node_modules/rxjs/bundles/Rx.js, and then exclude ./wwwroot/lib/hammer.js/hammer.js.")

Upvotes: 3

Related Questions