简体   繁体   English

gulp watch .js文件不起作用

[英]gulp watch .js file doesn't work

gulpfile.js gulpfile.js

'use strict';

var gulp = require('gulp');
var sass = require('gulp-sass');
var browserSync = require('browser-sync').create();
var reload      = browserSync.reload;

gulp.task('serve')

// Static Server + watching scss/html files
gulp.task('serve', function() {

    browserSync.init({
        server: "./public"
    });

    gulp.watch('./public/**/*.html').on('change', reload); // worked
    gulp.watch('./public/**/*.js').on('change', reload); // doesn't work
});

gulp.task('sass', function () {
  return gulp.src('./public/sass/**/*.scss')
    .pipe(sass().on('error', sass.logError))
    .pipe(gulp.dest('./public/css'))
    .pipe(reload({stream: true}));
});

gulp.task('sass:watch', function () {
  gulp.watch('./public/sass/**/*.scss', ['sass'])
});

gulp.task('default', ['serve','sass', 'sass:watch']);

With above gulp setup, my broswersnyc works fine when I make changes to my html and sass files but it doesn't work for javascript files. 使用上面的gulp设置,当我对我的html和sass文件进行更改时,我的broswersnyc工作正常,但它不适用于javascript文件。

This is my folder structure: 这是我的文件夹结构:

Does it have to do with AngularJS? 它与AngularJS有关吗? Because my app.js (which is within my public folder) consists of AngularJS controllers. 因为我的app.js (在我的public文件夹中)由AngularJS控制器组成。

If you move your app.js to "public/js" folder it will work fine 如果您将app.js移动到“public / js”文件夹,它将正常工作

gulp.watch('./public/**/*.js').on('change', reload); // doesn't work

because in the pervious snippet you're watching the js files in the child folders of the public dir. 因为在以前的片段中,您正在观看公共目录的子文件夹中的js文件。

you might use this line instead, it might work 你可能会使用这一行,它可能会起作用

gulp.watch(['./public/**/*.js', './public/*.js']).on('change', reload);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM