Class: Shakapacker::Instance
- Inherits:
-
Object
- Object
- Shakapacker::Instance
- Defined in:
- lib/shakapacker/instance.rb
Overview
Represents a single instance of Shakapacker configuration and state
An instance encapsulates all the configuration, compilation, and manifest lookup functionality for a specific Rails application. Most applications will use the shared instance accessible via #instance, but multiple instances can be created for testing or advanced scenarios.
Instance Attribute Summary collapse
-
#config_path ⇒ Pathname
readonly
The path to the Shakapacker configuration file.
-
#root_path ⇒ Pathname
readonly
The root path of the application.
Instance Method Summary collapse
-
#commands ⇒ Shakapacker::Commands
Returns the commands instance for build operations.
-
#compiler ⇒ Shakapacker::Compiler
Returns the compiler for this instance.
-
#config ⇒ Shakapacker::Configuration
Returns the configuration object for this instance.
-
#dev_server ⇒ Shakapacker::DevServer
Returns the development server instance.
-
#env ⇒ ActiveSupport::StringInquirer
Returns the current Rails environment as a StringInquirer.
-
#initialize(root_path: nil, config_path: nil) ⇒ Shakapacker::Instance
constructor
Creates a new Shakapacker instance.
-
#inlining_css? ⇒ Boolean
Returns whether CSS should be inlined by the dev server.
-
#logger ⇒ ActiveSupport::TaggedLogging
The shared logger used by all Shakapacker instances.
-
#manifest ⇒ Shakapacker::Manifest
Returns the manifest for looking up compiled assets.
-
#strategy ⇒ Shakapacker::CompilerStrategy
private
Returns the compiler strategy for determining staleness.
Constructor Details
#initialize(root_path: nil, config_path: nil) ⇒ Shakapacker::Instance
Creates a new Shakapacker instance
41 42 43 44 45 46 47 48 49 |
# File 'lib/shakapacker/instance.rb', line 41 def initialize(root_path: nil, config_path: nil) # Use Rails.root if Rails is defined and no root_path is provided @root_path = root_path || (defined?(Rails) && Rails&.root) || Pathname.new(Dir.pwd) # Use the determined root_path to construct the default config path default_config_path = @root_path.join("config/shakapacker.yml") @config_path = Pathname.new(ENV["SHAKAPACKER_CONFIG"] || config_path || default_config_path) end |
Instance Attribute Details
#config_path ⇒ Pathname (readonly)
The path to the Shakapacker configuration file
31 32 33 |
# File 'lib/shakapacker/instance.rb', line 31 def config_path @config_path end |
#root_path ⇒ Pathname (readonly)
The root path of the application
27 28 29 |
# File 'lib/shakapacker/instance.rb', line 27 def root_path @root_path end |
Instance Method Details
#commands ⇒ Shakapacker::Commands
Returns the commands instance for build operations
The commands object provides methods for bootstrapping, cleaning, clobbering, and compiling assets.
122 123 124 |
# File 'lib/shakapacker/instance.rb', line 122 def commands @commands ||= Shakapacker::Commands.new self end |
#compiler ⇒ Shakapacker::Compiler
Returns the compiler for this instance
The compiler is responsible for executing webpack/rspack to compile assets.
92 93 94 |
# File 'lib/shakapacker/instance.rb', line 92 def compiler @compiler ||= Shakapacker::Compiler.new self end |
#config ⇒ Shakapacker::Configuration
Returns the configuration object for this instance
The configuration is loaded from the shakapacker.yml file and provides access to all settings like source paths, output paths, and compilation options.
68 69 70 71 72 73 74 |
# File 'lib/shakapacker/instance.rb', line 68 def config @config ||= Shakapacker::Configuration.new( root_path: root_path, config_path: config_path, env: env ) end |
#dev_server ⇒ Shakapacker::DevServer
Returns the development server instance
The dev server instance can query the status of the webpack-dev-server, including whether it’s running, its host/port, and configuration.
102 103 104 |
# File 'lib/shakapacker/instance.rb', line 102 def dev_server @dev_server ||= Shakapacker::DevServer.new config end |
#env ⇒ ActiveSupport::StringInquirer
Returns the current Rails environment as a StringInquirer
This allows for convenient environment checking:
env.development? # => true/false
env.production? # => true/false
58 59 60 |
# File 'lib/shakapacker/instance.rb', line 58 def env @env ||= Shakapacker::Env.inquire self end |
#inlining_css? ⇒ Boolean
Returns whether CSS should be inlined by the dev server
CSS inlining is enabled when:
-
The dev server has inline_css enabled
-
Hot Module Replacement (HMR) is enabled
-
The dev server is currently running
134 135 136 |
# File 'lib/shakapacker/instance.rb', line 134 def inlining_css? dev_server.inline_css? && dev_server.hmr? && dev_server.running? end |
#logger ⇒ ActiveSupport::TaggedLogging
The shared logger used by all Shakapacker instances
23 |
# File 'lib/shakapacker/instance.rb', line 23 cattr_accessor(:logger) { ActiveSupport::TaggedLogging.new(ActiveSupport::Logger.new(STDOUT)) } |
#manifest ⇒ Shakapacker::Manifest
Returns the manifest for looking up compiled assets
The manifest reads the manifest.json file produced by webpack/rspack and provides methods to look up the compiled paths for source files.
112 113 114 |
# File 'lib/shakapacker/instance.rb', line 112 def manifest @manifest ||= Shakapacker::Manifest.new self end |
#strategy ⇒ Shakapacker::CompilerStrategy
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns the compiler strategy for determining staleness
The strategy (mtime or digest) determines how Shakapacker decides whether assets need recompilation.
83 84 85 |
# File 'lib/shakapacker/instance.rb', line 83 def strategy @strategy ||= Shakapacker::CompilerStrategy.from_config end |