lookbook 0.7.4 → 0.8.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 96f31c2b265d871a152627dfb10b0c0e1f6db8ae42e811abfeb138c7bf45d806
4
- data.tar.gz: 9ae695f9a3fc7c28bd5a7fbf77fc0959d92d46661525e7593961555cd41d76fb
3
+ metadata.gz: 9708e337537be23dffbb22a9789d424ecfd211599a97687ca7a075ff3e9b198e
4
+ data.tar.gz: 84feba4dabce56fff00ad0ba5d41a4d5c61bc5d4f46df5a439f2de84c993d4a5
5
5
  SHA512:
6
- metadata.gz: 8b658dc2f0e2b7213761be9197f916594381319927f02097c3901af13d9c35194e45d568d70a21c3da05407ea79e589ffa1c3530905c73dc5cbfa1100cc2ac3f
7
- data.tar.gz: 55bc108c5a1ee957970709efe01c75956ee7b925043bab7b5c6aba1bafa1f998191d7872a7e08ddbd85daad2d7713437ec9340c8f2273568ecffa17bbb78a738
6
+ metadata.gz: 8781278db46f412fd6fc8f013687ef9255bbc9a9ca8fa4a85a107c5e97f0ffd802cfa1362a8af20b94e9c38c5d375103e731ef35fb59d919b35d5921413b572b
7
+ data.tar.gz: 57ffc62626dc30dc3dcee163f8eb17558e9a8e1af863e7e47c776edb3a8eb4c57b20d60dfdd407123015d713bbf5e961dda326fa18dc1ecfb1dcf05cc6282124
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
@@ -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,47 @@ 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
+ @preview_controller = Lookbook.config.preview_controller.constantize
80
+ @preview_controller.include(Lookbook::PreviewController)
81
+
82
+ if config.lookbook.listen
83
+ @preview_listener = Listen.to(*config.lookbook.listen_paths, only: /\.(rb|html.*)$/) do |modified, added, removed|
84
+ begin
85
+ parser.parse
86
+ rescue
87
+ end
88
+ Lookbook::Preview.clear_cache
91
89
  Lookbook::Engine.websocket&.broadcast("reload", {
92
90
  modified: modified,
93
91
  removed: removed,
94
92
  added: added
95
93
  })
96
94
  end
97
- @page_listener.start
98
- end
95
+ @preview_listener.start
96
+
97
+ if Lookbook::Features.enabled?(:pages)
98
+ @page_listener = Listen.to(*config.lookbook.page_paths.filter { |dir| Dir.exist? dir }, only: /\.(html.*|md.*)$/) do |modified, added, removed|
99
+ Lookbook::Engine.websocket&.broadcast("reload", {
100
+ modified: modified,
101
+ removed: removed,
102
+ added: added
103
+ })
104
+ end
105
+ @page_listener.start
106
+ end
99
107
 
100
- parser.parse
108
+ if config.lookbook.runtime_parsing
109
+ parser.parse
110
+ else
111
+ unless File.exist?(config.lookbook.parser_registry_path)
112
+ Lookbook.logger.warn "
113
+ Runtime parsing is disabled but no registry file has been found.
114
+ Did you run `rake lookbook:preparse` before starting the app?
115
+ Expected to find registry file at #{config.lookbook.parser_registry_path}
116
+ "
117
+ end
118
+ end
119
+ end
101
120
  end
102
121
 
103
122
  at_exit do
@@ -133,8 +152,10 @@ module Lookbook
133
152
  end
134
153
 
135
154
  def parser
136
- @parser ||= Lookbook::Parser.new(config.lookbook.preview_paths)
155
+ @parser ||= Lookbook::Parser.new(config.lookbook.preview_paths, config.lookbook.parser_registry_path)
137
156
  end
157
+
158
+ attr_reader :preview_controller
138
159
  end
139
160
  end
140
161
  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.4"
2
+ VERSION = "0.8.1"
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|