Class: Shakapacker::Instance

Inherits:
Object
  • Object
show all
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.

Examples:

Using the default instance

instance = Shakapacker::Instance.new
instance.config.source_path
instance.manifest.lookup("application.js")

Creating an instance with custom paths

instance = Shakapacker::Instance.new(
  root_path: "/path/to/app",
  config_path: "/custom/config.yml"
)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Creates a new Shakapacker instance

Parameters:

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

    the application root path. Defaults to Rails.root if Rails is defined, otherwise uses current directory

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

    the path to shakapacker.yml. Can also be set via SHAKAPACKER_CONFIG environment variable. Defaults to config/shakapacker.yml within the root_path



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)

    the config file path



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)

    the application root path



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

def root_path
  @root_path
end

Instance Method Details

#commandsShakapacker::Commands

Returns the commands instance for build operations

The commands object provides methods for bootstrapping, cleaning, clobbering, and compiling assets.

Returns:



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

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

#compilerShakapacker::Compiler

Returns the compiler for this instance

The compiler is responsible for executing webpack/rspack to compile assets.

Returns:



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

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

#configShakapacker::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.

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_serverShakapacker::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.

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

This allows for convenient environment checking:

env.development? # => true/false
env.production?  # => true/false

Returns:

  • (ActiveSupport::StringInquirer)

    the environment



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

Returns:

  • (Boolean)

    true if CSS should be inlined



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)) }

#manifestShakapacker::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.

Returns:



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

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

#strategyShakapacker::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.

Returns:



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

def strategy
  @strategy ||= Shakapacker::CompilerStrategy.from_config
end