Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Dev mode setting #4

Merged
merged 2 commits into from

2 participants

Kevin Kuebler Charlotte Gore
Kevin Kuebler

This change coerces the model config value to 'debug' when 'development' is passed in. This is to allow the user to do this in their app.js:

var cassette = require('cassette-express')({mode: app.settings.env});

Since express sets the env setting to 'development' by default, this allows us to automatically get the correct behavior from cassette-express regardless of which env we're in, without changing any code.

An alternative to this change, would be to reverse the if check in the useAsset function in cassette.js and explicitly check for 'production', assuming any other value to mean debug.

Aaaaaannnd ... I screwed up the diff again. This time on cassette.js. The line I changed was line #39. Sorry about that - I'll get it right eventually. :-)

kevinkuebler added some commits
Kevin Kuebler kevinkuebler Fix src output on Windows systems
On Windows systems, the <script> tags were being output with \'s in the
src attribute instead of /'s.
768ad3b
Kevin Kuebler kevinkuebler Allow app env to be passed in for mode config.
Cassette defaults the mode to 'debug' so if anything other than 'debug'
is passed in for the mode config value, it will use the production mode.
Since express uses 'development' as it's env by default, this will
convert 'development' to 'debug' when saving the mode config value.
d96793e
Charlotte Gore CharlotteGore merged commit 038a803 into from
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Aug 9, 2012
  1. Kevin Kuebler

    Fix src output on Windows systems

    kevinkuebler authored
    On Windows systems, the <script> tags were being output with \'s in the
    src attribute instead of /'s.
Commits on Aug 10, 2012
  1. Kevin Kuebler

    Allow app env to be passed in for mode config.

    kevinkuebler authored
    Cassette defaults the mode to 'debug' so if anything other than 'debug'
    is passed in for the mode config value, it will use the production mode.
    Since express uses 'development' as it's env by default, this will
    convert 'development' to 'debug' when saving the mode config value.
This page is out of date. Refresh to see the latest.
5 EXAMPLE/app.js
View
@@ -5,11 +5,10 @@
var express = require('express')
, routes = require('./routes')
-
- // require cassette...
- , cassette = require('cassette-express')(); // we added an entry to package.json here..
+ , cassette
var app = module.exports = express.createServer();
+var cassette = require('cassette-express')({mode: app.settings.env});
// make the cassette middleware available inside templates..
app.locals( {assets : cassette.middleware()} );
4 lib/assembly.js
View
@@ -25,7 +25,7 @@ var Assembly = base.createChild().addInstanceMethods({
this.length++;
this.merged += '\n' + asset.data;
- var src = path.normalize(this.outputPath + '/' + asset.name);
+ var src = this.outputPath + asset.name;
if( /node_modules/.test(src) ){
@@ -36,7 +36,7 @@ var Assembly = base.createChild().addInstanceMethods({
fs.writeFileSync(this.assetsPath + this.buildPath + '/' + hash + ".js", asset.data);
- src = path.normalize(this.outputPath + this.buildPath + '/' + hash + ".js");
+ src = this.outputPath + this.buildPath + '/' + hash + ".js";
}
2  lib/asset.js
View
@@ -12,7 +12,7 @@ var Asset = base.createChild().addInstanceMethods({
init : function(name, lpath, assets, stat, nodeModules){
this.name = path.normalize(lpath + '/' + name);
-
+ this.webName = lpath.replace('\\', '/') + '/' + name;
this.path = path.normalize(lpath + '/');
assetsPath = assets;
210 lib/cassette.js
View
@@ -1,105 +1,105 @@
-var base = require('base-framework'),
- _ = require('underscore'),
- Manifest = require('./manifest.js');
-
-// some defaults for an Express environment.
-var assetsPath = './public/javascripts/';
-var nodeModulesPath = './node_modules';
-var outputPath = '/javascripts';
-var nodeModules = '/node_modules';
-var buildPath = './public/javascripts/cassette';
-
-
-var cassette = base.createChild().addInstanceMethods({
-
- init : function( config ){
-
- if(config){
-
- if(config.assetsPath){
-
- assetsPath = config.assetsPath;
-
- }
-
- if(config.outputPath){
-
- outputPath = config.outputPath;
-
- }
-
- if(config.buildPath){
-
- buildPath = config.buildPath;
-
- }
-
- if(config.mode){
-
- this.mode = config.mode;
-
- }
-
- if(config.nodeModules){
-
- nodeModules = config.nodeModules;
-
- }
-
-
- if(config.scanNodeModules){
-
- this.scanNodeModules = config.scanNodeModules;
-
- } else {
-
- this.scanNodeModules = false;
-
- }
-
- } else {
-
- this.mode = 'debug';
- this.scanNodeModules = false;
-
- }
-
- this.manifest = Manifest(assetsPath, outputPath, nodeModules, this.scanNodeModules);
- this.assemblies = {};
-
- return this;
-
- },
-
- middleware : function(){
-
- var self = this;
-
- return {
-
- useAsset : function( asset ){
-
- var str;
-
- if(self.mode==='debug'){
-
- str = self.manifest.getAssembly( asset, 'forceCheck' ).getDebugTags( outputPath );
-
- } else {
-
- str = self.manifest.getAssembly( asset ).getTag( outputPath );
-
- }
-
- return str;
-
- }
-
- };
-
- }
-
-});
-
-
-exports = module.exports = cassette;
+var base = require('base-framework'),
+ _ = require('underscore'),
+ Manifest = require('./manifest.js');
+
+// some defaults for an Express environment.
+var assetsPath = './public/javascripts/';
+var nodeModulesPath = './node_modules';
+var outputPath = '/javascripts';
+var nodeModules = '/node_modules';
+var buildPath = './public/javascripts/cassette';
+
+
+var cassette = base.createChild().addInstanceMethods({
+
+ init : function( config ){
+
+ if(config){
+
+ if(config.assetsPath){
+
+ assetsPath = config.assetsPath;
+
+ }
+
+ if(config.outputPath){
+
+ outputPath = config.outputPath;
+
+ }
+
+ if(config.buildPath){
+
+ buildPath = config.buildPath;
+
+ }
+
+ if(config.mode){
+
+ this.mode = config.mode === 'development' ? 'debug' : config.mode;
+
+ }
+
+ if(config.nodeModules){
+
+ nodeModules = config.nodeModules;
+
+ }
+
+
+ if(config.scanNodeModules){
+
+ this.scanNodeModules = config.scanNodeModules;
+
+ } else {
+
+ this.scanNodeModules = false;
+
+ }
+
+ } else {
+
+ this.mode = 'debug';
+ this.scanNodeModules = false;
+
+ }
+
+ this.manifest = Manifest(assetsPath, outputPath, nodeModules, this.scanNodeModules);
+ this.assemblies = {};
+
+ return this;
+
+ },
+
+ middleware : function(){
+
+ var self = this;
+
+ return {
+
+ useAsset : function( asset ){
+
+ var str;
+
+ if(self.mode==='debug'){
+
+ str = self.manifest.getAssembly( asset, 'forceCheck' ).getDebugTags( outputPath );
+
+ } else {
+
+ str = self.manifest.getAssembly( asset ).getTag( outputPath );
+
+ }
+
+ return str;
+
+ }
+
+ };
+
+ }
+
+});
+
+
+exports = module.exports = cassette;
720 lib/manifest.js
View
@@ -1,360 +1,360 @@
-var base = require('base-framework'),
- path = require('path'),
- fs = require('fs'),
- _ = require('underscore'),
-
- Asset = require('./asset.js'),
- Assembly = require('./assembly.js'),
- sorter = require('./sorter.js')
- Manifest;
-
-// var m = require('./lib/manifest.js')(process.env.PWD + '/test/examples');
-// m.createAssembly('/NonCircularFileRefs/a.js')
-
-// Here is the Bundle object.
-var Manifest = base.createChild().addInstanceMethods({
-
- init : function( assetsPath, outputPath, nodeModules, scanNodeModules ){
-
- this.assetsPath = path.normalize(assetsPath);
- this.outputPath = outputPath; // we don't normalize this! Windows style would be bad, mkay.
- this.nodeModules = nodeModules;
- this.buildPath = path.normalize("/cassette");
- this.scanNodeModules = scanNodeModules;
-
- this.reset();
-
- return this;
-
- },
-
- // the the files
- scanDirectory : function( currentPath ){
-
- var files = fs.readdirSync( this.assetsPath + currentPath);
-
- if(!this.bundleIndex[currentPath]){
-
- this.bundleIndex[currentPath] = {
- files : [],
- bundles : [],
- mtime : fs.statSync(this.assetsPath + currentPath).mtime.getTime()
- };
-
- };
-
- _.each(files, function(file){
-
- var currentFile = this.assetsPath + currentPath + '/' + file,
- stat = fs.statSync(currentFile),
- src = '',
- dependency;
-
- if(stat.isFile()){
-
- if(/\.js$/.test(file)){
-
- var asset = Asset(file, currentPath, this.assetsPath, stat, this.nodeModules );
-
- this.assetIndex[ asset.name ] = asset;
- this.bundleIndex[ currentPath ].files.push( asset.name );
-
- }
-
-
- }else if(stat.isDirectory()){
-
- if( path.normalize( currentPath + '/' + file ) !== this.buildPath ){
-
- this.bundleIndex[currentPath].bundles.push( path.normalize(currentPath + '/' + file) );
- this.scanDirectory(path.normalize(currentPath + '/' + file));
-
- }
-
- }
-
-
- }, this);
-
- return this;
-
- },
-
- assetsChanged : function(){
-
- // STUB: go through all the assets/directories, detect changes, refresh if necessary.
-
- return false;
-
- },
-
- convertToSortable : function( key ){
-
- var item = {
- key : key,
- dependsOn : []
-
- };
-
- var single = this.assetIndex[key].fileDependencies();
-
- var filesFromBundles = [];
-
- _.each(this.assetIndex[key].bundleDependencies(), function(bundle){
-
- filesFromBundles = filesFromBundles.concat(this.getFilesFromBundle(bundle));
-
- }, this);
-
- item.dependsOn = _.uniq(single.concat(filesFromBundles));
-
- return item;
-
- },
-
- reset : function(){
- this.assemblies = {};
- this.assetIndex = {};
- this.bundleIndex = {};
-
- var node = path.relative(this.assetsPath, this.nodeModules);
-
- if(this.scanNodeModules){
-
- this.scanDirectory( node );
- }
- this.scanDirectory('');
- },
-
- getAssembly : function( key, forceCheck ){
-
- var dirtyFiles = false;
-
- if(key){
-
- key = path.normalize(key);
-
- }else{
-
- throw new Error('No key specified for asset request');
-
- }
-
- if(this.assemblies[key] && typeof forceCheck==='undefined'){
-
- return this.assemblies[key];
-
- }
-
- if(forceCheck){
-
- _.each(this.assetIndex, function(asset){
-
- var hasChanged = asset.hasChanged();
-
- if(hasChanged){
-
- dirtyFiles = true;
-
- };
-
- });
-
-
-
- if(dirtyFiles===true || (this.fileSystemChanged()===true) ){
- // we invalidate all assemblies and get a fresh scan.
- this.reset();
-
- }
-
- }
-
- var list = [];
-
- _.each( // for each sorted element...
- sorter().sort(this.buildBundle( key ), function(item){
- return {
- key : item.key,
- dependsOn : item.dependsOn.slice(0)
- };
- }),
-
- // push the actual asset into the array...
- function( item ){
-
- list.push(this.assetIndex[item.key]);
-
- },this);
-
- this.assemblies[key] = Assembly(list, {
- outputPath : this.outputPath,
- assetsPath : this.assetsPath,
- buildPath : this.buildPath
- });
-
- return this.assemblies[key];
-
- },
-
- buildBundle : function( key ){
-
- var self = this; // keep a reference to this
-
- var single;
- var initialFiles = [];
-
- // an object to quickly check if we have a file.
- var lookup = {};
-
- // if it's a file...
- if(this.assetIndex[key]){
-
- // get the file requested...
- single = key;
-
- // add it to 'got'
- initialFiles.push(single);
- lookup[key] = this.assetIndex[single];
-
- if(_.isEmpty(this.assetIndex[key].fileDependencies()) && _.isEmpty(this.assetIndex[key].bundleDependencies()) ){
- // early exit: Assembly is a single file without dependences. We're done!
- return [this.convertToSortable(key)];
-
- }
- // else if it's a directory we know about..
- }else if(this.bundleIndex[key]){
-
- // get all the files from the directory requested (and the sub directories)
- initialFiles = this.getFilesFromBundle(key);
-
- _.each(initialFiles, function( file ){
-
- lookup[file] = this.assetIndex[file];
-
- }, this);
-
- // or we throw up.
- }else{
-
- throw new Error('No such asset: No files or directories found matching ' + key);
-
- }
-
- var filesThatNeedFiles = [];
- var finalList = [];
-
- // process 1: converting a collection of file keys to sortable objects (with keys an dependencies)
- _.each(initialFiles, function(file){
-
- filesThatNeedFiles = filesThatNeedFiles.concat(this.convertToSortable(file));
-
- }, this);
-
- // process 2 : find which items actually do need files...
- while(filesThatNeedFiles.length > 0){
-
- // pluck one from the filesThatNeedFiles
- var testItem = filesThatNeedFiles.splice(-1)[0];
-
- _.each(testItem.dependsOn, function( dependency, index){
-
- var done;
-
- if(!lookup[dependency]){
-
- // add the file to filesThatNeedFiles...
- if(this.assetIndex[dependency]){
-
- done = this.convertToSortable(dependency);
- lookup[done.key] = 1;
- filesThatNeedFiles.push( done )
-
- }
-
- }
-
- }, this);
-
- finalList.push(testItem);
-
- }
-
- return finalList;
-
- },
-
- getFilesFromBundle : function( key ){
-
- var self = this;
-
- return (function getList(id){
-
- var files = self.bundleIndex[id].files.slice(0); // get the files
-
- _.each(self.bundleIndex[id].bundles, function(bundle){
-
- files = files.concat( getList( bundle ) );
-
- }, this);
-
- return files;
-
- }(key));
-
- },
-
- fileSystemChanged : function(){
-
- var start = (new Date()).getTime();
- var changesDetected = false;
-
- _.each(this.bundleIndex, function(bundle, index){
-
- try{
-
- var current = fs.statSync(path.normalize(this.assetsPath + '/' + index)).mtime.getTime();
-
- if(current!==bundle.mtime ){
-
- changesDetected = true;
- bundle.mtime = current;
-
- }
-
- }catch(e){
-
- // we've got a missing directory.
- changesDetected = true;
-
- }
-
-
- }, this);
-
-
-
- if(changesDetected){
-
- return true;
-
- }else{
-
- console.log( 'Elapsed: ' + ((new Date()).getTime() - start) );
-
- return false;
-
- }
-
- },
-
- // unit testing helper..
- query : function(){
-
- return this.bundleIndex;
-
- }
-});
-
-exports = module.exports = Manifest;
-
+var base = require('base-framework'),
+ path = require('path'),
+ fs = require('fs'),
+ _ = require('underscore'),
+
+ Asset = require('./asset.js'),
+ Assembly = require('./assembly.js'),
+ sorter = require('./sorter.js')
+ Manifest;
+
+// var m = require('./lib/manifest.js')(process.env.PWD + '/test/examples');
+// m.createAssembly('/NonCircularFileRefs/a.js')
+
+// Here is the Bundle object.
+var Manifest = base.createChild().addInstanceMethods({
+
+ init : function( assetsPath, outputPath, nodeModules, scanNodeModules ){
+
+ this.assetsPath = path.normalize(assetsPath);
+ this.outputPath = outputPath; // we don't normalize this! Windows style would be bad, mkay.
+ this.nodeModules = nodeModules;
+ this.buildPath = "/cassette";
+ this.scanNodeModules = scanNodeModules;
+
+ this.reset();
+
+ return this;
+
+ },
+
+ // the the files
+ scanDirectory : function( currentPath ){
+
+ var files = fs.readdirSync( this.assetsPath + currentPath);
+
+ if(!this.bundleIndex[currentPath]){
+
+ this.bundleIndex[currentPath] = {
+ files : [],
+ bundles : [],
+ mtime : fs.statSync(this.assetsPath + currentPath).mtime.getTime()
+ };
+
+ };
+
+ _.each(files, function(file){
+
+ var currentFile = this.assetsPath + currentPath + '/' + file,
+ stat = fs.statSync(currentFile),
+ src = '',
+ dependency;
+
+ if(stat.isFile()){
+
+ if(/\.js$/.test(file)){
+
+ var asset = Asset(file, currentPath, this.assetsPath, stat, this.nodeModules );
+
+ this.assetIndex[ asset.name ] = asset;
+ this.bundleIndex[ currentPath ].files.push( asset.name );
+
+ }
+
+
+ }else if(stat.isDirectory()){
+
+ if( (currentPath + '/' + file) !== this.buildPath ){
+
+ this.bundleIndex[currentPath].bundles.push( path.normalize(currentPath + '/' + file) );
+ this.scanDirectory(path.normalize(currentPath + '/' + file));
+
+ }
+
+ }
+
+
+ }, this);
+
+ return this;
+
+ },
+
+ assetsChanged : function(){
+
+ // STUB: go through all the assets/directories, detect changes, refresh if necessary.
+
+ return false;
+
+ },
+
+ convertToSortable : function( key ){
+
+ var item = {
+ key : key,
+ dependsOn : []
+
+ };
+
+ var single = this.assetIndex[key].fileDependencies();
+
+ var filesFromBundles = [];
+
+ _.each(this.assetIndex[key].bundleDependencies(), function(bundle){
+
+ filesFromBundles = filesFromBundles.concat(this.getFilesFromBundle(bundle));
+
+ }, this);
+
+ item.dependsOn = _.uniq(single.concat(filesFromBundles));
+
+ return item;
+
+ },
+
+ reset : function(){
+ this.assemblies = {};
+ this.assetIndex = {};
+ this.bundleIndex = {};
+
+ var node = path.relative(this.assetsPath, this.nodeModules);
+
+ if(this.scanNodeModules){
+
+ this.scanDirectory( node );
+ }
+ this.scanDirectory('');
+ },
+
+ getAssembly : function( key, forceCheck ){
+
+ var dirtyFiles = false;
+
+ if(key){
+
+ key = path.normalize(key);
+
+ }else{
+
+ throw new Error('No key specified for asset request');
+
+ }
+
+ if(this.assemblies[key] && typeof forceCheck==='undefined'){
+
+ return this.assemblies[key];
+
+ }
+
+ if(forceCheck){
+
+ _.each(this.assetIndex, function(asset){
+
+ var hasChanged = asset.hasChanged();
+
+ if(hasChanged){
+
+ dirtyFiles = true;
+
+ };
+
+ });
+
+
+
+ if(dirtyFiles===true || (this.fileSystemChanged()===true) ){
+ // we invalidate all assemblies and get a fresh scan.
+ this.reset();
+
+ }
+
+ }
+
+ var list = [];
+
+ _.each( // for each sorted element...
+ sorter().sort(this.buildBundle( key ), function(item){
+ return {
+ key : item.key,
+ dependsOn : item.dependsOn.slice(0)
+ };
+ }),
+
+ // push the actual asset into the array...
+ function( item ){
+
+ list.push(this.assetIndex[item.key]);
+
+ },this);
+
+ this.assemblies[key] = Assembly(list, {
+ outputPath : this.outputPath,
+ assetsPath : this.assetsPath,
+ buildPath : this.buildPath
+ });
+
+ return this.assemblies[key];
+
+ },
+
+ buildBundle : function( key ){
+
+ var self = this; // keep a reference to this
+
+ var single;
+ var initialFiles = [];
+
+ // an object to quickly check if we have a file.
+ var lookup = {};
+
+ // if it's a file...
+ if(this.assetIndex[key]){
+
+ // get the file requested...
+ single = key;
+
+ // add it to 'got'
+ initialFiles.push(single);
+ lookup[key] = this.assetIndex[single];
+
+ if(_.isEmpty(this.assetIndex[key].fileDependencies()) && _.isEmpty(this.assetIndex[key].bundleDependencies()) ){
+ // early exit: Assembly is a single file without dependences. We're done!
+ return [this.convertToSortable(key)];
+
+ }
+ // else if it's a directory we know about..
+ }else if(this.bundleIndex[key]){
+
+ // get all the files from the directory requested (and the sub directories)
+ initialFiles = this.getFilesFromBundle(key);
+
+ _.each(initialFiles, function( file ){
+
+ lookup[file] = this.assetIndex[file];
+
+ }, this);
+
+ // or we throw up.
+ }else{
+
+ throw new Error('No such asset: No files or directories found matching ' + key);
+
+ }
+
+ var filesThatNeedFiles = [];
+ var finalList = [];
+
+ // process 1: converting a collection of file keys to sortable objects (with keys an dependencies)
+ _.each(initialFiles, function(file){
+
+ filesThatNeedFiles = filesThatNeedFiles.concat(this.convertToSortable(file));
+
+ }, this);
+
+ // process 2 : find which items actually do need files...
+ while(filesThatNeedFiles.length > 0){
+
+ // pluck one from the filesThatNeedFiles
+ var testItem = filesThatNeedFiles.splice(-1)[0];
+
+ _.each(testItem.dependsOn, function( dependency, index){
+
+ var done;
+
+ if(!lookup[dependency]){
+
+ // add the file to filesThatNeedFiles...
+ if(this.assetIndex[dependency]){
+
+ done = this.convertToSortable(dependency);
+ lookup[done.key] = 1;
+ filesThatNeedFiles.push( done )
+
+ }
+
+ }
+
+ }, this);
+
+ finalList.push(testItem);
+
+ }
+
+ return finalList;
+
+ },
+
+ getFilesFromBundle : function( key ){
+
+ var self = this;
+
+ return (function getList(id){
+
+ var files = self.bundleIndex[id].files.slice(0); // get the files
+
+ _.each(self.bundleIndex[id].bundles, function(bundle){
+
+ files = files.concat( getList( bundle ) );
+
+ }, this);
+
+ return files;
+
+ }(key));
+
+ },
+
+ fileSystemChanged : function(){
+
+ var start = (new Date()).getTime();
+ var changesDetected = false;
+
+ _.each(this.bundleIndex, function(bundle, index){
+
+ try{
+
+ var current = fs.statSync(path.normalize(this.assetsPath + '/' + index)).mtime.getTime();
+
+ if(current!==bundle.mtime ){
+
+ changesDetected = true;
+ bundle.mtime = current;
+
+ }
+
+ }catch(e){
+
+ // we've got a missing directory.
+ changesDetected = true;
+
+ }
+
+
+ }, this);
+
+
+
+ if(changesDetected){
+
+ return true;
+
+ }else{
+
+ console.log( 'Elapsed: ' + ((new Date()).getTime() - start) );
+
+ return false;
+
+ }
+
+ },
+
+ // unit testing helper..
+ query : function(){
+
+ return this.bundleIndex;
+
+ }
+});
+
+exports = module.exports = Manifest;
+
Something went wrong with that request. Please try again.