Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

istatic

Package Overview
Dependencies
Maintainers
1
Versions
16
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

istatic

Add compressed inline css and scripts to your html, but write them as seperated files.

  • 0.3.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
10
increased by66.67%
Maintainers
1
Weekly downloads
 
Created
Source

Inline Static Files for your template

Why this?

  1. Add compressed inline css and scripts to your html, but write them as seperated files.
  2. Access template's variables from a JS file.

For concat and serve external css/jss, go have a look at autostatic.

Usage:

var istatic = require('istatic')
va app = express.createServer()

app.locals({
  istatic: istatic.serve({ compress: false })
})

Available options for istatic.server([options]) are:

namedescriptiondefault
rootThe root of your inline static files. process.cwd() + '/public/'
ttlBy 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, then the cache will be cleared every that much of time. undefined
charsetThe charset of your static files. utf-8
compressWhether to compress the included contents. false
cacheWhether to cache file contents. true
debug When set to true, will output the absolute path of included file. And `compress` & `cache` will all be set to `false`. process.env.DEBUG
jsOptions passed to UglifyJS, for compressing JS files. undefined
cssOptions passed to clean-css undefined

For css and js options, you can also define a js.filter or css.filter, to do some extra filtering (like remove console.log()) before compressing.

eg.

{
  js: {
    filter: function(str) {
      return str.replace(/console.log(.+?)/, '')
    }
  }
}

In template:

Now you can include static files in your template like this:

   html
    title Sweet Page.
    head
     style.
       #{istatic(filename)}

filename is the path of your static file.

Templates' Local Variables:

Access template's render context (locals) inside your JS file, in the form you already very familiar with:

#{data.title}

You can even excecute a local funtion just like what you will do in html templates:

#{usr.getId('haha...')}

Attention: no matter what templating language you are using, you must always use this syntax in your static files. And don't put {} inside the curly braces. This is for performance consideration.


API

These APIs are not for templates.

istatic.serve([options])

Return a function of istatic(filename, [options]), for reading files.

The function is typically used as a template helper, the signature is:

istatic(filename)

Return the string content of given file. If filename begins with a '/', the real path will be process.cwd() + filename. Otherwise, the file will be looked up from the options.root, as you configured in istatic.serve(options).

istatic.default(options)

Explicitly set default options for any other istatic.serve() calls. The default options may have been set implicitly during the first call of istatic.serve(options).

Example

In /app.js:

var express = require('express')
var istatic = require('istatic')

var app1 = express.createServer()
var app2 = express.createServer()

app1.locals({
  istatic: istatic.serve()
})
app2.locals({
  istatic: istatic.serve({ compress: false })
})

var compressed_css = istatic.uglify.css('.class1 { font: Arial }');
var compressed_js = istatic.uglify.js('// some javascript codes..')

// 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:

script
  !{istatic('js/log_user.js')}

In /public/js/log_user.js:

var user = "#{user}"
user && $.post('/log', { user: user })

Licence

(The MIT License)

Copyright (c) 2012 Jesse Yang

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Keywords

FAQs

Package last updated on 17 Dec 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc