【问题标题】:gulp process terminated with code 0 when attempting to minify js files尝试缩小 js 文件时,gulp 进程以代码 0 终止
【发布时间】:2016-09-08 04:32:39
【问题描述】:

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

进程以代码 0 终止

这里是npm创建的目录结构

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

  {
    "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 中,我试图包含所有 angular 2 相关的库文件,而不必在使用它时键入每一行 -> 'node_modules/@angular/**/bundles/*.umd.js',

上面的错误消息让我不知道哪里出了问题。我的 gulp 脚本有什么问题吗?我的意图是将所有 angular 2 相关脚本缩小到一个文件中,而无需输入每个库。稍后我想对 css 文件执行相同的操作。

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

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

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

EDIT:运行 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.

【问题讨论】:

  • 你试过减少 gulpfile 吗?例如如果gulp.task('default',function(){console.log("no code 0 plz")}); 会发生什么,那么保持default 不变并将cleanminify-js 更改为只记录消息
  • 给出了这个 >>>> [22:47:20] 开始'默认'...没有代码 0 plz [22:47:20] 182 μs 后完成'默认'进程终止于代码 0.
  • 很好,所以我们知道 gulp 正在工作。听起来你还没有尝试调试这个?接下来我会尝试gulp clean,看看是否可行,然后gulp minify-js,看看是否可行
  • 使用干净的 minify-js 我得到了这个 >>>> [22:48:48] Starting 'clean'... [22:48:48] Starting 'minify-js'... [22:49:02] Finished 'clean' after 15 s [22:49:05] Finished 'minify-js' after 18 s [22:49:05] Starting 'default'... [22:49:05] Finished 'default' after 33 μs Process terminated with code 0.
  • clean 似乎在清理文件夹时工作

标签: visual-studio-2015 npm gulp


【解决方案1】:

正如您在 cmets 中发现的那样 [编辑:嗯……我想象到了该评论还是您删除了它? [编辑:啊哈它回来了! haha]],问题是将数组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']);
});

(我实际上不确定 究竟 数组在换入变量时在 A 中的表示方式。但即使,例如,它是'不读,结果是等价的:在AB中,gulp都会抛出同样的“Error: Missing positive glob”。)

由于在您提供的内容中没有理由为源文件设置单独的变量,您可以保存几个字节并直接在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 行说,“获取node_modules/@angular/**/bundles/*.umd.jsnode_modules/systemjs/dist/system.src.js,和node_modules/rxjs/bundles/Rx.js,然后排除./wwwroot/lib/hammer.js/hammer.js。")

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-12
    • 2011-12-04
    • 1970-01-01
    • 2022-07-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多