Class: Shakapacker::DevServerRunner

Inherits:
Runner
  • Object
show all
Defined in:
lib/shakapacker/dev_server_runner.rb

Constant Summary

Constants inherited from Runner

Runner::BASE_COMMANDS

Instance Attribute Summary

Attributes inherited from Runner

#config

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Runner

execute_bundler_command, get_bundler_help, get_bundler_version, init_config_file, #initialize, list_builds, #package_json, print_bundler_help

Constructor Details

This class inherits a constructor from Shakapacker::Runner

Class Method Details

.filter_managed_options(help_text) ⇒ Object

Filter dev server help output to remove Shakapacker-managed options

This method processes the raw help output from webpack-dev-server/rspack serve and removes options that Shakapacker manages automatically:

  • –config (set from config/webpack or config/rspack)

  • –host, –port (set from config/shakapacker.yml dev_server settings)

  • –help, –version (shown separately in Shakapacker’s help)

The filtering uses skip_until_blank to track multi-line option descriptions and skip them entirely when the option header matches a managed option.

Note: This relies on dev server help format conventions. If webpack-dev-server or rspack significantly changes their help output format, this may need adjustment.



179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/shakapacker/dev_server_runner.rb', line 179

def self.filter_managed_options(help_text)
  lines = help_text.lines
  filtered_lines = []
  skip_until_blank = false

  lines.each do |line|
    # Skip options that Shakapacker manages and their descriptions
    # These options are shown in the "Options managed by Shakapacker" section
    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*--host\b/) ||
       line.match?(/^\s*--port\b/) ||
       line.match?(/^\s*--https\b/) ||
       line.match?(/^\s*(-h,\s*)?--help\b/) ||
       line.match?(/^\s*(-v,\s*)?--version\b/)
      skip_until_blank = true
      next
    end

    # Continue skipping lines that are part of a filtered option's description
    # Reset when we hit a blank line or the start of a new option (starts with -)
    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_dev_server_helpObject



162
163
164
# File 'lib/shakapacker/dev_server_runner.rb', line 162

def self.get_dev_server_help
  Runner.execute_bundler_command("serve", "--help") { |stdout| stdout }
end


138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/shakapacker/dev_server_runner.rb', line 138

def self.print_dev_server_help
  bundler_type, bundler_help = get_dev_server_help

  if bundler_help
    bundler_name = bundler_type == :rspack ? "RSPACK" : "WEBPACK"
    puts "=" * 80
    puts "AVAILABLE #{bundler_name} DEV SERVER 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/config/dev-server"
    else
      puts "  https://webpack.js.org/configuration/dev-server/"
    end
  else
    puts "For complete documentation:"
    puts "  Webpack: https://webpack.js.org/configuration/dev-server/"
    puts "  Rspack:  https://rspack.dev/config/dev-server"
  end
end


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
# File 'lib/shakapacker/dev_server_runner.rb', line 90

def self.print_help
  puts "    ================================================================================\n    SHAKAPACKER DEV SERVER - Development Server with Hot Module Replacement\n    ================================================================================\n\n    Usage: bin/shakapacker-dev-server [options]\n\n    Shakapacker-specific options:\n      -h, --help              Show this help message\n      -v, --version           Show Shakapacker version\n      --debug-shakapacker     Enable Node.js debugging (--inspect-brk)\n      --build <name>          Run a specific build configuration\n\n    Build configurations (config/shakapacker-builds.yml):\n      bin/shakapacker-dev-server --build dev-hmr    # Run the 'dev-hmr' build\n\n      To manage builds:\n      bin/shakapacker --init                        # Create config file\n      bin/shakapacker --list-builds                 # List available builds\n\n      Note: You can also use bin/shakapacker --build with a build that has\n      WEBPACK_SERVE=true, and it will automatically use the dev server.\n\n    Examples:\n      bin/shakapacker-dev-server                    # Start dev server\n      bin/shakapacker-dev-server --no-hot           # Disable HMR\n      bin/shakapacker-dev-server --open             # Open browser automatically\n      bin/shakapacker-dev-server --debug-shakapacker # Debug with Node inspector\n\n  HELP\n\n  print_dev_server_help\n\n  puts <<~HELP\n\n    Options managed by Shakapacker (configured in config/shakapacker.yml):\n      --host                  Set from dev_server.host (default: localhost)\n      --port                  Set from dev_server.port (default: 3035)\n      --https                 Set from dev_server.server (http or https)\n      --config                Set automatically to config/webpack/webpack.config.js\n                              or config/rspack/rspack.config.js\n\n    Note: CLI flags for --host, --port, and --https are NOT supported.\n    Configure these in config/shakapacker.yml instead.\n  HELP\nend\n"


216
217
218
219
220
221
222
223
224
225
226
# File 'lib/shakapacker/dev_server_runner.rb', line 216

def self.print_version
  puts "Shakapacker #{Shakapacker::VERSION}"
  puts "Framework: Rails #{Rails.version}" if defined?(Rails)

  # Try to get bundler version
  bundler_type, bundler_version = Runner.get_bundler_version
  if bundler_version
    bundler_name = bundler_type == :rspack ? "Rspack" : "Webpack"
    puts "Bundler: #{bundler_name} #{bundler_version}"
  end
end

.run(argv) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/shakapacker/dev_server_runner.rb', line 11

def self.run(argv)
  # Show Shakapacker help and exit (don't call bundler)
  if argv.include?("--help") || argv.include?("-h")
    print_help
    exit(0)
  elsif argv.include?("--version") || argv.include?("-v")
    print_version
    exit(0)
  end

  Shakapacker.ensure_node_env!

  # Check for --build flag
  build_index = argv.index("--build")
  if build_index
    build_name = argv[build_index + 1]

    unless build_name
      $stderr.puts "[Shakapacker] Error: --build requires a build name"
      $stderr.puts "Usage: bin/shakapacker-dev-server --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
      build_config = loader.resolve_build_config(build_name)

      # Check if this build is meant for dev server
      unless loader.uses_dev_server?(build_config)
        $stderr.puts "[Shakapacker] Error: Build '#{build_name}' is not configured for dev server (dev_server: false)"
        $stderr.puts "[Shakapacker] Use this command instead:"
        $stderr.puts "  bin/shakapacker --build #{build_name}"
        exit(1)
      end

      # Remove --build and build name from argv
      remaining_argv = argv.dup
      remaining_argv.delete_at(build_index + 1)
      remaining_argv.delete_at(build_index)

      run_with_build_config(remaining_argv, build_config)
      return
    rescue ArgumentError => e
      $stderr.puts "[Shakapacker] #{e.message}"
      exit(1)
    end
  end

  new(argv).run
end

.run_with_build_config(argv, build_config) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/shakapacker/dev_server_runner.rb', line 69

def self.run_with_build_config(argv, build_config)
  Shakapacker.ensure_node_env!

  # Apply build config environment variables
  build_config[:environment].each do |key, value|
    ENV[key] = value.to_s
  end

  # Set SHAKAPACKER_ASSETS_BUNDLER so JS/TS config files use the correct bundler
  # This ensures the bundler override (from --bundler or build config) is respected
  ENV["SHAKAPACKER_ASSETS_BUNDLER"] = build_config[:bundler]

  puts "[Shakapacker] Running dev server for build: #{build_config[:name]}"
  puts "[Shakapacker] Description: #{build_config[:description]}" if build_config[:description]
  puts "[Shakapacker] Bundler: #{build_config[:bundler]}"
  puts "[Shakapacker] Config file: #{build_config[:config_file]}" if build_config[:config_file]

  # Pass bundler override so Configuration.assets_bundler reflects the build
  new(argv, build_config, build_config[:bundler]).run
end

Instance Method Details

#runObject



228
229
230
231
232
233
# File 'lib/shakapacker/dev_server_runner.rb', line 228

def run
  load_config
  detect_unsupported_switches!
  detect_port!
  execute_cmd
end