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/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
Overview
Shakapacker
Shakapacker is a Ruby gem that integrates webpack and rspack with Rails applications, providing a modern asset pipeline for JavaScript, CSS, and other web assets.
The main Shakapacker module provides singleton-style access to configuration, compilation, and asset manifest functionality. Most methods delegate to a shared Instance object.
Basic Usage
# Access configuration
Shakapacker.config.source_path
#=> Pathname("/path/to/app/packs")
# Check if dev server is running
Shakapacker.dev_server.running?
#=> true
# Look up compiled assets
Shakapacker.manifest.lookup("application.js")
#=> "/packs/application-abc123.js"
# Compile assets
Shakapacker.compile
Configuration
Configuration is loaded from config/shakapacker.yml and can be accessed via #config. The configuration determines the source paths, output paths, compilation settings, and dev server options.
Defined Under Namespace
Modules: Helper, 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
"development".freeze
- DEV_TEST_ENVS =
Environments that use their RAILS_ENV value for NODE_ENV All other environments (production, staging, etc.) use “production” for webpack optimizations
%w[development test].freeze
- VERSION =
Change the version in package.json too, please!
"9.5.0".freeze
- SHELL =
Thor::Shell::Color.new
Instance Method Summary collapse
-
#bootstrap ⇒ void
Creates the default configuration files and directory structure.
-
#clean(count = nil, age = nil) ⇒ void
Removes old compiled packs, keeping the most recent versions.
-
#clobber ⇒ void
Removes all compiled packs.
-
#commands ⇒ Shakapacker::Commands
Returns the commands instance for build operations.
-
#compile ⇒ Boolean
Compiles all webpack/rspack packs.
-
#compiler ⇒ Shakapacker::Compiler
Returns the compiler instance for compiling assets.
-
#config ⇒ Shakapacker::Configuration
Returns the Shakapacker configuration object.
-
#dev_server ⇒ Shakapacker::DevServer
Returns the dev server instance for querying server status.
-
#ensure_log_goes_to_stdout { ... } ⇒ Object
Temporarily redirects Shakapacker logging to STDOUT.
-
#ensure_node_env! ⇒ String
private
Sets NODE_ENV based on RAILS_ENV if not already set.
-
#env ⇒ ActiveSupport::StringInquirer
Returns the current Rails environment as an ActiveSupport::StringInquirer.
-
#inlining_css? ⇒ Boolean
Returns whether CSS inlining is enabled.
-
#instance ⇒ Shakapacker::Instance
Returns the shared Shakapacker instance.
-
#instance=(instance) ⇒ Shakapacker::Instance
Sets the shared Shakapacker instance.
-
#logger ⇒ Logger
Returns the logger instance used by Shakapacker.
-
#logger=(logger) ⇒ Logger
Sets the logger instance used by Shakapacker.
-
#manifest ⇒ Shakapacker::Manifest
Returns the manifest instance for looking up compiled assets.
- #puts_deprecation_message(message) ⇒ Object
-
#with_node_env(env) { ... } ⇒ Object
Temporarily overrides NODE_ENV for the duration of the block.
Instance Method Details
#bootstrap ⇒ void
This method returns an undefined value.
Creates the default configuration files and directory structure
187 |
# File 'lib/shakapacker.rb', line 187 delegate :bootstrap, :clean, :clobber, :compile, to: :commands |
#clean(count = nil, age = nil) ⇒ void
This method returns an undefined value.
Removes old compiled packs, keeping the most recent versions
187 |
# File 'lib/shakapacker.rb', line 187 delegate :bootstrap, :clean, :clobber, :compile, to: :commands |
#clobber ⇒ void
This method returns an undefined value.
Removes all compiled packs
187 |
# File 'lib/shakapacker.rb', line 187 delegate :bootstrap, :clean, :clobber, :compile, to: :commands |
#commands ⇒ Shakapacker::Commands
Returns the commands instance for build operations
167 |
# File 'lib/shakapacker.rb', line 167 delegate :config, :compiler, :manifest, :commands, :dev_server, to: :instance |
#compile ⇒ Boolean
Compiles all webpack/rspack packs
187 |
# File 'lib/shakapacker.rb', line 187 delegate :bootstrap, :clean, :clobber, :compile, to: :commands |
#compiler ⇒ Shakapacker::Compiler
Returns the compiler instance for compiling assets
167 |
# File 'lib/shakapacker.rb', line 167 delegate :config, :compiler, :manifest, :commands, :dev_server, to: :instance |
#config ⇒ Shakapacker::Configuration
Returns the Shakapacker configuration object
167 |
# File 'lib/shakapacker.rb', line 167 delegate :config, :compiler, :manifest, :commands, :dev_server, to: :instance |
#dev_server ⇒ Shakapacker::DevServer
Returns the dev server instance for querying server status
167 |
# File 'lib/shakapacker.rb', line 167 delegate :config, :compiler, :manifest, :commands, :dev_server, to: :instance |
#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.
120 121 122 123 124 125 126 |
# File 'lib/shakapacker.rb', line 120 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:
-
developmentandtestenvironments use their RAILS_ENV value for NODE_ENV -
All other environments (
production,staging, etc.) use “production” for webpack optimizations
This method is typically called automatically during Rails initialization.
104 105 106 |
# File 'lib/shakapacker.rb', line 104 def ensure_node_env! ENV["NODE_ENV"] ||= DEV_TEST_ENVS.include?(ENV["RAILS_ENV"]) ? ENV["RAILS_ENV"] : "production" end |
#env ⇒ ActiveSupport::StringInquirer
Returns the current Rails environment as an ActiveSupport::StringInquirer
145 |
# File 'lib/shakapacker.rb', line 145 delegate :logger, :logger=, :env, :inlining_css?, to: :instance |
#inlining_css? ⇒ Boolean
Returns whether CSS inlining is enabled
145 |
# File 'lib/shakapacker.rb', line 145 delegate :logger, :logger=, :env, :inlining_css?, to: :instance |
#instance ⇒ Shakapacker::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.
68 69 70 |
# File 'lib/shakapacker.rb', line 68 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.
57 58 59 |
# File 'lib/shakapacker.rb', line 57 def instance=(instance) @instance = instance end |
#logger ⇒ Logger
Returns the logger instance used by Shakapacker
145 |
# File 'lib/shakapacker.rb', line 145 delegate :logger, :logger=, :env, :inlining_css?, to: :instance |
#logger=(logger) ⇒ Logger
Sets the logger instance used by Shakapacker
145 |
# File 'lib/shakapacker.rb', line 145 delegate :logger, :logger=, :env, :inlining_css?, to: :instance |
#manifest ⇒ Shakapacker::Manifest
Returns the manifest instance for looking up compiled assets
167 |
# File 'lib/shakapacker.rb', line 167 delegate :config, :compiler, :manifest, :commands, :dev_server, to: :instance |
#puts_deprecation_message(message) ⇒ Object
6 7 8 |
# File 'lib/shakapacker/deprecation_helper.rb', line 6 def () SHELL.say "\n#{message}\n", :yellow end |
#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.
86 87 88 89 90 91 92 |
# File 'lib/shakapacker.rb', line 86 def with_node_env(env) original = ENV["NODE_ENV"] ENV["NODE_ENV"] = env yield ensure ENV["NODE_ENV"] = original end |