Gulp和index.html问题

时间:2017-02-28 09:56:29

标签: javascript angularjs gulp

我有4个html文件。

的index.html,

menu.html,

dishdetail.html,

contactus.html,

我正在使用gulp进行测试和实时预览。但是,index.html不会在dist文件夹中连接。所有其他文件夹(如图像,样式和脚本)都是连接在一起的。所以这就是我设置gulp的方式。

var gulp = require('gulp'),
minifycss = require('gulp-minify-css'),
jshint = require('gulp-jshint'),
stylish = require('jshint-stylish'),
uglify = require('gulp-uglify'),
usemin = require('gulp-usemin'),
imagemin = require('gulp-imagemin'),
rename = require('gulp-rename'),
concat = require('gulp-concat'),
notify = require('gulp-notify'),
cache = require('gulp-cache'),
changed = require('gulp-changed'),
rev = require('gulp-rev'),
ngannotate = require('gulp-ng-annotate'),
browserSync = require('browser-sync'),
del = require('del');

 gulp.task('jshint', function() {
 return gulp.src('app/scripts/**/*.js')
.pipe(jshint())
.pipe(jshint.reporter(stylish));
});

 // Clean
   gulp.task('clean', function() {
   return del(['dist']);
 });

 //Default task
 gulp.task('default', ['clean'], function() {
 gulp.start('usemin', 'imagemin','copyfonts');
 });

gulp.task('usemin',['jshint'], function () {
return gulp.src('app/*.html')
  .pipe(usemin({
    css:[minifycss(),rev()],
    js: [ngannotate(),uglify(),rev()]
  }))
  .pipe(gulp.dest('dist/'));
});

// Images
gulp.task('imagemin', function() {
 return del(['dist/images']), gulp.src('app/images/**/*')
   .pipe(cache(imagemin({ optimizationLevel: 3, progressive: true, 
   interlaced:    
   true })))
  .pipe(gulp.dest('dist/images'))
  .pipe(notify({ message: 'Images task complete' }));
});

gulp.task('copyfonts', ['clean'], function() {
gulp.src('./bower_components/font-awesome/fonts/**/*.{ttf,woff,eof,svg}*')
.pipe(gulp.dest('./dist/fonts'));
gulp.src('./bower_components/bootstrap/dist/fonts/**/*.{ttf,woff,eof,svg}*')
.pipe(gulp.dest('./dist/fonts'));
});

// Watch
gulp.task('watch', ['browser-sync'], function() {
// Watch .js files
gulp.watch('{app/scripts/**/*.js,app/styles/**/*.css,app/**/*.html}',    
['usemin']);
  // Watch image files
gulp.watch('app/images/**/*', ['imagemin']);

});

gulp.task('browser-sync', ['default'], function () {
var files = [
  'app/**/*.html',
  'app/styles/**/*.css',
  'app/images/**/*.png',
  'app/scripts/**/*.js',
  'dist/**/*'
 ];

 browserSync.init(files, {
  server: {
    baseDir: 'dist',
    index: 'index.html'
  },
 reloadDelay: 1000
});
    // Watch any files in dist/, reload on change
 gulp.watch(['dist/**']).on('change', browserSync.reload);
});

当我输入cmd gulp 时,我收到此错误。

events.js:160
  throw er; // Unhandled 'error' event
  ^

Error: write after end
at writeAfterEnd (C:\Users\Theodosios\Desktop\AngularJs\Week3\03_SPAs\node_m
odules\gulp-minify-css\node_modules\readable-  
stream\lib\_stream_writable.js:229:
12)
at Transform.Writable.write (C:\Users\Theodosios\Desktop\AngularJs\Week3\03_
SPAs\node_modules\gulp-minify-css\node_modules\readable-  
stream\lib\_stream_writa
ble.js:272:20)

为什么会发生这种情况?

所有模块都是通过npm进行的。它们显示在这里

的package.json

{
"name": "conFusion",
"private": true,
"devDependencies": {
  "browser-sync": "^2.18.8",
  "del": "^2.2.2",
  "gulp": "^3.9.1",
  "gulp-cache": "^0.4.5",
  "gulp-changed": "^2.0.0",
  "gulp-concat": "^2.6.1",
  "gulp-imagemin": "^3.1.1",
  "gulp-jshint": "^2.0.4",
  "gulp-minify-css": "^1.2.4",
  "gulp-ng-annotate": "^2.0.0",
  "gulp-notify": "^3.0.0",
  "gulp-rename": "^1.2.2",
  "gulp-rev": "^7.1.2",
  "gulp-uglify": "^2.0.1",
  "gulp-usemin": "^0.3.28",
  "jshint": "^2.9.4",
  "jshint-stylish": "^2.2.1"
},
"engines": {
  "node": ">=0.10.0"
}
}

谢谢,

西奥

2 个答案:

答案 0 :(得分:1)

使用gulp-useref插件

npm install --save-dev gulp-useref

更新gulpfile.js,如下所示

var useref = require('gulp-useref');

gulp.task('usemin',['jshint'], function () {
  return gulp.src('./app/*.html')
  .pipe(useref()) 
  .pipe(usemin({
    css:[minifycss(),rev()],
    js: [ngannotate(),uglify(),rev()]
  }))
  .pipe(gulp.dest('dist/'));
});

答案 1 :(得分:0)

有几个可能的原因:

首先,gulp-minify-css已被弃用,他们建议您使用gulp-clean-css,因此您应该尝试更新它。

其次,gulp-usemin是为节点>=4.0设计的,而您的engines.node设置为>=0.10.0