Browserify - multiple entry points

前端 未结 4 1535
夕颜
夕颜 2020-12-13 18:55

I am using Browserify within gulp. I am trying to compile down my tests to a single file as well. But unlike my main app, which I have working just fine, I am having trouble

相关标签:
4条回答
  • 2020-12-13 19:02

    A little more complicated example to build files by glob pattern into many files with watching and rebuilding separated files. Not for .coffee, for es2015, but not a big difference:

    var gulp = require("gulp");
    var babelify = require("babelify");
    var sourcemaps = require("gulp-sourcemaps");
    var gutil = require("gulp-util");
    var handleErrors = require("../utils/handleErrors.js");
    var browserify = require("browserify");
    var eventStream = require("event-stream");
    var glob = require("glob");
    var source = require("vinyl-source-stream");
    var buffer = require("vinyl-buffer");
    var watchify = require("watchify");
    
    var SRC_PATH = "./src";
    var BUILD_PATH = "./build";
    
    var bundle = function (bundler, entryFilepath) {
      console.log(`Build: ${entryFilepath}`);
    
      return bundler.bundle()
        .on("error", handleErrors)
        .pipe(source(entryFilepath.replace(SRC_PATH, BUILD_PATH)))
        .pipe(buffer())
        .on("error", handleErrors)
        .pipe(
          process.env.TYPE === "development" ?
            sourcemaps.init({loadMaps: true}) :
            gutil.noop()
        )
        .on("error", handleErrors)
        .pipe(
          process.env.TYPE === "development" ?
            sourcemaps.write() :
            gutil.noop()
        )
        .on("error", handleErrors)
        .pipe(gulp.dest("."))
        .on("error", handleErrors);
    };
    
    var buildScripts = function (done, watch) {
      glob(`${SRC_PATH}/**/[A-Z]*.js`, function (err, files) {
        if (err) {
          done(err);
        }
    
        var tasks = files.map(function (entryFilepath) {
          var bundler = browserify({
            entries: [entryFilepath],
            debug: process.env.TYPE === "development",
            plugin: watch ? [watchify] : undefined
          })
            .transform(
              babelify,
              {
                presets: ["es2015"]
              });
    
          var build = bundle.bind(this, bundler, entryFilepath);
    
          if (watch) {
            bundler.on("update", build);
          }
    
          return build();
        });
    
        return eventStream
          .merge(tasks)
          .on("end", done);
      });
    };
    
    gulp.task("scripts-build", function (done) {
      buildScripts(done);
    });
    
    gulp.task("scripts-watch", function (done) {
      buildScripts(done, true);
    });
    

    Complete code here https://github.com/BigBadAlien/browserify-multy-build

    0 讨论(0)
  • 2020-12-13 19:03

    Below is a task I was able to build that seems to solve the problem. Basically I use an outside library to gather the files names as an array. And then pass that array as the entry points

    'use strict;'
    
    var config = require('../config');
    var gulp = require('gulp');
    var plumber = require('gulp-plumber');
    var glob = require('glob');
    var browserify  = require('browserify');
    var source = require('vinyl-source-stream');
    
    gulp.task('tests', function(){
      var testFiles = glob.sync('./spec/**/*.js');
      return browserify({
          entries: testFiles,
          extensions: ['.jsx']
        })
        .bundle({debug: true})
        .pipe(source('app.js'))
        .pipe(plumber())
        .pipe(gulp.dest(config.dest.development));
    });
    
    0 讨论(0)
  • 2020-12-13 19:14

    For start, you can write a suite.js to require all the tests which you want to run and browserify them.

    You can see two examples from my project https://github.com/mallim/sbangular.

    One example for grunt-mocha-phantomjs

    https://github.com/mallim/sbangular/blob/master/src/main/resources/js/suite.js

    One example for protractor

    https://github.com/mallim/sbangular/blob/master/src/main/resources/js/suite.js

    This is just a start and I am sure there are more fancy ways available.

    0 讨论(0)
  • 2020-12-13 19:20

    Here's an alternate recipe that fits more with the gulp paradigm using gulp.src()

    var gulp = require('gulp');
    var browserify = require('browserify');
    var transform = require('vinyl-transform');
    var concat = require('gulp-concat');
    
    gulp.task('browserify', function () {
    
      // use `vinyl-transform` to wrap around the regular ReadableStream returned by b.bundle();
      // so that we can use it down a vinyl pipeline as a vinyl file object.
      // `vinyl-transform` takes care of creating both streaming and buffered vinyl file objects.
      var browserified = transform(function(filename) {
        var b = browserify(filename, {
          debug: true,
          extensions: ['.coffee']
        });
        // you can now further configure/manipulate your bundle
        // you can perform transforms, for e.g.: 'coffeeify'
        // b.transform('coffeeify');
        // or even use browserify plugins, for e.g. 'minifyiy'
        // b.plugins('minifyify');
        // consult browserify documentation at: https://github.com/substack/node-browserify#methods for more available APIs
        return b.bundle();
      });
    
      return gulp.src(['./app/js/**/*Spec.coffee'])
        .pipe(browserified)/
        .pipe(concat('spec.js'))
        .pipe(gulp.dest('./specs'));
    });
    
    gulp.task('default', ['browserify']);
    

    For more details about how this work, this article that I wrote goes more in-depth: http://medium.com/@sogko/gulp-browserify-the-gulp-y-way-bb359b3f9623

    0 讨论(0)
提交回复
热议问题