Permalink
Browse files

Created example app that shows what happens

  • Loading branch information...
1 parent 8f1f739 commit eaa0614349923dfba64e909065cc5c1420091b01 @evdb evdb committed May 23, 2012
Showing with 144 additions and 0 deletions.
  1. +1 −0 .gitignore
  2. +5 −0 README.md
  3. +41 −0 example/app.js
  4. +22 −0 example/views/index.jade
  5. +9 −0 example/views/layout.jade
  6. +1 −0 index.js
  7. +38 −0 lib/jade-amd.js
  8. +27 −0 package.json
View
@@ -0,0 +1 @@
+node_modules
View
@@ -15,3 +15,8 @@ But without:
* mandating naming or directory layouts
* feature creeping :)
+## Inspiration
+
+ * https://github.com/visionmedia/jade/issues/634
+ * https://github.com/pgherveou/squid/blob/master/src/lib/JadeBuilder.coffee
+
View
@@ -0,0 +1,41 @@
+var express = require('express'),
+ jade = require('jade'),
+ toAmdString = require('../lib/jade-amd').toAmdString,
+ jadeRuntimeAmdString = require('../lib/jade-amd').jadeRuntimeAmdString;
+
+var app = module.exports = express.createServer();
+
+
+app.configure(function(){
+ app.use(express.logger('dev'));
+ app.set('view engine', 'jade');
+ app.set('views', __dirname + '/views');
+ app.set('view options', { layout: false, pretty: true, });
+ app.use(express.bodyParser());
+ app.use(express.methodOverride());
+
+ app.use(express.static(__dirname + '/public'));
+
+ app.use(app.router);
+
+ app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
+});
+
+app.get('/', function (req,res) {
+ var sampleJade = "h1 Hello #{locals.name || 'World'}!";
+ var sampleFunction = jade.compile(sampleJade, {client: true, compileDebug: false})
+
+ res.locals({
+ sampleJade : sampleJade,
+ sampleHTML : jade.compile(sampleJade)({}),
+ sampleFunction : sampleFunction.toString(),
+ sampleAMD : toAmdString(sampleFunction),
+ jadeRuntimeAmdString: jadeRuntimeAmdString,
+ });
+
+
+ res.render('index');
+});
+
+app.listen(3000);
+console.log("Example jade-amd Express app started: http://localhost:3000");
View
@@ -0,0 +1,22 @@
+extend layout
+
+block content
+ h1 Sample app
+
+ p This is an example app that demonstrates the wrapping that occurs
+
+
+ h2 Sample Jade
+ pre #{ sampleJade }
+
+ h2 Jade as HTML
+ pre #{ sampleHTML }
+
+ h2 Compiled Jade client function
+ pre #{ sampleFunction }
+
+ h2 AMD wrapped compiled Jade client function
+ pre #{ sampleAMD }
+
+ h2 AMD wrapped Jade runtime
+ pre #{ jadeRuntimeAmdString }
@@ -0,0 +1,9 @@
+!!!
+
+head
+ title jade-amd example app
+
+body
+ block content
+
+ p content goes here
View
@@ -0,0 +1 @@
+module.exports = require('./lib/jade-amd');
View
@@ -0,0 +1,38 @@
+var jade = require('jade'),
+ fs = require('fs');
+
+/**
+ * Wrap the output of `jade.compile` in an AMD string.
+ *
+ * Options:
+ *
+ * - `jadeRuntime` the name to use in the define, default to 'jadeRuntime'
+ *
+ * @param {Function} fn
+ * @param {Options} options
+ * @return {String}
+ * @api public
+ */
+
+exports.toAmdString = function (fn, options) {
+
+ // make this optional
+ options = options || {};
+
+ var jadeRuntime = options.jadeRuntime || 'jadeRuntime';
+
+ // top and tail the function as a string
+ var amdString = "define(['" + jadeRuntime +"'], function(jade) {\nreturn " + fn.toString() + ";\n});\n";
+
+ return amdString;
+
+};
+
+var jadeRuntimePath = require.resolve('jade/runtime');
+var jadeRuntimeSource = fs.readFileSync( jadeRuntimePath );
+
+exports.jadeRuntimeAmdString = "define([], function() {\nvar "
+ + jadeRuntimeSource
+ + "\nreturn jade;\n});\n";
+
+
View
@@ -0,0 +1,27 @@
+{
+ "name": "jade-amd",
+ "version": "0.0.1",
+ "private": false,
+ "dependencies": {
+ "jade": "",
+ "underscore": ">= 1.3.1"
+ },
+ "scripts": {
+ },
+ "devDependencies": {
+ "express": ">=2.5.9 <3"
+ },
+ "optionalDependencies": {},
+ "engines": {
+ "node": "*"
+ },
+ "author": "mySociety <popit@mysociety.org> (http://www.mysociety.org/)",
+ "description": "Utilities for working with jade in an AMD setting",
+ "repository": {
+ "type": "git",
+ "url": "http://github.com/mysociety/node-jade-amd.git"
+ },
+ "bugs": {
+ "url" : "http://github.com/mysociety/node-jade-amd/issues"
+ }
+}

0 comments on commit eaa0614

Please sign in to comment.