Class: Inesita::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/inesita/server.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Server

Returns a new instance of Server.



19
20
21
22
23
24
25
26
# File 'lib/inesita/server.rb', line 19

def initialize(opts = {})
  setup_dirs(opts)
  setup_env(opts)
  @assets_app = create_assets_app
  @source_maps_app = create_source_maps_app
  @app = create_app
  Inesita.assets_code = assets_code
end

Instance Attribute Details

#assets_appObject (readonly)

Returns the value of attribute assets_app.



17
18
19
# File 'lib/inesita/server.rb', line 17

def assets_app
  @assets_app
end

Instance Method Details

#assets_codeObject



39
40
41
42
43
44
45
# File 'lib/inesita/server.rb', line 39

def assets_code
  assets_prefix = @dist ? '' : Config::ASSETS_PREFIX
  %(
    <link rel="stylesheet" type="text/css" href="#{assets_prefix}/stylesheet.css">
    #{Opal::Sprockets.javascript_include_tag('application', sprockets: @assets_app, prefix: assets_prefix, debug: !@dist)}
   )
end

#call(env) ⇒ Object



108
109
110
# File 'lib/inesita/server.rb', line 108

def call(env)
  @app.call(env)
end

#configure_sprockets(sprockets) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/inesita/server.rb', line 90

def configure_sprockets(sprockets)
			if sprockets.respond_to?(:register_transformer)
sprockets.register_mime_type 'text/html', extensions: ['.slim', '.html.slim', '.slim.html']
sprockets.register_preprocessor 'text/html', SlimTransformer
			elsif sprockets.respond_to?(:register_engine)
sprockets.register_engine '.slim', Slim::Template
			end

  sprockets.context_class.class_eval do
    include SprocketsContext
  end
end

#create_appObject



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/inesita/server.rb', line 47

def create_app
  assets_app = @assets_app
  source_maps_app = @source_maps_app
  static_dir = @static_dir

  Rack::Builder.new do
    use Rack::Static, :urls => [static_dir]

    use Rack::Rewrite do
      rewrite(/^(?!#{Config::ASSETS_PREFIX}|#{Config::SOURCE_MAP_PREFIX}).*/, Config::ASSETS_PREFIX)
    end

    map Config::ASSETS_PREFIX do
      run assets_app
    end

    map Config::SOURCE_MAP_PREFIX do
      run source_maps_app
    end
  end
end

#create_assets_appObject



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/inesita/server.rb', line 69

def create_assets_app
  Opal::Server.new do |s|
    s.append_path @app_dir
    if @dist
      s.append_path @app_dist_dir
    else
      s.append_path @app_dev_dir
    end

    Opal.paths.each do |p|
      s.append_path p
    end

    RailsAssets.load_paths.each do |p|
      s.append_path p
    end if defined?(RailsAssets)

    configure_sprockets(s.sprockets)
  end.sprockets
end

#create_source_maps_appObject



103
104
105
106
# File 'lib/inesita/server.rb', line 103

def create_source_maps_app
  ::Opal::Sprockets::SourceMapHeaderPatch.inject!(Config::SOURCE_MAP_PREFIX)
  Opal::SourceMapServer.new(@assets_app, Config::SOURCE_MAP_PREFIX)
end

#setup_dirs(opts) ⇒ Object



32
33
34
35
36
37
# File 'lib/inesita/server.rb', line 32

def setup_dirs(opts)
  @static_dir = opts[:static_dir] || Config::STATIC_DIR
  @app_dir = opts[:app_dir] || Config::APP_DIR
  @app_dist_dir = opts[:app_dist_dir] || Config::APP_DIST_DIR
  @app_dev_dir = opts[:app_dev_dir] || Config::APP_DEV_DIR
end

#setup_env(opts) ⇒ Object



28
29
30
# File 'lib/inesita/server.rb', line 28

def setup_env(opts)
  @dist = opts[:dist] || false
end