sprockets-jsx 0.0.1

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,2 @@
1
+ coverage/
2
+ *.gem
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ gem 'sprockets', '~> 2.0'
2
+
3
+ group :test do
4
+ gem "rspec"
5
+ gem "simplecov", :require => false
6
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,32 @@
1
+ GEM
2
+ specs:
3
+ diff-lcs (1.1.3)
4
+ hike (1.2.1)
5
+ multi_json (1.3.5)
6
+ rack (1.4.1)
7
+ rspec (2.10.0)
8
+ rspec-core (~> 2.10.0)
9
+ rspec-expectations (~> 2.10.0)
10
+ rspec-mocks (~> 2.10.0)
11
+ rspec-core (2.10.1)
12
+ rspec-expectations (2.10.0)
13
+ diff-lcs (~> 1.1.3)
14
+ rspec-mocks (2.10.1)
15
+ simplecov (0.6.4)
16
+ multi_json (~> 1.0)
17
+ simplecov-html (~> 0.5.3)
18
+ simplecov-html (0.5.3)
19
+ sprockets (2.4.3)
20
+ hike (~> 1.2)
21
+ multi_json (~> 1.0)
22
+ rack (~> 1.0)
23
+ tilt (~> 1.1, != 1.3.0)
24
+ tilt (1.3.3)
25
+
26
+ PLATFORMS
27
+ ruby
28
+
29
+ DEPENDENCIES
30
+ rspec
31
+ simplecov
32
+ sprockets (~> 2.0)
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 uu59
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.mkd ADDED
@@ -0,0 +1,151 @@
1
+ `sprockets-jsx` is a plugin that provides compile [JSX](https://github.com/jsx/JSX) files and serves it by [Sprockets](https://github.com/sstephenson/sprockets).
2
+
3
+ # Install
4
+
5
+ $ gem i sprockets-jsx
6
+
7
+ # Usage
8
+
9
+ $ echo "sprockets-jsx" >> Gemfile
10
+ $ bundle install
11
+
12
+ and configure as below:
13
+
14
+ Sprockets::JSXTemplate.configure do |conf|
15
+ # The compiler path. It should be executable
16
+ conf.jsx_bin = "/path/to/jsx"
17
+
18
+ # JSX compile options such as "--optimize inline"
19
+ conf.compile_options = "--release"
20
+
21
+ # JSX compiles to $__jsx_classMap = { "/fullpath/asset/foo.jsx": { .. }}
22
+ # and call it by `JSX.require("/fullpath/asset/foo.jsx")._Main.main$AS(args)`
23
+ # if conf.root is given as "/fullpath", classMap will be transform to {"asset/foo.jsx"}
24
+ # then you can call it such as `JSX.require("asset/foo.jsx")._Main.main$AS(args)`
25
+ conf.root = "/path/to/asset_root"
26
+ end
27
+
28
+ # Assets Integration
29
+
30
+ $ cd assets/javascripts
31
+ $ tree
32
+ .
33
+ ├── app.js
34
+ ├── hello.jsx
35
+ └── test.js
36
+
37
+ $ cat app.js
38
+ //= require test
39
+ //= require hello
40
+ JSX.require("hello.jsx")._Main.main$AS();
41
+
42
+ $ cat test.js
43
+ var test = "this is test";
44
+
45
+ $ cat hello.jsx
46
+ class _Main {
47
+ static function main(args : string[]) :void {
48
+ log "Hello, world!";
49
+ }
50
+ }
51
+
52
+ Visit the `/assets/app.js` with Sprockets proccessed, you'll get
53
+
54
+ var test = "this is test";
55
+ var JSX = {};
56
+ (function () {
57
+
58
+ /**
59
+ * copies the implementations from source interface to target
60
+ */
61
+ function $__jsx_merge_interface(target, source) {
62
+ for (var k in source.prototype)
63
+ if (source.prototype.hasOwnProperty(k))
64
+ target.prototype[k] = source.prototype[k];
65
+ }
66
+
67
+ /**
68
+ * defers the initialization of the property
69
+ */
70
+ function $__jsx_lazy_init(obj, prop, func) {
71
+ function reset(obj, prop, value) {
72
+ Object.defineProperty(obj, prop, {
73
+ value: value,
74
+ enumerable: true,
75
+ writable: true,
76
+ configurable: true
77
+ });
78
+ return value;
79
+ }
80
+
81
+ Object.defineProperty(obj, prop, {
82
+ get: function () {
83
+ return reset(obj, prop, func());
84
+ },
85
+ set: function (v) {
86
+ reset(obj, prop, v);
87
+ },
88
+ enumerable: true,
89
+ configurable: true
90
+ });
91
+ }
92
+
93
+ /*
94
+ * global functions called by JSX as Number.* (renamed so that they do not conflict with local variable names)
95
+ */
96
+ var $__jsx_parseInt = parseInt;
97
+ var $__jsx_parseFloat = parseFloat;
98
+ var $__jsx_isNaN = isNaN;
99
+ var $__jsx_isFinite = isFinite;
100
+
101
+ var $__jsx_ObjectToString = Object.prototype.toString;
102
+ var $__jsx_ObjectHasOwnProperty = Object.prototype.hasOwnProperty;
103
+
104
+ /*
105
+ * public interface to JSX code
106
+ */
107
+ JSX.require = function (path) {
108
+ var m = $__jsx_classMap[path];
109
+ return m !== undefined ? m : null;
110
+ }
111
+ /**
112
+ * class _Main extends Object
113
+ * @constructor
114
+ */
115
+ function _Main() {
116
+ }
117
+
118
+ _Main.prototype = new Object;
119
+ /**
120
+ * @constructor
121
+ */
122
+ function _Main$() {
123
+ };
124
+
125
+ _Main$.prototype = new _Main;
126
+
127
+ /**
128
+ * @param {Array.<undefined|!string>} args
129
+ */
130
+ _Main.main$AS = function (args) {
131
+ console.log("Hello, world!");
132
+ };
133
+
134
+ _Main$main$AS = _Main.main$AS;
135
+
136
+ var $__jsx_classMap = {
137
+ "hello.jsx": {
138
+ _Main: _Main,
139
+ _Main$: _Main$
140
+ }
141
+ };
142
+
143
+
144
+ }());
145
+
146
+
147
+ JSX.require("hello.jsx")._Main.main$AS();
148
+
149
+
150
+
151
+
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.1
@@ -0,0 +1 @@
1
+ require "sprockets/jsx_template"
@@ -0,0 +1,67 @@
1
+ require 'tilt'
2
+
3
+ module Sprockets
4
+ class JSXTemplate < Tilt::Template
5
+ self.default_mime_type = 'application/javascript'
6
+
7
+ CONFIG = Struct.new("Config", :jsx_bin, :compile_options, :root).new
8
+
9
+ def self.engine_initialized?
10
+ true
11
+ end
12
+
13
+ def initialize_engine
14
+ end
15
+
16
+ def prepare
17
+ end
18
+
19
+ def evaluate(scope, locals, &block)
20
+ options = CONFIG.compile_options || ENV["JSX_OPTS"] || ""
21
+
22
+ dir = nil
23
+ filepath = file()
24
+ if CONFIG.root
25
+ dir = Dir.pwd
26
+ Dir.chdir(CONFIG.root)
27
+ filepath = filepath.dup.sub(CONFIG.root + "/", "")
28
+ end
29
+
30
+ content = begin
31
+ %x"#{CONFIG.jsx_bin || ENV["JSX_BIN"] || "jsx"} #{options} #{filepath}"
32
+ rescue Errno::ENOENT => e
33
+ raise Sprockets::ArgumentError.new e
34
+ end
35
+
36
+ Dir.chdir(dir) if dir
37
+
38
+ content
39
+ end
40
+
41
+ # Configure compile option
42
+ #
43
+ # Sprockets::JSXTemplate.configure do |conf|
44
+ # # The compiler path. It should be executable
45
+ # conf.jsx_bin = "/path/to/jsx"
46
+ #
47
+ # # JSX compile options such as "--optimize inline"
48
+ # conf.compile_options = "--release"
49
+ #
50
+ # # JSX compiles to $__jsx_classMap = { "/fullpath/asset/foo.jsx": { .. }}
51
+ # # and call it by JSX.require("/fullpath/asset/foo.jsx")._Main.main$AS(args)
52
+ # # if conf.root is given as "/fullpath", classMap will be transform to {"asset/foo.jsx": { .. }}
53
+ # # then you can call such as JSX.require("asset/foo.jsx")._Main.main$AS(args)
54
+ # conf.root = "/path/to/asset_root"
55
+ # end
56
+ def self.configure(&block)
57
+ begin
58
+ CONFIG.members.each{|m| CONFIG[m] = nil}
59
+ block.call(CONFIG)
60
+ rescue NoMethodError => e
61
+ raise Sprockets::ArgumentError.new e.message
62
+ end
63
+ end
64
+ end
65
+
66
+ register_engine ".jsx", JSXTemplate
67
+ end
@@ -0,0 +1,5 @@
1
+ class _Main {
2
+ static function main(args : string[]) :void {
3
+ log "Hello, world!";
4
+ }
5
+ }
data/spec/jsx_spec.rb ADDED
@@ -0,0 +1,57 @@
1
+ require "spec_helper"
2
+
3
+ describe Sprockets::JSXTemplate do
4
+ let(:env) do
5
+ Sprockets::Environment.new.tap do |env|
6
+ env.append_path(FIXTURE_PATH)
7
+ end
8
+ end
9
+
10
+ it "should compile" do
11
+ asset = env["hello.jsx"]
12
+ asset.to_s.match /^var JSX=/
13
+ asset.content_type.should == "application/javascript"
14
+ asset.to_s.should match /"#{FIXTURE_PATH}/
15
+ end
16
+
17
+ it "should compile by relative path" do
18
+ Sprockets::JSXTemplate.configure do |conf|
19
+ conf.root = FIXTURE_PATH
20
+ end
21
+ asset = env["hello.jsx"]
22
+ asset.to_s.match /^var JSX=/
23
+ asset.content_type.should == "application/javascript"
24
+ asset.to_s.should_not match /"#{FIXTURE_PATH}/
25
+ end
26
+
27
+ it "should be raise if `jsx` is not found" do
28
+ Sprockets::JSXTemplate.configure do |conf|
29
+ conf.jsx_bin = "foo"
30
+ end
31
+ lambda { env["hello.jsx"] }.should raise_error(Sprockets::ArgumentError)
32
+ end
33
+
34
+ it "should be configure by class method" do
35
+ Sprockets::JSXTemplate.configure do |conf|
36
+ conf.jsx_bin = "foo"
37
+ conf.compile_options = "--bar"
38
+ end
39
+ conf = Sprockets::JSXTemplate::CONFIG
40
+ conf.jsx_bin.should == "foo"
41
+ conf.compile_options.should == "--bar"
42
+ end
43
+
44
+ it "should be reset configure for each time" do
45
+ Sprockets::JSXTemplate.configure {|conf| conf.jsx_bin = "bar"}
46
+ Sprockets::JSXTemplate.configure {|conf|}
47
+ Sprockets::JSXTemplate::CONFIG.jsx_bin.should be_nil
48
+ end
49
+
50
+ it "should be raise if invalid config method call" do
51
+ lambda do
52
+ Sprockets::JSXTemplate.configure do |conf|
53
+ conf.invalid_property = "hello"
54
+ end
55
+ end.should raise_error(Sprockets::ArgumentError)
56
+ end
57
+ end
@@ -0,0 +1,18 @@
1
+ require "rubygems"
2
+ require "bundler/setup"
3
+ Bundler.require(:default, :test)
4
+
5
+ if ENV["COVERAGE"]
6
+ require "simplecov"
7
+ SimpleCov.start
8
+ end
9
+
10
+ require File.expand_path("../../lib/sprockets-jsx.rb", __FILE__)
11
+
12
+ RSpec.configure do |conf|
13
+ #conf.mock_with :rr
14
+ #conf.include Rack::Test::Methods
15
+ #conf.include Capybara::DSL
16
+
17
+ FIXTURE_PATH = File.expand_path("../fixtures", __FILE__)
18
+ end
@@ -0,0 +1,15 @@
1
+ # -*- encoding: utf-8 -*-
2
+ Gem::Specification.new do |gem|
3
+ gem.authors = ["uu59"]
4
+ gem.email = ["k@uu59.org"]
5
+ gem.description = %q{JSX support for Sprockets}
6
+ gem.summary = %q{JSX support for Sprockets}
7
+ gem.homepage = ""
8
+
9
+ gem.files = `git ls-files`.split($\)
10
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
11
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
12
+ gem.name = "sprockets-jsx"
13
+ gem.require_paths = ["lib"]
14
+ gem.version = File.read(File.expand_path("../VERSION", __FILE__))
15
+ end
metadata ADDED
@@ -0,0 +1,60 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sprockets-jsx
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - uu59
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-06-04 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: JSX support for Sprockets
15
+ email:
16
+ - k@uu59.org
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - .gitignore
22
+ - Gemfile
23
+ - Gemfile.lock
24
+ - LICENSE
25
+ - README.mkd
26
+ - VERSION
27
+ - lib/sprockets-jsx.rb
28
+ - lib/sprockets/jsx_template.rb
29
+ - spec/fixtures/hello.jsx
30
+ - spec/jsx_spec.rb
31
+ - spec/spec_helper.rb
32
+ - sprockets-jsx.gemspec
33
+ homepage: ''
34
+ licenses: []
35
+ post_install_message:
36
+ rdoc_options: []
37
+ require_paths:
38
+ - lib
39
+ required_ruby_version: !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ! '>='
43
+ - !ruby/object:Gem::Version
44
+ version: '0'
45
+ required_rubygems_version: !ruby/object:Gem::Requirement
46
+ none: false
47
+ requirements:
48
+ - - ! '>='
49
+ - !ruby/object:Gem::Version
50
+ version: '0'
51
+ requirements: []
52
+ rubyforge_project:
53
+ rubygems_version: 1.8.11
54
+ signing_key:
55
+ specification_version: 3
56
+ summary: JSX support for Sprockets
57
+ test_files:
58
+ - spec/fixtures/hello.jsx
59
+ - spec/jsx_spec.rb
60
+ - spec/spec_helper.rb