我正在使用这个要点为我的gulpfile:https://gist.github.com/httpNick/708219f5002b6a9dc578
它适用于ES6 babelfiying,但我想查看./public/js文件夹中的所有文件,并将构建的版本输出到构建文件夹。有没有办法修改这个gulpfile来观察文件夹中的所有js文件?
从要点中复制粘贴:
var gulp = require('gulp')
var browserify = require('browserify')
var watchify = require('watchify')
var babelify = require('babelify')
var source = require('vinyl-source-stream')
var buffer = require('vinyl-buffer')
var merge = require('utils-merge')
var rename = require('gulp-rename')
var uglify = require('gulp-uglify')
var sourcemaps = require('gulp-sourcemaps')
/* nicer browserify errors */
var gutil = require('gulp-util')
var chalk = require('chalk')
function map_error(err) {
if (err.fileName) {
// regular error
gutil.log(chalk.red(err.name)
+ ': '
+ chalk.yellow(err.fileName.replace(__dirname + '/public/js/', ''))
+ ': '
+ 'Line '
+ chalk.magenta(err.lineNumber)
+ ' & '
+ 'Column '
+ chalk.magenta(err.columnNumber || err.column)
+ ': '
+ chalk.blue(err.description))
} else {
// browserify error..
gutil.log(chalk.red(err.name)
+ ': '
+ chalk.yellow(err.message))
}
this.end()
}
/* */
gulp.task('watchify', function () {
var args = merge(watchify.args, { debug: true })
var bundler = watchify(browserify('./public/js/index.js', args)).transform(babelify, {presets: ["es2015", "react"]})
bundle_js(bundler)
bundler.on('update', function () {
bundle_js(bundler)
})
})
function bundle_js(bundler) {
return bundler.bundle()
.on('error', map_error)
.pipe(source('index.js'))
.pipe(buffer())
.pipe(gulp.dest('public/dist'))
.pipe(rename('index.min.js'))
.pipe(sourcemaps.init({ loadMaps: true }))
// capture sourcemaps from transforms
.pipe(uglify())
.pipe(sourcemaps.write('.'))
.pipe(gulp.dest('public/dist'))
}
// Without watchify
gulp.task('browserify', function () {
var bundler = browserify('./public/js/index.js', { debug: true }).transform(babelify, {presets: ["es2015", "react"]})
return bundle_js(bundler)
})
// Without sourcemaps
gulp.task('browserify-production', function () {
var bundler = browserify('./src/js/app.js').transform(babelify, {presets: ["es2015", "react"]})
return bundler.bundle()
.on('error', map_error)
.pipe(source('index.js'))
.pipe(buffer())
.pipe(rename('index.min.js'))
.pipe(uglify())
.pipe(gulp.dest('public/dist'))
})
答案 0 :(得分:0)
Watchify将通过您指定的入口点(在您的情况下为browserify
)观看传递给index.js
命令的所有文件。因此,应自动监视index.js
文件中所需的所有内容。
您的watchify
命令看起来不正确。您已将watchify args传递给browserify
函数,而不是watchify
函数。
这里是我的gulpfile的相关部分,你的结构有点不同,但你会在watchify
命令中获得要点:
.task('js', ['clean'], function() {
var b = browserify(packageJson.paths.app);
var w = watchify(b, watchify.args); // Pass watchify.args here vs. in browserify fn.
w = w
.transform(babelify)
.transform(browserifyShim);
w.on('update', rebundle);
function rebundle() {
// my rebundle function, pretty much same as your `bundle_js`
}
return rebundle();
})