我正在尝试使用 browserify 和 babelify 创建一个 gulp 任务.这是任务:
I'm trying to create a gulp task with browserify and babelify. Here is the task:
var gulp = require('gulp');
var browserify = require('gulp-browserify');
var source = require('vinyl-source-stream');
var babelify = require('babelify');
gulp.task('js', function () {
browserify('./resources/js/*.js')
.transform(babelify)
.bundle()
.pipe(source('*.js'))
.pipe(gulp.dest('./public/js'));
});
我找到了一些示例代码,尝试使用它们,但结果总是一样.
I found a few sample code, tried to use them, but the result was always the same.
当我运行任务并保存我的 example.js 文件时,出现以下错误:
When i run the task, and save my example.js file, the following error occurs:
TypeError: browserify(...).transform 不是函数
我做错了什么?
你混淆了 browserify
和 gulp-browserify
的 API.
You're mixing up the API for browserify
and gulp-browserify
.
从 gulp-browserify docs,您需要执行以下操作:
From the gulp-browserify docs, you'll want to do something like this:
var gulp = require('gulp')
var browserify = require('gulp-browserify')
gulp.task('js', function () {
gulp.src('./resources/js/*.js')
.pipe(browserify({
transform: ['babelify'],
}))
.pipe(gulp.dest('./public/js'))
});
<小时>
自从第一次回答这个问题以来,gulp-browserify 已被放弃 并且 gulp 已经发展了很多.如果您想使用更新版本的 gulp 实现相同的功能,您可以 遵循 gulp 团队提供的指南.
Since this question was first answered, gulp-browserify has been abandoned and gulp has evolved a great deal. If you'd like to achieve the same thing with a newer version of gulp, you can follow the guides provided by the gulp team.
你最终会得到如下的结果:
You'll end up with something like the following:
var browserify = require('browserify');
var babelify = require('babelify');
var gulp = require('gulp');
var source = require('vinyl-source-stream');
var buffer = require('vinyl-buffer');
var sourcemaps = require('gulp-sourcemaps');
var util = require('gulp-util');
gulp.task('default', function () {
var b = browserify({
entries: './resources/test.js',
debug: true,
transform: [babelify.configure({
presets: ['es2015']
})]
});
return b.bundle()
.pipe(source('./resources/test.js'))
.pipe(buffer())
.pipe(sourcemaps.init({ loadMaps: true }))
// Add other gulp transformations (eg. uglify) to the pipeline here.
.on('error', util.log)
.pipe(sourcemaps.write('./'))
.pipe(gulp.dest('./public/js/'));
});
这篇关于Gulp + babelify + browserify 问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!