Class: Shakapacker::Instance

Inherits:
Object
  • Object
show all
Defined in:
lib/shakapacker/instance.rb,
sig/shakapacker/instance.rbs

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.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root_path: nil, config_path: nil) ⇒ Instance

Creates a new Shakapacker instance

Parameters:

  • root_path: (String, Pathname, nil) (defaults to: nil)
  • config_path: (String, Pathname, nil) (defaults to: nil)


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_pathPathname (readonly)

The path to the Shakapacker configuration file

Returns:

  • (Pathname)


31
32
33
# File 'lib/shakapacker/instance.rb', line 31

def config_path
  @config_path
end

#root_pathPathname (readonly)

The root path of the application

Returns:

  • (Pathname)


27
28
29
# File 'lib/shakapacker/instance.rb', line 27

def root_path
  @root_path
end

Class Method Details

.loggerActiveSupport::TaggedLogging

The shared logger used by all Shakapacker instances

Returns:

  • (ActiveSupport::TaggedLogging)


7
# File 'sig/shakapacker/instance.rbs', line 7

def self.logger: () -> ActiveSupport::TaggedLogging

.logger=ActiveSupport::TaggedLogging

Sets the shared logger used by all Shakapacker instances

Parameters:

  • logger (ActiveSupport::TaggedLogging)

Returns:

  • (ActiveSupport::TaggedLogging)


10
# File 'sig/shakapacker/instance.rbs', line 10

def self.logger=: (ActiveSupport::TaggedLogging logger) -> ActiveSupport::TaggedLogging

Instance Method Details

#commandsCommands

Returns the commands instance for build operations

Returns:



122
123
124
# File 'lib/shakapacker/instance.rb', line 122

def commands
  @commands ||= Shakapacker::Commands.new self
end

#compilerCompiler

Returns the compiler for this instance

Returns:



92
93
94
# File 'lib/shakapacker/instance.rb', line 92

def compiler
  @compiler ||= Shakapacker::Compiler.new self
end

#configConfiguration

Returns the configuration object for this instance

Returns:



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_serverDevServer

Returns the development server instance

Returns:



102
103
104
# File 'lib/shakapacker/instance.rb', line 102

def dev_server
  @dev_server ||= Shakapacker::DevServer.new config
end

#envActiveSupport::StringInquirer

Returns the current Rails environment as a StringInquirer

Returns:

  • (ActiveSupport::StringInquirer)


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

Returns:

  • (Boolean)


134
135
136
# File 'lib/shakapacker/instance.rb', line 134

def inlining_css?
  dev_server.inline_css? && dev_server.hmr? && dev_server.running?
end

#loggerActiveSupport::TaggedLogging

The shared logger used by all Shakapacker instances

Returns:

  • (ActiveSupport::TaggedLogging)

    the logger



23
# File 'lib/shakapacker/instance.rb', line 23

cattr_accessor(:logger) { ActiveSupport::TaggedLogging.new(ActiveSupport::Logger.new(STDOUT)) }

#manifestManifest

Returns the manifest for looking up compiled assets

Returns:



112
113
114
# File 'lib/shakapacker/instance.rb', line 112

def manifest
  @manifest ||= Shakapacker::Manifest.new self
end

#strategyMtimeStrategy, DigestStrategy

Returns the compiler strategy for determining staleness



83
84
85
# File 'lib/shakapacker/instance.rb', line 83

def strategy
  @strategy ||= Shakapacker::CompilerStrategy.from_config(self)
end