Class: ReactManifest::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/react_manifest/configuration.rb

Overview

Holds all configuration for the gem. Obtain via configure.

Examples:

ReactManifest.configure do |c|
  c.ux_root           = "app/assets/javascripts/ux"
  c.extensions        = %w[js jsx ts tsx]
  c.size_threshold_kb = 1000
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfiguration

Returns a new instance of Configuration.



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/react_manifest/configuration.rb', line 108

def initialize
  @ux_root           = "app/assets/javascripts/ux"
  @app_dir           = "app"
  @output_dir        = "app/assets/javascripts"
  @manifest_subdir   = "ux_manifests"
  @manage_gitignore  = true
  @shared_bundle     = "ux_shared"
  @auto_shared       = true
  @always_include    = []
  @ignore            = []
  @isolated_app_dirs = []
  @exclude_paths     = %w[react react_dev vendor]
  @size_threshold_kb = 500
  @extensions        = %w[js jsx]
  @dry_run           = false
  @verbose           = false
  @stdout_logging    = false
  @external_providers = {}
  @external_roots     = []
end

Instance Attribute Details

#always_includeObject

Bundles always prepended by react_bundle_tag (e.g. ["ux_main"])



37
38
39
# File 'lib/react_manifest/configuration.rb', line 37

def always_include
  @always_include
end

#app_dirObject

Subdir within ux_root that contains per-controller JSX



15
16
17
# File 'lib/react_manifest/configuration.rb', line 15

def app_dir
  @app_dir
end

#auto_sharedObject

When true (default), a component defined under app_dir but used by any other bundle is emitted into the shared bundle (loaded once per page) instead of being inlined into each consumer. Set false to restore legacy inlining.



34
35
36
# File 'lib/react_manifest/configuration.rb', line 34

def auto_shared
  @auto_shared
end

#dry_runObject

Preview mode: print what would change, write nothing. Applies anywhere generation runs (manual task, boot sync, watcher).



73
74
75
# File 'lib/react_manifest/configuration.rb', line 73

def dry_run
  @dry_run
end

#exclude_pathsObject

Path segments to exclude while scanning files under ux_root. This is segment matching (not full path matching), so "vendor" excludes ux/vendor/foo.js and ux/app/users/vendor/bar.jsx, but not ux/vendor_custom/x.js. These are not application.js includes; they only affect ux tree scanning.



63
64
65
# File 'lib/react_manifest/configuration.rb', line 63

def exclude_paths
  @exclude_paths
end

#extensionsObject

File extensions to scan (default: js and jsx; add "ts", "tsx" for TypeScript)



69
70
71
# File 'lib/react_manifest/configuration.rb', line 69

def extensions
  @extensions
end

#external_providersObject

Explicit symbol-to-require-path mapping for external globals. Use for third-party libraries that export a PascalCase or camelCase symbol not located inside ux_root (e.g. MiniSearch, Chart.js wrappers).

Keys are exact symbol names (case-sensitive); values are the Sprockets require path that should be emitted into the controller manifest.

Example:

config.external_providers = { "MiniSearch" => "mini-search", "axios" => "vendor/axios" }


96
97
98
# File 'lib/react_manifest/configuration.rb', line 96

def external_providers
  @external_providers
end

#external_rootsObject

Extra root directories (absolute or relative to Rails.root) to scan for symbol definitions outside of ux_root. Symbols found here are added to the shared symbol index and will trigger an include when used by a controller. Empty by default; files in these dirs are subject to the same exclude_paths and extensions filters.

Example:

config.external_roots = ["app/assets/javascripts/vendor_components"]


106
107
108
# File 'lib/react_manifest/configuration.rb', line 106

def external_roots
  @external_roots
end

#ignoreObject

Controller directory names directly under ux_root/app_dir to skip entirely. Example: ignore = ["admin"] skips ux/app/admin/* when building ux_.js.



41
42
43
# File 'lib/react_manifest/configuration.rb', line 41

def ignore
  @ignore
end

#isolated_app_dirsObject

Controller directory names (under ux_root/app_dir) whose files must never be inlined into any OTHER controller's manifest via inferred cross-app dependency detection. Their own ux_.js manifest is unaffected — still includes all of their own files as usual.

Symbol usage detection is pure regex (no AST), so a generic component name can collide with an unrelated word appearing as plain JSX text elsewhere (e.g. a "Show" component vs a "Show More" button label in a completely different controller) and get wrongly inlined everywhere. Use this for self-contained dirs (e.g. a page-builder tool bundling its own vendored library) that should truly never leak into other bundles.

Example:

config.isolated_app_dirs = ["rvb"]


57
58
59
# File 'lib/react_manifest/configuration.rb', line 57

def isolated_app_dirs
  @isolated_app_dirs
end

#manage_gitignoreObject

When true (default), the gem ensures the generated manifest dir is gitignored (adds the entry on dev boot if missing). Set false to manage .gitignore yourself.



26
27
28
# File 'lib/react_manifest/configuration.rb', line 26

def manage_gitignore
  @manage_gitignore
end

#manifest_subdirObject

Subdirectory under output_dir that holds generated ux_*.js manifests. Keeping generated files out of output_dir root avoids clutter.



22
23
24
# File 'lib/react_manifest/configuration.rb', line 22

def manifest_subdir
  @manifest_subdir
end

#output_dirObject

Where generated ux_*.js manifests are written (relative to Rails.root)



18
19
20
# File 'lib/react_manifest/configuration.rb', line 18

def output_dir
  @output_dir
end

#shared_bundleObject

Bundle name for auto-generated shared bundle (all non-app/ dirs)



29
30
31
# File 'lib/react_manifest/configuration.rb', line 29

def shared_bundle
  @shared_bundle
end

#size_threshold_kbObject

Warn if a bundle exceeds this size in KB (0 = disabled)



66
67
68
# File 'lib/react_manifest/configuration.rb', line 66

def size_threshold_kb
  @size_threshold_kb
end

#stdout_loggingObject

Also print ReactManifest status lines directly to stdout, in addition to Rails.logger (which always receives them regardless of this flag). Off by default: many development setups already have Rails.logger broadcast to the terminal (RAILS_LOG_TO_STDOUT, Docker/Foreman, etc.), and enabling this on top of that prints every line twice. Turn it on only if your Rails.logger does NOT already surface output in your terminal and you want a guaranteed visible line per event.



85
86
87
# File 'lib/react_manifest/configuration.rb', line 85

def stdout_logging
  @stdout_logging
end

#ux_rootObject

Root of the ux/ tree to scan (relative to Rails.root)



12
13
14
# File 'lib/react_manifest/configuration.rb', line 12

def ux_root
  @ux_root
end

#verboseObject

Extra diagnostic logging (summary lines and richer error context).



76
77
78
# File 'lib/react_manifest/configuration.rb', line 76

def verbose
  @verbose
end

Instance Method Details

#abs_app_dirObject



164
165
166
# File 'lib/react_manifest/configuration.rb', line 164

def abs_app_dir
  File.join(abs_ux_root, app_dir)
end

#abs_manifest_dirObject



172
173
174
175
176
177
# File 'lib/react_manifest/configuration.rb', line 172

def abs_manifest_dir
  subdir = normalized_manifest_subdir
  return abs_output_dir if subdir.empty?

  File.join(abs_output_dir, subdir)
end

#abs_output_dirObject



168
169
170
# File 'lib/react_manifest/configuration.rb', line 168

def abs_output_dir
  Rails.root.join(output_dir).to_s
end

#abs_ux_rootObject

Absolute path helpers (requires Rails.root to be set)



160
161
162
# File 'lib/react_manifest/configuration.rb', line 160

def abs_ux_root
  Rails.root.join(ux_root).to_s
end

#auto_shared?Boolean

Returns:

  • (Boolean)


133
134
135
# File 'lib/react_manifest/configuration.rb', line 133

def auto_shared?
  !!@auto_shared
end

#cache_keyObject



188
189
190
# File 'lib/react_manifest/configuration.rb', line 188

def cache_key
  [ux_root, app_dir, extensions, always_include, exclude_paths, external_providers, isolated_app_dirs].hash
end

#dry_run?Boolean

Returns:

  • (Boolean)


129
130
131
# File 'lib/react_manifest/configuration.rb', line 129

def dry_run?
  !!@dry_run
end

#excluded_path?(abs_path) ⇒ Boolean

Returns:

  • (Boolean)


179
180
181
182
# File 'lib/react_manifest/configuration.rb', line 179

def excluded_path?(abs_path)
  parts = abs_path.split(File::SEPARATOR)
  exclude_paths.any? { |ep| parts.include?(ep) }
end

#extensions_globObject

Glob fragment used by Dir.glob, e.g. ".js,jsx" or ".js,jsx,ts,tsx"



150
151
152
# File 'lib/react_manifest/configuration.rb', line 150

def extensions_glob
  "*.{#{extensions.join(',')}}"
end

#extensions_patternObject

Regexp used by the file watcher to filter events, e.g. /.(js|jsx)$/



155
156
157
# File 'lib/react_manifest/configuration.rb', line 155

def extensions_pattern
  Regexp.new("\\.(#{extensions.map { |e| Regexp.escape(e) }.join('|')})$")
end

#manage_gitignore?Boolean

Returns:

  • (Boolean)


145
146
147
# File 'lib/react_manifest/configuration.rb', line 145

def manage_gitignore?
  !!@manage_gitignore
end

#normalized_manifest_subdirObject



184
185
186
# File 'lib/react_manifest/configuration.rb', line 184

def normalized_manifest_subdir
  manifest_subdir.to_s.gsub(%r{\A/+|/+\z}, "")
end

#stdout_logging?Boolean

Returns:

  • (Boolean)


141
142
143
# File 'lib/react_manifest/configuration.rb', line 141

def stdout_logging?
  !!@stdout_logging
end

#verbose?Boolean

Returns:

  • (Boolean)


137
138
139
# File 'lib/react_manifest/configuration.rb', line 137

def verbose?
  !!@verbose
end