Comparing version 0.0.7 to 0.0.8
@@ -9,35 +9,11 @@ var uglify = require('./uglify.js'); | ||
// Add the `istatic` helper for express to use. | ||
function bind(app, options) { | ||
// inline static files | ||
var read = theRead(options || defaultOptions); | ||
if (!defaultRead) defaultRead = read; | ||
app.dynamicHelpers({ | ||
// inline static | ||
istatic: function(req, res) { | ||
return function() { | ||
var locals = this; | ||
var str = read.apply(this, arguments) || ''; | ||
// replace inlined arguments | ||
if (str.indexOf('#{') > -1) { | ||
str = str.replace(reg_val, function(m0, m1) { | ||
var fn = new Function('locals', 'with (locals) { return ' + m1 + '; }'); | ||
return fn(locals); | ||
}); | ||
} | ||
return str; | ||
}; | ||
} | ||
}); | ||
return exports; | ||
} | ||
var exports = function(path, forceReload, options) { | ||
if (options) defaultOptions = options; | ||
if (!defaultRead) defaultRead = theRead(defaultOptions); | ||
return defaultRead(path, forceReload); | ||
var exports = function(path, options) { | ||
if (!defaultRead) { | ||
if (options) defaultOptions = options; | ||
defaultRead = theRead(defaultOptions); | ||
} | ||
return defaultRead(path, options); | ||
}; | ||
exports.uglify = uglify; | ||
@@ -50,5 +26,38 @@ exports.default = function(options) { | ||
exports.enable = bind; | ||
exports.uglify = uglify; | ||
// inline static files | ||
exports.serve = function(options) { | ||
// the local read method | ||
var read; | ||
if (!defaultRead) { | ||
defaultRead = theRead(options || defaultOptions); | ||
read = defaultRead; | ||
} else { | ||
read = options ? theRead(options) : defaultRead; | ||
} | ||
// compiled functions cache | ||
var compiled_fns = {}; | ||
return function(path, opts) { | ||
// this is the express templates' locals | ||
if (path in compiled_fns) return compiled_fns[path](this); | ||
var str = read(path, opts) || ''; | ||
// replace inlined arguments | ||
if (str.indexOf('#{') > -1) { | ||
compiled_fns[path] = function(locals) { | ||
return str.replace(reg_val, function(m0, m1) { | ||
var fn = new Function('locals', 'with (locals) { return ' + m1 + '; }'); | ||
return fn(locals); | ||
}); | ||
}; | ||
return compiled_fns[path](this); | ||
} | ||
return str; | ||
}; | ||
}; | ||
module.exports = exports; |
@@ -20,4 +20,3 @@ var fs = require('fs'); | ||
if (opt && opt.filter) { | ||
str = opt.filter(str); | ||
delete opt.filter; | ||
str = opt.filter(str) || ''; | ||
} | ||
@@ -98,10 +97,14 @@ if (!doCompress) return str; | ||
default: | ||
if (filetype == 'css' || filetype == 'js') | ||
if (filetype == 'css' || filetype == 'js') { | ||
str = _uglify(filetype, str, opt[filetype], doCompress); | ||
} | ||
} | ||
if (debug) str = '\n/* == istatic: ' + filepath + ' == */\n' + str; | ||
if (_doCache) cache[filepath] = global_cache[filepath] = str; | ||
return str; | ||
var ret = new String(str); | ||
// cache as a string object | ||
if (_doCache) cache[filepath] = global_cache[filepath] = ret; | ||
return ret; | ||
}; | ||
}; | ||
{ | ||
"name": "istatic", | ||
"version": "0.0.7", | ||
"version": "0.0.8", | ||
"homepage": "http://github.com/ktmud/express-istatic", | ||
@@ -5,0 +5,0 @@ "description": "Add compressed inline css and scripts to your html, but write them as seperated files.", |
153
README.md
@@ -9,2 +9,4 @@ # Inline Static Files for express / connect / nodejs | ||
You may also like to have a loot at [autostatic](https://github.com/ktmud/autostatic). | ||
## Usage: | ||
@@ -16,43 +18,84 @@ | ||
istatic.enable(app, { compress: false }); | ||
app.helpers({ | ||
istatic: istatic.serve({ compress: false }) | ||
}); | ||
``` | ||
The parameter passed to `istatic.serve` is an options object, which is optional. | ||
The second parameter is an options object, which is optional. Available options are: | ||
Available options are: | ||
### compress | ||
<table> | ||
<tr> | ||
<th>name</th> | ||
<th>description</th> | ||
<th>default</th> | ||
</tr> | ||
<tr> | ||
<th>root</th> | ||
<td>The root of your inline static files.</td> | ||
<td> | ||
process.cwd() + '/public/' | ||
</td> | ||
</tr> | ||
<tr> | ||
<th>ttl</th> | ||
<td>By default, the contents of your static files are cached in memory forever, until the process dies. You can set the <code>ttl</code> to a number of seconds, so the cache will be cleared every that much of time. | ||
</td> | ||
<td> | ||
undefined | ||
</td> | ||
</tr> | ||
<tr> | ||
<th>charset </th> | ||
<td>The charset of your static files.</td> | ||
<td> | ||
utf-8 | ||
</td> | ||
</tr> | ||
<tr> | ||
<th>compress </th> | ||
<td>Whether to compress the included contents.</td> | ||
<td> | ||
true | ||
</td> | ||
</tr> | ||
<tr> | ||
<th>debug</th> | ||
<td>When set to true, will output the absolute path of included file.</td> | ||
<td> | ||
false || <code>process.env.DEBUG</code> | ||
</td> | ||
</tr> | ||
<tr> | ||
<th>js</th> | ||
<td>The options object for compressing a js file. It will be passed to <a href="https://github.com/mishoo/UglifyJS">UglifyJS</a>. </td> | ||
<td> | ||
undefined | ||
</td> | ||
</tr> | ||
<tr> | ||
<th>css</th> | ||
<td>The options object for compressing a css file. It will be passed to <a href="https://github.com/fmarcia/UglifyCSS">UglifyCSS</a></td> | ||
<td> | ||
undefined | ||
</td> | ||
</tr> | ||
</table> | ||
Whether to compress the css or js. Default: `true`. | ||
For css and js options, you can define an `js.filter` or `css.filter`, to do some filtering(like remove `console.log()`) before compressing. | ||
### showPath | ||
eg. | ||
Whether to include the file's path in the output. Default: `false`. | ||
```javascript | ||
{ | ||
js: { | ||
filter: function(str) { | ||
return str.replace(/console.log(.+?)/, ''); | ||
} | ||
} | ||
} | ||
``` | ||
### root | ||
## In template: | ||
The root of your inline static files. Default: `process.cwd() + '/public/'`. | ||
### ttl | ||
By default, the contents of your static files are cached in memory forever, until the process dies. You can set the `ttl` to a number of seconds, so the cache will be cleared every that much of time. | ||
### charset | ||
The charset of your static files. Default: `utf-8`. | ||
### js | ||
The options object for compressing a js file. It will be passed to [UglifyJS](https://github.com/mishoo/UglifyJS). | ||
Default: `undefined`. | ||
### css | ||
The options object for compressing a css file. It will be passed to [UglifyCSS](https://github.com/fmarcia/UglifyCSS). | ||
Default: `undefined`. | ||
For css and js options, you can define an `js.filter` or `css.filter`, to do some filtering(like remove `console.log()`) before compressing. | ||
## Use inside template: | ||
Now you can include static files in your template like this: | ||
@@ -73,3 +116,3 @@ | ||
- istatic_opt = { showPath: (DEBUG ? true : false) } | ||
istatic_opt = { showPath: (DEBUG ? true : false) } | ||
script | ||
@@ -80,3 +123,3 @@ #{istatic('js/my.js', istatic_opt)} | ||
## Get access to templates' local variables: | ||
## Templates' Local Variables: | ||
@@ -104,15 +147,15 @@ Just get in touch with them in the form you already very familiar with: | ||
When passing `options`, these options will be saved as default options for any other later `istatic` or `istatic.enable` calls. | ||
Everytime you call `istatic` directly, it the `options` is given, these options will be saved as default options for any other later `istatic` or `istatic.serve` calls. | ||
But when you call `istatic(filename, options)` in a template, the options **will not** be save as default options. | ||
But when you call `istatic(filename, options)` from a template, the options **will not** be saved as default options. | ||
APIs listed below are not suitable for an inside template call. | ||
### istatic.enable(app) | ||
### istatic.serve([options]) | ||
To enable the `istatic` helper for an express `app`. | ||
To return a function of `istatic(filename, [options])`, to read the file. This is typically used as an express helper. | ||
### istatic.default(options) | ||
Set default options for `istatic.enable` and `istatic.inline`. This can be implictly done by call `istatic(filename, [forceReload, options])` and pass the `options` object. | ||
Specificly set default options for `istatic('filepath')`, which will be set implicitly at the first call of `istatic('filename', options)` or `istatic.serve(options)`. | ||
@@ -129,2 +172,4 @@ ### istatic.uglify.css(str, [options]) | ||
In **/app.js**: | ||
```javascript | ||
@@ -137,3 +182,8 @@ var express = require('express'); | ||
istatic.default({ compress: false }).enable(app1).enable(app2); | ||
app1.helpers({ | ||
istatic: istatic.serve() | ||
}); | ||
app2.helpers({ | ||
istatic: istatic.serve({ compress: false }) | ||
}); | ||
@@ -143,5 +193,26 @@ var compressed_css = istatic.uglify.css('.class1 { font: Arial; }'); | ||
// will be compressed | ||
var str_pinyin_js = istatic('/utils/pinyin.js'); | ||
app1.get('/example', function(req, res, next) { | ||
res.render('example.jade', { | ||
user: req.user | ||
}); | ||
}); | ||
``` | ||
In **/view/example.jade**: | ||
```haml | ||
script | ||
!{istatic('js/log_user.js')} | ||
``` | ||
In **/public/js/log_user.js**: | ||
```javascript | ||
var user = "#{user}" | ||
user && $.post('/log', { user: user }); | ||
``` | ||
## Licence | ||
@@ -148,0 +219,0 @@ |
12867
151
221