lookbook 0.7.3 → 0.8.0

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
  SHA256:
3
- metadata.gz: 40ea30d04c53c14779624094908938ce4c795a75a773f24226c7905e12afcdca
4
- data.tar.gz: 15b596f54edd9cff72d28cf2c3b88a5d28e05b3a323f69033bd44c50c890c3c2
3
+ metadata.gz: 79511e3188b92969dfb927515e82464ad712490ed56d4651316f237f2792d9be
4
+ data.tar.gz: 7afbe921d118e685c4d7ec1619f9431296099a9690947baca8bc041b609e8b46
5
5
  SHA512:
6
- metadata.gz: 2c998b17b156fae344b99131d7487ba74052d1aeaba6ac9ff2cd2c13371f6aa0f3fb1cea6d47b806d4cd3479d93e678fd165b8f85bc30b2c7e35bac1b2685ea5
7
- data.tar.gz: f347430b5596aaefc63c24a1db82758a86f317cc728bd7c8cb1eefe9efd243e3072186b33fc0144fd7f0fea148d584d3b350b2da09b59ba37be37e41b706f4b2
6
+ metadata.gz: e300adbcb09915cb48862621e6809a5713c8250e9122c109b3eaadcf770c1c11f4ac7865a78caac233b452f38ca05e499899baf07edcce82d499b3110c4580dc
7
+ data.tar.gz: 4385b86b3115d4ed88d13f8d7b2cb57a15792ac8ccdeb526ff6ffe3a2e35d45b6047b17b10d322d711d7087b26af7f7352aec717f01932263118fd75d40aa475
data/README.md CHANGED
@@ -13,7 +13,7 @@
13
13
  ---
14
14
 
15
15
  <div align="center">
16
- <a href="#installing">Installing</a> • <a href="#previews">Previews</a> • <a href="#pages">Pages</a> • <a href="#config">Configuration</a>
16
+ <a href="#installing">Installing</a> • <a href="#previews">Previews</a> • <a href="#pages">Pages</a> • <a href="#deployment">Deployment</a> • <a href="#config">Config</a>
17
17
  </div>
18
18
 
19
19
  ---
@@ -72,13 +72,6 @@ The `at` property determines the root URL that the Lookbook UI will be served at
72
72
 
73
73
  Then you can start your app as normal and navigate to `http://localhost:3000/lookbook` (or whatever mount path you specified) to view your component previews in the Lookbook UI.
74
74
 
75
- #### Mounting in Production
76
-
77
- If you would like to expose the Lookbook UI in production as well as in development
78
-
79
- 1. Remove the `if Rails.env.development?` condition from around the mount statement in `routes.rb`
80
- 2. Add `config.view_component.show_previews = true` to `config/environments/production.rb`
81
-
82
75
 
83
76
  <h2 id="previews">Previews</h2>
84
77
 
@@ -548,7 +541,7 @@ config.lookbook.page_options = {
548
541
  footer: false,
549
542
  data: {
550
543
  brand_colors: {
551
- red: #ff0000
544
+ red: "#ff0000"
552
545
  }
553
546
  }
554
547
  }
@@ -687,6 +680,75 @@ Default: `pages`
687
680
  config.lookbook.page_route = `docs`
688
681
  ```
689
682
 
683
+ <h2 id="deployment">Deploying in Production</h2>
684
+
685
+ Lookbook is intended to be a tool for aiding the ViewComponent development process, and so is usually restricted to running only when the app is in `development` mode.
686
+
687
+ However, it is possible to run Lookbook in a production environment if you wish.
688
+
689
+ ### Differences between development and production
690
+
691
+ By default, Lookbook will behave a little differently in production than it does in development:
692
+
693
+ 1. Watching files for changes is disabled
694
+ 2. Parsing preview files for annotations does **not** happen at runtime. Instead the preview files must be pre-parsed via a Rake task before starting the app (much like asset precompilation).
695
+
696
+ ### Pre-parsing preview files
697
+
698
+ Run the following command to pre-parse the preview files annotations:
699
+
700
+ ```
701
+ rake lookbook:previews:preparse
702
+ ```
703
+
704
+ If you wish to run this as part of your existing assets precompilation step, you can add the following into your app's `Rakefile`:
705
+
706
+ ```ruby
707
+ if Rails.env.production?
708
+ Rake::Task['assets:precompile'].enhance do
709
+ Rake::Task["lookbook:previews:preparse"].invoke
710
+ end
711
+ end
712
+ ```
713
+
714
+ The pre-parsing of preview files will then take place every time `rake assets:precompile` is called and so will not need to be run separately.
715
+
716
+ ### Configuration changes for production
717
+
718
+ You will also need to make sure that the following configuration changes have been made when deploying to production:
719
+
720
+ 1. Make sure ViewComponent is [configured to show previews in production](https://viewcomponent.org/api.html#show_previews) (by default it is disabled when not in development):
721
+
722
+ ```ruby
723
+ # config/environments/production.rb
724
+ config.view_component.show_previews = true
725
+ ```
726
+
727
+ 2. Remove any environment checking from around the Lookbook mounting declaration (if added as per install instructions):
728
+
729
+ ```ruby
730
+ # config/routes.rb
731
+ Rails.application.routes.draw do
732
+ # if Rails.env.development? <- remove
733
+ mount Lookbook::Engine, at: "/lookbook"
734
+ # end
735
+ end
736
+ ```
737
+
738
+ ### Overriding production default behaviours
739
+
740
+ If for some reason you wish to enable file watching or runtime preview annotation parsing in production, you can always override the default behaviour using thie following config options:
741
+
742
+ ```ruby
743
+ # config/environments/production.rb
744
+
745
+ # enable file-change listening
746
+ config.lookbook.listen = true
747
+
748
+ # enable runtime preview parsing
749
+ config.lookbook.runtime_parsing = true
750
+ ```
751
+
690
752
  <h2 id="config">General Configuration</h2>
691
753
 
692
754
  Lookbook will use the ViewComponent [configuration](https://viewcomponent.org/api.html#configuration) for your project to find and render your previews so you generally you won't need to configure much else separately.
@@ -3,13 +3,24 @@ import config from "../config";
3
3
  export default function createSidebarStore(Alpine) {
4
4
  const { defaultWidth, minWidth, maxWidth } = config.sidebar;
5
5
  return {
6
- open: Alpine.$persist(true).as("sidebar-open"),
6
+ openDesktop: Alpine.$persist(true).as("sidebar-open-desktop"),
7
+ openMobile: Alpine.$persist(false).as("sidebar-open-mobile"),
7
8
  width: Alpine.$persist(defaultWidth).as("sidebar-width"),
8
9
  panelSplits: Alpine.$persist([1.0, 1.0]).as(`sidebar-panel-splits`),
9
10
  minWidth,
10
11
  maxWidth,
12
+ get open() {
13
+ return Alpine.store("sidebar")[
14
+ Alpine.store("layout").desktop ? "openDesktop" : "openMobile"
15
+ ];
16
+ },
11
17
  toggle() {
12
- Alpine.store("sidebar").open = !Alpine.store("sidebar").open;
18
+ const sidebar = Alpine.store("sidebar");
19
+ if (Alpine.store("layout").desktop) {
20
+ sidebar.openDesktop = !sidebar.openDesktop;
21
+ } else {
22
+ sidebar.openMobile = !sidebar.openMobile;
23
+ }
13
24
  },
14
25
  };
15
26
  }
@@ -47,6 +47,9 @@ module Lookbook
47
47
  if params[:path] == @preview&.lookup_path
48
48
  redirect_to show_path "#{params[:path]}/#{@preview.default_example.name}"
49
49
  end
50
+ else
51
+ first_example = Lookbook.previews.find(params[:path])&.examples&.first
52
+ redirect_to show_path(first_example.lookup_path) if first_example
50
53
  end
51
54
  end
52
55
 
@@ -143,10 +146,8 @@ module Lookbook
143
146
  end
144
147
 
145
148
  def preview_controller
146
- return @preview_controller if @preview_controller.present?
147
- controller_class = Lookbook.config.preview_controller.constantize
148
- controller_class.class_eval { include Lookbook::PreviewController }
149
- controller = controller_class.new
149
+ return @preview_controller if @preview_controller
150
+ controller = Lookbook::Engine.preview_controller.new
150
151
  controller.request = request
151
152
  controller.response = response
152
153
  @preview_controller ||= controller
@@ -1,4 +1,4 @@
1
- <div class="bg-gray-50 h-full">
1
+ <div class="bg-gray-50 h-full overflow-auto">
2
2
  <% if @example.type == :group %>
3
3
  <div class="p-4 prose prose-sm">
4
4
  <em class='opacity-50'>Params are not yet supported for grouped examples.</em>
@@ -49,6 +49,7 @@ module Lookbook
49
49
  options.preview_srcdoc = false if options.preview_srcdoc.nil?
50
50
  options.preview_display_params ||= {}.with_indifferent_access
51
51
 
52
+ options.listen = Rails.env.development? if options.listen.nil?
52
53
  options.listen_paths = options.listen_paths.map(&:to_s)
53
54
  options.listen_paths += options.preview_paths
54
55
  options.listen_paths << (vc_options.view_component_path || Rails.root.join("app/components"))
@@ -57,6 +58,9 @@ module Lookbook
57
58
  options.cable_mount_path ||= "/lookbook-cable"
58
59
  options.cable_logger ||= Rails.logger
59
60
 
61
+ options.runtime_parsing = !Rails.env.production? if options.runtime_parsing.nil?
62
+ options.parser_registry_path ||= Rails.root.join("tmp/storage/.yardoc")
63
+
60
64
  options.experimental_features = false unless options.experimental_features.present?
61
65
  end
62
66
 
@@ -72,32 +76,44 @@ module Lookbook
72
76
  end
73
77
 
74
78
  config.after_initialize do
75
- @preview_listener = Listen.to(*config.lookbook.listen_paths, only: /\.(rb|html.*)$/) do |modified, added, removed|
76
- begin
77
- parser.parse
78
- rescue
79
- end
80
- Lookbook::Preview.clear_cache
81
- Lookbook::Engine.websocket&.broadcast("reload", {
82
- modified: modified,
83
- removed: removed,
84
- added: added
85
- })
86
- end
87
- @preview_listener.start
88
-
89
- if Lookbook::Features.enabled?(:pages)
90
- @page_listener = Listen.to(*config.lookbook.page_paths.filter { |dir| Dir.exist? dir }, only: /\.(html.*|md.*)$/) do |modified, added, removed|
79
+ if config.lookbook.listen
80
+ @preview_listener = Listen.to(*config.lookbook.listen_paths, only: /\.(rb|html.*)$/) do |modified, added, removed|
81
+ begin
82
+ parser.parse
83
+ rescue
84
+ end
85
+ Lookbook::Preview.clear_cache
91
86
  Lookbook::Engine.websocket&.broadcast("reload", {
92
87
  modified: modified,
93
88
  removed: removed,
94
89
  added: added
95
90
  })
96
91
  end
97
- @page_listener.start
98
- end
92
+ @preview_listener.start
93
+
94
+ if Lookbook::Features.enabled?(:pages)
95
+ @page_listener = Listen.to(*config.lookbook.page_paths.filter { |dir| Dir.exist? dir }, only: /\.(html.*|md.*)$/) do |modified, added, removed|
96
+ Lookbook::Engine.websocket&.broadcast("reload", {
97
+ modified: modified,
98
+ removed: removed,
99
+ added: added
100
+ })
101
+ end
102
+ @page_listener.start
103
+ end
99
104
 
100
- parser.parse
105
+ if config.lookbook.runtime_parsing
106
+ parser.parse
107
+ else
108
+ unless File.exist?(config.lookbook.parser_registry_path)
109
+ Lookbook.logger.warn "
110
+ Runtime parsing is disabled but no registry file has been found.
111
+ Did you run `rake lookbook:preparse` before starting the app?
112
+ Expected to find registry file at #{config.lookbook.parser_registry_path}
113
+ "
114
+ end
115
+ end
116
+ end
101
117
  end
102
118
 
103
119
  at_exit do
@@ -133,7 +149,14 @@ module Lookbook
133
149
  end
134
150
 
135
151
  def parser
136
- @parser ||= Lookbook::Parser.new(config.lookbook.preview_paths)
152
+ @parser ||= Lookbook::Parser.new(config.lookbook.preview_paths, config.lookbook.parser_registry_path)
153
+ end
154
+
155
+ def preview_controller
156
+ return @preview_controller if @preview_controller
157
+ controller = Lookbook.config.preview_controller.constantize
158
+ controller.class_eval { include Lookbook::PreviewController }
159
+ @preview_controller || controller
137
160
  end
138
161
  end
139
162
  end
@@ -2,27 +2,31 @@ require "yard"
2
2
 
3
3
  module Lookbook
4
4
  class Parser
5
- YARDOC_FILE_PATH = Rails.root.join("tmp/storage/.yardoc").to_s
6
-
7
- def initialize(paths)
5
+ attr_reader :registry_path
6
+ def initialize(paths, registry_path)
8
7
  @paths = paths.map { |p| "#{p}/**/*preview.rb" }
9
- YARD::Registry.yardoc_file = YARDOC_FILE_PATH
8
+ @registry_path = registry_path.to_s
9
+ YARD::Registry.yardoc_file = registry_path
10
10
  end
11
11
 
12
12
  def parse
13
13
  YARD::Registry.clear
14
14
  YARD::Registry.lock_for_writing do
15
15
  YARD.parse(@paths)
16
- YARD::Registry.save(false, YARDOC_FILE_PATH)
16
+ YARD::Registry.save(false, registry_path)
17
17
  end
18
18
  end
19
19
 
20
20
  def get_code_object(path)
21
21
  registry = YARD::RegistryStore.new
22
- registry.load!(YARDOC_FILE_PATH)
22
+ registry.load!(registry_path)
23
23
  registry.get(path)
24
24
  end
25
25
 
26
+ # def yardoc_file_path
27
+ # Rails&.root ? Rails.root.join(YARDOC_FILE_PATH) : YARDOC_FILE_PATH
28
+ # end
29
+
26
30
  class << self
27
31
  def define_tags
28
32
  YARD::Tags::Library.define_tag("Hidden status", :hidden)
@@ -1,3 +1,3 @@
1
1
  module Lookbook
2
- VERSION = "0.7.3"
2
+ VERSION = "0.8.0"
3
3
  end
@@ -1,6 +1,15 @@
1
1
  require_relative "../lookbook"
2
2
 
3
3
  namespace :lookbook do
4
+
5
+ namespace :previews do
6
+ desc "Preparse the previews"
7
+ task :preparse do
8
+ Lookbook::Engine.parser.parse
9
+ puts "Lookbook preview parsing complete"
10
+ end
11
+ end
12
+
4
13
  namespace :release do
5
14
  desc "Bump the Lookbook engine version number"
6
15
  task :bump_version, [:version] do |t, args|