Class: Shakapacker::DevServerRunner

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

Class Method Summary collapse

Instance Method Summary collapse

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.



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
215
216
217
218
219
220
221
222
223
224
# File 'lib/shakapacker/dev_server_runner.rb', line 189

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



172
173
174
# File 'lib/shakapacker/dev_server_runner.rb', line 172

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


148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/shakapacker/dev_server_runner.rb', line 148

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


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

def self.print_help
  puts <<~HELP
    ================================================================================
    SHAKAPACKER DEV SERVER - Development Server with Hot Module Replacement
    ================================================================================

    Usage: bin/shakapacker-dev-server [options]

    Shakapacker-specific options:
      -h, --help              Show this help message
      -v, --version           Show Shakapacker version
      --debug-shakapacker     Enable Node.js debugging (--inspect-brk)
      --trace-deprecation     Show stack traces for Node.js deprecations
      --no-deprecation        Silence Node.js deprecation warnings
      --build <name>          Run a specific build configuration

      Put Shakapacker-specific options before --. Arguments after -- are
      passed directly to the selected bundler, except --host and --port,
      which remain managed by config/shakapacker.yml.

    Build configurations (config/shakapacker-builds.yml):
      bin/shakapacker-dev-server --build dev-hmr    # Run the 'dev-hmr' build

      To manage builds:
      bin/shakapacker --init                        # Create config file
      bin/shakapacker --list-builds                 # List available builds

      Note: You can also use bin/shakapacker --build with a build that has
      WEBPACK_SERVE=true, and it will automatically use the dev server.

    Examples:
      bin/shakapacker-dev-server                    # Start dev server
      bin/shakapacker-dev-server --no-hot           # Disable HMR
      bin/shakapacker-dev-server --open             # Open browser automatically
      bin/shakapacker-dev-server --debug-shakapacker # Debug with Node inspector
      bin/shakapacker-dev-server --trace-deprecation # Show Node deprecation traces

  HELP

  print_dev_server_help

  puts <<~HELP

    Options managed by Shakapacker (configured in config/shakapacker.yml):
      --host                  Set from dev_server.host (default: localhost)
      --port                  Set from dev_server.port (default: 3035)
      --https                 Requires dev_server.server: https before forwarding
      --config                Set automatically to config/webpack/webpack.config.js
                              or config/rspack/rspack.config.js

    Note: CLI flags for --host and --port are NOT supported.
    Configure these in config/shakapacker.yml instead. --https is accepted
    only when dev_server.server is already set to https.
  HELP
end


226
227
228
229
230
231
232
233
234
235
236
# File 'lib/shakapacker/dev_server_runner.rb', line 226

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

def self.run(argv)
  runner_argv, passthrough_argv = split_passthrough_argv(argv)

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

  Shakapacker.ensure_node_env!

  # Check for --build flag
  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-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 = runner_argv.dup
      remaining_argv.delete_at(build_index + 1)
      remaining_argv.delete_at(build_index)

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

  new(runner_argv, nil, nil, passthrough_argv).run
end

.run_with_build_config(argv, build_config, passthrough_argv = []) ⇒ Object



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

def self.run_with_build_config(argv, build_config, passthrough_argv = [])
  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], passthrough_argv).run
end

Instance Method Details

#runObject



238
239
240
241
242
243
# File 'lib/shakapacker/dev_server_runner.rb', line 238

def run
  load_config
  detect_unsupported_switches!
  detect_port!
  execute_cmd
end