Class: Shakapacker::Runner
- Inherits:
-
Object
- Object
- Shakapacker::Runner
show all
- Defined in:
- lib/shakapacker/runner.rb
Constant Summary
collapse
- BASE_COMMANDS =
Common commands that don't work with --config option
[
"help",
"h",
"--help",
"-h",
"version",
"v",
"--version",
"-v",
"info",
"i"
].freeze
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(argv, build_config = nil, bundler_override = nil, passthrough_argv = nil) ⇒ Runner
Returns a new instance of Runner.
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
|
# File 'lib/shakapacker/runner.rb', line 228
def initialize(argv, build_config = nil, bundler_override = nil, passthrough_argv = nil)
@argv, @passthrough_argv =
unless passthrough_argv.nil?
[argv, passthrough_argv]
else
self.class.split_passthrough_argv(argv)
end
@build_config = build_config
@bundler_override = bundler_override
@json_output = self.class.json_output?(bundler_argv)
@app_path = File.expand_path(".", Dir.pwd)
@shakapacker_config = ENV["SHAKAPACKER_CONFIG"] || File.join(@app_path, "config/shakapacker.yml")
config_opts = {
root_path: Pathname.new(@app_path),
config_path: Pathname.new(@shakapacker_config),
env: ENV["RAILS_ENV"] || ENV["NODE_ENV"] || "development"
}
config_opts[:bundler_override] = bundler_override if bundler_override
@config = Configuration.new(**config_opts)
@webpack_config = find_webpack_config_from_build_or_default
Shakapacker::Utils::Manager.error_unless_package_manager_is_obvious!
end
|
Instance Attribute Details
#config ⇒ Object
Returns the value of attribute config.
13
14
15
|
# File 'lib/shakapacker/runner.rb', line 13
def config
@config
end
|
Class Method Details
.execute_bundler_command(*bundler_args) {|stdout| ... } ⇒ Object
Shared helper to execute bundler commands with output suppression
Returns [bundler_type, processed_output] or [nil, nil] on error
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
|
# File 'lib/shakapacker/runner.rb', line 597
def self.execute_bundler_command(*bundler_args)
app_path = File.expand_path(".", Dir.pwd)
config_path = ENV["SHAKAPACKER_CONFIG"] || File.join(app_path, "config/shakapacker.yml")
return [nil, nil] unless File.exist?(config_path)
original_stdout = $stdout
original_stderr = $stderr
begin
$stdout = StringIO.new
$stderr = StringIO.new
runner = new([])
return [nil, nil] unless runner.config
bundler_type = runner.config.rspack? ? :rspack : :webpack
bundler_name = bundler_type == :rspack ? "rspack" : "webpack"
cmd = runner.package_json.manager.native_exec_command(bundler_name, bundler_args.flatten)
$stdout = original_stdout
$stderr = original_stderr
require "open3"
stdout, _stderr, status = Open3.capture3(*cmd)
return [nil, nil] unless status.success?
processed_output = yield(stdout)
[bundler_type, processed_output]
rescue StandardError => e
[nil, nil]
ensure
$stdout = original_stdout
$stderr = original_stderr
end
end
|
.filter_managed_options(help_text) ⇒ Object
Filter bundler help output to remove Shakapacker-managed options
This method processes the raw help output from webpack/rspack and removes:
- Command sections (e.g., "Commands: webpack build")
- Options that Shakapacker manages automatically (--config, --nodeEnv, etc.)
- Help/version flags (shown separately in Shakapacker's help)
The filtering uses stateful line-by-line processing:
- in_commands_section: tracks when we're inside a Commands: block
- skip_until_blank: tracks multi-line option descriptions to skip entirely
Note: This relies on bundler help format conventions. If webpack/rspack
significantly changes their help output format, this may need adjustment.
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
|
# File 'lib/shakapacker/runner.rb', line 476
def self.filter_managed_options(help_text)
lines = help_text.lines
filtered_lines = []
skip_until_blank = false
in_commands_section = false
lines.each do |line|
if line.match?(/^\[options\]/) || line.match?(/^Commands:/)
in_commands_section = true
next
end
if in_commands_section
if line.match?(/^Options:/) || (line.strip.empty? && filtered_lines.last&.strip&.empty?)
in_commands_section = false
else
next
end
end
if line.match?(/^\s*(-c,\s*)?--config\b/) ||
line.match?(/^\s*--configName\b/) ||
line.match?(/^\s*--configLoader\b/) ||
line.match?(/^\s*--nodeEnv\b/) ||
line.match?(/^\s*(-h,\s*)?--help\b/) ||
line.match?(/^\s*(-v,\s*)?--version\b/)
skip_until_blank = true
next
end
if skip_until_blank
if line.strip.empty? || line.match?(/^\s*-/)
skip_until_blank = false
else
next
end
end
filtered_lines << line
end
filtered_lines.join
end
|
.get_bundler_help(help_flag = "--help") ⇒ Object
459
460
461
|
# File 'lib/shakapacker/runner.rb', line 459
def self.get_bundler_help(help_flag = "--help")
execute_bundler_command(help_flag) { |stdout| stdout }
end
|
.get_bundler_version ⇒ Object
586
587
588
|
# File 'lib/shakapacker/runner.rb', line 586
def self.get_bundler_version
execute_bundler_command("--version") { |stdout| stdout.strip }
end
|
.init_config_file ⇒ Object
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
|
# File 'lib/shakapacker/runner.rb', line 540
def self.init_config_file
loader = BuildConfigLoader.new
config_path = loader.config_file_path
if loader.exists?
puts "[Shakapacker] Config file already exists: #{config_path}"
puts "Use --list-builds to see available builds"
return
end
app_path = File.expand_path(".", Dir.pwd)
shakapacker_config_path = File.join(app_path, "bin", "shakapacker-config")
unless File.exist?(shakapacker_config_path)
$stderr.puts "[Shakapacker] Error: bin/shakapacker-config not found"
$stderr.puts "Please ensure Shakapacker is properly installed"
exit(1)
end
unless system(shakapacker_config_path, "--init")
exit_code = $?.exitstatus || 1
$stderr.puts "[Shakapacker] Error: Failed to run: #{shakapacker_config_path} --init"
$stderr.puts "[Shakapacker] Command exited with status: #{exit_code}"
exit(exit_code)
end
end
|
.json_output?(argv) ⇒ Boolean
46
47
48
|
# File 'lib/shakapacker/runner.rb', line 46
def self.json_output?(argv)
argv.include?("--json") || argv.include?("-j")
end
|
.list_builds ⇒ Object
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
|
# File 'lib/shakapacker/runner.rb', line 569
def self.list_builds
loader = BuildConfigLoader.new
unless loader.exists?
puts "[Shakapacker] No config file found: #{loader.config_file_path}"
puts "Run 'bin/shakapacker --init' to create one"
return
end
begin
loader.list_builds
rescue ArgumentError => e
$stderr.puts "[Shakapacker] Error: #{e.message}"
exit(1)
end
end
|
.log_output_for(argv) ⇒ Object
50
51
52
|
# File 'lib/shakapacker/runner.rb', line 50
def self.log_output_for(argv)
json_output?(argv) ? $stderr : $stdout
end
|
.print_bundler_help(verbose: false) ⇒ Object
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
|
# File 'lib/shakapacker/runner.rb', line 434
def self.print_bundler_help(verbose: false)
help_flag = verbose ? "--help=verbose" : "--help"
bundler_type, bundler_help = get_bundler_help(help_flag)
if bundler_help
bundler_name = bundler_type == :rspack ? "RSPACK" : "WEBPACK"
puts "=" * 80
puts "AVAILABLE #{bundler_name} OPTIONS (Passed directly to #{bundler_name.downcase})"
puts "=" * 80
puts
puts filter_managed_options(bundler_help)
puts
puts "For complete documentation:"
if bundler_type == :rspack
puts " https://rspack.dev/api/cli"
else
puts " https://webpack.js.org/api/cli/"
end
else
puts "For complete documentation:"
puts " Webpack: https://webpack.js.org/api/cli/"
puts " Rspack: https://rspack.dev/api/cli"
end
end
|
.print_help(verbose: false) ⇒ Object
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
|
# File 'lib/shakapacker/runner.rb', line 374
def self.print_help(verbose: false)
puts <<~HELP
================================================================================
SHAKAPACKER - Rails Webpack/Rspack Integration
================================================================================
Usage: bin/shakapacker [options]
Shakapacker-specific options:
-h, --help Show this help message
--help=verbose Show verbose help including all bundler options
-v, --version Show Shakapacker version
--debug-shakapacker Enable Node.js debugging (--inspect-brk)
--trace-deprecation Show stack traces for deprecations
--no-deprecation Silence deprecation warnings
--bundler <webpack|rspack>
Override bundler (defaults to shakapacker.yml)
Put Shakapacker-specific options before --. Arguments after -- are
passed directly to the selected bundler.
Build configurations (config/shakapacker-builds.yml):
--init Create config/shakapacker-builds.yml
--list-builds List available builds
--build <name> Run a specific build configuration
Examples (build configs):
bin/shakapacker --init # Create config file
bin/shakapacker --list-builds # Show available builds
bin/shakapacker --build dev-hmr # Run the 'dev-hmr' build
bin/shakapacker --build prod # Run the 'prod' build
bin/shakapacker --build prod --bundler rspack # Override to use rspack
Note: If a build has dev_server: true in its config, it will
automatically use bin/shakapacker-dev-server instead.
Advanced: Use bin/shakapacker-config for more config management options
(validate builds, export configs, etc.)
HELP
print_bundler_help(verbose: verbose)
puts <<~HELP
Examples (passing options to webpack/rspack):
bin/shakapacker # Build for production
bin/shakapacker --bundler rspack # Build with rspack instead of webpack
bin/shakapacker --mode development # Build for development
bin/shakapacker --watch # Watch mode
bin/shakapacker --mode development --analyze # Development build with analysis
bin/shakapacker --debug-shakapacker # Debug with Node inspector
Options managed by Shakapacker (configured via config files):
--config Set automatically based on assets_bundler_config_path
(defaults to config/webpack or config/rspack)
--node-env Set from RAILS_ENV or NODE_ENV
HELP
end
|
.print_version ⇒ Object
528
529
530
531
532
533
534
535
536
537
538
|
# File 'lib/shakapacker/runner.rb', line 528
def self.print_version
puts "Shakapacker #{Shakapacker::VERSION}"
puts "Framework: Rails #{Rails.version}" if defined?(Rails)
bundler_type, bundler_version = get_bundler_version
if bundler_version
bundler_name = bundler_type == :rspack ? "Rspack" : "Webpack"
puts "Bundler: #{bundler_name} #{bundler_version}"
end
end
|
.run(argv) ⇒ Object
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
|
# File 'lib/shakapacker/runner.rb', line 61
def self.run(argv)
$stdout.sync = true
runner_argv, passthrough_argv = split_passthrough_argv(argv)
help_verbose = runner_argv.any? { |arg| arg == "--help=verbose" }
if runner_argv.include?("--help") || runner_argv.include?("-h") || help_verbose
print_help(verbose: help_verbose)
exit(0)
elsif runner_argv.include?("--version") || runner_argv.include?("-v")
print_version
exit(0)
elsif runner_argv.include?("--init")
init_config_file
exit(0)
elsif runner_argv.include?("--list-builds")
list_builds
exit(0)
end
bundler_override = nil
bundler_index = runner_argv.index("--bundler")
if bundler_index
bundler_value = runner_argv[bundler_index + 1]
unless bundler_value && %w[webpack rspack].include?(bundler_value)
$stderr.puts "[Shakapacker] Error: --bundler requires 'webpack' or 'rspack'"
$stderr.puts "Usage: bin/shakapacker --bundler <webpack|rspack>"
exit(1)
end
bundler_override = bundler_value
end
build_index = runner_argv.index("--build")
if build_index
build_name = runner_argv[build_index + 1]
unless build_name
$stderr.puts "[Shakapacker] Error: --build requires a build name"
$stderr.puts "Usage: bin/shakapacker --build <name>"
exit(1)
end
loader = BuildConfigLoader.new
unless loader.exists?
$stderr.puts "[Shakapacker] Config file not found: #{loader.config_file_path}"
$stderr.puts "Run 'bin/shakapacker --init' to create one"
exit(1)
end
begin
resolve_opts = {}
resolve_opts[:default_bundler] = bundler_override if bundler_override
build_config = loader.resolve_build_config(build_name, **resolve_opts)
remaining_argv = runner_argv.dup
remaining_argv.delete_at(build_index + 1)
remaining_argv.delete_at(build_index)
if bundler_index
bundler_idx_in_remaining = remaining_argv.index("--bundler")
if bundler_idx_in_remaining
remaining_argv.delete_at(bundler_idx_in_remaining + 1)
remaining_argv.delete_at(bundler_idx_in_remaining)
end
end
if loader.uses_dev_server?(build_config)
log = log_output_for(runner_argv + passthrough_argv)
log.puts "[Shakapacker] Build '#{build_name}' requires dev server"
log.puts "[Shakapacker] Running: bin/shakapacker-dev-server --build #{build_name}"
log.puts ""
require_relative "dev_server_runner"
DevServerRunner.run_with_build_config(remaining_argv, build_config, passthrough_argv)
return
end
run_with_build_config(remaining_argv, build_config, passthrough_argv)
return
rescue ArgumentError => e
$stderr.puts "[Shakapacker] #{e.message}"
exit(1)
end
end
Shakapacker.ensure_node_env!
remaining_argv = runner_argv.dup
if bundler_index
bundler_idx = remaining_argv.index("--bundler")
if bundler_idx
remaining_argv.delete_at(bundler_idx + 1)
remaining_argv.delete_at(bundler_idx)
end
end
ENV["SHAKAPACKER_ASSETS_BUNDLER"] = bundler_override if bundler_override
runner = new(remaining_argv, nil, bundler_override, passthrough_argv)
use_rspack = bundler_override ? (bundler_override == "rspack") : runner.config.rspack?
if use_rspack
require_relative "rspack_runner"
runner.extend(RSPACK_RUNNER_EXTENSION)
runner.run
else
require_relative "webpack_runner"
runner.extend(WEBPACK_RUNNER_EXTENSION)
runner.run
end
end
|
.run_with_build_config(argv, build_config, passthrough_argv = []) ⇒ Object
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
|
# File 'lib/shakapacker/runner.rb', line 192
def self.run_with_build_config(argv, build_config, passthrough_argv = [])
$stdout.sync = true
Shakapacker.ensure_node_env!
build_config[:environment].each do |key, value|
ENV[key] = value.to_s
end
ENV["SHAKAPACKER_ASSETS_BUNDLER"] = build_config[:bundler]
log = log_output_for(argv + passthrough_argv)
log.puts "[Shakapacker] Running build: #{build_config[:name]}"
log.puts "[Shakapacker] Description: #{build_config[:description]}" if build_config[:description]
log.puts "[Shakapacker] Bundler: #{build_config[:bundler]}"
log.puts "[Shakapacker] Config file: #{build_config[:config_file]}" if build_config[:config_file]
runner = new(argv, build_config, build_config[:bundler], passthrough_argv)
if build_config[:bundler] == "rspack"
require_relative "rspack_runner"
runner.extend(RSPACK_RUNNER_EXTENSION)
runner.run
else
require_relative "webpack_runner"
runner.extend(WEBPACK_RUNNER_EXTENSION)
runner.run
end
end
|
.split_passthrough_argv(argv) ⇒ Object
54
55
56
57
58
59
|
# File 'lib/shakapacker/runner.rb', line 54
def self.split_passthrough_argv(argv)
separator_index = argv.index("--")
return [argv.dup, []] unless separator_index
[argv[0...separator_index], argv[(separator_index + 1)..]]
end
|
Instance Method Details
#package_json ⇒ Object
258
259
260
|
# File 'lib/shakapacker/runner.rb', line 258
def package_json
@package_json ||= PackageJson.read(@app_path)
end
|
#run ⇒ Object
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
|
# File 'lib/shakapacker/runner.rb', line 262
def run
log_output.puts "[Shakapacker] Preparing environment for assets bundler execution..."
env = Shakapacker::Compiler.env
env["SHAKAPACKER_CONFIG"] = @shakapacker_config
env["NODE_OPTIONS"] = ENV["NODE_OPTIONS"] || ""
cmd = build_cmd
log_output.puts "[Shakapacker] Base command: #{cmd.join(" ")}"
detect_shakapacker_flags_in_passthrough!
if @argv.delete("--debug-shakapacker")
log_output.puts "[Shakapacker] Debug mode enabled (--debug-shakapacker)"
env["NODE_OPTIONS"] = "#{env["NODE_OPTIONS"]} --inspect-brk"
end
if @argv.delete "--trace-deprecation"
log_output.puts "[Shakapacker] Trace deprecation enabled (--trace-deprecation)"
env["NODE_OPTIONS"] = "#{env["NODE_OPTIONS"]} --trace-deprecation"
end
if @argv.delete "--no-deprecation"
log_output.puts "[Shakapacker] Deprecation warnings disabled (--no-deprecation)"
env["NODE_OPTIONS"] = "#{env["NODE_OPTIONS"]} --no-deprecation"
end
incompatible_args = config_incompatible_args
if incompatible_args.empty?
log_output.puts "[Shakapacker] Adding config file: #{@webpack_config}"
cmd += ["--config", @webpack_config]
else
log_output.puts "[Shakapacker] Skipping config file (running assets bundler command: #{incompatible_args.join(", ")})"
end
cmd += bundler_argv
log_output.puts "[Shakapacker] Final command: #{cmd.join(" ")}"
log_output.puts "[Shakapacker] Working directory: #{@app_path}"
watch_mode = bundler_argv.include?("--watch") || bundler_argv.include?("-w")
start_time = Time.now unless watch_mode
Dir.chdir(@app_path) do
child_pid = nil
trap("TERM") do
if child_pid
Process.kill("TERM", child_pid)
else
raise SignalException, "TERM"
end
rescue Errno::ESRCH
nil
end
child_pid = spawn(env, *cmd)
Process.wait(child_pid)
end
if !watch_mode && start_time
bundler_name = @config.rspack? ? "rspack" : "webpack"
elapsed_time = Time.now - start_time
minutes = (elapsed_time / 60).floor
seconds = (elapsed_time % 60).round(2)
time_display = minutes > 0 ? "#{minutes}:#{format('%05.2f', seconds)}s" : "#{elapsed_time.round(2)}s"
log_output.puts "[Shakapacker] Completed #{bundler_name} build in #{time_display} (#{elapsed_time.round(2)}s)"
end
exit($?.exitstatus || 1) unless $?.success?
end
|