【问题标题】:Gulp + browserify example (without gulp-browserify)Gulp + browserify 示例(没有 gulp-browserify)
【发布时间】:2015-10-02 07:53:46
【问题描述】:

现在gulp-browserify is no longer supported 我正在寻找一个简单的教程,如何现在将 browserify 与 gulp 一起使用。 This 似乎是一种选择,但它仍然相当复杂。任何指针将不胜感激。

【问题讨论】:

  • 也在找这个。只想要最简单的方法来配置 gulp 和 browserify 以在一个文件夹中获取一堆 JS 文件,并将它们全部合并到一个公共 application.js 文件中。

标签: gulp browserify


【解决方案1】:

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'));
});

【讨论】:

    【解决方案2】:

    您可能想立即使用 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找到。

    【讨论】:

      【解决方案3】:

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

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

      这是我可以为您的 gulpfile.js 提供的最简洁的代码 sn-p(带有一些 cmets)(您可以/应该(?)将其放在与您的 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]'))
      });
      

      某人,我在这个问题上卡了一段时间。

      【讨论】:

        【解决方案4】:

        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 文件中;以及所有 external 模块和依赖项,我使用 NPM 管理并在此处在 assets 数组中指定。

        【讨论】:

          猜你喜欢
          • 2015-04-12
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多