gulp js concat问题

时间:2015-06-07 07:44:19

标签: gulp gulp-concat gulp-uglify

/*global -$ */
'use strict';
var gulp = require('gulp');
var $ = require('gulp-load-plugins')();
var jshint = require('gulp-jshint');
var concat = require('gulp-concat');
var rename = require('gulp-rename');
var uglify = require('gulp-uglify');
var csso = require('gulp-csso');
var argv = require('yargs').argv;
var gulpif = require('gulp-if');
var inquirer = require('inquirer');
var minifyHTML = require('gulp-minify-html');
var browserSync = require('browser-sync');
var del = require('del');
var reload = browserSync.reload;

// variables
var production = !!(argv.production);  
var dev = !!(argv.dev);  
var move = !!(argv.move); 
var app = 'app';
var dist = 'dist';
var src = {
  scss : app+'/style.scss',  
  scripts:{
  modernizr:'bower_components/modernizr/modernizr.js',
  vendor:['bower_components/jquery/dist/jquery.js',
  'bower_components/bootstrap-sass-official/assets/javascripts/bootstrap.js',
  ],
  main:app+'/scripts/main.js'
  }
};

// Vendor js
gulp.task('vendorScripts', function(){
  return gulp.src(src.scripts.vendor)
    .pipe(concat('vendor.js'))    
    .pipe(gulpif(dev,gulp.dest(app+'/js/vendor/')))
    .pipe(gulpif(production,gulp.dest(app+'/js/vendor/')))
    .pipe(rename('vendor.min.js'))
    .pipe(uglify())
    .pipe(gulpif(dev,gulp.dest(app+'/js/vendor/')))
    .pipe(gulpif(production,gulp.dest(dist+'/js/vendor/')));
});

当我运行gulp vendorScripts --production时 只编译vendor.min.js 但是当我运行gulp vendorScripts --dev 编译vendor.js和vendor.min.js

我想在dist文件夹中编译这两个文件。 发生了什么问题?

1 个答案:

答案 0 :(得分:0)

似乎你想为生产编写dist,但你写了app。更改第二行使用dist而不是app。

.pipe(gulpif(dev,gulp.dest(app+'/js/vendor/')))
.pipe(gulpif(production,gulp.dest(app+'/js/vendor/')))

下面是正确的

.pipe(gulpif(dev,gulp.dest(app+'/js/vendor/')))
.pipe(gulpif(production,gulp.dest(dist+'/js/vendor/')));

只是一个建议:而不是使用gulp-if,应用js if-else逻辑来获取输出目录名称并在任务中使用它...