Gruntjs 吞下手表,浏览一下。手表,但不再制造
因此,今天是我第一天与gulp和grunt或js的任何任务运行人员一起玩。我到了一个地方,可以在js文件中更改代码,然后运行Gruntjs 吞下手表,浏览一下。手表,但不再制造,gruntjs,gulp,grunt-contrib-watch,gulp-watch,Gruntjs,Gulp,Grunt Contrib Watch,Gulp Watch,因此,今天是我第一天与gulp和grunt或js的任何任务运行人员一起玩。我到了一个地方,可以在js文件中更改代码,然后运行 gulp browserify 这个很好用。然而,这很烦人,我想给它添加一个手表,这样当我对脚本进行任何更改时,它会自动运行gulp browserify或其他东西,而我不必手动执行。这就是我对我的胃所做的 var gulp = require('./gulp')({ }); gulp.task('watch', function() { /
gulp browserify
这个很好用。然而,这很烦人,我想给它添加一个手表,这样当我对脚本进行任何更改时,它会自动运行gulp browserify或其他东西,而我不必手动执行。这就是我对我的胃所做的
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是提高了!!还有更好的主意吗
谢谢对于我来说,问题是由于目录结构,gulp似乎不能很好地处理相对路径,至少在我的情况下是这样 我的项目设置如下:
/
project/
gulpfile.js
src/
app.js
build/
mybuiltfile.js
最后,我将其全部移动到了一个文件夹中,这解决了我的问题。您应该使用来监视文件更改,而性能成本更低。当您的应用程序开始扩展时,您的代码库将花费大量时间进行捆绑,因为Browserify将重建每个文件,即使在最新的修改中只有一个文件发生了更改
Watchify只重建它需要的东西。初始构建(当您运行一个gulp任务时)与以前一样,但是每次更改时,您都会看到不同之处
在5578610字节JavaScript应用程序中,初始构建时间为6.67s,文件更改时重建时间为~400ms。仅使用Browserify时,每次更改都有6.67s
要开始,请安装NPM软件包:
npm install browserify watchify --save-dev
在gulpfile.js中导入Browserify和Watchify:
var browserify = require('browserify');
var watchify = require('watchify');
初始化bundler(我对商品使用\ucode>)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.
});
运行galpwatch
,您就可以开始工作了
注意:您应该只将入口点设置为绑定器入口。Browserify和dependency分支将处理其余部分,您不会两次构建同一个文件。为什么要使用grunt标记?