命令在没有watchify的情况下使用brfs进行观看和捆绑
Command to watch and bundle using brfs without watchify
我正试图用brfs
转换复制watchify
的行为,但我需要直接使用brfs
,因为我想避免在使用带有browserify/watchify的require
时添加到脚本中的额外代码。直接使用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来监视文件,这与gullow用于gulp.watch
的监视程序相同。
您可以尝试gullowshell、
那么吞咽任务将是
gulp.task('default', shell.task(['brfs main.js > bundle.js']);
不确定这是否适合您的情况。。。但你也可以使用这个插件来检测新文件并将其导出:
https://www.npmjs.com/package/gulp-newy/
使用更少文件的示例,并与一个级联的css文件进行比较:
function lessVersusOneFile(projectDir, srcFile, absSrcFile) {
//newy gives projectDir arg wich is '/home/one/github/foo/`
var compareFileAgainst = "compiled/css/application.css";
var destination = path.join(projectDir, compareFileAgainst);
// distination returned will be /home/one/github/foo/compiled/css/application.css
return destination;
}
// all *.less files will be compared against
// /home/one/github/foo/compiled/css/application.css
gulp.task('compileLessToCss', function () {
return gulp.src('app/css/**/*.less')
.pipe(newy(lessVersusOneFile))
.pipe(less())
.pipe(gulp.dest('compiled/css'));
});
相关文章:
- 如何在映射数组中添加换行符
- JavaScript数组排序(函数)用于对表行进行排序,而不是排序
- 在jQuery中获取表的行索引
- 使用jquery将mysql数据获取到新的表行中
- 如何删除多行HTML排列中的空白
- 如何在Google柱状图中动态添加行/列
- Jquery在函数之间传递表行
- 如何从rails中的代码中删除新行( )
- 使用jQuery动态添加表并在其中动态添加行
- jquery.each(第2行)添加背景色
- 鼠标悬停事件影响列表中所有行中的按钮,而不仅仅是特定按钮
- 拆分文本以每隔n个字符添加一行新行,并注意空格
- 通过命令行/批处理文件打开页面时,将javascript代码注入Google Chrome
- NodeJS-readline暂停和恢复事件发射器(逐行读取)
- 可以'我不明白为什么;t将行和单元格添加到表中
- RubyonRails中的可点击表行
- Jquery行删除对我不起作用,如何设置它
- 是否可以添加这行“;“照原样”;在HTML标记中
- 将行添加到具有固定标题的HTML表中
- Javascript历史记录转换为php行