本文介绍了使用brfs进行监视和捆绑的命令,而无需进行watchify的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用brfs转换来复制watchify的行为,但是我需要直接使用brfs,因为我想避免在将require与browserify一起使用时避免添加到脚本中的其他代码/watchify.直接使用brfs只需将require(theFile)替换为其内容即可.

I'm trying to replicate the behavior of watchify with the brfs transform, but I need to use brfs directly because I want to avoid the additional code added to the script when using require with browserify/watchify. Using brfs directly simply replaces require(theFile) with its contents and nothing else.

使用此命令捆绑以下代码会产生我想要的结果:

Using this command to bundle the following code produces my intended result:

brfs main.js > bundle.js
// main.js
var fs = require('fs');
var templates = {
    'header': fs.readFileSync('app/templates/header.html', 'utf8'),
    'nav-menu': fs.readFileSync('app/templates/nav-menu.html', 'utf8')
};

如何设置某些内容以监视更改,并在发生某些更改时重新绑定brfs脚本?

How do I set something up to watch for changes and have brfs bundle the script again when something changes?

推荐答案

我认为使用brfs编程api的这种方法是最合适的.与我们在js聊天中讨论的示例的唯一区别是,您需要使用fs.createReadStream将文件通过管道传输到brfs,而不是像我们那样直接将文件名传递给brfs.

I consider this approach using the brfs programmatic api to be most proper. The only difference from the example we discussed in the js chat is that you need to use fs.createReadStream to pipe the file into brfs, rather than passing the filename directly to brfs as we were doing.

var gulp = require('gulp');
var brfs = require('brfs');
var fs   = require('fs');

// task without watch
gulp.task('bundle-templates', function() {
  fs.createReadStream('main.js')
    .pipe(brfs())
    .pipe(fs.createWriteStream('bundle.js'))
  ;
});

// this runs the `bundle-templates` task before starting the watch (initial bundle)
gulp.task('watch-templates', ['bundle-templates'], function() {
  // now watch the templates and the js file and rebundle on change
  gulp.watch([
    'templates/*.html',
    'main.js'
  ], ['bundle-templates']);
});

现在运行此命令,您已准备就绪:

Now run this command and you're all set:

gulp watch-templates

在这里不需要

gulp.您可以使用任何任务运行程序或直接执行节点脚本来重新创建它.您可以直接使用 凝视 来监视文件,该监视者与gulp用于gulp.watch.

gulp is not necessary here. You can recreate this using any task runner or by executing a node script directly. You can use gaze directly to watch the files, which is the same watcher that gulp is using for gulp.watch.

这篇关于使用brfs进行监视和捆绑的命令,而无需进行watchify的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-06 22:44