Permalink
Browse files

rebuilt using npm-trospect, now registry focused

  • Loading branch information...
khrome committed Dec 29, 2012
1 parent 5dc38ae commit 32edd95eb7fe12467725523943dd8e248294ce86
Showing with 448 additions and 258 deletions.
  1. +17 −13 README.md
  2. +8 −6 demo.js
  3. +27 −0 handler-coffee.js
  4. +22 −0 handler-css.js
  5. +48 −0 handler-js.js
  6. +25 −0 handler-less.js
  7. +26 −0 handler-scss.js
  8. +15 −5 package.json
  9. +253 −229 protolus-resource.js
  10. +7 −5 test.js
View
@@ -7,18 +7,22 @@ Server Side
-----------
First require the module:
- var resource = require('protolus-resource');
+ var Resource = require('protolus-resource');
+
+Next you'll need a registry to keep track of all the modules that get unrolled:
+
+ var register = new Resource.Registry();
Then, to actually register a resource:
- resource('my_npm_module', function(moduleResources){
+ Resource.import('my_npm_module', registry, function(resource){
//all done
});
once you've done this you can access all the included resources, with or without dependencies
- var resourceList = resource.allResources();
- var fullResourceList = resource.allResourcesWithDependencies();
+ var resourceList = Resource.includes(registry, [excludes], [callback]);
+ var fullResourceList = Resource.explicit(registry, [excludes], [callback]);
you'll need this to generate your own URL back to fetch the right resource bundle, each filetype can be requested from from it's own endpoint, for example to request the js files from the 'my_npm_module' npm module I would use:
@@ -28,32 +32,32 @@ if I wanted it minified with npm module 'another_module':
'/js/compact/my_npm_module.another_module'
-I'd produce a url like that after having used resource() on the modules I need:
-
- var location = '/js/compact/'+resource.allResourcesWithDependencies().join('.');
-
or I can get an array of head tags, after resources are registered, using resource.headIncludes(<options>, <callback>) where options may be a complex object or a boolean representing the 'combined' option.
- *combined* : are the resources a series of sequential tags or one large combined one?
- *compact* : are the resources built uncompressed, or are they minified/compacted/etc. ?
- resource.headIncludes(true, function(tags){
+ Resource.head(true, function(tags){
res.end('<html><head>'+(tags.join("\n"))+'</head><body><h1>Heya!</h1></body></html>');
});
On the other side of things, in your server, theres a passthrough for handling the serving of all these resources which we can use to put it all together:
var app = require('http').createServer(function handler(req, res) {
- resource.handleResourceCalls(req, res, function(){
- resource('test-component', function(){
- resource.headIncludes(true, function(tags){
+ Resource.handle(req, res, function(){
+ Resource.import('test-component', registry, function(){
+ Resource.head(true, function(tags){
res.end('<html><head>'+(tags.join("\n"))+'</head><body><h1>Heya!</h1></body></html>');
});
});
});
});
app.listen(80);
-Then we can get combined payloads into the browser without committing to a build process or async loading every required module individually.
+Then we can get combined payloads into the browser without committing to a build process or async loading every required module, individually.
+
+Currently supported are: scss, less, css, javascript and coffeescript
+
+ require('protolus-resource/handler-<type>');
Client Side
-----------
View
14 demo.js
@@ -1,11 +1,13 @@
-var resource = require('./protolus-resource');
+require('npm-trospect').require = require; //global require
+var Resource = require('./protolus-resource');
+require('./handler-js');
-var app = require('http').createServer(function handler(req, res) {
- resource.handleResourceCalls(req, res, function(){
- resource('test-component', function(){
- resource.headIncludes(true, function(tags){
+var app = require('http').createServer(function handler(req, res){
+ Resource.handle(req, res, function(){
+ new Resource('test-component', function(){
+ Resource.head(true, function(tags){
res.end('<html>'+"\n "+'<head>'+"\n "+(tags.join("\n "))+"\n "+'</head>'+"\n "+'<body><h1>Heya!</h1>'+"\n "+'</body>'+"\n"+'</html>');
- });
+ });
});
});
});
View
@@ -0,0 +1,27 @@
+var Resource = require('./protolus-resource');
+var Class = require('Classy');
+var coffee = new require('coffeescript-compiler')();
+
+var CoffeeScriptHandler = new Class({
+ Extends : Resource.Handler,
+ initialize : function(options){
+ this.parent('coffee');
+ },
+ handle : function(options, callback){
+ coffee.compile(options.body, {bare: !options.compact}, function(js){
+ js += "\n"+' //@sourceURL='+options.location+"\n"
+ callback(js);
+ });
+ },
+ tagProfile : function(){
+ return {
+ name : 'script',
+ attrs : {
+ },
+ target : 'src'
+ };
+ }
+});
+Resource.registerHandler('coffee', new CoffeeScriptHandler());
+
+module.exports = CoffeeScriptHandler;
View
@@ -0,0 +1,22 @@
+var Resource = require('./protolus-resource');
+var Class = require('Classy');
+
+var CSSHandler = new Class({
+ Extends : Resource.Handler,
+ initialize : function(options){
+ this.parent('css');
+ },
+ handle : function(options, callback){
+ callback(options.body);
+ },
+ tagProfile : function(){
+ return {
+ name : 'link',
+ attrs : {
+ },
+ pathTarget : 'href'
+ };
+ }
+});
+Resource.registerHandler('css', new CSSHandler());
+module.exports = CSSHandler;
View
@@ -0,0 +1,48 @@
+var Resource = require('./protolus-resource');
+var Class = require('Classy');
+
+var JavascriptHandler = new Class({
+ Extends : Resource.Handler,
+ initialize : function(options){
+ this.parent('js');
+ },
+ handle : function(options, callback){
+ options.body += "\n"+' //@sourceURL='+options.location+"\n"
+ callback(options.body);
+ },
+ tagProfile : function(){
+ return {
+ name : 'script',
+ attrs : {
+ },
+ target : 'src'
+ };
+ }
+});
+var JavascriptMainHandler = new Class({
+ Extends : Resource.Handler,
+ initialize : function(options){
+ this.parent('js')
+ },
+ handle : function(options, callback){
+ var text = options.body;
+ var lines = text.split("\n");
+ lines.push(' //@sourceURL='+options.location.replace('/./', '/'));
+ text = JSON.stringify(lines).replace( /\\/g, '\\\\').replace(/'/g, "\\'");
+ options.body = 'Protolus.register(\''+options.name+'\', \''+text+'\')';
+ callback(options.body);
+ },
+ tagProfile : function(){
+ return {
+ name : 'script',
+ attrs : {
+ },
+ target : 'src'
+ };
+ }
+});
+Resource.registerHandler('js', new JavascriptHandler());
+Resource.registerHandler('main', new JavascriptMainHandler());
+
+module.exports = JavascriptHandler;
+module.exports.Main = JavascriptMainHandler;
View
@@ -0,0 +1,25 @@
+var Resource = require('./protolus-resource');
+var Class = require('Classy');
+var less = require('less');
+
+var LessHandler = new Class({
+ Extends : Resource.Handler,
+ initialize : function(options){
+ this.parent('less');
+ },
+ handle : function(options, callback){
+ less.render(options.body, function (e, css) {
+ callback(css);
+ });
+ },
+ tagProfile : function(){
+ return {
+ name : 'link',
+ attrs : {
+ },
+ pathTarget : 'href'
+ };
+ }
+});
+Resource.registerHandler('less', new LessHandler());
+module.exports = LessHandler;
View
@@ -0,0 +1,26 @@
+var Resource = require('./protolus-resource');
+var Class = require('Classy');
+var less = require('less');
+var sass = require('node-sass');
+
+var SCSSHandler = new Class({
+ Extends : Resource.Handler,
+ initialize : function(options){
+ this.parent('scss');
+ },
+ handle : function(options, callback){
+ sass.render(options.body, function (err, css) {
+ callback(css);
+ });
+ },
+ tagProfile : function(){
+ return {
+ name : 'link',
+ attrs : {
+ },
+ pathTarget : 'href'
+ };
+ }
+});
+Resource.registerHandler('scss', new SCSSHandler());
+module.exports = SCSSHandler;
View
@@ -1,15 +1,19 @@
{
"name": "protolus-resource",
"homepage": "https://github.com/Protolus/protolus-resource",
- "version": "0.0.1-alpha",
+ "version": "0.0.2-alpha",
"main": "protolus-resource.js",
"description": "a CommonJS based server-side resource delivery mechanism with client side shim",
"keywords": [
"url",
"router",
+ "resources",
+ "scss",
+ "less",
"protolus",
- "ini",
- "conf"
+ "coffeescript",
+ "browser",
+ "css"
],
"author": "Abbey Hawk Sparrow <@khrome> (http://patternweaver.com)",
"contributors": [],
@@ -24,14 +28,20 @@
"dependencies": {
"prime": "*",
"url":"*",
- "Classy":"*"
+ "Classy":"*",
+ "npm-trospect" :"*",
+ "prime-ext" :"*"
},
"devDependencies": {
"mocha": "*",
"should": "*",
"request":"*"
},
- "optionalDependencies": {},
+ "optionalDependencies": {
+ "node-sass": "*",
+ "less": "*",
+ "coffeescript-compiler": "*"
+ },
"engines": {
"node": "*"
}
Oops, something went wrong.

0 comments on commit 32edd95

Please sign in to comment.