compiled-files

0.1.1 • Public • Published

compiled-files

build status NPM version Coverage Status gemnasium Dependency Status Davis Dependency status

statically serve compiled files with gzip & cache

Example

CompiledFiles will generate a route handler that serves a file including running it through a custom compilation step, optional caching and optional gzipping.

var CompiledFiles = require("compiled-files")
var resolve = require("resolve")
var path = require("path")
var url = require("url")
var browserify = require("browserify")
var http = require("http")
 
var ServeBrowserify = CompiledFiles({
    // custom function to take the Request and 'parse' what
    // resource the HTTP client has asked for.
    // This function finds the last part so `/js/foo` returns
    // `$opts.root/foo`
    findResource: function findResource(req, res, opts) {
        var pathname = url.parse(req.url).pathname
        var parts = pathname.split("/")
        return path.join(opts.root, parts[parts.length - 1])
    },
    // A custom compilation function. The notion is that you
    // are serving files that have a compilation step
    // here you just take the location returned from findResource
    // and return a string to the callback
    compile: function (location, opts, callback) {
        resolve(location, function (err, fileUri) {
            if (err) {
                return callback(err)
            }
 
            var b = browserify([fileUri])
            b.bundle(opts, callback)
        })
    },
    contentType: "application/json"
})
 
/* The ServeBrowserify that is returned is a function that takes
  options (which are passed to findResource & compile). It also
  takes default options like `cache` and `gzip` used to enable
  HTTP/compilation caching and gzipping support.
*/
 
http.createServer(ServeBrowserify({
    cache: true,
    gzip: true,
    root: path.join(__dirname, "scripts")
}))

Installation

npm install compiled-files

Contributors

  • Raynos

MIT Licenced

Readme

Keywords

none

Package Sidebar

Install

npm i compiled-files

Weekly Downloads

8

Version

0.1.1

License

none

Last publish

Collaborators

  • raynos