Giter VIP home page Giter VIP logo

cassette-express's Introduction

#Cassette Express

The short version: CI friendly Javascript asset bundling for those of us who don't want to rewrite our client side Javascript as CommonJS modules

The long version:

  • Reference a Javascript file in your template. Get it, and all dependencies, in the right order, in the page.
  • CI Friendly. No separate build process. No command line tools.
  • Declare dependencies as comments with relative paths, i.e, // @reference ../lib/jquery.1.7.2.js
  • Reference entire directories instead of individual files with // @reference ../lib
  • Debug mode returns individual javascript files to the browser and dynamically responds to your changes.
  • Production mode bundles all javascript into a single minified download using Uglify.

Because this isn't a CommonJS based system, you can use all the same javascript libraries and files you're already using. You can even use it with Ender, too, by referencing the unminified version, ie., // @reference ../lib/ender.js.

##Status

Working, ready for real world testing

##Known issues

  • Some test coverage for debug and production mode specific behaviours not there yet
  • Insufficient documentation.
  • No ability to reference external scripts yet
  • Insufficient real world testing to know what the known issues really are.

##Background

Cassette-Express is an adaptation of Andrew Davey's Cassette (https://github.com/andrewdavey/cassette), a .net package which helps developers manage CSS, Javascript, Coffeescript assets. It is amazingly useful. I wanted to use the same client side javascript in Node projects and discovered the same functionality - working with any javascript, not just CommonJS modules - didn't exist.

##Installing

npm install cassette-express

##Implementation

In typical ExpressJS app.js file, assuming you're still using the default /public/javascripts

// debug mode, every request the files are checked for changes & individual file downloads.
var cassette = require('cassette-express')();

// or production mode, generated once per restart. One single minified download.
var cassette = require('cassette-express')({ mode : 'production' });

A little later on in app.js we make sure we have access to Cassette inside the templates..

app.set('view options', {
	assets : cassette.middleware()
});

Then in our template, i.e, layout.jade:

!= assets.useAsset('/app/client-app.js')

Which, in debug mode, would output something like...

<script src="/javascripts/lib/jquery.js"></script>
<script src="/javascripts/lib/jquery-someplugin.js"></script>
<script src="/javascripts/lib/jquery-some-other-plugin.js"></script>
<script src="/javascripts/lib/underscore.js"></script>
<script src="/javascripts/lib/my-own-library-stuff.js"></script>
<script src="/javascripts/app/app-namespace.js"></script>
<script src="/javascripts/app/features/navigator.js"></script>
<script src="/javascripts/app/features/content.js"></script>
<script src="/javascripts/app/client-app.js"></script>

And in production mode all those scripts are merged, minified and then you get...

<script src="/javascripts/cassette/AEEE546E7B7C7BCEBC.min.js"></script>

By default the compiled files are put in /public/javascripts/cassette.

The gathering and sorting of dependencies is done automatically. In debug mode bundles are reassembled if there are any changes to teh sources. In production mode, node must be restarted before a new minified bundle is generated.

##Example

In the repo, in the EXAMPLE folder, there's a skeleton Express.js application. The files of interest are app.js, views/index.jade and the javascript files in /public/javascripts. If you go into this folder, do npm install then node app.js you can test it's working.

##Cassette Options

The only real configuration is to do with paths.

// this...
var cassette = require('cassette-express')();
// is a shortcut for..
var cassette = require('cassette-express')({
		assetsPath : '/public/javascripts',
		outputPath : '/javascripts',
		buildPath : '/cassette',
		mode : 'debug'
	})

If your javascript assets are somewhere else, this is how you would configure that. It's probably best to leave the buildPath as '/cassette' - this makes your minified bundles output to /public/javascripts/cassette by default.

This API is pretty horrible. Sorry.

##Next Steps

  • Integration with Amazon S3 to allow automatic upload of minified bundles
  • Add a dependency by referencing an external file, have that file downloaded and added to your bundle.
  • If people want it, I could add automatic Coffeescript compiling. So far no-one wants it.

Differences between Cassette-Express and Cassette MVC

  • Bundles are automatically generated by requesting any particular resource inside templates. You don't need to pre-reference them.
  • Bundles will always be one single file in production mode, not multiple files depending on how they were pre-referenced.
  • No automagic coffeescript compiling. Poor Coffeescript.
  • No stylesheet merging/compiling.
  • Significantly less features. Ha.

cassette-express's People

Contributors

avnerus avatar kevinkuebler avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

cassette-express's Issues

Windows paths not handled correctly

Thanks for creating this package! I'm hoping to use it on a production site sometime soon. However, there's a blocking issue for us which is that it doesn't work when using node on Windows, and it looks like the problems are related to file paths. Starting with just a clone of the repo and changing to the Example directory, I ran npm install and then node app.js. This immediately throws the following error:

fs.js:400
  return binding.readdir(pathModule._makeLong(path));
Error: ENOENT, no such file or directory 'D:\node_modules'
    at Object.readdirSync (fs.js:400:18)
    at base.scanDirectory (D:\Dev\Cassette-Express\EXAMPLE\node_modules\cassette-express\lib\manifest.js:32:18)
    at base.reset (D:\Dev\Cassette-Express\EXAMPLE\node_modules\cassette-express\lib\manifest.js:122:8)
    at base.init (D:\Dev\Cassette-Express\EXAMPLE\node_modules\cassette-express\lib\manifest.js:24:8)

The problem stems from the call this.scanDirectory( node );on line 119 of manifest.js. The call to path.relative on the preceding line results in a path like ..\..\..\..\..\node_modules in my case (I have the repo located at D:\dev\cassette-express on my system). So that resolves to D:\node_modules which doesn't exist.

I'm not sure exactly why it's trying to scan the node_modules folder, but if I take that line out the app will run. But then another path problem becomes apparent. If I browse to localhost:3000 the page loads but none of the scripts get loaded. And that's because if you look at the source, it's rendering script tags with a src attribute like this:

src="\javascripts\lib\underscore.js"

Notice that the src is using \ instead of / which is preventing the browser from loading them.

If I can figure out what exactly needs changed in the source to fix these issues, I'll be happy to issue a pull request. Not sure how long it will take me to figure out what's going on though. :-)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.