GithubHelp home page GithubHelp logo

koajs / bundle Goto Github PK

View Code? Open in Web Editor NEW
36.0 9.0 3.0 3.5 MB

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

Makefile 0.62% HTML 1.39% JavaScript 96.13% CSS 1.85%

bundle's Introduction

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, 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)
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 <[email protected]>

bundle's People

Contributors

matthewmueller avatar niftylettuce avatar

Stargazers

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

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

bundle's Issues

Browserify example in documentation does not run.

Hi!

I'm on [email protected], koa-bundle@^1.0.13 and koa@^1.0.0. The first example, the browserify one, in the documentation fails to run. Here is my code:

var koa = require('koa');
var Bundle = require('koa-bundle');
var browserify = require('browserify');

var app = koa()

var bundle = Bundle({debug:true},function(file, fn) {
  browserify({debug:true}).add(file.path).transform(require('babelify')).bundle(fn)
});
app.use(bundle('js/index.js'))

app.listen(3000);

Here's what happens when I try to run it:

Venjetindan:mobill jonpacker$ node index.js
path.js:8
    throw new TypeError('Path must be a string. Received ' +
    ^

TypeError: Path must be a string. Received undefined
    at assertPath (path.js:8:11)
    at posix.join (path.js:477:5)
    at fullpath (/Users/jonpacker/Development/Projects/mobill/node_modules/koa-bundle/index.js:426:30)
    at entry (/Users/jonpacker/Development/Projects/mobill/node_modules/koa-bundle/index.js:116:12)
    at _bundle (/Users/jonpacker/Development/Projects/mobill/node_modules/koa-bundle/index.js:87:19)
    at Object.<anonymous> (/Users/jonpacker/Development/Projects/mobill/index.js:10:9)
    at Module._compile (module.js:434:26)
    at Object.Module._extensions..js (module.js:452:10)
    at Module.load (module.js:355:32)
    at Function.Module._load (module.js:310:12)

Caching?

How can we cache responses from bundled assets? What would be your recommended approach? Using gulp?

display error in the browser when we're in dev

cleaned up version of:

var toHTML = require('ansii-html')
this.body = 'document.body.innerHTML = "<pre style=\\"padding:50px;\\">' + toHTML(msg).replace(/['"]/gm, '\\\"').replace(/(\r\n|\n|\r)/gm, '<br/>') + '</pre>";';

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.