svelte-rack 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f75e6f882ad966f7162d1ede2d54554f0b6a5b11
4
- data.tar.gz: 4727209786305ec228b836ac29ee455128e43df2
3
+ metadata.gz: 8b117fbcf00b33dae587fac2181a2c5df32fac42
4
+ data.tar.gz: d22cba61869370da7124a9ef8a51fcd7e729600e
5
5
  SHA512:
6
- metadata.gz: 475d21f50ae1f1cea105c0e199b149c6c33560528ba45b936951f2706dd61d1e95e8f77b2cdb12702d332aff7691418b7619798899d133ef0db2e14bafe9ee65
7
- data.tar.gz: 1b615152a3937a0e6258efb23587133a8590dc5c24dcbacbf2b6c92e1cb7e98dbd6b471c79244011a5d265ef01f9ffadfcf2468ee41b1833ce8071acd316b147
6
+ metadata.gz: 3709f09d2684c31f543ed9186394d86305c22bb03e22ae3658483497d55797820bc08ba4075c8d35ae19ef31c3ab77033fbe4a86acd96e0ab11fcf2120d3805d
7
+ data.tar.gz: 31a8b04b7f2eb30dd02267791e019e0917b1b53e7d1f2c9c1a12767a0561d1902c3faae597c5242c55f5046dca99b2c4e4073e549ada066af63433e712bec974
data/README.md CHANGED
@@ -3,32 +3,44 @@
3
3
  Rack middleware for compiling [Svelte components] to Javascript
4
4
 
5
5
  [Svelte components]: https://svelte.technology
6
+
7
+ [![Gem Version](https://badge.fury.io/rb/svelte-rack.svg)](https://badge.fury.io/rb/svelte-rack)
6
8
 
7
9
 
8
10
  ## Usage
9
11
 
10
12
  ```
11
- # Within a rackup file (or with Rack::Builder):
13
+ # @example Within a rackup file (or with Rack::Builder)
12
14
  # require 'rack/svelte'
13
15
  # 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'
16
+ # #app_root_dir: Rack::Directory.new('').root,
17
+ # app_root_dir: ::File.expand_path(File.dirname(__FILE__)),
18
+ # components_dir_in: '/app/components',
19
+ # components_dir_out: '/public/app/js',
20
+ # default_dir_out_overwrite: false,
21
+ # default_dir_out_clear: false,
22
+ # format: 'iife'
19
23
  # run app
20
24
  #
21
- # Rails example:
25
+ # @example Rails example
22
26
  # # above Rails::Initializer block
23
27
  # require 'rack/svelte'
24
28
  #
25
29
  # # inside Rails::Initializer block
26
30
  # 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
-
31
+ # app_root_dir: Rails.root.to_s,
32
+ # components_dir_in: '/app/components',
33
+ # components_dir_out: '/public/app/js',
34
+ # default_dir_out_overwrite: false,
35
+ # default_dir_out_clear: false,
36
+ # format: 'iife'
37
+ #
38
+ # Default svelte-rack options are:
39
+ # default_root: '/'
40
+ # default_components_in: '/app/components'
41
+ # default_components_out: '/public/app/js'
42
+ # default_dir_out_overwrite: false
43
+ # default_dir_out_clear: false
32
44
  ```
33
45
 
34
46
  ## Installation
data/lib/rack/svelte.rb CHANGED
@@ -2,35 +2,58 @@ require 'rack'
2
2
  require 'svelte'
3
3
  require 'FileUtils' unless Object.const_defined?('FileUtils')
4
4
 
5
- # Svelte for Rack w/ Cogs
5
+ # Svelte for Rack w/ Cogs(tm)
6
6
  #
7
- # Within a rackup file (or with Rack::Builder):
7
+ # @example Within a rackup file (or with Rack::Builder)
8
8
  # require 'rack/svelte'
9
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'
10
+ # #app_root_dir: Rack::Directory.new('').root,
11
+ # app_root_dir: ::File.expand_path(File.dirname(__FILE__)),
12
+ # components_dir_in: '/app/components',
13
+ # components_dir_out: '/public/app/js',
14
+ # default_dir_out_overwrite: false,
15
+ # default_dir_out_clear: false,
16
+ # format: 'iife'
15
17
  # run app
16
18
  #
17
- # Rails example:
19
+ # @example Rails example
18
20
  # # above Rails::Initializer block
19
21
  # require 'rack/svelte'
20
22
  #
21
23
  # # inside Rails::Initializer block
22
24
  # 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'
25
+ # app_root_dir: Rails.root.to_s,
26
+ # components_dir_in: '/app/components',
27
+ # components_dir_out: '/public/app/js',
28
+ # default_dir_out_overwrite: false,
29
+ # default_dir_out_clear: false,
30
+ # format: 'iife'
31
+ #
27
32
  module Rack::Svelte
28
33
  autoload :Cogs, 'rack/svelte/cogs'
29
34
 
30
35
  # 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
36
+ # using the svelte-ruby gem. The +options+ Hash can include any Svelte compiler
37
+ # options AND components_dir_in, components_dir_in
38
+ #
33
39
  # @see Svelte on Github for options: https://github.com/sveltejs/svelte#options
40
+ #
41
+ # @param backend [#rack_object] set implicitly by Rack, use the var: `backend`
42
+ # @param [Hash] options for processing and svelte compiler
43
+ # @option options [String] :app_root_dir app's root dir (path expanded)
44
+ # @option options [String] :components_dir_in where svelte-rack look for components (path begins at app's root dir)
45
+ # @option options [String] :components_dir_out where svelte-rack writes compiled js (path begins at app's root dir)
46
+ # @option options [Boolean] :dir_out_overwrite overwrite any files with matching name in the components_dir_out dir
47
+ # @option options [Boolean] :dir_out_clear clear entire components_dir_out dir before writing new files
48
+ # @return [void]
49
+ #
50
+ # Default svelte-rack options are:
51
+ # default_root: '/'
52
+ # default_components_in: '/app/components'
53
+ # default_components_out: '/public/app/js'
54
+ # default_dir_out_overwrite: false
55
+ # default_dir_out_clear: false
56
+ #
34
57
  def self.new(backend, options = {})
35
58
  Cogs.new(backend, options)
36
59
  end
@@ -12,60 +12,48 @@ module Rack::Svelte
12
12
  DEFAULT_COMPONENTS_OUT = '/public/app/js'
13
13
  DEFAULT_DIR_OUT_OVERWRITE = false
14
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
15
+ SVELTE_RACK_ATTRS = [:app_root_dir, :components_dir_in, :components_dir_out, :dir_out_overwrite, :dir_out_clear]
25
16
 
26
17
  def initialize(app, options = {})
27
18
  @app = app
28
19
  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)
20
+ @app_root_dir = options.fetch(:app_root_dir, DEFAULT_ROOT)
21
+ @components_dir_in = options.fetch(:components_dir_in, DEFAULT_COMPONENTS_IN)
22
+ @components_dir_out = options.fetch(:components_dir_out, DEFAULT_COMPONENTS_OUT)
23
+ @dir_out_overwrite = options.fetch(:dir_out_overwrite, DEFAULT_DIR_OUT_OVERWRITE)
24
+ @dir_out_clear = options.fetch(:dir_out_clear, DEFAULT_DIR_OUT_CLEAR)
25
+ @svelte_options = DEFAULT_SVELTE_OPTS.merge(options.reject {|x| SVELTE_RACK_ATTRS.include?(x)})
35
26
  end
36
27
 
37
- # method required by Rack interface
38
28
  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)
29
+ dir_in = File.join(@app_root_dir, @components_dir_in)
30
+ dir_out = File.join(@app_root_dir, @components_dir_out)
46
31
 
47
32
  raise "ERROR: Dir In cannot be found: #{dir_in}" unless Dir.exist? dir_in
48
33
  raise "ERROR: Dir Out cannot be found: #{dir_out}" unless Dir.exist? dir_out
49
34
 
50
- FileUtils.rm File.join(dir_out, '*') if self.dir_out_clear
35
+ FileUtils.rm File.join(dir_out, '*') if @dir_out_clear
51
36
 
52
37
  Dir.glob(File.join(dir_in, '**/*')).each do |filename_in|
53
38
  # only html components
54
39
  next unless filename_in =~ /.\.html$/i
55
40
 
56
41
  # create output filename
57
- fn_in = filename_in[dir_in.length..-1] # remove input dir
58
- fn_out_basename = File.basename(fn_in).split('.')[0] + '.js'
59
- fn_out_dir = File.dirname(fn_in)
60
- fn_out = File.join(dir_out, fn_out_dir, fn_out_basename) # Kenny Powers
42
+ fn_in = filename_in[dir_in.length..-1] # remove input dir
43
+ fn_out_basename = File.basename(fn_in, '.html')
44
+ fn_out_basename_js = File.basename(fn_in, '.html') + '.js'
45
+ fn_out_dir = File.dirname(fn_in)
46
+ fn_out = File.join(dir_out, fn_out_dir, fn_out_basename + '.js') # Kenny Powers
61
47
 
62
48
  # ensure output dir
63
49
  FileUtils.mkdir_p(fn_out_dir) unless Dir.exist?(fn_out_dir)
64
50
 
65
51
  # compile
66
- sv_hash = ::Svelte.exec_method('svelte.compile', filename_in, *self.svelte_options)
52
+ comp_opts = @svelte_options.dup
53
+ comp_opts.merge!(name: fn_out_basename) unless comp_opts.key?(:name)
54
+ sv_hash = ::Svelte.exec_method('svelte.compile', filename_in, nil, comp_opts)
67
55
 
68
- unless self.dir_out_overwrite
56
+ unless @dir_out_overwrite
69
57
  raise "Error: file exists: #{fn_out}\n use option: 'dir_out_overwrite: true'" if File.exist?(fn_out)
70
58
  end
71
59
 
@@ -73,10 +61,7 @@ module Rack::Svelte
73
61
  IO.write(fn_out, sv_hash['code'])
74
62
  end
75
63
 
76
- ##
77
- # TODO: research thread safety (the following is adopted)
78
- @env = env.dup
79
- @app.call(@env)
64
+ @app.call(env)
80
65
  end
81
66
 
82
67
  private
@@ -1,5 +1,5 @@
1
1
  module Rack
2
2
  module Svelte
3
- VERSION = '0.1.1'
3
+ VERSION = '0.1.2'
4
4
  end
5
5
  end
@@ -27,7 +27,7 @@ function renderMainFragment ( root, component ) {
27
27
  };
28
28
  }
29
29
 
30
- function SvelteComponent ( options ) {
30
+ function hello_world ( options ) {
31
31
  options = options || {};
32
32
  this._state = options.data || {};
33
33
 
@@ -47,14 +47,14 @@ function SvelteComponent ( options ) {
47
47
  if ( options.target ) this._fragment.mount( options.target, null );
48
48
  }
49
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;
50
+ hello_world.prototype.get = get;
51
+ hello_world.prototype.fire = fire;
52
+ hello_world.prototype.observe = observe;
53
+ hello_world.prototype.on = on;
54
+ hello_world.prototype.set = set;
55
+ hello_world.prototype._flush = _flush;
56
56
 
57
- SvelteComponent.prototype._set = function _set ( newState ) {
57
+ hello_world.prototype._set = function _set ( newState ) {
58
58
  var oldState = this._state;
59
59
  this._state = Object.assign( {}, oldState, newState );
60
60
 
@@ -63,7 +63,7 @@ SvelteComponent.prototype._set = function _set ( newState ) {
63
63
  dispatchObservers( this, this._observers.post, newState, oldState );
64
64
  };
65
65
 
66
- SvelteComponent.prototype.teardown = SvelteComponent.prototype.destroy = function destroy ( detach ) {
66
+ hello_world.prototype.teardown = hello_world.prototype.destroy = function destroy ( detach ) {
67
67
  this.fire( 'destroy' );
68
68
 
69
69
  this._fragment.teardown( detach !== false );
@@ -176,4 +176,4 @@ function _flush() {
176
176
  }
177
177
  }
178
178
 
179
- export default SvelteComponent;
179
+ export default hello_world;
@@ -1,4 +1,4 @@
1
- var SvelteComponent = (function () { 'use strict';
1
+ var hello_world = (function () { 'use strict';
2
2
 
3
3
  function renderMainFragment ( root, component ) {
4
4
  var h1 = createElement( 'h1' );
@@ -29,7 +29,7 @@ function renderMainFragment ( root, component ) {
29
29
  };
30
30
  }
31
31
 
32
- function SvelteComponent ( options ) {
32
+ function hello_world ( options ) {
33
33
  options = options || {};
34
34
  this._state = options.data || {};
35
35
 
@@ -49,14 +49,14 @@ function SvelteComponent ( options ) {
49
49
  if ( options.target ) this._fragment.mount( options.target, null );
50
50
  }
51
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;
52
+ hello_world.prototype.get = get;
53
+ hello_world.prototype.fire = fire;
54
+ hello_world.prototype.observe = observe;
55
+ hello_world.prototype.on = on;
56
+ hello_world.prototype.set = set;
57
+ hello_world.prototype._flush = _flush;
58
58
 
59
- SvelteComponent.prototype._set = function _set ( newState ) {
59
+ hello_world.prototype._set = function _set ( newState ) {
60
60
  var oldState = this._state;
61
61
  this._state = Object.assign( {}, oldState, newState );
62
62
 
@@ -65,7 +65,7 @@ SvelteComponent.prototype._set = function _set ( newState ) {
65
65
  dispatchObservers( this, this._observers.post, newState, oldState );
66
66
  };
67
67
 
68
- SvelteComponent.prototype.teardown = SvelteComponent.prototype.destroy = function destroy ( detach ) {
68
+ hello_world.prototype.teardown = hello_world.prototype.destroy = function destroy ( detach ) {
69
69
  this.fire( 'destroy' );
70
70
 
71
71
  this._fragment.teardown( detach !== false );
@@ -178,6 +178,6 @@ function _flush() {
178
178
  }
179
179
  }
180
180
 
181
- return SvelteComponent;
181
+ return hello_world;
182
182
 
183
- }());
183
+ }());
@@ -34,6 +34,18 @@ describe "Rack::Svelte" do
34
34
  act = IO.read(@hello_world_test_js)
35
35
  assert_equal exp, act
36
36
  end
37
+
38
+ it "should compile with iife" do
39
+ assert File.exist?(@hello_world_test_html)
40
+ refute File.exist?(@hello_world_test_js)
41
+
42
+ get_response('/', '<!--BODY-->', 'text/html', @options.merge(format: 'iife'))
43
+ assert File.exist? @hello_world_test_js
44
+
45
+ exp = @hello_world_output_iife
46
+ act = IO.read(@hello_world_test_js)
47
+ assert_equal exp, act
48
+ end
37
49
  end
38
50
 
39
51
  after do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: svelte-rack
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - So Awesome Man