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

js-loader

Package Overview
Dependencies
Maintainers
0
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

js-loader

On-the-fly javascript contacatenator, minifier and dependency resolver for client-side JS

  • 0.1.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
313
increased by85.21%
Maintainers
0
Weekly downloads
 
Created
Source

#Node JS Loader (client-side)

This is a web-service which will serve your JS files for you. It does a few spiffy things:

  • Concatenate a set of JS files on-the-fly so you only download one file
  • Automatically resolve dependencies

This allows you to accomplish the following objectives:

  • Keep JS tidy in as many separate files as you want without peformance concerns
  • Get the benefit of concatenated JS without having to constantly update the concatenated files
  • Get the benefit of dependency resolution, like you would with any other language (if a.js depends on b.js, you only need to ask for a.js)

This app is written in node.js, but is really not for node.js. You can use it for any web project. It is for clinet-side javascript, not server-side.

#Installation

npm install js-loader@latest

#Synopsis

##Using as a standalone server

Usage: jsloader ADDRESS PORT JS_SOURCE_DIR [JS_SOURCE_DIR...]

Run the server

jsloader 127.0.0.1 1234 /path/to/js

Request your files

<script type="text/javascript" src="http://js.mysite.com/?sources=a.js,b.js,c.js"></script>

And if the first line of c.js looks like this:

//require d.js

Then d.js will be included in the output.

You can also request minfied output

<script type="text/javascript" src="http://js.mysite.com/?sources=a.js,b.js,c.js&minify=1"></script>

And you can have multiple source directories

jsloader 127.0.0.1 1234 /path/to/js1 /path/to/js2

##Advanced integration with Node.js projects

If your project is written in Node.js and you are using connect, then you have two more options. First, set up the connect middleware.

app.configure(function() {
    ...
    require('js-loader').JSLoader.connect('/js', ['/path/to/js'])
    ...
});

The first parameter is the pathname that the jsloader should handle. The second is an array of javascript source directories. With this in place, you can use your existing connect server to serve your js.

<script type="text/javascript" src="http://www.mysite.com/js?sources=a.js,b.js,c.js"></script>

The connect middleware will also make the jsloader instance available on all requests so you can request JavaScript content on-the-fly and embed it directly in the page. To do this, you'll want to generate the JavaScript in your controller and pass it to your view.

var minify = true;
req.jsloader.getContent(['cool.js'], function(err, jsCode) {
    res.render('myview.ejs', {
        locals: { jsCode: jsCode }   
    }); 
}, minify);

Then, in your view, display the content. Be sure not to escape it.

<script type="text/javascript"><%- jsCode %></script>

#Planned features

  • Caching
  • GZip (maybe use Connect?)
  • Conditional Get support (maybe use Connect?)

FAQs

Package last updated on 16 Feb 2011

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