Created
December 19, 2012 09:45
-
-
Save garth/4335604 to your computer and use it in GitHub Desktop.
Packaging javascript with asset-rack example code
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
window.App = Ember.Application.create({}) | |
require('./applicationController') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require('./applicationView') | |
var Model = require('./model') | |
App.ApplicationController = Ember.ObjectController.extend({}) | |
// ... do more stuff here with model and controller ... |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
App.ApplicationView = Ember.View.extend({ | |
templateName: 'application' | |
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<title>MyApp</title> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
<body> | |
<%- assets.tag('/js/vendor.js') %> | |
<%- assets.tag('/js/app.js') %> | |
</body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This model class can be used on both the server and the client | |
module.exports = function () { | |
this.name = "" | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "MyApp", | |
"version": "0.0.1", | |
"dependencies": { | |
"express": "3.0.4", | |
"ejs": "0.8.3", | |
"asset-rack": "1.1.6" | |
}, | |
"engine": { | |
"node": "0.8.x" | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// dependencies | |
var express = require('express') | |
var http = require('http') | |
var path = require('path') | |
var rack = require('asset-rack') | |
//import sample client model for use server side | |
var model = require('./model') | |
// create the express app | |
var app = express() | |
// prepare assets | |
console.log('Preparing assets for ' + app.settings.env) | |
var isProduction = app.settings.env === 'production' | |
var assetRack = new rack.AssetRack([ | |
new rack.SnocketsAsset({ | |
url: '/js/vendor.js', | |
filename: path.join(__dirname, 'assets/js/vendor.js'), | |
debug: !isProduction, | |
compress: isProduction | |
}), | |
new rack.BrowserifyAsset({ | |
url: '/js/app.js', | |
filename: path.join(__dirname, 'assets/js/app.js'), | |
debug: !isProduction, | |
compress: isProduction | |
}) | |
]) | |
assetRack.on('complete', function () { | |
// configure express | |
app.configure(function () { | |
app.set('port', process.env.PORT || 3000) | |
app.set('views', path.join(__dirname, 'views')) | |
app.set('view engine', 'ejs') | |
app.use(express.cookieParser()) | |
app.use(express.bodyParser()) | |
app.use(express.methodOverride()) | |
app.use(express.compress()) | |
app.use(app.router) | |
app.use(assetRack) | |
app.use(express['static'](path.join(__dirname, 'public'))) | |
}) | |
app.configure('development', function () { | |
app.use(express.errorHandler()) | |
}) | |
// Routes | |
app.get('/', function (req, res) { | |
res.render('index', { assets: assetRack }) | |
}) | |
// start express | |
http.createServer(app).listen(app.get('port'), function () { | |
console.log("Express server listening on port ".cyan + app.get('port')) | |
}) | |
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//= require '/vendor/jquery/jquery' | |
//= require '/vendor/bootstrap/js/bootstrap-dropdown' | |
//= require '/vendor/handlebars/handlebars' | |
//= require '/vendor/ember/ember' | |
//= require '/vendor/ember-data/ember-data' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
See the full blog post for more details http://garthw.wordpress.com/2012/12/19/packaging-javascript/