Module: Shakapacker

Extended by:
Shakapacker
Included in:
Shakapacker
Defined in:
lib/shakapacker.rb,
lib/shakapacker/doctor.rb,
lib/shakapacker/runner.rb,
lib/shakapacker/version.rb,
lib/shakapacker/utils/misc.rb,
lib/shakapacker/install/env.rb,
lib/shakapacker/swc_migrator.rb,
lib/shakapacker/base_strategy.rb,
lib/shakapacker/rspack_runner.rb,
lib/shakapacker/utils/manager.rb,
lib/shakapacker/mtime_strategy.rb,
lib/shakapacker/webpack_runner.rb,
lib/shakapacker/digest_strategy.rb,
lib/shakapacker/version_checker.rb,
lib/shakapacker/bundler_switcher.rb,
lib/shakapacker/compiler_strategy.rb,
lib/shakapacker/dev_server_runner.rb,
lib/shakapacker/deprecation_helper.rb,
lib/shakapacker/build_config_loader.rb,
lib/shakapacker/utils/version_syntax_converter.rb,
sig/shakapacker.rbs,
sig/shakapacker/helper.rbs,
sig/shakapacker/version.rbs,
sig/shakapacker/compiler_strategy.rbs

Overview

Compiler strategy for determining staleness

Defined Under Namespace

Modules: Helper, Install, Utils Classes: BaseStrategy, BuildConfigLoader, BundlerSwitcher, Commands, Compiler, CompilerStrategy, Configuration, DevServer, DevServerProxy, DevServerRunner, DigestStrategy, Doctor, Engine, Env, Instance, Manifest, MtimeStrategy, RspackRunner, Runner, SwcMigrator, VersionChecker, WebpackRunner

Constant Summary collapse

DEFAULT_ENV =

Default environment when RAILS_ENV is not set

Returns:

  • (String)
"development".freeze
DEV_ENVS =

Environments that use "development" for NODE_ENV All other environments (production, staging, etc.) use "production" for webpack optimizations Note: Both development and test RAILS_ENV use NODE_ENV=development because webpack/rspack only recognize "development" and "production" values for NODE_ENV. Using "test" causes DefinePlugin conflicts with optimization.nodeEnv.

%w[development test].freeze
VERSION =

The current version of Shakapacker

Returns:

  • (String)
"10.3.1".freeze
SHELL =
Thor::Shell::Color.new
DEV_TEST_ENVS =

Environments that use their RAILS_ENV value for NODE_ENV

Returns:

  • (Array[String])

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.bootstrapvoid

This method returns an undefined value.

Creates the default configuration files and directory structure



56
# File 'sig/shakapacker.rbs', line 56

def self.bootstrap: () -> void

.cleanBoolean

Removes old compiled packs, keeping the most recent versions

Parameters:

  • (Integer)
  • (Integer)

Returns:

  • (Boolean)


59
# File 'sig/shakapacker.rbs', line 59

def self.clean: (?Integer, ?Integer) -> bool

.clobbervoid

This method returns an undefined value.

Removes all compiled packs



62
# File 'sig/shakapacker.rbs', line 62

def self.clobber: () -> void

.commandsCommands

Returns the commands instance for build operations

Returns:



50
# File 'sig/shakapacker.rbs', line 50

def self.commands: () -> Commands

.compileBoolean

Compiles all webpack/rspack packs

Returns:

  • (Boolean)


65
# File 'sig/shakapacker.rbs', line 65

def self.compile: () -> bool

.compilerCompiler

Returns the compiler instance for compiling assets

Returns:



44
# File 'sig/shakapacker.rbs', line 44

def self.compiler: () -> Compiler

.configConfiguration

Returns the Shakapacker configuration object

Returns:



41
# File 'sig/shakapacker.rbs', line 41

def self.config: () -> Configuration

.dev_serverDevServer

Returns the dev server instance for querying server status

Returns:



53
# File 'sig/shakapacker.rbs', line 53

def self.dev_server: () -> DevServer

.ensure_log_goes_to_stdoutvoid

This method returns an undefined value.

Temporarily redirects Shakapacker logging to STDOUT



26
# File 'sig/shakapacker.rbs', line 26

def self.ensure_log_goes_to_stdout: [T] () { () -> T } -> T

.ensure_node_env!String

Sets NODE_ENV based on RAILS_ENV if not already set

Returns:

  • (String)


23
# File 'sig/shakapacker.rbs', line 23

def self.ensure_node_env!: () -> String

.envActiveSupport::StringInquirer

Returns the current Rails environment as an ActiveSupport::StringInquirer

Returns:

  • (ActiveSupport::StringInquirer)


35
# File 'sig/shakapacker.rbs', line 35

def self.env: () -> ActiveSupport::StringInquirer

.inlining_css?Boolean

Returns whether CSS inlining is enabled

Returns:

  • (Boolean)


38
# File 'sig/shakapacker.rbs', line 38

def self.inlining_css?: () -> bool

.instanceInstance

Returns the shared Shakapacker instance

Returns:



14
# File 'sig/shakapacker.rbs', line 14

def self.instance: () -> Instance

.instance=Instance

Sets the shared Shakapacker instance

Parameters:

Returns:



17
# File 'sig/shakapacker.rbs', line 17

def self.instance=: (Instance instance) -> Instance

.loggerActiveSupport::TaggedLogging

Returns the logger instance used by Shakapacker

Returns:

  • (ActiveSupport::TaggedLogging)


29
# File 'sig/shakapacker.rbs', line 29

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

.logger=ActiveSupport::TaggedLogging

Sets the logger instance used by Shakapacker

Parameters:

  • logger (ActiveSupport::TaggedLogging)

Returns:

  • (ActiveSupport::TaggedLogging)


32
# File 'sig/shakapacker.rbs', line 32

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

.manifestManifest

Returns the manifest instance for looking up compiled assets

Returns:



47
# File 'sig/shakapacker.rbs', line 47

def self.manifest: () -> Manifest

.with_node_envvoid

This method returns an undefined value.

Temporarily overrides NODE_ENV for the duration of the block



20
# File 'sig/shakapacker.rbs', line 20

def self.with_node_env: [T] (String env) { () -> T } -> T

Instance Method Details

#append_javascript_pack_tagnil

Appends javascript pack names to the queue

Parameters:

  • names (String)
  • defer: (Boolean)
  • async: (Boolean)

Returns:

  • (nil)


50
# File 'sig/shakapacker/helper.rbs', line 50

def append_javascript_pack_tag: (*String names, ?defer: bool, ?async: bool) -> nil

#append_stylesheet_pack_tagnil

Appends stylesheet pack names to the queue

Parameters:

  • names (String)

Returns:

  • (nil)


47
# File 'sig/shakapacker/helper.rbs', line 47

def append_stylesheet_pack_tag: (*String names) -> nil

#asset_pack_pathString

Computes the relative path for a given Shakapacker asset

Parameters:

  • name (String)
  • options (Object)

Returns:

  • (String)


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

def asset_pack_path: (String name, **untyped options) -> String

#asset_pack_urlString

Computes the absolute path for a given Shakapacker asset

Parameters:

  • name (String)
  • options (Object)

Returns:

  • (String)


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

def asset_pack_url: (String name, **untyped options) -> String

#available_sources_from_manifest_entrypointsArray[String]

Parameters:

  • names (Array[String | Symbol])
  • type: (Symbol)

Returns:

  • (Array[String])


63
# File 'sig/shakapacker/helper.rbs', line 63

def available_sources_from_manifest_entrypoints: (Array[String | Symbol] names, type: Symbol) -> Array[String]

#bootstrapvoid

This method returns an undefined value.

Creates the default configuration files and directory structure



194
# File 'lib/shakapacker.rb', line 194

delegate :bootstrap, :clean, :clobber, :compile, to: :commands

Parameters:

  • source_path (String)
  • source (String, Hash[String, untyped])
  • as: (String)
  • rel: (String)

Returns:

  • (String)


87
# File 'sig/shakapacker/helper.rbs', line 87

def build_link_header: (String source_path, String | Hash[String, untyped] source, as: String, ?rel: String) -> String

#clean(count = nil, age = nil) ⇒ void

This method returns an undefined value.

Removes old compiled packs, keeping the most recent versions

Parameters:

  • count (Integer, nil) (defaults to: nil)

    number of versions to keep per entry

  • age (Integer, nil) (defaults to: nil)

    maximum age in seconds for packs to keep

See Also:



194
# File 'lib/shakapacker.rb', line 194

delegate :bootstrap, :clean, :clobber, :compile, to: :commands

#clobbervoid

This method returns an undefined value.

Removes all compiled packs



194
# File 'lib/shakapacker.rb', line 194

delegate :bootstrap, :clean, :clobber, :compile, to: :commands

#commandsShakapacker::Commands

Returns the commands instance for build operations

Returns:

See Also:



174
# File 'lib/shakapacker.rb', line 174

delegate :config, :compiler, :manifest, :commands, :dev_server, to: :instance

#compileBoolean

Compiles all webpack/rspack packs

Returns:

  • (Boolean)

    true if compilation succeeded

See Also:



194
# File 'lib/shakapacker.rb', line 194

delegate :bootstrap, :clean, :clobber, :compile, to: :commands

#compilerShakapacker::Compiler

Returns the compiler instance for compiling assets

Returns:

See Also:



174
# File 'lib/shakapacker.rb', line 174

delegate :config, :compiler, :manifest, :commands, :dev_server, to: :instance

#configShakapacker::Configuration

Returns the Shakapacker configuration object

Returns:

See Also:



174
# File 'lib/shakapacker.rb', line 174

delegate :config, :compiler, :manifest, :commands, :dev_server, to: :instance

#current_shakapacker_instanceInstance

Returns the current Shakapacker instance

Returns:



4
# File 'sig/shakapacker/helper.rbs', line 4

def current_shakapacker_instance: () -> Instance

#dev_serverShakapacker::DevServer

Returns the dev server instance for querying server status

Returns:

See Also:



174
# File 'lib/shakapacker.rb', line 174

delegate :config, :compiler, :manifest, :commands, :dev_server, to: :instance

#early_hints_debug_enabled?Boolean

Returns:

  • (Boolean)


77
# File 'sig/shakapacker/helper.rbs', line 77

def early_hints_debug_enabled?: () -> bool

#early_hints_enabled?Boolean

Returns:

  • (Boolean)


75
# File 'sig/shakapacker/helper.rbs', line 75

def early_hints_enabled?: () -> bool

#early_hints_skip_reasonString

Returns:

  • (String)


85
# File 'sig/shakapacker/helper.rbs', line 85

def early_hints_skip_reason: () -> String

#early_hints_storeHash[String, untyped]

Returns:

  • (Hash[String, untyped])


89
# File 'sig/shakapacker/helper.rbs', line 89

def early_hints_store: () -> Hash[String, untyped]

#early_hints_supported?Boolean

Returns:

  • (Boolean)


73
# File 'sig/shakapacker/helper.rbs', line 73

def early_hints_supported?: () -> bool

#ensure_log_goes_to_stdout { ... } ⇒ Object

Temporarily redirects Shakapacker logging to STDOUT

This is useful for debugging or when you want to see compilation output in the console instead of the Rails log.

Yields:

  • the block to execute with STDOUT logging

Returns:

  • (Object)

    the return value of the block



127
128
129
130
131
132
133
# File 'lib/shakapacker.rb', line 127

def ensure_log_goes_to_stdout
  old_logger = Shakapacker.logger
  Shakapacker.logger = Logger.new(STDOUT)
  yield
ensure
  Shakapacker.logger = old_logger
end

#ensure_node_env!String

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.

Sets NODE_ENV based on RAILS_ENV if not already set

Environment mapping:

  • development and test environments use "development" for NODE_ENV
  • All other environments (+production+, staging, etc.) use "production" for webpack optimizations

Note: We always use "development" (not "test") for test environments because webpack/rspack only recognize "development" and "production" as valid NODE_ENV values. Using "test" causes DefinePlugin conflicts with optimization.nodeEnv.

This method is typically called automatically during Rails initialization.

Returns:

  • (String)

    the NODE_ENV value that was set



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

def ensure_node_env!
  ENV["NODE_ENV"] ||= DEV_ENVS.include?(ENV["RAILS_ENV"]) ? "development" : "production"
end

#envActiveSupport::StringInquirer

Returns the current Rails environment as an ActiveSupport::StringInquirer

Returns:

  • (ActiveSupport::StringInquirer)

    the environment

See Also:



152
# File 'lib/shakapacker.rb', line 152

delegate :logger, :logger=, :env, :inlining_css?, to: :instance

#favicon_pack_tagActiveSupport::SafeBuffer

Creates a link tag for a favicon that references the named pack file

Parameters:

  • name (String)
  • options (Object)

Returns:

  • (ActiveSupport::SafeBuffer)


22
# File 'sig/shakapacker/helper.rbs', line 22

def favicon_pack_tag: (String name, **untyped options) -> ActiveSupport::SafeBuffer

#flush_early_hintsvoid

This method returns an undefined value.



93
# File 'sig/shakapacker/helper.rbs', line 93

def flush_early_hints: () -> void

#image_pack_pathString

Computes the relative path for a given Shakapacker image

Parameters:

  • name (String)
  • options (Object)

Returns:

  • (String)


13
# File 'sig/shakapacker/helper.rbs', line 13

def image_pack_path: (String name, **untyped options) -> String

#image_pack_tagActiveSupport::SafeBuffer

Creates an image tag that references the named pack file

Parameters:

  • name (String)
  • options (Object)

Returns:

  • (ActiveSupport::SafeBuffer)


19
# File 'sig/shakapacker/helper.rbs', line 19

def image_pack_tag: (String name, **untyped options) -> ActiveSupport::SafeBuffer

#image_pack_urlString

Computes the absolute path for a given Shakapacker image

Parameters:

  • name (String)
  • options (Object)

Returns:

  • (String)


16
# File 'sig/shakapacker/helper.rbs', line 16

def image_pack_url: (String name, **untyped options) -> String

#inlining_css?Boolean

Returns whether CSS inlining is enabled

Returns:

  • (Boolean)

    true if CSS should be inlined

See Also:



152
# File 'lib/shakapacker.rb', line 152

delegate :logger, :logger=, :env, :inlining_css?, to: :instance

#instanceShakapacker::Instance

Returns the shared Shakapacker instance

This instance is used by all module-level delegate methods. It provides access to configuration, compilation, manifest lookup, and more.

Returns:



71
72
73
# File 'lib/shakapacker.rb', line 71

def instance
  @instance ||= Shakapacker::Instance.new
end

#instance=(instance) ⇒ Shakapacker::Instance

Sets the shared Shakapacker instance

This is primarily used for testing or advanced customization scenarios. In most applications, the default instance is sufficient.

Parameters:

Returns:



60
61
62
# File 'lib/shakapacker.rb', line 60

def instance=(instance)
  @instance = instance
end

#javascript_pack_tagObject

Creates script tags that reference the js chunks from entrypoints



25
# File 'sig/shakapacker/helper.rbs', line 25

def javascript_pack_tag: (

#javascript_pack_tag_queueHash[Symbol, Array[String]]

Returns:

  • (Hash[Symbol, Array[String]])


59
# File 'sig/shakapacker/helper.rbs', line 59

def javascript_pack_tag_queue: () -> Hash[Symbol, Array[String]]

#loggerLogger

Returns the logger instance used by Shakapacker

Returns:

  • (Logger)

    the logger instance

See Also:



152
# File 'lib/shakapacker.rb', line 152

delegate :logger, :logger=, :env, :inlining_css?, to: :instance

#logger=(logger) ⇒ Logger

Sets the logger instance used by Shakapacker

Parameters:

  • logger (Logger)

    the logger to use

Returns:

  • (Logger)

    the logger that was set

See Also:



152
# File 'lib/shakapacker.rb', line 152

delegate :logger, :logger=, :env, :inlining_css?, to: :instance

#lookup_integrityString?

Parameters:

  • source (String, Hash[String, untyped])

Returns:

  • (String, nil)


67
# File 'sig/shakapacker/helper.rbs', line 67

def lookup_integrity: (String | Hash[String, untyped] source) -> String?

#lookup_sourceString

Parameters:

  • source (String, Hash[String, untyped])

Returns:

  • (String)


69
# File 'sig/shakapacker/helper.rbs', line 69

def lookup_source: (String | Hash[String, untyped] source) -> String

#manifestShakapacker::Manifest

Returns the manifest instance for looking up compiled assets

Returns:

See Also:



174
# File 'lib/shakapacker.rb', line 174

delegate :config, :compiler, :manifest, :commands, :dev_server, to: :instance

#normalize_hint_valueString

Parameters:

  • value (String, bool, nil)

Returns:

  • (String)


83
# File 'sig/shakapacker/helper.rbs', line 83

def normalize_hint_value: (String | bool | nil value) -> String

#normalize_pack_hintsHash[String, String]

Parameters:

  • packs (Array[String])
  • early_hints (Hash[String | Symbol, String], String, bool)

Returns:

  • (Hash[String, String])


81
# File 'sig/shakapacker/helper.rbs', line 81

def normalize_pack_hints: (Array[String] packs, Hash[String | Symbol, String] | String | bool early_hints) -> Hash[String, String]

#preload_pack_assetActiveSupport::SafeBuffer

Creates a link tag for preloading that references a given Shakapacker asset

Parameters:

  • name (String)
  • options (Object)

Returns:

  • (ActiveSupport::SafeBuffer)


34
# File 'sig/shakapacker/helper.rbs', line 34

def preload_pack_asset: (String name, **untyped options) -> ActiveSupport::SafeBuffer

#prepend_javascript_pack_tagnil

Prepends javascript pack names to the queue

Parameters:

  • names (String)
  • defer: (Boolean)
  • async: (Boolean)

Returns:

  • (nil)


53
# File 'sig/shakapacker/helper.rbs', line 53

def prepend_javascript_pack_tag: (*String names, ?defer: bool, ?async: bool) -> nil

#puts_deprecation_message(message) ⇒ Object



6
7
8
# File 'lib/shakapacker/deprecation_helper.rb', line 6

def puts_deprecation_message(message)
  SHELL.say "\n#{message}\n", :yellow
end

#render_tagsnil

Parameters:

  • sources (Array[String])
  • type (Symbol)
  • options (Hash[Symbol, untyped])

Returns:

  • (nil)


71
# File 'sig/shakapacker/helper.rbs', line 71

def render_tags: (Array[String] sources, Symbol type, Hash[Symbol, untyped] options) -> nil

#resolve_early_hints_valueString

Parameters:

  • early_hints (Hash[String | Symbol, String], String, bool, nil)
  • asset_type (Symbol)

Returns:

  • (String)


79
# File 'sig/shakapacker/helper.rbs', line 79

def resolve_early_hints_value: ((Hash[String | Symbol, String] | String | bool)? early_hints, Symbol asset_type) -> String

#resolve_path_to_imageString

Parameters:

  • name (String)
  • options (Object)

Returns:

  • (String)


65
# File 'sig/shakapacker/helper.rbs', line 65

def resolve_path_to_image: (String name, **untyped options) -> String

#send_early_hints_internalvoid

This method returns an undefined value.

Parameters:

  • config (Hash[String, String])
  • type: (Symbol)


91
# File 'sig/shakapacker/helper.rbs', line 91

def send_early_hints_internal: (Hash[String, String] config, type: Symbol) -> void

#send_javascript_early_hints_internalvoid

This method returns an undefined value.

Parameters:

  • config (Hash[String, String])


95
# File 'sig/shakapacker/helper.rbs', line 95

def send_javascript_early_hints_internal: (Hash[String, String] config) -> void

#send_pack_early_hintsnil

Sends HTTP 103 Early Hints for specified packs

Parameters:

  • config (Hash[String | Symbol, Hash[String | Symbol, String | bool]])

Returns:

  • (nil)


37
# File 'sig/shakapacker/helper.rbs', line 37

def send_pack_early_hints: (Hash[String | Symbol, Hash[String | Symbol, String | bool]] config) -> nil

#send_stylesheet_early_hints_internalvoid

This method returns an undefined value.

Parameters:

  • config (Hash[String, String])


97
# File 'sig/shakapacker/helper.rbs', line 97

def send_stylesheet_early_hints_internal: (Hash[String, String] config) -> void

#sources_from_manifest_entrypointsArray[String]

Parameters:

  • names (Array[String | Symbol])
  • type: (Symbol)

Returns:

  • (Array[String])


61
# File 'sig/shakapacker/helper.rbs', line 61

def sources_from_manifest_entrypoints: (Array[String | Symbol] names, type: Symbol) -> Array[String]

#stylesheet_pack_tagObject

Creates link tags that reference the css chunks from entrypoints



40
# File 'sig/shakapacker/helper.rbs', line 40

def stylesheet_pack_tag: (

#update_javascript_pack_tag_queue {|arg0| ... } ⇒ nil

Parameters:

  • defer: (Boolean)
  • async: (Boolean)

Yields:

Yield Parameters:

  • arg0 (Symbol)

Yield Returns:

  • (void)

Returns:

  • (nil)


57
# File 'sig/shakapacker/helper.rbs', line 57

def update_javascript_pack_tag_queue: (defer: bool, async: bool) { (Symbol) -> void } -> nil

#with_node_env(env) { ... } ⇒ Object

Temporarily overrides NODE_ENV for the duration of the block

This is useful when you need to perform operations with a specific NODE_ENV value without permanently changing the environment.

Examples:

Shakapacker.with_node_env("production") do
  # This code runs with NODE_ENV=production
  Shakapacker.compile
end

Parameters:

  • env (String)

    the NODE_ENV value to use temporarily

Yields:

  • the block to execute with the temporary NODE_ENV

Returns:

  • (Object)

    the return value of the block



89
90
91
92
93
94
95
# File 'lib/shakapacker.rb', line 89

def with_node_env(env)
  original = ENV["NODE_ENV"]
  ENV["NODE_ENV"] = env
  yield
ensure
  ENV["NODE_ENV"] = original
end