hanging_gardens 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in hanging_gardens.gemspec
4
+ gemspec
data/README.md ADDED
@@ -0,0 +1,22 @@
1
+ # Hanging Gardens — CommonJS for Sprockets
2
+
3
+ ## Usage
4
+
5
+ in your **Gemfile**:
6
+
7
+ ```ruby
8
+ gem 'hanging_gardens'
9
+ ```
10
+
11
+ after setting up your Sprockets environment:
12
+
13
+ ```ruby
14
+ HangingGardens.register(env)
15
+ ```
16
+
17
+ add your first CommonJS file 'app/assets/index.common':
18
+
19
+ ```js
20
+ var window = require('browser/window');
21
+ console.log(window);
22
+ ```
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ require 'rspec/core/rake_task'
4
+ RSpec::Core::RakeTask.new do |t|
5
+ t.pattern = "./spec/**/*_spec.rb"
6
+ t.rspec_opts = [
7
+ '-r', File.expand_path("../spec/spec_helper.rb", __FILE__)]
8
+ end
9
+
10
+ task :default => :spec
@@ -0,0 +1,23 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "hanging_gardens/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "hanging_gardens"
7
+ s.version = HangingGardens::VERSION
8
+ s.authors = ["Simon Menke"]
9
+ s.email = ["simon.menke@gmail.com"]
10
+ s.homepage = "https://github.com/hanging-gardens"
11
+ s.summary = %q{Hanging Gardens - CommonJS for Sprockets}
12
+ s.description = %q{Hanging Gardens - CommonJS for Sprockets}
13
+
14
+ s.rubyforge_project = "hanging_gardens"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+
21
+ s.add_development_dependency "rspec"
22
+ s.add_runtime_dependency "sprockets"
23
+ end
@@ -0,0 +1,13 @@
1
+ module HangingGardens
2
+
3
+ require 'tilt'
4
+ require 'sprockets'
5
+ require "hanging_gardens/version"
6
+ require "hanging_gardens/module_wrapper"
7
+
8
+ def self.register(env)
9
+ env.register_engine '.common', ModuleWrapper
10
+ env.append_path File.expand_path('../../runtime', __FILE__)
11
+ end
12
+
13
+ end
@@ -0,0 +1,45 @@
1
+ class HangingGardens::ModuleWrapper < Tilt::Template
2
+ RE_REQUIRE = /(?:\s+|(?:[=]\s*)|(?:^\s*))require\s*\((.+)\)/
3
+
4
+ SANDBOX = %w(
5
+ module
6
+ exports
7
+ require
8
+ window
9
+ document
10
+ console
11
+ screen
12
+ history
13
+ location
14
+ navigartor
15
+ __filename
16
+ __dirname
17
+ setInterval
18
+ setTimeout
19
+ clearInterval
20
+ clearTimeout
21
+ )
22
+
23
+ self.default_mime_type = "application/javascript"
24
+
25
+ def prepare
26
+ end
27
+
28
+ def evaluate(context, locals, &block)
29
+ context.require_asset 'hanging_gardens/runtime.js'
30
+ unless context.logical_path == 'browser/console' or context.logical_path == 'browser/window'
31
+ context.require_asset 'browser/console'
32
+ end
33
+
34
+ data.scan(RE_REQUIRE) do |m|
35
+ path = YAML.load(m.first)
36
+ if path[0,2] == './' or path[0,3] == '../'
37
+ path = File.join(File.dirname(context.logical_path), path)
38
+ end
39
+ context.require_asset path
40
+ end
41
+
42
+ "HG.register(#{context.logical_path.inspect}, function(#{SANDBOX.join(', ')}){\n#{data}\n});\n"
43
+ end
44
+
45
+ end
@@ -0,0 +1,3 @@
1
+ module HangingGardens
2
+ VERSION = "1.0.0"
3
+ end
@@ -0,0 +1,16 @@
1
+ var window = require('browser/window');
2
+
3
+ module.exports = (window.console || {});
4
+
5
+ var members, member, noop;
6
+
7
+ members = ['log', 'debug', 'info', 'warn', 'error', 'assert', 'dir', 'dirxml', 'trace', 'group', 'groupEnd', 'time', 'timeEnd', 'profile', 'profileEnd', 'count'];
8
+
9
+ noop = function(){};
10
+
11
+ for (member in members){
12
+ member = members[member];
13
+ if (!module.exports[member]) {
14
+ module.exports[member] = noop;
15
+ }
16
+ }
@@ -0,0 +1,2 @@
1
+ var window = require('browser/window');
2
+ module.exports = window.document;
@@ -0,0 +1,2 @@
1
+ var window = require('browser/window');
2
+ module.exports = window.history;
@@ -0,0 +1,7 @@
1
+ exports.console = require('browser/console');
2
+ exports.document = require('browser/document');
3
+ exports.history = require('browser/history');
4
+ exports.location = require('browser/location');
5
+ exports.navigator = require('browser/navigator');
6
+ exports.screen = require('browser/screen');
7
+ exports.window = require('browser/window');
@@ -0,0 +1,2 @@
1
+ var window = require('browser/window');
2
+ module.exports = window.location;
@@ -0,0 +1,2 @@
1
+ var window = require('browser/window');
2
+ module.exports = window.navigator;
@@ -0,0 +1,2 @@
1
+ var window = require('browser/window');
2
+ module.exports = window.screen;
@@ -0,0 +1 @@
1
+ module.exports = require.window;
@@ -0,0 +1,173 @@
1
+ (function(window, undefined){
2
+
3
+ var real_require
4
+ , normalize_id
5
+ , resolve_id
6
+ , modules = {}
7
+ , sources = {}
8
+ , aliases = {}
9
+ , embedded = false
10
+ , parent_module
11
+ , __filename, __dirname, scripts, script
12
+ ;
13
+
14
+ real_require = function(id, callsite){
15
+ var source
16
+ , module
17
+ , require
18
+ , original_id
19
+ ;
20
+
21
+ id = normalize_id(id, callsite['id']);
22
+ original_id = id;
23
+
24
+ id = resolve_id(id);
25
+ source = sources[id];
26
+ module = modules[id];
27
+
28
+ if (!source) {
29
+ throw('Connot find module \''+original_id+'\'');
30
+ }
31
+
32
+ if (!module) {
33
+ module =
34
+ { "exports": {}
35
+ , "id": id
36
+ };
37
+
38
+ modules[id] = module;
39
+
40
+ require = function(id){ return real_require(id, module); };
41
+
42
+ require.global = function(id) {
43
+ window.require(id);
44
+ };
45
+
46
+ if (id == 'browser/window'){
47
+ require.window = window;
48
+ }
49
+
50
+ source.call(
51
+ /* this */ {}
52
+ , /* module */ module
53
+ , /* exports */ module.exports
54
+ , /* require */ require
55
+ , /* window */ undefined
56
+ , /* document */ undefined
57
+ , /* console */ require("browser/console")
58
+ , /* screen */ undefined
59
+ , /* history */ undefined
60
+ , /* location */ undefined
61
+ , /* navigartor */ undefined
62
+ , /* __filename */ __filename
63
+ , /* __dirname */ __dirname
64
+ , /* setInterval */ window.setInterval
65
+ , /* setTimeout */ window.setTimeout
66
+ , /* clearInterval */ window.clearInterval
67
+ , /* clearTimeout */ window.clearTimeout
68
+ );
69
+ }
70
+
71
+ return module['exports'];
72
+ };
73
+
74
+ normalize_id = function(id, base){
75
+ var id_parts
76
+ , base_parts
77
+ , abs_parts = []
78
+ , part
79
+ ;
80
+
81
+ base_parts = base.split('/');
82
+ id_parts = id.split('/');
83
+
84
+ base_parts.pop();
85
+
86
+ if (id_parts[0] == '.' || id_parts[0] == '..') {
87
+ id_parts = base_parts.concat(id_parts);
88
+ }
89
+
90
+ while (part = id_parts.shift()) {
91
+ if (part == '..') {
92
+ abs_parts.pop();
93
+ } else if (part != '.' && part != '') {
94
+ abs_parts.push(part);
95
+ }
96
+ }
97
+
98
+ return abs_parts.join('/');
99
+ };
100
+
101
+ resolve_id = function(id) {
102
+ if (aliases[id]) { return resolve_id(aliases[id]); }
103
+ if (aliases[id+'/index']) { return resolve_id(aliases[id+'/index']); }
104
+ if (sources[id]) { return id; }
105
+ if (sources[id+'/index']) { return id+'/index'; }
106
+ return id;
107
+ };
108
+
109
+ // handle embedded archives
110
+ // if (window.require && !window.require.external) {
111
+ // embedded = true;
112
+ // parent_module = window;
113
+ // window = window.require('browser/window');
114
+ // }
115
+
116
+ // get the script location
117
+ scripts = window.document.getElementsByTagName("script");
118
+ script = scripts[scripts.length-1];
119
+ __filename = script.src;
120
+ __dirname = __filename.split('/');
121
+ __dirname.pop();
122
+ __dirname = __dirname.join('/');
123
+
124
+ // export the external require
125
+ // script.require = function(id) {
126
+ // return real_require(id, { 'id': '.' });
127
+ // };
128
+
129
+ // if (window.require) {
130
+ // var _require = window.require;
131
+ // }
132
+ // window.require = function(id) {
133
+ // var src
134
+ // ;
135
+ //
136
+ // for (src in window.require.archives) {
137
+ // try { return window.require.archives[src](id); } catch (e) {}
138
+ // }
139
+ //
140
+ // throw('Connot find module \''+id+'\'');
141
+ // };
142
+ // window.require.external = true;
143
+ // window.require.archives = (_require && _require.archives) || {};
144
+ // window.require.archives[__filename] = script.require;
145
+ // window.require.makeCompatible = function(){
146
+ // var r = window.require;
147
+ // window.require = _require;
148
+ // return r;
149
+ // };
150
+
151
+ // load the ES5 shim
152
+ // real_require('es5-shim', { 'id': '.' });
153
+
154
+ // boot the application
155
+ // main_id = resolve_id(main_id);
156
+ // if (sources[main_id]) {
157
+ // real_require(main_id, { 'id': '.' });
158
+ //
159
+ // if (embedded) { parent_module.exports = modules[id]['exports']; }
160
+ // }
161
+
162
+ window.HG = {};
163
+ window.HG.require = function(id){
164
+ return real_require(id, { 'id': '.' });
165
+ };
166
+ window.HG.register = function(id, source){
167
+ sources[id] = source;
168
+ if (id == "index") {
169
+ real_require("index", { 'id': '.' });
170
+ }
171
+ };
172
+
173
+ })(this);
@@ -0,0 +1,6 @@
1
+ <html>
2
+ <head>
3
+ <script src="build.js" type="text/javascript" charset="utf-8"></script>
4
+ </head>
5
+ <body></body>
6
+ </html>
@@ -0,0 +1,215 @@
1
+ (function(window, undefined){
2
+
3
+ var real_require
4
+ , normalize_id
5
+ , resolve_id
6
+ , modules = {}
7
+ , sources = {}
8
+ , aliases = {}
9
+ , embedded = false
10
+ , parent_module
11
+ , __filename, __dirname, scripts, script
12
+ ;
13
+
14
+ real_require = function(id, callsite){
15
+ var source
16
+ , module
17
+ , require
18
+ , original_id
19
+ ;
20
+
21
+ id = normalize_id(id, callsite['id']);
22
+ original_id = id;
23
+
24
+ id = resolve_id(id);
25
+ source = sources[id];
26
+ module = modules[id];
27
+
28
+ if (!source) {
29
+ throw('Connot find module \''+original_id+'\'');
30
+ }
31
+
32
+ if (!module) {
33
+ module =
34
+ { "exports": {}
35
+ , "id": id
36
+ };
37
+
38
+ modules[id] = module;
39
+
40
+ require = function(id){ return real_require(id, module); };
41
+
42
+ require.global = function(id) {
43
+ window.require(id);
44
+ };
45
+
46
+ if (id == 'browser/window'){
47
+ require.window = window;
48
+ }
49
+
50
+ source.call(
51
+ /* this */ {}
52
+ , /* module */ module
53
+ , /* exports */ module.exports
54
+ , /* require */ require
55
+ , /* window */ undefined
56
+ , /* document */ undefined
57
+ , /* console */ require("browser/console")
58
+ , /* screen */ undefined
59
+ , /* history */ undefined
60
+ , /* location */ undefined
61
+ , /* navigartor */ undefined
62
+ , /* __filename */ __filename
63
+ , /* __dirname */ __dirname
64
+ , /* setInterval */ window.setInterval
65
+ , /* setTimeout */ window.setTimeout
66
+ , /* clearInterval */ window.clearInterval
67
+ , /* clearTimeout */ window.clearTimeout
68
+ );
69
+ }
70
+
71
+ return module['exports'];
72
+ };
73
+
74
+ normalize_id = function(id, base){
75
+ var id_parts
76
+ , base_parts
77
+ , abs_parts = []
78
+ , part
79
+ ;
80
+
81
+ base_parts = base.split('/');
82
+ id_parts = id.split('/');
83
+
84
+ base_parts.pop();
85
+
86
+ if (id_parts[0] == '.' || id_parts[0] == '..') {
87
+ id_parts = base_parts.concat(id_parts);
88
+ }
89
+
90
+ while (part = id_parts.shift()) {
91
+ if (part == '..') {
92
+ abs_parts.pop();
93
+ } else if (part != '.' && part != '') {
94
+ abs_parts.push(part);
95
+ }
96
+ }
97
+
98
+ return abs_parts.join('/');
99
+ };
100
+
101
+ resolve_id = function(id) {
102
+ if (aliases[id]) { return resolve_id(aliases[id]); }
103
+ if (aliases[id+'/index']) { return resolve_id(aliases[id+'/index']); }
104
+ if (sources[id]) { return id; }
105
+ if (sources[id+'/index']) { return id+'/index'; }
106
+ return id;
107
+ };
108
+
109
+ // handle embedded archives
110
+ // if (window.require && !window.require.external) {
111
+ // embedded = true;
112
+ // parent_module = window;
113
+ // window = window.require('browser/window');
114
+ // }
115
+
116
+ // get the script location
117
+ scripts = window.document.getElementsByTagName("script");
118
+ script = scripts[scripts.length-1];
119
+ __filename = script.src;
120
+ __dirname = __filename.split('/');
121
+ __dirname.pop();
122
+ __dirname = __dirname.join('/');
123
+
124
+ // export the external require
125
+ // script.require = function(id) {
126
+ // return real_require(id, { 'id': '.' });
127
+ // };
128
+
129
+ // if (window.require) {
130
+ // var _require = window.require;
131
+ // }
132
+ // window.require = function(id) {
133
+ // var src
134
+ // ;
135
+ //
136
+ // for (src in window.require.archives) {
137
+ // try { return window.require.archives[src](id); } catch (e) {}
138
+ // }
139
+ //
140
+ // throw('Connot find module \''+id+'\'');
141
+ // };
142
+ // window.require.external = true;
143
+ // window.require.archives = (_require && _require.archives) || {};
144
+ // window.require.archives[__filename] = script.require;
145
+ // window.require.makeCompatible = function(){
146
+ // var r = window.require;
147
+ // window.require = _require;
148
+ // return r;
149
+ // };
150
+
151
+ // load the ES5 shim
152
+ // real_require('es5-shim', { 'id': '.' });
153
+
154
+ // boot the application
155
+ // main_id = resolve_id(main_id);
156
+ // if (sources[main_id]) {
157
+ // real_require(main_id, { 'id': '.' });
158
+ //
159
+ // if (embedded) { parent_module.exports = modules[id]['exports']; }
160
+ // }
161
+
162
+ window.HG = {};
163
+ window.HG.require = function(id){
164
+ return real_require(id, { 'id': '.' });
165
+ };
166
+ window.HG.register = function(id, source){
167
+ sources[id] = source;
168
+ if (id == "index") {
169
+ real_require("index", { 'id': '.' });
170
+ }
171
+ };
172
+
173
+ })(this);
174
+ HG.register("browser/window", function(module, exports, require, window, document, console, screen, history, location, navigartor, __filename, __dirname, setInterval, setTimeout, clearInterval, clearTimeout){
175
+ module.exports = require.window;
176
+
177
+ });
178
+ HG.register("browser/console", function(module, exports, require, window, document, console, screen, history, location, navigartor, __filename, __dirname, setInterval, setTimeout, clearInterval, clearTimeout){
179
+ var window = require('browser/window');
180
+
181
+ module.exports = (window.console || {});
182
+
183
+ var members, member, noop;
184
+
185
+ members = ['log', 'debug', 'info', 'warn', 'error', 'assert', 'dir', 'dirxml', 'trace', 'group', 'groupEnd', 'time', 'timeEnd', 'profile', 'profileEnd', 'count'];
186
+
187
+ noop = function(){};
188
+
189
+ for (member in members){
190
+ member = members[member];
191
+ if (!module.exports[member]) {
192
+ module.exports[member] = noop;
193
+ }
194
+ }
195
+
196
+ });
197
+ HG.register("helpers/faders", function(module, exports, require, window, document, console, screen, history, location, navigartor, __filename, __dirname, setInterval, setTimeout, clearInterval, clearTimeout){
198
+ function fader() { console.log('ubercool'); }
199
+ exports.fader = fader;
200
+
201
+ });
202
+ HG.register("helpers/index", function(module, exports, require, window, document, console, screen, history, location, navigartor, __filename, __dirname, setInterval, setTimeout, clearInterval, clearTimeout){
203
+ function helper() { console.log('cool'); }
204
+ var Fader = require('./faders');
205
+
206
+ exports.helper = helper;
207
+ exports.fader = Fader.fader;
208
+
209
+ });
210
+ HG.register("index", function(module, exports, require, window, document, console, screen, history, location, navigartor, __filename, __dirname, setInterval, setTimeout, clearInterval, clearTimeout){
211
+ var Helpers = require('./helpers');
212
+ Helpers.helper();
213
+ Helpers.fader();
214
+
215
+ });
@@ -0,0 +1,2 @@
1
+ function fader() { console.log('ubercool'); }
2
+ exports.fader = fader;
@@ -0,0 +1,5 @@
1
+ function helper() { console.log('cool'); }
2
+ var Fader = require('./faders');
3
+
4
+ exports.helper = helper;
5
+ exports.fader = Fader.fader;
@@ -0,0 +1,3 @@
1
+ var Helpers = require('./helpers');
2
+ Helpers.helper();
3
+ Helpers.fader();
@@ -0,0 +1,19 @@
1
+ require File.expand_path('../spec_helper', __FILE__)
2
+
3
+ describe "Integration" do
4
+
5
+ before do
6
+ @env = Sprockets::Environment.new(File.expand_path('spec'))
7
+ @env.append_path 'assets'
8
+ HangingGardens.register(@env)
9
+ end
10
+
11
+ it "processes index.js.common" do
12
+ asset = @env.find_asset('index')
13
+ asset.should_not be_nil
14
+ asset.to_s.should include('function helper')
15
+ asset.to_s.should include('function fader')
16
+ File.open('spec/assets/build.js', 'w+') { |f| f.write asset.to_s }
17
+ end
18
+
19
+ end
@@ -0,0 +1,4 @@
1
+ require 'hanging_gardens'
2
+
3
+ RSpec.configure do |config|
4
+ end
metadata ADDED
@@ -0,0 +1,122 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hanging_gardens
3
+ version: !ruby/object:Gem::Version
4
+ hash: 23
5
+ prerelease:
6
+ segments:
7
+ - 1
8
+ - 0
9
+ - 0
10
+ version: 1.0.0
11
+ platform: ruby
12
+ authors:
13
+ - Simon Menke
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-11-03 00:00:00 Z
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: rspec
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ hash: 3
29
+ segments:
30
+ - 0
31
+ version: "0"
32
+ type: :development
33
+ version_requirements: *id001
34
+ - !ruby/object:Gem::Dependency
35
+ name: sprockets
36
+ prerelease: false
37
+ requirement: &id002 !ruby/object:Gem::Requirement
38
+ none: false
39
+ requirements:
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ hash: 3
43
+ segments:
44
+ - 0
45
+ version: "0"
46
+ type: :runtime
47
+ version_requirements: *id002
48
+ description: Hanging Gardens - CommonJS for Sprockets
49
+ email:
50
+ - simon.menke@gmail.com
51
+ executables: []
52
+
53
+ extensions: []
54
+
55
+ extra_rdoc_files: []
56
+
57
+ files:
58
+ - .gitignore
59
+ - Gemfile
60
+ - README.md
61
+ - Rakefile
62
+ - hanging_gardens.gemspec
63
+ - lib/hanging_gardens.rb
64
+ - lib/hanging_gardens/module_wrapper.rb
65
+ - lib/hanging_gardens/version.rb
66
+ - runtime/browser/console.common
67
+ - runtime/browser/document.common
68
+ - runtime/browser/history.common
69
+ - runtime/browser/index.common
70
+ - runtime/browser/location.common
71
+ - runtime/browser/navigator.common
72
+ - runtime/browser/screen.common
73
+ - runtime/browser/window.common
74
+ - runtime/hanging_gardens/runtime.js
75
+ - spec/assets/build.html
76
+ - spec/assets/build.js
77
+ - spec/assets/helpers/faders.common
78
+ - spec/assets/helpers/index.common
79
+ - spec/assets/index.js.common
80
+ - spec/integration_spec.rb
81
+ - spec/spec_helper.rb
82
+ homepage: https://github.com/hanging-gardens
83
+ licenses: []
84
+
85
+ post_install_message:
86
+ rdoc_options: []
87
+
88
+ require_paths:
89
+ - lib
90
+ required_ruby_version: !ruby/object:Gem::Requirement
91
+ none: false
92
+ requirements:
93
+ - - ">="
94
+ - !ruby/object:Gem::Version
95
+ hash: 3
96
+ segments:
97
+ - 0
98
+ version: "0"
99
+ required_rubygems_version: !ruby/object:Gem::Requirement
100
+ none: false
101
+ requirements:
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ hash: 3
105
+ segments:
106
+ - 0
107
+ version: "0"
108
+ requirements: []
109
+
110
+ rubyforge_project: hanging_gardens
111
+ rubygems_version: 1.8.6
112
+ signing_key:
113
+ specification_version: 3
114
+ summary: Hanging Gardens - CommonJS for Sprockets
115
+ test_files:
116
+ - spec/assets/build.html
117
+ - spec/assets/build.js
118
+ - spec/assets/helpers/faders.common
119
+ - spec/assets/helpers/index.common
120
+ - spec/assets/index.js.common
121
+ - spec/integration_spec.rb
122
+ - spec/spec_helper.rb