【问题标题】:gulp watch and browserify. watches but doesnt build againgulp 观看和浏览器化。观看但不再建造
【发布时间】:2014-08-22 21:49:54
【问题描述】:

所以今天是我玩 gulp 和 grunt 或任何 js 任务运行器的第一天。我到了一个地方,我可以在我的 js 文件中更改我的代码,然后运行 ​​

gulp browserify 

这很好用。但是这很烦人,我想为此添加一个监视,以便当我对脚本进行任何更改时,它会自动运行 gulp browserify 或其他东西,而我不必手动执行。所以这就是我对 gulp.js 所做的事情

    var gulp = require('./gulp')({


});



gulp.task('watch', function() {
    // Watch .js files
    gulp.watch('jsfolder/**/*.js', ['scripts']);

});
gulp.task('release', ['build']);
gulp.task('build', ['scripts', 'browserify']);
gulp.task('default', ['watch']);

所以当我这样做时

gulp watch

当我保存我的更改时,它会给我

14:37:21] Starting 'clean'...
[14:37:21] Finished 'clean' after 3.18 ms
[14:37:21] Starting 'concat'...
[14:37:21] Finished 'concat' after 263 μs
[14:37:21] Starting 'checksum'...
[14:37:21] Finished 'checksum' after 19 ms
[14:37:21] Starting 'scripts'...
[14:37:21] Finished 'scripts' after 455 μs
[14:38:41] Starting 'clean'...
[14:38:41] Finished 'clean' after 2.9 ms
[14:38:41] Starting 'concat'...
[14:38:41] Finished 'concat' after 218 μs
[14:38:41] Starting 'checksum'...
[14:38:41] Finished 'checksum' after 18 ms
[14:38:41] Starting 'scripts'...
[14:38:41] Finished 'scripts' after 302 μs

但我的更改从未显示在页面上。我假设它只是观看而不是建造?我错过了什么?

编辑

我添加了这个

 gulp.watch('ui.id.go.com/public/**/*.js', ['scripts','browserify']);

但现在它检查它的方式太频繁了,即使更新,我的机器 cpu 也被调高了!!有什么更好的想法吗?

谢谢

【问题讨论】:

  • 为什么是 grunt 标签?

标签: gruntjs gulp grunt-contrib-watch gulp-watch


【解决方案1】:

您应该使用WatchifyBrowserify 来观察文件更改,从而降低性能成本。当您的应用程序开始扩展时,您的代码库将花费大量时间来打包,因为 Browserify 会重新构建每个文件,即使在最近的修改中只有一个文件发生了变化。

Watchify 只重建它需要的东西。初始构建(当您运行 gulp 任务时)与以前相同,但在每次更改时,您都会看到不同之处。

5578610 字节 JavaScript 应用程序中,初始构建需要 6.67 秒,文件更改重建需要 ~400 毫秒。仅使用 Browserify,每次更改都有 6.67s

首先,安装 NPM 包:

npm install browserify watchify --save-dev

在您的gulpfile.js 中导入 Browserify 和 Watchify:

var browserify = require('browserify');
var watchify = require('watchify');

初始化捆绑器(我使用 Lodash _ 作为商品)。 client.js 是这里的应用入口:

var bundler = watchify(browserify(_.assign(watchify.args, {
  entries: ['./src/client.js'],
  debug: true
})));

bundler.on('log', gutil.log); // Output build logs to terminal using gulp-util.

然后使用 Watchify 创建您的 bundle() 函数:

function bundle() {
  return bundler.bundle()

    // Log errors if they happen.
    .on('error', gutil.log.bind(gutil, 'Browserify Error'))
    .pipe(source('client.js'))

    // Optional, remove if you don't need to buffer file contents.
    .pipe(buffer())

    // Optional, remove if you dont want sourcemaps.
    // Loads map from Browserify file using gulp-sourcemaps.
    .pipe(sourcemaps.init({loadMaps: true}))

    // Add transformation tasks to the pipeline here.
    .pipe(sourcemaps.write('./')) // Writes .map file.
    .pipe(size(config.size)) // Checks output file size with gulp.size.
    .pipe(gulp.dest('./build'));
}

最后,当有依赖更新时运行打包器:

gulp.task('scripts', bundle);
gulp.task('watch', ['scripts'], function() {
  bundler.on('update', bundle); // On any dependency update, runs the bundler.
});

运行gulp watch,您就可以开始工作了。

注意:您应该只将入口点设置为捆绑器入口。 Browserify 和依赖分支会处理剩下的事情,你不会两次构建同一个文件。

【讨论】:

    【解决方案2】:

    对我来说,问题出在目录结构上,似乎 gulp 不能很好地处理相对路径,至少在我的情况下是这样。

    我的项目是这样设置的:

    /
        project/
            gulpfile.js
            src/
                app.js
        build/
            mybuiltfile.js
    

    我最终将它全部移动到一个文件夹中,这解决了我的问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-05-06
      • 1970-01-01
      • 1970-01-01
      • 2016-12-15
      • 2016-07-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多