compiled-files

statically serve compiled files with gzip & cache

compiled-files

statically serve compiled files with gzip & cache

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(reqresopts) {
        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 
    compilefunction (locationoptscallback) {
        resolve(location, function (errfileUri) {
            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")
}))

npm install compiled-files

  • Raynos