Gulp + browserify示例(没有gulp-browserify)

时间:2015-07-13 21:21:48

标签: gulp browserify

现在gulp-browserify is no longer supported我正在寻找一个简单的教程,现在如何使用浏览器和gulp。 This似乎是一种选择,但它仍然非常复杂。任何指针都会受到赞赏。

4 个答案:

答案 0 :(得分:0)

请参阅this answer

关于链接的帖子,代码将更改为

var gulp = require('gulp');
var through2 = require('through2');
var rename     = require('gulp-rename');
var browserify = require('browserify');

function browserified() {
    return through2.obj(function(file, enc, next) {
        browserify(file.path, {
                debug: false
            })
            .plugin(collapse)
            .bundle(function(err, res) {
                if (err) {
                    return next(err);
                }

                file.contents = res;
                next(null, file);
            });
    });
}

gulp.task('bundle', function() {
    return gulp.src(['./app/main-a.js', './app/main-b.js'])
        .pipe(browserified())
        .pipe(rename({
            extname: '.bundle.js'
        }))
        .pipe(gulp.dest('dest'));
});

答案 1 :(得分:0)

您可能希望立即使用watchify(迟早您需要观看javascript文件以进行更改)。请看这个例子:

'use strict';

var watchify = require('watchify');
var browserify = require('browserify');
var gulp = require('gulp');
var source = require('vinyl-source-stream');
var buffer = require('vinyl-buffer');
var gutil = require('gulp-util');
var sourcemaps = require('gulp-sourcemaps');
var assign = require('lodash.assign');

// add custom browserify options here
var customOpts = {
  entries: ['./src/index.js'],
  debug: true
};
var opts = assign({}, watchify.args, customOpts);
var b = watchify(browserify(opts)); 

// add transformations here
// i.e. b.transform(coffeeify);

gulp.task('js', bundle); // so you can run `gulp js` to build the file
b.on('update', bundle); // on any dep update, runs the bundler
b.on('log', gutil.log); // output build logs to terminal

function bundle() {
  return b.bundle()
    // log errors if they happen
    .on('error', gutil.log.bind(gutil, 'Browserify Error'))
    .pipe(source('bundle.js'))
    // optional, remove if you don't need to buffer file contents
    .pipe(buffer())
    // optional, remove if you dont want sourcemaps
    .pipe(sourcemaps.init({loadMaps: true})) // loads map from browserify file
       // Add transformation tasks to the pipeline here.
    .pipe(sourcemaps.write('./')) // writes .map file
    .pipe(gulp.dest('./dist'));
}

该示例取自here
可以找到更有用的示例here

答案 2 :(得分:0)

我能找到的最好的是这个视频:

https://egghead.io/lessons/javascript-gulp-and-browserify-initial-setup

这是最简洁的代码片段(带有一些注释)我可以为你的gulpfile.js(它可以/应该(?)放在与你的npm package.json文件相同的文件夹中):

"use strict";

// you have to install each of these via npm install,
// and add them to your package.json file
// eg npm install gulp --save [or --save-dev]
var gulp = require('gulp');
var gutil = require('gulp-util');
var source = require('vinyl-source-stream');
var browserify = require('browserify');

// once you've installed gulp globally, you can run gulp tasks like:
// gulp name-of-task
// if you make name-of-task 'default', then just running
// gulp
// will run it
// if you install gulp only locally, you'll need to add to your 'scripts' block
// in package.json eg
// 
// "scripts": {
//     "gulp": "gulp",
//     ...[other scripts go here]...
// }
// and run like:
// npm run gulp (or whatever you named it)
gulp.task('[name-of-task]', function() {
    return browserify('[path to entry file for app *relative to the location of the gulpfile.js*]')
        // bundle is a function of the browserfy API
        .bundle()
        // I'm honestly a little baffled, I'm not sure where 'pipe' is documented...
        // vinyl-source-stream ('source') also a little unclear on that one
        .pipe(source('[name of the bundled js file]'))
        // gulp.dest writes files for you
        .pipe(gulp.dest('[path to where to write the bundled js file, again relative to location of gulpfile.js]'))
});
有人,我被困在这一段时间了。

答案 3 :(得分:0)

Gulp offers some answers in its repo

我在gulpfile.babel.js

中找到了这段代码
// Package
import fs from 'fs';
const pkg = JSON.parse(fs.readFileSync('./package.json'));

// Gulp
import gulp from 'gulp';
import addSrc from 'gulp-add-src';
import concat from 'gulp-concat';
import sourcemaps from 'gulp-sourcemaps';
import uglify from 'gulp-uglify-es';

// Misc
import babelify from 'babelify';
import browserify from 'browserify';
import buffer from 'vinyl-buffer';
import source from 'vinyl-source-stream';

然后:

export function scripts() {

    const src = pkg.settings.src.scripts;

    const bundler = browserify({
        entries: src,
        debug: true,
        transform: [babelify]
    });

    const outputPath = path.join(__dirname, pkg.settings.out.assets);

    const assets = [
        './node_modules/...'
    ]

    return bundler.bundle()
        .pipe(source(src))
        .pipe(buffer())
        .on('error', function(err) { console.error(err); this.emit('end'); })
        .pipe(addSrc.prepend(assets))
        .pipe(sourcemaps.init())
        .pipe(concat(pkg.name + '-' + pkg.version + '.js'))
        .pipe(uglify())
        .pipe(sourcemaps.write())
        .pipe(gulp.dest(outputPath));
}

我还有一个.babelrc

{
  "presets": ["env"]
}

这里棘手的部分是避免忘记'vinyl-source-stream'和'vinyl-buffer',因此你可以从browserify包中操作流和乙烯基格式。

另请注意,我认为只有两种不同的“类型”js模块: my 模块,导入到我给browserify的主src文件中;以及我使用NPM管理的所有外部模块和依赖项,并在此处指定assets数组。