svelte-rack 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 5cf57e5229f24f2d4536b9aba98318d5f82b870b
4
+ data.tar.gz: 41225ffc522598de062385d0a3ed71ebad676dab
5
+ SHA512:
6
+ metadata.gz: cf7e7b5e16034573d18cd1ee03a4f4c8b266ef8d618ec43d124562920f89dc711a8244cfadc745d5da8d1e67b133b77888700dd3c031894ad3410262ecb5e20a
7
+ data.tar.gz: cf47b515022c7714591e41c05436bde4df3f900676f0653f4f010e1370d1eed781fc882d6ee3f5bdfd46b28cde8420a13110976ae0168f65edf3d20ae1a9e309
data/.autotest ADDED
@@ -0,0 +1,25 @@
1
+ # -*- ruby -*-
2
+
3
+ require "autotest/restart"
4
+
5
+ # Autotest.add_hook :initialize do |at|
6
+ # at.testlib = "minitest/unit"
7
+ #
8
+ # at.extra_files << "../some/external/dependency.rb"
9
+ #
10
+ # at.libs << ":../some/external"
11
+ #
12
+ # at.add_exception "vendor"
13
+ #
14
+ # at.add_mapping(/dependency.rb/) do |f, _|
15
+ # at.files_matching(/test_.*rb$/)
16
+ # end
17
+ #
18
+ # %w(TestA TestB).each do |klass|
19
+ # at.extra_class_map[klass] = "test/test_misc.rb"
20
+ # end
21
+ # end
22
+
23
+ # Autotest.add_hook :run_command do |at|
24
+ # system "rake build"
25
+ # end
data/.gitignore ADDED
@@ -0,0 +1,53 @@
1
+ *.gem
2
+ *.rbc
3
+ /.config
4
+ /coverage/
5
+ /InstalledFiles
6
+ /pkg/
7
+ /spec/reports/
8
+ /spec/examples.txt
9
+ /test/tmp/
10
+ /test/version_tmp/
11
+ /tmp/
12
+
13
+ # Used by dotenv library to load environment variables.
14
+ # .env
15
+
16
+ ## Specific to RubyMotion:
17
+ .dat*
18
+ .repl_history
19
+ build/
20
+ *.bridgesupport
21
+ build-iPhoneOS/
22
+ build-iPhoneSimulator/
23
+
24
+ ## Specific to RubyMotion (use of CocoaPods):
25
+ #
26
+ # We recommend against adding the Pods directory to your .gitignore. However
27
+ # you should judge for yourself, the pros and cons are mentioned at:
28
+ # https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control
29
+ #
30
+ # vendor/Pods/
31
+
32
+ ## Documentation cache and generated files:
33
+ /.yardoc/
34
+ /_yardoc/
35
+ /doc/
36
+ /rdoc/
37
+
38
+ ## Environment normalization:
39
+ /.bundle/
40
+ /vendor/bundle
41
+ /lib/bundler/man/
42
+
43
+ # for a library or gem, you might want to ignore these files since the code is
44
+ # intended to run in multiple environments; otherwise, check them in:
45
+ Gemfile.lock
46
+ .ruby-version
47
+ .ruby-gemset
48
+
49
+ # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
50
+ .rvmrc
51
+
52
+ .DS_Store
53
+ *.sw?
data/.hoeignore ADDED
@@ -0,0 +1,12 @@
1
+ Gemfile.lock
2
+ .ruby-version
3
+
4
+ .git/*
5
+
6
+ *.sw?
7
+ .*.sw?
8
+ **/*.sw?
9
+ **/.*.sw?
10
+
11
+ .DS_Store
12
+ **/.DS_Store
data/Gemfile ADDED
@@ -0,0 +1,18 @@
1
+ # -*- ruby -*-
2
+
3
+ # DO NOT EDIT THIS FILE. Instead, edit Rakefile, and run `rake bundler:gemfile`.
4
+
5
+ source "https://rubygems.org/"
6
+
7
+
8
+ gem "minitest", "~>5.10", :group => [:development, :test]
9
+ gem "hoe-yard", ">=0.1.3", :group => [:development, :test]
10
+ gem "hoe-ignore", "~>1.0", :group => [:development, :test]
11
+ gem "hoe-bundler", "~>1.2", :group => [:development, :test]
12
+ gem "hoe-gemspec", "~>1.0", :group => [:development, :test]
13
+ gem "hoe-git", "~>1.6", :group => [:development, :test]
14
+ gem "yard", "~>0.8", :group => [:development, :test]
15
+ gem "redcarpet", "~>3.3", :group => [:development, :test]
16
+ gem "hoe", "~>3.16", :group => [:development, :test]
17
+
18
+ # vim: syntax=ruby
data/History.txt ADDED
@@ -0,0 +1,5 @@
1
+ === 0.0.1 / 2017-03-21
2
+
3
+ * It's alive!
4
+
5
+ * Bornday!
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Phil Misiowiec, Webficient LLC
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Manifest.txt ADDED
@@ -0,0 +1,19 @@
1
+ .autotest
2
+ .gitignore
3
+ .hoeignore
4
+ Gemfile
5
+ History.txt
6
+ LICENSE
7
+ Manifest.txt
8
+ README.md
9
+ Rakefile
10
+ lib/rack/svelte.rb
11
+ lib/rack/svelte/cogs.rb
12
+ lib/rack/svelte/version.rb
13
+ test/files/hello_world.html
14
+ test/files/hello_world_output.txt
15
+ test/files/hello_world_output_iife.txt
16
+ test/rack_svelte_test.rb
17
+ test/test_app_template/app/components/hello_world.html
18
+ test/test_app_template/public/app/js/.keep
19
+ test/test_helper.rb
data/README.md ADDED
@@ -0,0 +1,137 @@
1
+ # svelte-rack
2
+
3
+ Rack middleware for compiling [Svelte components] to Javascript
4
+
5
+ [Svelte components]: https://svelte.technology
6
+
7
+
8
+ ## Usage
9
+
10
+ ```
11
+ # Within a rackup file (or with Rack::Builder):
12
+ # require 'rack/svelte'
13
+ # use Rack::Svelte,
14
+ # #:app_root_dir => ::File.expand_path('..', __FILE__),
15
+ # :app_root_dir => Rack::Directory.new('').root,
16
+ # :components_dir_in => '/app/components',
17
+ # :components_dir_out => '/public/app/js',
18
+ # :format => 'iife'
19
+ # run app
20
+ #
21
+ # Rails example:
22
+ # # above Rails::Initializer block
23
+ # require 'rack/svelte'
24
+ #
25
+ # # inside Rails::Initializer block
26
+ # config.middleware.use Rack::Svelte,
27
+ # #:app_root_dir => Rails.root.to_s,
28
+ # :components_dir_in => '/app/components',
29
+ # :components_dir_out => '/public/app/js',
30
+ # :format => 'iife'
31
+
32
+ ```
33
+
34
+ ## Installation
35
+
36
+ Command Line
37
+
38
+ ```
39
+ gem install svelte-rack
40
+ ```
41
+
42
+ Gemfile
43
+
44
+ ```
45
+ gem "svelte-rack", "~>0.1"
46
+ ```
47
+
48
+ ## Development
49
+
50
+ ### Svelte
51
+
52
+ https://svelte.technology
53
+
54
+ * svelte-rack compiles components from html markup on every request.
55
+ * svelte-rack relies on the power of the [svelte-ruby gem] and defaults to settings based on convention. However, you can override these through configuration.
56
+ * svelte-rack will assume the root dir is the expand path from the `./` dir relative to config.ru location.
57
+
58
+ [svelte-ruby gem]: https://github.com/bordeeinc/svelte-ruby
59
+
60
+
61
+ ### Dev Requirements
62
+
63
+ * [hoe](https://github.com/seattlerb/hoe) gem manager
64
+ * [hoe-bundler] may need `gem install hoe-bundler` installation before using `rake bundler:gemfile`
65
+ * [YARD](http://yardoc.org) docs
66
+ * [redcarpet](https://github.com/vmg/redcarpet) for yardoc
67
+
68
+ [hoe-bundler]: https://github.com/flavorjones/hoe-bundler
69
+
70
+ ### Testing
71
+
72
+ Tests written with [minitest]
73
+
74
+ ```
75
+ rake test
76
+ ```
77
+
78
+ [minitest]: https://github.com/seattlerb/minitest
79
+
80
+ ### Contributing
81
+
82
+ Send tested code.
83
+ Thank you, [contributors]!
84
+
85
+ [contributors]: https://github.com/bordeeinc/svelte-rack/graphs/contributors
86
+
87
+ ### To Do
88
+
89
+ * More tests
90
+ * More docs
91
+
92
+ ## License
93
+
94
+ MIT License
95
+
96
+ Copyright (c) 2017 Bordee Inc.
97
+
98
+ Permission is hereby granted, free of charge, to any person obtaining
99
+ a copy of this software and associated documentation files (the
100
+ 'Software'), to deal in the Software without restriction, including
101
+ without limitation the rights to use, copy, modify, merge, publish,
102
+ distribute, sublicense, and/or sell copies of the Software, and to
103
+ permit persons to whom the Software is furnished to do so, subject to
104
+ the following conditions:
105
+
106
+ The above copyright notice and this permission notice shall be
107
+ included in all copies or substantial portions of the Software.
108
+
109
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
110
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
111
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
112
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
113
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
114
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
115
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
116
+
117
+ ## About
118
+
119
+ ![bordee](http://bordee.com/src/img/surf-with-bordee-github.png)
120
+
121
+ svelte-rack is maintained and funded by Bordee Inc.
122
+ The names and logos for Bordee are trademarks of [Bordee Inc.][bordeeinc]
123
+
124
+ [bordeeinc]: http://bordee.com
125
+
126
+ Thanks to [Phil Misiowiec] and [Robert Bialek] of [Tidy gem]--Tidy was used as boilerplate.
127
+
128
+ [Tidy gem]: https://github.com/rbialek/rack-tidy
129
+ [Phil Misiowiec]: https://github.com/philm
130
+ [Robert Bialek]: https://github.com/rbialek
131
+
132
+ We love open source software!
133
+ See [our other projects][bordee-github]
134
+ and [check out Seattle.rb!][community]
135
+
136
+ [bordee-github]: https://github.com/bordeeinc
137
+ [community]: https://seattlerb.org
data/Rakefile ADDED
@@ -0,0 +1,126 @@
1
+ # -*- ruby -*-
2
+
3
+ require "rubygems"
4
+ require "hoe"
5
+ require './lib/rack/svelte/version.rb'
6
+
7
+ Hoe.plugin :gemspec
8
+ Hoe.plugin :minitest
9
+ Hoe.plugin :yard
10
+ Hoe.plugin :bundler
11
+ Hoe.plugin :git
12
+ Hoe.plugin :ignore
13
+
14
+ Hoe.spec "svelte-rack" do
15
+ developer("So Awesome Man", "callme@1800aweso.me")
16
+
17
+ license "MIT" # this should match the license in the README
18
+
19
+ self.email = 'support@bordee.com'
20
+
21
+ self.name = 'svelte-rack'
22
+ self.version = Rack::Svelte::VERSION
23
+ self.summary = 'Rack middleware to render Svelte components to Javascript'
24
+ self.description = self.summary
25
+ self.urls = ['https://github.com/bordeeinc/svelte-rack']
26
+ self.testlib = :minitest
27
+ self.readme_file = 'README.md'
28
+ self.history_file = 'History.txt'
29
+
30
+ # third-party
31
+ self.yard_title = self.name
32
+ self.yard_markup = 'markdown'
33
+
34
+ if Object.const_defined?('Yard')
35
+ self.extra_deps += [
36
+ ['svelte-ruby', '~> 0.1'],
37
+ ['rack', '~> 2.0']
38
+ ]
39
+ end
40
+
41
+ self.extra_dev_deps += [
42
+ ["hoe-yard", "~> 0.1"],
43
+ ["hoe-ignore", "~> 1.0"],
44
+ ["hoe-bundler", "~> 1.2"],
45
+ ["hoe-gemspec", "~> 1.0"],
46
+ ["hoe-git", "~> 1.6"],
47
+ ["minitest", "~> 5.9"],
48
+ ["yard", "~> 0.8"],
49
+ ["redcarpet", "~> 3.3"] # yard/markdown
50
+ ]
51
+
52
+ self.clean_globs += [
53
+ '.yardoc',
54
+ 'vendor',
55
+ 'Gemfile.lock',
56
+ '.bundle',
57
+ ]
58
+
59
+ self.spec_extras = {
60
+ # Rack 2.0 require Ruby 2.2.2
61
+ :required_ruby_version => '>= 2.2.2'
62
+ }
63
+ end
64
+
65
+ # require rake tasks
66
+ current_dir = File.expand_path(File.dirname(__FILE__))
67
+ Dir.glob(File.join(current_dir, 'lib/tasks/*.rake')).each {|r| import r}
68
+
69
+ # vim: syntax=ruby
70
+
71
+ #
72
+ # require 'rake'
73
+ #
74
+ # begin
75
+ # require 'jeweler'
76
+ # Jeweler::Tasks.new do |gem|
77
+ # gem.name = "rack-tidy"
78
+ # gem.summary = %Q{Rack middleware for automatically cleaning markup using Tidy}
79
+ # gem.description = %Q{Rack middleware for automatically cleaning markup using Tidy}
80
+ # gem.email = "rbialek@gmail.com"
81
+ # gem.homepage = "http://github.com/rbialek/rack-tidy"
82
+ # gem.authors = ["Phil Misiowiec","Robert Bialek"]
83
+ # gem.files = FileList['lib/**/*.rb']
84
+ # gem.add_dependency 'rack', '>= 1.0.0'
85
+ # gem.add_dependency 'tidy', '>= 1.1.2'
86
+ # end
87
+ #
88
+ # rescue LoadError
89
+ # puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
90
+ # end
91
+ #
92
+ # require 'rake/testtask'
93
+ # Rake::TestTask.new(:test) do |test|
94
+ # test.libs << 'lib' << 'test'
95
+ # test.pattern = 'test/**/*_test.rb'
96
+ # test.verbose = true
97
+ # end
98
+ #
99
+ # begin
100
+ # require 'rcov/rcovtask'
101
+ # Rcov::RcovTask.new do |test|
102
+ # test.libs << 'test'
103
+ # test.pattern = 'test/**/*_test.rb'
104
+ # test.verbose = true
105
+ # end
106
+ # rescue LoadError
107
+ # task :rcov do
108
+ # abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
109
+ # end
110
+ # end
111
+ #
112
+ # task :default => :test
113
+ #
114
+ # require 'rake/rdoctask'
115
+ # Rake::RDocTask.new do |rdoc|
116
+ # if File.exist?('VERSION')
117
+ # version = File.read('VERSION')
118
+ # else
119
+ # version = ""
120
+ # end
121
+ #
122
+ # rdoc.rdoc_dir = 'rdoc'
123
+ # rdoc.title = "rack-tidy #{version}"
124
+ # rdoc.rdoc_files.include('README*')
125
+ # rdoc.rdoc_files.include('lib/**/*.rb')
126
+ # end
@@ -0,0 +1,37 @@
1
+ require 'rack'
2
+ require 'svelte'
3
+ require 'FileUtils' unless Object.const_defined?('FileUtils')
4
+
5
+ # Svelte for Rack w/ Cogs
6
+ #
7
+ # Within a rackup file (or with Rack::Builder):
8
+ # require 'rack/svelte'
9
+ # use Rack::Svelte,
10
+ # #:app_root_dir => ::File.expand_path('..', __FILE__),
11
+ # #:app_root_dir => Rack::Directory.new('').root,
12
+ # :components_dir_in => '/app/components',
13
+ # :components_dir_out => '/public/app/js',
14
+ # :format => 'iife'
15
+ # run app
16
+ #
17
+ # Rails example:
18
+ # # above Rails::Initializer block
19
+ # require 'rack/svelte'
20
+ #
21
+ # # inside Rails::Initializer block
22
+ # config.middleware.use Rack::Svelte,
23
+ # :app_root_dir => Rails.root.to_s,
24
+ # :components_dir_in => '/app/components',
25
+ # :components_dir_out => '/public/app/js',
26
+ # :format => 'iife'
27
+ module Rack::Svelte
28
+ autoload :Cogs, 'rack/svelte/cogs'
29
+
30
+ # Create a new Rack::Svelte middleware component that builds Svelte components
31
+ # using the svelte-ruby gem. The +options+ Hash can include any Svelte compiler
32
+ # options AND components_dir_in, components_dir_in
33
+ # @see Svelte on Github for options: https://github.com/sveltejs/svelte#options
34
+ def self.new(backend, options = {})
35
+ Cogs.new(backend, options)
36
+ end
37
+ end
@@ -0,0 +1,96 @@
1
+ require 'svelte' unless Object.const_defined?('Svelte')
2
+ require 'FileUtils' unless Object.const_defined?('FileUtils')
3
+
4
+ module Rack::Svelte
5
+ # This class is the interface between Rack and the svelte-ruby gem
6
+ class Cogs
7
+
8
+ # defaults for the Svelte gem config
9
+ DEFAULT_SVELTE_OPTS = {}
10
+ DEFAULT_ROOT = '/'
11
+ DEFAULT_COMPONENTS_IN = '/app/components'
12
+ DEFAULT_COMPONENTS_OUT = '/public/app/js'
13
+ DEFAULT_DIR_OUT_OVERWRITE = false
14
+ DEFAULT_DIR_OUT_CLEAR = false
15
+
16
+ # svelte-ruby gem options, see https://github.com/sveltejs/svelte#options
17
+ attr_accessor :svelte_options
18
+
19
+ # options for Rack::Svelte processing
20
+ attr_accessor :app_root_dir
21
+ attr_accessor :components_dir_in
22
+ attr_accessor :components_dir_out
23
+ attr_accessor :dir_out_overwrite
24
+ attr_accessor :dir_out_clear
25
+
26
+ def initialize(app, options = {})
27
+ @app = app
28
+ symbolize_keys!(options)
29
+ self.app_root_dir = options.delete(:app_root_dir) || DEFAULT_ROOT
30
+ self.components_dir_in = options.delete(:components_dir_in) || DEFAULT_COMPONENTS_IN
31
+ self.components_dir_out = options.delete(:components_dir_out) || DEFAULT_COMPONENTS_OUT
32
+ self.dir_out_overwrite = options.delete(:dir_out_overwrite) || DEFAULT_DIR_OUT_OVERWRITE
33
+ self.dir_out_clear = options.delete(:dir_out_clear) || DEFAULT_DIR_OUT_CLEAR
34
+ self.svelte_options = DEFAULT_SVELTE_OPTS.merge(options)
35
+ end
36
+
37
+ # method required by Rack interface
38
+ def call(env)
39
+ call! env
40
+ end
41
+
42
+ # thread safe version using shallow copy of env
43
+ def call!(env)
44
+ dir_in = File.join(self.app_root_dir, self.components_dir_in)
45
+ dir_out = File.join(self.app_root_dir, self.components_dir_out)
46
+
47
+ raise "ERROR: Dir In cannot be found: #{dir_in}" unless Dir.exist? dir_in
48
+ raise "ERROR: Dir Out cannot be found: #{dir_out}" unless Dir.exist? dir_out
49
+
50
+ FileUtils.rm File.join(dir_out, '*') if self.dir_out_clear
51
+
52
+ Dir.glob(File.join(dir_in, '**/*')).each do |filename_in|
53
+ # only html components
54
+ next unless filename_in =~ /.\.html$/i
55
+
56
+ # create output filename
57
+ puts "dir_in: #{dir_in}"
58
+ puts "filename_in: #{filename_in.inspect}"
59
+ fn_in = filename_in[dir_in.length..-1] # remove input dir
60
+ fn_out_basename = File.basename(fn_in).split('.')[0] + '.js'
61
+ fn_out_dir = File.dirname(fn_in)
62
+ fn_out = File.join(dir_out, fn_out_dir, fn_out_basename) # Kenny Powers
63
+
64
+ # ensure output dir
65
+ FileUtils.mkdir_p(fn_out_dir) unless Dir.exist?(fn_out_dir)
66
+
67
+ # compile
68
+ sv_hash = ::Svelte.exec_method('svelte.compile', filename_in, *self.svelte_options)
69
+ #puts "sv_hash: #{sv_hash.inspect}"
70
+
71
+ unless self.dir_out_overwrite
72
+ raise "Error: file exists: #{fn_out}\n use option: 'dir_out_overwrite: true'" if File.exist?(fn_out)
73
+ end
74
+
75
+ # write
76
+ IO.write(fn_out, sv_hash['code'])
77
+ end
78
+
79
+ ##
80
+ # TODO: research thread safety (the following is adopted)
81
+ @env = env.dup
82
+ @app.call(@env)
83
+ end
84
+
85
+ private
86
+
87
+ def symbolize_keys(arg_hash)
88
+ arg_hash.each_with_object({}) {|(k,v),h| h[k.to_sym] = v}
89
+ end
90
+
91
+ def symbolize_keys!(arg_hash)
92
+ arg_hash = symbolize_keys(arg_hash)
93
+ end
94
+
95
+ end
96
+ end
@@ -0,0 +1,5 @@
1
+ module Rack
2
+ module Svelte
3
+ VERSION = '0.1.0'
4
+ end
5
+ end
@@ -0,0 +1 @@
1
+ <h1>Hello {{name}}</h1>
@@ -0,0 +1,179 @@
1
+ function renderMainFragment ( root, component ) {
2
+ var h1 = createElement( 'h1' );
3
+
4
+ appendNode( createText( "Hello " ), h1 );
5
+ var last_text1 = root.name
6
+ var text1 = createText( last_text1 );
7
+ appendNode( text1, h1 );
8
+
9
+ return {
10
+ mount: function ( target, anchor ) {
11
+ insertNode( h1, target, anchor );
12
+ },
13
+
14
+ update: function ( changed, root ) {
15
+ var __tmp;
16
+
17
+ if ( ( __tmp = root.name ) !== last_text1 ) {
18
+ text1.data = last_text1 = __tmp;
19
+ }
20
+ },
21
+
22
+ teardown: function ( detach ) {
23
+ if ( detach ) {
24
+ detachNode( h1 );
25
+ }
26
+ }
27
+ };
28
+ }
29
+
30
+ function SvelteComponent ( options ) {
31
+ options = options || {};
32
+ this._state = options.data || {};
33
+
34
+ this._observers = {
35
+ pre: Object.create( null ),
36
+ post: Object.create( null )
37
+ };
38
+
39
+ this._handlers = Object.create( null );
40
+
41
+ this._root = options._root;
42
+ this._yield = options._yield;
43
+
44
+ this._torndown = false;
45
+
46
+ this._fragment = renderMainFragment( this._state, this );
47
+ if ( options.target ) this._fragment.mount( options.target, null );
48
+ }
49
+
50
+ SvelteComponent.prototype.get = get;
51
+ SvelteComponent.prototype.fire = fire;
52
+ SvelteComponent.prototype.observe = observe;
53
+ SvelteComponent.prototype.on = on;
54
+ SvelteComponent.prototype.set = set;
55
+ SvelteComponent.prototype._flush = _flush;
56
+
57
+ SvelteComponent.prototype._set = function _set ( newState ) {
58
+ var oldState = this._state;
59
+ this._state = Object.assign( {}, oldState, newState );
60
+
61
+ dispatchObservers( this, this._observers.pre, newState, oldState );
62
+ if ( this._fragment ) this._fragment.update( newState, this._state );
63
+ dispatchObservers( this, this._observers.post, newState, oldState );
64
+ };
65
+
66
+ SvelteComponent.prototype.teardown = SvelteComponent.prototype.destroy = function destroy ( detach ) {
67
+ this.fire( 'destroy' );
68
+
69
+ this._fragment.teardown( detach !== false );
70
+ this._fragment = null;
71
+
72
+ this._state = {};
73
+ this._torndown = true;
74
+ };
75
+
76
+ function createElement( name ) {
77
+ return document.createElement( name );
78
+ }
79
+
80
+ function detachNode( node ) {
81
+ node.parentNode.removeChild( node );
82
+ }
83
+
84
+ function insertNode( node, target, anchor ) {
85
+ target.insertBefore( node, anchor );
86
+ }
87
+
88
+ function createText( data ) {
89
+ return document.createTextNode( data );
90
+ }
91
+
92
+ function appendNode( node, target ) {
93
+ target.appendChild( node );
94
+ }
95
+
96
+ function dispatchObservers( component, group, newState, oldState ) {
97
+ for ( var key in group ) {
98
+ if ( !( key in newState ) ) continue;
99
+
100
+ var newValue = newState[ key ];
101
+ var oldValue = oldState[ key ];
102
+
103
+ if ( newValue === oldValue && typeof newValue !== 'object' ) continue;
104
+
105
+ var callbacks = group[ key ];
106
+ if ( !callbacks ) continue;
107
+
108
+ for ( var i = 0; i < callbacks.length; i += 1 ) {
109
+ var callback = callbacks[i];
110
+ if ( callback.__calling ) continue;
111
+
112
+ callback.__calling = true;
113
+ callback.call( component, newValue, oldValue );
114
+ callback.__calling = false;
115
+ }
116
+ }
117
+ }
118
+
119
+ function get( key ) {
120
+ return key ? this._state[ key ] : this._state;
121
+ }
122
+
123
+ function fire( eventName, data ) {
124
+ var handlers = eventName in this._handlers && this._handlers[ eventName ].slice();
125
+ if ( !handlers ) return;
126
+
127
+ for ( var i = 0; i < handlers.length; i += 1 ) {
128
+ handlers[i].call( this, data );
129
+ }
130
+ }
131
+
132
+ function observe( key, callback, options ) {
133
+ var group = ( options && options.defer ) ? this._observers.pre : this._observers.post;
134
+
135
+ ( group[ key ] || ( group[ key ] = [] ) ).push( callback );
136
+
137
+ if ( !options || options.init !== false ) {
138
+ callback.__calling = true;
139
+ callback.call( this, this._state[ key ] );
140
+ callback.__calling = false;
141
+ }
142
+
143
+ return {
144
+ cancel: function () {
145
+ var index = group[ key ].indexOf( callback );
146
+ if ( ~index ) group[ key ].splice( index, 1 );
147
+ }
148
+ };
149
+ }
150
+
151
+ function on( eventName, handler ) {
152
+ if ( eventName === 'teardown' ) return this.on( 'destroy', handler );
153
+
154
+ var handlers = this._handlers[ eventName ] || ( this._handlers[ eventName ] = [] );
155
+ handlers.push( handler );
156
+
157
+ return {
158
+ cancel: function () {
159
+ var index = handlers.indexOf( handler );
160
+ if ( ~index ) handlers.splice( index, 1 );
161
+ }
162
+ };
163
+ }
164
+
165
+ function set( newState ) {
166
+ this._set( newState );
167
+ ( this._root || this )._flush();
168
+ }
169
+
170
+ function _flush() {
171
+ if ( !this._renderHooks ) return;
172
+
173
+ while ( this._renderHooks.length ) {
174
+ var hook = this._renderHooks.pop();
175
+ hook.fn.call( hook.context );
176
+ }
177
+ }
178
+
179
+ export default SvelteComponent;
@@ -0,0 +1,183 @@
1
+ var SvelteComponent = (function () { 'use strict';
2
+
3
+ function renderMainFragment ( root, component ) {
4
+ var h1 = createElement( 'h1' );
5
+
6
+ appendNode( createText( "Hello " ), h1 );
7
+ var last_text1 = root.name
8
+ var text1 = createText( last_text1 );
9
+ appendNode( text1, h1 );
10
+
11
+ return {
12
+ mount: function ( target, anchor ) {
13
+ insertNode( h1, target, anchor );
14
+ },
15
+
16
+ update: function ( changed, root ) {
17
+ var __tmp;
18
+
19
+ if ( ( __tmp = root.name ) !== last_text1 ) {
20
+ text1.data = last_text1 = __tmp;
21
+ }
22
+ },
23
+
24
+ teardown: function ( detach ) {
25
+ if ( detach ) {
26
+ detachNode( h1 );
27
+ }
28
+ }
29
+ };
30
+ }
31
+
32
+ function SvelteComponent ( options ) {
33
+ options = options || {};
34
+ this._state = options.data || {};
35
+
36
+ this._observers = {
37
+ pre: Object.create( null ),
38
+ post: Object.create( null )
39
+ };
40
+
41
+ this._handlers = Object.create( null );
42
+
43
+ this._root = options._root;
44
+ this._yield = options._yield;
45
+
46
+ this._torndown = false;
47
+
48
+ this._fragment = renderMainFragment( this._state, this );
49
+ if ( options.target ) this._fragment.mount( options.target, null );
50
+ }
51
+
52
+ SvelteComponent.prototype.get = get;
53
+ SvelteComponent.prototype.fire = fire;
54
+ SvelteComponent.prototype.observe = observe;
55
+ SvelteComponent.prototype.on = on;
56
+ SvelteComponent.prototype.set = set;
57
+ SvelteComponent.prototype._flush = _flush;
58
+
59
+ SvelteComponent.prototype._set = function _set ( newState ) {
60
+ var oldState = this._state;
61
+ this._state = Object.assign( {}, oldState, newState );
62
+
63
+ dispatchObservers( this, this._observers.pre, newState, oldState );
64
+ if ( this._fragment ) this._fragment.update( newState, this._state );
65
+ dispatchObservers( this, this._observers.post, newState, oldState );
66
+ };
67
+
68
+ SvelteComponent.prototype.teardown = SvelteComponent.prototype.destroy = function destroy ( detach ) {
69
+ this.fire( 'destroy' );
70
+
71
+ this._fragment.teardown( detach !== false );
72
+ this._fragment = null;
73
+
74
+ this._state = {};
75
+ this._torndown = true;
76
+ };
77
+
78
+ function createElement( name ) {
79
+ return document.createElement( name );
80
+ }
81
+
82
+ function detachNode( node ) {
83
+ node.parentNode.removeChild( node );
84
+ }
85
+
86
+ function insertNode( node, target, anchor ) {
87
+ target.insertBefore( node, anchor );
88
+ }
89
+
90
+ function createText( data ) {
91
+ return document.createTextNode( data );
92
+ }
93
+
94
+ function appendNode( node, target ) {
95
+ target.appendChild( node );
96
+ }
97
+
98
+ function dispatchObservers( component, group, newState, oldState ) {
99
+ for ( var key in group ) {
100
+ if ( !( key in newState ) ) continue;
101
+
102
+ var newValue = newState[ key ];
103
+ var oldValue = oldState[ key ];
104
+
105
+ if ( newValue === oldValue && typeof newValue !== 'object' ) continue;
106
+
107
+ var callbacks = group[ key ];
108
+ if ( !callbacks ) continue;
109
+
110
+ for ( var i = 0; i < callbacks.length; i += 1 ) {
111
+ var callback = callbacks[i];
112
+ if ( callback.__calling ) continue;
113
+
114
+ callback.__calling = true;
115
+ callback.call( component, newValue, oldValue );
116
+ callback.__calling = false;
117
+ }
118
+ }
119
+ }
120
+
121
+ function get( key ) {
122
+ return key ? this._state[ key ] : this._state;
123
+ }
124
+
125
+ function fire( eventName, data ) {
126
+ var handlers = eventName in this._handlers && this._handlers[ eventName ].slice();
127
+ if ( !handlers ) return;
128
+
129
+ for ( var i = 0; i < handlers.length; i += 1 ) {
130
+ handlers[i].call( this, data );
131
+ }
132
+ }
133
+
134
+ function observe( key, callback, options ) {
135
+ var group = ( options && options.defer ) ? this._observers.pre : this._observers.post;
136
+
137
+ ( group[ key ] || ( group[ key ] = [] ) ).push( callback );
138
+
139
+ if ( !options || options.init !== false ) {
140
+ callback.__calling = true;
141
+ callback.call( this, this._state[ key ] );
142
+ callback.__calling = false;
143
+ }
144
+
145
+ return {
146
+ cancel: function () {
147
+ var index = group[ key ].indexOf( callback );
148
+ if ( ~index ) group[ key ].splice( index, 1 );
149
+ }
150
+ };
151
+ }
152
+
153
+ function on( eventName, handler ) {
154
+ if ( eventName === 'teardown' ) return this.on( 'destroy', handler );
155
+
156
+ var handlers = this._handlers[ eventName ] || ( this._handlers[ eventName ] = [] );
157
+ handlers.push( handler );
158
+
159
+ return {
160
+ cancel: function () {
161
+ var index = handlers.indexOf( handler );
162
+ if ( ~index ) handlers.splice( index, 1 );
163
+ }
164
+ };
165
+ }
166
+
167
+ function set( newState ) {
168
+ this._set( newState );
169
+ ( this._root || this )._flush();
170
+ }
171
+
172
+ function _flush() {
173
+ if ( !this._renderHooks ) return;
174
+
175
+ while ( this._renderHooks.length ) {
176
+ var hook = this._renderHooks.pop();
177
+ hook.fn.call( hook.context );
178
+ }
179
+ }
180
+
181
+ return SvelteComponent;
182
+
183
+ }());
@@ -0,0 +1,42 @@
1
+ require 'test_helper'
2
+
3
+ describe "Rack::Svelte" do
4
+ before do
5
+ @test_root = File.expand_path(File.dirname(__FILE__))
6
+ @test_app_root = File.join(@test_root, "test_app")
7
+ @test_app_template = File.join(@test_root, "test_app_template")
8
+ @files = File.join(@test_root, "files")
9
+
10
+ @hello_world_filename = File.join(@files, 'hello_world.html')
11
+ @hello_world_output = IO.read(File.join(@files, 'hello_world_output.txt'))
12
+ @hello_world_output_iife = IO.read(File.join(@files, 'hello_world_output_iife.txt'))
13
+
14
+ @hello_world_test_html = File.join(@test_app_root, Rack::Svelte::Cogs::DEFAULT_COMPONENTS_IN, 'hello_world.html')
15
+ @hello_world_test_js = File.join(@test_app_root, Rack::Svelte::Cogs::DEFAULT_COMPONENTS_OUT, 'hello_world.js')
16
+
17
+ FileUtils.rm_rf @test_app_root
18
+ FileUtils.cp_r @test_app_template, @test_app_root
19
+
20
+ @options = {
21
+ app_root_dir: @test_app_root
22
+ }
23
+ end
24
+
25
+ describe "with content type equal to 'text/html'" do
26
+ it "should compile" do
27
+ assert File.exist?(@hello_world_test_html)
28
+ refute File.exist?(@hello_world_test_js)
29
+
30
+ get_response('/', '<!--BODY-->', 'text/html', @options)
31
+ assert File.exist? @hello_world_test_js
32
+
33
+ exp = @hello_world_output
34
+ act = IO.read(@hello_world_test_js)
35
+ assert_equal exp, act
36
+ end
37
+ end
38
+
39
+ after do
40
+ FileUtils.rm_rf @test_app_root
41
+ end
42
+ end
@@ -0,0 +1 @@
1
+ <h1>Hello {{name}}</h1>
File without changes
@@ -0,0 +1,15 @@
1
+ gem "minitest"
2
+ require 'minitest/spec'
3
+ require "minitest/autorun"
4
+ require "rack/svelte"
5
+ require 'rack/mock'
6
+ #require 'FileUtils' unless Object.const_defined?('FileUtils')
7
+
8
+ def get_response(path, body, content_type = 'text/html', options = {})
9
+ app = Rack::Builder.new do
10
+ use Rack::Svelte, options
11
+ run lambda {|env| [200, {'Content-Type' => content_type}, [body]]}
12
+ end
13
+
14
+ Rack::MockRequest.new(app).get(path)
15
+ end
metadata ADDED
@@ -0,0 +1,196 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: svelte-rack
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - So Awesome Man
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-03-22 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: minitest
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '5.10'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '5.10'
27
+ - !ruby/object:Gem::Dependency
28
+ name: hoe-yard
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 0.1.3
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: 0.1.3
41
+ - !ruby/object:Gem::Dependency
42
+ name: hoe-ignore
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: hoe-bundler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.2'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.2'
69
+ - !ruby/object:Gem::Dependency
70
+ name: hoe-gemspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '1.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '1.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: hoe-git
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '1.6'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '1.6'
97
+ - !ruby/object:Gem::Dependency
98
+ name: yard
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '0.8'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '0.8'
111
+ - !ruby/object:Gem::Dependency
112
+ name: redcarpet
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: '3.3'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: '3.3'
125
+ - !ruby/object:Gem::Dependency
126
+ name: hoe
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - "~>"
130
+ - !ruby/object:Gem::Version
131
+ version: '3.16'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - "~>"
137
+ - !ruby/object:Gem::Version
138
+ version: '3.16'
139
+ description: Rack middleware to render Svelte components to Javascript
140
+ email: support@bordee.com
141
+ executables: []
142
+ extensions: []
143
+ extra_rdoc_files:
144
+ - History.txt
145
+ - Manifest.txt
146
+ - README.md
147
+ files:
148
+ - ".autotest"
149
+ - ".gitignore"
150
+ - ".hoeignore"
151
+ - Gemfile
152
+ - History.txt
153
+ - LICENSE
154
+ - Manifest.txt
155
+ - README.md
156
+ - Rakefile
157
+ - lib/rack/svelte.rb
158
+ - lib/rack/svelte/cogs.rb
159
+ - lib/rack/svelte/version.rb
160
+ - test/files/hello_world.html
161
+ - test/files/hello_world_output.txt
162
+ - test/files/hello_world_output_iife.txt
163
+ - test/rack_svelte_test.rb
164
+ - test/test_app_template/app/components/hello_world.html
165
+ - test/test_app_template/public/app/js/.keep
166
+ - test/test_helper.rb
167
+ homepage: https://github.com/bordeeinc/svelte-rack
168
+ licenses:
169
+ - MIT
170
+ metadata: {}
171
+ post_install_message:
172
+ rdoc_options:
173
+ - "--title"
174
+ - svelte-rack
175
+ - "--markup"
176
+ - markdown
177
+ - "--quiet"
178
+ require_paths:
179
+ - lib
180
+ required_ruby_version: !ruby/object:Gem::Requirement
181
+ requirements:
182
+ - - ">="
183
+ - !ruby/object:Gem::Version
184
+ version: 2.2.2
185
+ required_rubygems_version: !ruby/object:Gem::Requirement
186
+ requirements:
187
+ - - ">="
188
+ - !ruby/object:Gem::Version
189
+ version: '0'
190
+ requirements: []
191
+ rubyforge_project:
192
+ rubygems_version: 2.5.2
193
+ signing_key:
194
+ specification_version: 4
195
+ summary: Rack middleware to render Svelte components to Javascript
196
+ test_files: []