Comparing version 2.1.4 to 2.2.0
@@ -14,3 +14,3 @@ USAGE | ||
--global <name> Name of the global to export. Default: "Foo" | ||
--global <name> Name of the global to export. Default: "App" | ||
@@ -17,0 +17,0 @@ --basepath <path> Base path for the list of files. Default: process.cwd() |
34
index.js
@@ -130,2 +130,36 @@ var os = require('os'), | ||
}; | ||
// Express Middleware | ||
API.middleware = function (opts) { | ||
// -- Set some sane defaults | ||
opts = opts || {}; | ||
opts.include = opts.include || './lib'; | ||
opts.basepath = opts.basepath || (Array.isArray(opts.include) ? opts.include[0] : opts.include); | ||
opts.main = opts.main || 'index.js'; | ||
// -- Create an instance of the API to use | ||
var glue = new API() | ||
.include(opts.include) // Use API function to define | ||
.basepath(opts.basepath) // Use API function to define | ||
// -- All other options are set by clobbering the glue.options hash | ||
Object.keys(opts).forEach(function (key) { | ||
glue.set(key, opts[key]); | ||
}); | ||
// -- Middleware to return | ||
return function (req, res, next) { | ||
// -- Return all non GET requests | ||
if('GET' !== req.method) return next(); | ||
// -- Set content-type | ||
res.set('Content-Type', 'application/javascript'); | ||
// -- Render file and pipe to response | ||
glue.render(res); | ||
} | ||
}; | ||
API.prototype.handler = function(regex, fn) {}; | ||
@@ -132,0 +166,0 @@ API.prototype.define = function(module, code) {}; |
@@ -32,6 +32,11 @@ var fs = require('fs'), | ||
function getMainFile(basepath, currentDirFiles) { | ||
var mainFile; | ||
if(currentDirFiles && exists(currentDirFiles, /\/package.json$/)) { | ||
var mainFile, | ||
packageJsonPath = path.normalize(basepath+'/package.json'); | ||
// use existsSync; for example, underscore has a .gitignore | ||
// that filters out the package.json file; we need to treat it as existing | ||
// even if it is excluded | ||
if(fs.existsSync(packageJsonPath)) { | ||
// 1. package.json | ||
var data = JSON.parse(fs.readFileSync(path.normalize(basepath+'/package.json'))); | ||
var data = JSON.parse(fs.readFileSync(packageJsonPath)); | ||
@@ -168,2 +173,4 @@ // Note: lookups from list.files are intentional: need to look at all the available | ||
// console.log(require('util').inspect(list.packages, null, 5, true), require('util').inspect(packages, null, 5, true)); | ||
list.packages = packages; | ||
@@ -170,0 +177,0 @@ }; |
@@ -47,3 +47,3 @@ var path = require('path'); | ||
var exportVariableName = options['export'] || 'foo'; | ||
var exportVariableName = options['export'] || 'App'; | ||
@@ -50,0 +50,0 @@ // transforms |
@@ -25,3 +25,3 @@ var fs = require('fs'), | ||
// unpack options | ||
var exportVariableName = options['export'] || 'foo', | ||
var exportVariableName = options['export'] || 'App', | ||
packageRootFileName, | ||
@@ -28,0 +28,0 @@ // normalize basepath |
{ | ||
"name": "gluejs", | ||
"description": "Build CommonJS modules for the browser via a chainable API", | ||
"version": "2.1.4", | ||
"version": "2.2.0", | ||
"author": { | ||
@@ -6,0 +6,0 @@ "name": "Mikito Takada", |
@@ -19,3 +19,3 @@ # gluejs V2 | ||
## Usage example | ||
## Usage example: console | ||
@@ -32,2 +32,19 @@ gluejs \ | ||
## Usage example: express middleware (new in v2.2!) | ||
var express = require('express'), | ||
glue = require('gluejs'), | ||
app = express(); | ||
app.use(express.static(__dirname + '/index.html')); | ||
app.use('/app.js', glue.middleware({ | ||
include: [ './lib', './node_modules/microee/' ] | ||
})); | ||
app.listen(3000); | ||
console.log('Listening on port 3000'); | ||
`glue.middleware()` can accept most of the options supported by the Node API. | ||
## Using the resulting file | ||
@@ -56,2 +73,8 @@ | ||
## What's new in v2.2 | ||
Note: if you are upgrading from an older version: the default value for `--global` is now `App` rather than `Foo`. | ||
gluejs v2.2 adds Express middleware for serving gluejs packages, thanks [@JibSales](https://github.com/JibSales). | ||
## What's new in v2.1 | ||
@@ -96,5 +119,5 @@ | ||
--command 'uglifyjs --no-copyright' \ | ||
--global Foo \ | ||
--global App \ | ||
--main lib/index.js \ | ||
--out dist/foo.js | ||
--out app.js | ||
@@ -149,3 +172,3 @@ With that option, all files are piped through `uglifyjs` before writing to disk. | ||
--out File to write. Default: stdout | ||
--global Name of the global to export. Default: "Foo" | ||
--global Name of the global to export. Default: "App" | ||
--basepath Base path for relative file paths. Default: process.cwd() | ||
@@ -244,3 +267,3 @@ --main Name of the main file/module to export. Default: index.js | ||
`--global <name>` / `.export(name)`: Name of the global name to export. Default: `foo` (e.g. `window.foo`) | ||
`--global <name>` / `.export(name)`: Name of the global name to export. Default: `App` (e.g. `window.App`) | ||
@@ -261,2 +284,21 @@ ## --basepath | ||
## .middleware | ||
`.middleware({ include: ... })`: Returns a Express/Connect compatible request handler. | ||
For example: | ||
app.use('/js/app.js', glue.middleware({ | ||
include: __dirname + '/lib' | ||
})); | ||
Or at the route level: | ||
app.use(app.router); | ||
app.get('/js/app.js', glue.middleware({ | ||
include: __dirname + '/lib' | ||
})); | ||
Using full paths is recommended to avoid ambiguity. `basepath` defaults to the `include` path, and `main` defaults to `index.js`. | ||
## --replace | ||
@@ -263,0 +305,0 @@ |
Sorry, the diff of this file is not supported yet
90383
1570
494