Git Product home page Git Product logo

multibuild's Introduction

multibuild

Let's say that you're developing an email platform. You use a rich text editor in a lot of views—the email editor itself, the signature editor, the template editor—and you'd like to reuse that component between them without bundling all of the view-specific JavaScript together.

If you factor your codebase using ES6 modules, you can have per-view "entry" scripts that import that component and other view-specific modules. A tool like Rollup can then produce per-view bundles that only include the JavaScript used by each view. But how do you coordinate this multi-target build process?

That's where multibuild comes in. It builds related ES6 module bundles using gulp and rollup, and then rebuilds them as files change.

multibuild benefits:

  • Modules are cached between build targets
    • Modules IDs can be flagged to bypass resolution caching for target-derived aliasing
  • Only affected targets are rebuilt when a file changes
  • Bundling is simpler and more consistent

Even if you don't have multiple views in your application, multibuild can still be used to simultaneously build your application bundle and your test bundle.

Installation

$ npm install multibuild

or

$ npm install multibuild --save

Usage

var gulp = require('gulp');
var multiEntry  = require('rollup-plugin-multi-entry');
var MultiBuild = require('multibuild');
var rename = require('gulp-rename');

var build = new MultiBuild({
  // The Gulp instance with which to register the build tasks.
  gulp,

  // The targets to build, arbitrary identifiers. Can't contain spaces since they'll be used to
  // form the names of the build tasks.
  targets: [
    'app',
    'legacy-app',
    'app-vendor',
    'spec'
  ],

  // Names of targets that should not use rollup's cache, eg  because they are processed differently
  // than other targets.
  //
  // Defaults to [].
  skipCache: [
    'spec'
  ],

  // Object/Map that specifies groups of targets which should share cache artifacts. Defaults to
  // sharing the cache between all targets. Targets not specified here will use a default cache
  // group, so you can add new cache groups separate from most other targets.
  cacheGroups: {
    // app-vendor uses rollup-plugin-alias, which would otherwise pollute the build cache with the
    // contents of some aliased modules from this target. Without this cache group, that code would
    // get used in other targets despite the alias not being specified for them.
    vendored: [
      'app-vendor'
    ]
  },

  // Don't cache the resolved path of these module IDs. This lets you do fun things with aliasing
  // and conditionally changing the resolved module based on bundle, without losing the benefits of
  // caching across multiple targets.
  skipResolveCache: ['jquery'],

  /**
   * Optional handler for rollup-emitted errors. We allow the passing of an error handler instead of
   * conditionally applying `gulp-plumber` because `gulp-plumber` is incompatible with
   * `rollup-stream` per https://github.com/Permutatrix/rollup-stream/issues/1.
   */
  errorHandler(e) {
    if (process.env.NODE_ENV !== 'production') {
      // Keep watching for changes on failure.
      console.error(e);
    } else {
      // Throw so that gulp exits.
      throw(e);
    }
  },

  // A function that returns the Rollup entry point when invoked with a target.
  entry: (target) => (target === 'spec') ? 'spec/**/*.js' : `src/js/main-${target}.js`,

  // Options to pass to Rollup's `rollup` and `generate` methods, or a function that returns such
  // options when invoked with a target. Default: {}
  rollupOptions: (target) => {
    var options = {
      plugins: [],
      format: 'iife',
      exports: 'none'
    };
    if (target === 'spec') {
      // multi-entry lets us include all the test specs without having to explicitly import them
      // in a single `main-spec.js` script.
      options.plugins.push(multiEntry({exports: false}));

      // Additional processing done here (eg target-specific transpilation) may entice you to use the `skipCache` option.
    }
    return options;
  },

  // A function that will be invoked with a target and a readable stream containing the bundled JS
  // as a Vinyl buffer, ready for piping through further transformations or to disk. The buffer will
  // be given the filename `${target}.js` (you may of course rename). The function should return the
  // final stream.
  output: (target, input) => {
    if (target === 'spec') {
      return input
        .pipe(rename('spec.js'))
        .pipe(gulp.dest('./spec'));
    } else {
      return input
        .pipe(rename(`build-${target}.js`))
        .pipe(gulp.dest('./src'));
    }
  }
});

// Builds all target bundles and registers dependencies on the files that comprise each bundle.
// Alternatively, you can use build.runAllSequential to run the build groups in series. This
// sometimes yields performance gains, and sometimes performance losses. It likely depends on the
// overlap of work done by the different groups, as that may lead to disk contention.
gulp.task('js', (cb) => build.runAll(cb));

gulp.task('watch', function() {
  // Rebuild bundles that include the file that changed.
  gulp.watch(['src/js/**/*.js'], (event) => build.changed(event.path));
});

gulp.task('default', ['js', 'watch']);

You can get the name of a task generated for a target with MultiBuild.task. This can be useful for specifying MultiBuild-generated build tasks as dependencies of your other tasks without having to hard-code the task name.

var generatedTaskName = MultiBuild.task('targetName');

Contributing

We welcome pull requests! Please lint your code using the JSHint configuration in this project.

multibuild's People

Contributors

bradvogel avatar dnechay avatar guilhermemj avatar jsalvata avatar megantinleywilson avatar renovate[bot] avatar skeggse avatar spencer-brown avatar wearhere avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

multibuild's Issues

Action Required: Fix Renovate Configuration

There is an error with this repository's Renovate configuration that needs to be fixed. As a precaution, Renovate will stop PRs until it is resolved.

Error type: Cannot find preset's package (local>mixmaxhq/renovate-config)

Investigate parallelizing builds

Now that rollup/rollup#1010 is fixed it should be possible, but I'm unsure if running the builds in parallel will outweigh the advantage of reusing the cache between builds as made possible by running in serial.

If the overhead of running individual target builds (i.e. starting up rollup) is greater than the savings of reusing a single module's cache, we might do something like do the initial build in serial, then run the change builds in parallel.

Concurrent builds of watched files in the same cache group can cause corruption

This is due to how we modify the cached modules in the post-build callback.

Error: Could not load <redacted remote import> (imported by <redacted root shim>): ENOENT: no such file or directory, open '<redacted remote import>'
    at .../node_modules/rollup/dist/rollup.js:21385:19
    at <anonymous>
    at process._tickDomainCallback (internal/process/next_tick.js:228:7)
Error: Could not load underscore (imported by <redacted>): ENOENT: no such file or directory, open 'underscore'
    at .../node_modules/rollup/dist/rollup.js:21385:19
    at <anonymous>
Error: Could not load backbone (imported by <redacted>): ENOENT: no such file or directory, open 'backbone'
    at .../node_modules/rollup/dist/rollup.js:21385:19
    at <anonymous>

cc @wearhere

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.