# koa-bundle

> Generic asset pipeline with caching, etags, and sourcemaps

Latest version **2.0.0** (published 2015-10-02) · 0 weekly downloads

## Install

```sh
npm install koa-bundle
pnpm add koa-bundle
yarn add koa-bundle
bun add koa-bundle
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.0.0 |
| Published | 2015-10-02 |
| First published | 2015-03-08 |
| Weekly downloads | 0 |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 12 |
| Known vulnerabilities | 0 (+1 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 37 |
| Author | Matthew Mueller |
| Maintainers | mattmueller |

## Links

- npm: https://www.npmjs.com/package/koa-bundle
- Repository: https://github.com/koajs/bundle
- Homepage: https://github.com/koajs/bundle#readme
- Issues: https://github.com/koajs/bundle/issues
- npm.io page: https://npm.io/package/koa-bundle

## Dependencies (12)

- [csso](https://npm.io/package/csso.md) ^1.3.11
- [debug](https://npm.io/package/debug.md) ^2.1.2
- [wrap-fn](https://npm.io/package/wrap-fn.md) ^0.1.4
- [ansi-html](https://npm.io/package/ansi-html.md) 0.0.4
- [file-deps](https://npm.io/package/file-deps.md) 0.0.8
- [uglify-js](https://npm.io/package/uglify-js.md) ^2.4.23
- [mime-types](https://npm.io/package/mime-types.md) ^2.0.9
- [escape-html](https://npm.io/package/escape-html.md) ^1.0.2
- [compressible](https://npm.io/package/compressible.md) ^2.0.2
- [object-assign](https://npm.io/package/object-assign.md) ^2.0.0
- [browser-resolve](https://npm.io/package/browser-resolve.md) ^1.9.0
- [convert-source-map](https://npm.io/package/convert-source-map.md) ^1.1.1

## Recent versions

- 2.0.0 (latest) — 2015-10-02
- 1.0.14 — 2015-10-01
- 1.0.13 — 2015-08-14
- 1.0.12 — 2015-07-22
- 1.0.11 — 2015-07-20
- 1.0.10 — 2015-07-20
- 1.0.9 — 2015-07-19
- 1.0.8 — 2015-06-12
- 1.0.7 — 2015-06-10
- 1.0.6 — 2015-06-06
- 1.0.5 — 2015-06-06
- 1.0.4 — 2015-06-06
- 1.0.3 — 2015-06-01
- 1.0.2 — 2015-06-01
- 1.0.1 — 2015-06-01
- … 9 more at https://npm.io/package/koa-bundle/versions

## README

# koa-bundle

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

  The child of [node-enchilada](https://github.com/defunctzombie/node-enchilada) and [static-cache](https://github.com/koajs/static-cache).

## Examples

- Browserify (with a callback and options)

```js
var bundle = Bundle({ debug: true, root: __dirname) }, function(file, fn) {
  Browserify({ debug: file.debug })
    .add(file.path)
    .transform(require('babelify'))
    .bundle(fn);
}))
app.use(bundle('app.js'));
```

- Duo (using generators)

```js
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)

```js
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

```js
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:

```js
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:

```js
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

- [node-enchilada](https://github.com/defunctzombie/node-enchilada) and [browserify-middleware](https://github.com/forbeslindesay/browserify-middleware) for some ideas and general design.
- [static-cache](https://github.com/koajs/static-cache) for the caching, etagging and gzipping.
- sponsored by [Lapwing Labs](http://lapwinglabs.com).

## License

MIT

Copyright (c) 2015 Matthew Mueller &lt;matt@lapwinglabs.com&gt;

---
_Source: https://npm.io/package/koa-bundle · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
