2.0.0 • Published 10 years ago

koa-bundle v2.0.0

Weekly downloads
75
License
-
Repository
github
Last release
10 years ago

koa-bundle

Generic asset pipeline with caching, etags, minification, gzipping and sourcemaps.

The child of node-enchilada and static-cache.

Examples

  • Browserify (with a callback and options)
var bundle = Bundle({ debug: true }, function(file, fn) {
  Browserify({ debug: file.debug })
    .add(file.path)
    .transform(require('babelify'))
    .bundle(fn);
}))
app.use(bundle('app.js'));
  • Duo (using generators)
var bundle = Bundle(function *(file) {
  return yield Duo(file.root)
    .entry(file.path)
    .use(require('duo-sass')())
    .run();
})
app.use(bundle('app.css'));
  • Gulp (using currying and globbing)
var bundle = Bundler({ root: __dirname }, function(file, fn) {
  var gulp = Gulp.src(file.path, { cwd: file.root });

  if ('styl' == file.type) {
    gulp.pipe(styl())
      .on('error', fn);
  }

  gulp.pipe(myth())
    .on('error', fn)

  if ('production' == process.env.NODE_ENV) {
    gulp
      .pipe(csso())
      .on('error', fn);
  }

  gulp.on('end', fn);
});

// ... in another file, single middleware
app.use(bundle());

// multiple endpoints
bundle('app.styl');
bundle('app.js');

Installation

npm install koa-bundle

API

bundle(settings, handler) => bundler([path]) => middleware

bundle(handler)(glob) => bundler([path]) => middleware

Create a bundler with an optional set of settings and a handler.

A handler can be a synchronous function, asynchronous function, generator or promise. The handler passes a File object that has the following properties:

var File = {
  type: "js",
  src: "... JS ...",
  path: "dashboard.js",
  root: "/Users/Matt/Projects/..."
  minify: true,
  debug: false,
  cache: true,
  gzip: true,
}

The available settings are:

  • debug: enables sourcemaps
  • minify: minify JS and CSS
  • cache: cache responses across requests and add etags
  • gzip: gzip the response if it's supported

The default settings depend on the environment (NODE_ENV):

  • Production:

    • debug: false
    • minify: true
    • cache: true
    • gzip: true
  • Development:

    • debug: true
    • minify: false
    • cache: false
    • gzip: false

The bundler returns a function that you can then pass a path into:

var bundle = Bundler(settings, handler);
app.use(bundle('app.js'));

The path is relative to settings.root or process.cwd(). The script[src] and link[href] is relative the root specified.

TODO

  • Warmup cache in production
  • More examples
  • Testing

Credits

License

MIT

Copyright (c) 2015 Matthew Mueller <matt@lapwinglabs.com>

2.0.0

10 years ago

1.0.14

10 years ago

1.0.13

10 years ago

1.0.12

10 years ago

1.0.11

10 years ago

1.0.10

10 years ago

1.0.9

10 years ago

1.0.8

10 years ago

1.0.7

10 years ago

1.0.6

10 years ago

1.0.5

10 years ago

1.0.4

10 years ago

1.0.3

10 years ago

1.0.2

10 years ago

1.0.1

10 years ago

1.0.0

10 years ago

0.1.3

10 years ago

0.1.2

10 years ago

0.1.1

10 years ago

0.1.0

10 years ago

0.0.4

10 years ago

0.0.3

10 years ago

0.0.2

10 years ago

0.0.1

10 years ago