gpt4 book ai didi

gulp - Watchify检测变化但输出没有变化

转载 作者:行者123 更新时间:2023-12-05 00:39:46 27 4
gpt4 key购买 nike

我有以下 gulpfile

gulp.task('browserify', function() {
bundle(false);
});

gulp.task('browserify-watch', function() {
bundle(true);
});

function bundle (performWatch) {
var bify = (performWatch === true
? watchify(browserify(finalBrowserifyOptions))
: browserify(finalBrowserifyOptions));

if (performWatch) {
bify.on('update', function () {
console.log('Updating project files...');
rebundle(bify);
});
}

bify.transform(babelify.configure({
compact: false
}));

function rebundle(bify) {
return bify.bundle()
.on('error', function () {
plugins.util.log('Browserify error');
})
.pipe(source('bundle.js'))
.pipe(buffer())
.pipe(plugins.sourcemaps.init({loadMaps: true}))
.pipe(plugins.sourcemaps.write('./'))
.pipe(gulp.dest(paths.build + assets.js));
}

return rebundle(bify);
}

麻烦的是 gulp browserify工作得很好。然而, gulp browserify-watch检测更改但输出永远不会更新。

我究竟做错了什么?

最佳答案

我遇到了同样的watchify文件更改检测失败(gulp 和 grunt)。 CLI watchify 可以按预期工作。例如:

watchify ./src/app.js -t babelify --outfile ./build/bundle.js -v

目前我切换到 browserify-incremental .关于 watchify 方法的观点是不同的。这是他们 Github 页面上的一段,其中包含了最好的内容:

browserify-incremental can detect changes which occured in between runs, which means it can be used as part of build systems which are invoked on demand, without requiring a long lived process. Whereas watchify is slow for the first run upon each startup, browserify-incremental is fast every time after the very first.



这是使用 browserify-incremental 的“翻译”CLI 命令:
browserifyinc ./src/app.js -t babelify --outfile ./build/bundle.js -v

这是一个用于观看和 [重新] 捆绑的简单 gulpfile.js 脚本:
var gulp = require('gulp');
var sourcemaps = require('gulp-sourcemaps');
var source = require('vinyl-source-stream');
var buffer = require('vinyl-buffer');
var browserify = require('browserify');
var babel = require('babelify');
var browserifyInc = require('browserify-incremental');

var bundleApp = function() {
var browserifyObj = browserify('./src/app.js', { debug: false, cache: {}, packageCache: {}, fullPaths: true }).transform(babel);
var bundler = browserifyInc(browserifyObj, {cacheFile: './browserify-cache.json'});

bundler.on('time', function (time) {
console.log('-> Done in ' + time/1000 + ' ms');
});

console.log('-> Bundling...');

bundler.bundle()
.on('error', function(err) { console.error(err); this.emit('end'); })
.pipe(source('bundle.js'))
.pipe(buffer())
.pipe(sourcemaps.init({ loadMaps: true }))
.pipe(sourcemaps.write('./'))
.pipe(gulp.dest('./build'));
};


gulp.task('browserify', function () {
gulp.watch('./src/**/*.js', function () {
return bundleApp();
});
});


gulp.task('default', ['browserify']);

关于gulp - Watchify检测变化但输出没有变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30389031/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com