使用 brfs 进行观看和捆绑而不使用 watchify 的命令

2024-04-06

我正在尝试复制的行为watchifybrfs变换,但我需要使用brfs直接因为我想避免在使用时添加到脚本中的额外代码require使用 browserify/watchify。使用brfs直接简单替换require(theFile)及其内容,仅此而已。

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

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当发生变化时再次捆绑脚本?


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

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']);
});

现在运行此命令,一切就绪:

gulp watch-templates

gulp这里没有必要。您可以使用任何任务运行程序或直接执行节点脚本来重新创建它。您可以使用gaze https://github.com/shama/gaze直接观察文件,这与 gulp 使用的观察器相同gulp.watch.

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 brfs 进行观看和捆绑而不使用 watchify 的命令 的相关文章

随机推荐