shakapacker 10.1.0 → 10.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -27,6 +27,34 @@ require "active_support/core_ext/hash/indifferent_access"
27
27
  #
28
28
  # @see https://github.com/shakacode/shakapacker/blob/main/docs/shakapacker.yml.md
29
29
  class Shakapacker::Configuration
30
+ # Shared Ruby source of truth for Shakapacker-owned Node flags; Runner aliases this constant.
31
+ SHAKAPACKER_NODE_FLAGS = %w[--debug-shakapacker --trace-deprecation --no-deprecation].freeze
32
+
33
+ SHAKAPACKER_RUNNER_COMMANDS = %w[help h --help -h --help=verbose version v --version -v info i].freeze
34
+ private_constant :SHAKAPACKER_RUNNER_COMMANDS
35
+
36
+ SHAKAPACKER_HELP_FLAG_PATTERN = /\A(?:--help|-h)(?:=.*)?\z/
37
+ private_constant :SHAKAPACKER_HELP_FLAG_PATTERN
38
+
39
+ SHAKAPACKER_WATCH_FLAGS = %w[--watch -w].freeze
40
+ private_constant :SHAKAPACKER_WATCH_FLAGS
41
+
42
+ SHAKAPACKER_WATCH_FLAG_PATTERN = /\A(?:--watch|-w)(?:=.*)?\z/
43
+ private_constant :SHAKAPACKER_WATCH_FLAG_PATTERN
44
+
45
+ SHAKAPACKER_MANAGED_COMPILE_FLAGS =
46
+ %w[--config -c --node-env --nodeEnv --bundler --build --init --list-builds].freeze
47
+ private_constant :SHAKAPACKER_MANAGED_COMPILE_FLAGS
48
+
49
+ SHAKAPACKER_MANAGED_COMPILE_FLAG_PATTERN =
50
+ /\A(?:--config|-c|--node-env|--nodeEnv|--bundler|--build|--init|--list-builds)(?:=.*)?\z/
51
+ private_constant :SHAKAPACKER_MANAGED_COMPILE_FLAG_PATTERN
52
+
53
+ DISALLOWED_WEBPACK_COMPILE_FLAGS =
54
+ (SHAKAPACKER_NODE_FLAGS + SHAKAPACKER_RUNNER_COMMANDS + SHAKAPACKER_WATCH_FLAGS +
55
+ SHAKAPACKER_MANAGED_COMPILE_FLAGS).freeze
56
+ private_constant :DISALLOWED_WEBPACK_COMPILE_FLAGS
57
+
30
58
  class << self
31
59
  # Flag indicating whether Shakapacker is currently being installed
32
60
  # Used to suppress certain validations during installation
@@ -233,6 +261,25 @@ class Shakapacker::Configuration
233
261
  fetch(:webpack_compile_output)
234
262
  end
235
263
 
264
+ # Returns extra command-line flags passed to the webpack/rspack compile command
265
+ #
266
+ # @return [Array<String>] bundler CLI flags
267
+ def webpack_compile_flags
268
+ flags = fetch(:webpack_compile_flags)
269
+ return [] if flags.nil?
270
+
271
+ valid_flags = flags.is_a?(Array) && flags.all? { |flag| valid_webpack_compile_flag?(flag) }
272
+
273
+ unless valid_flags
274
+ disallowed_flags = DISALLOWED_WEBPACK_COMPILE_FLAGS.join(", ")
275
+ raise "Shakapacker configuration error: compile flags (webpack_compile_flags) must be an array of " \
276
+ "non-empty strings and must not include \"--\" or Shakapacker-specific " \
277
+ "wrapper/short-circuit/watch/managed flags (#{disallowed_flags})"
278
+ end
279
+
280
+ flags
281
+ end
282
+
236
283
  # Returns the compiler strategy for determining staleness
237
284
  #
238
285
  # Options:
@@ -399,6 +446,16 @@ class Shakapacker::Configuration
399
446
 
400
447
  private
401
448
 
449
+ def valid_webpack_compile_flag?(flag)
450
+ flag.is_a?(String) &&
451
+ !flag.empty? &&
452
+ flag != "--" &&
453
+ !DISALLOWED_WEBPACK_COMPILE_FLAGS.include?(flag) &&
454
+ !SHAKAPACKER_HELP_FLAG_PATTERN.match?(flag) &&
455
+ !SHAKAPACKER_WATCH_FLAG_PATTERN.match?(flag) &&
456
+ !SHAKAPACKER_MANAGED_COMPILE_FLAG_PATTERN.match?(flag)
457
+ end
458
+
402
459
  def default_javascript_transpiler
403
460
  # RSpack has built-in SWC support, use it by default
404
461
  rspack? ? "swc" : "babel"
@@ -9,11 +9,13 @@ require_relative "version"
9
9
  module Shakapacker
10
10
  class DevServerRunner < Shakapacker::Runner
11
11
  def self.run(argv)
12
+ runner_argv, passthrough_argv = split_passthrough_argv(argv)
13
+
12
14
  # Show Shakapacker help and exit (don't call bundler)
13
- if argv.include?("--help") || argv.include?("-h")
15
+ if runner_argv.include?("--help") || runner_argv.include?("-h")
14
16
  print_help
15
17
  exit(0)
16
- elsif argv.include?("--version") || argv.include?("-v")
18
+ elsif runner_argv.include?("--version") || runner_argv.include?("-v")
17
19
  print_version
18
20
  exit(0)
19
21
  end
@@ -21,9 +23,9 @@ module Shakapacker
21
23
  Shakapacker.ensure_node_env!
22
24
 
23
25
  # Check for --build flag
24
- build_index = argv.index("--build")
26
+ build_index = runner_argv.index("--build")
25
27
  if build_index
26
- build_name = argv[build_index + 1]
28
+ build_name = runner_argv[build_index + 1]
27
29
 
28
30
  unless build_name
29
31
  $stderr.puts "[Shakapacker] Error: --build requires a build name"
@@ -51,11 +53,11 @@ module Shakapacker
51
53
  end
52
54
 
53
55
  # Remove --build and build name from argv
54
- remaining_argv = argv.dup
56
+ remaining_argv = runner_argv.dup
55
57
  remaining_argv.delete_at(build_index + 1)
56
58
  remaining_argv.delete_at(build_index)
57
59
 
58
- run_with_build_config(remaining_argv, build_config)
60
+ run_with_build_config(remaining_argv, build_config, passthrough_argv)
59
61
  return
60
62
  rescue ArgumentError => e
61
63
  $stderr.puts "[Shakapacker] #{e.message}"
@@ -63,10 +65,10 @@ module Shakapacker
63
65
  end
64
66
  end
65
67
 
66
- new(argv).run
68
+ new(runner_argv, nil, nil, passthrough_argv).run
67
69
  end
68
70
 
69
- def self.run_with_build_config(argv, build_config)
71
+ def self.run_with_build_config(argv, build_config, passthrough_argv = [])
70
72
  Shakapacker.ensure_node_env!
71
73
 
72
74
  # Apply build config environment variables
@@ -84,7 +86,7 @@ module Shakapacker
84
86
  puts "[Shakapacker] Config file: #{build_config[:config_file]}" if build_config[:config_file]
85
87
 
86
88
  # Pass bundler override so Configuration.assets_bundler reflects the build
87
- new(argv, build_config, build_config[:bundler]).run
89
+ new(argv, build_config, build_config[:bundler], passthrough_argv).run
88
90
  end
89
91
 
90
92
  def self.print_help
@@ -99,8 +101,14 @@ module Shakapacker
99
101
  -h, --help Show this help message
100
102
  -v, --version Show Shakapacker version
101
103
  --debug-shakapacker Enable Node.js debugging (--inspect-brk)
104
+ --trace-deprecation Show stack traces for Node.js deprecations
105
+ --no-deprecation Silence Node.js deprecation warnings
102
106
  --build <name> Run a specific build configuration
103
107
 
108
+ Put Shakapacker-specific options before --. Arguments after -- are
109
+ passed directly to the selected bundler, except --host and --port,
110
+ which remain managed by config/shakapacker.yml.
111
+
104
112
  Build configurations (config/shakapacker-builds.yml):
105
113
  bin/shakapacker-dev-server --build dev-hmr # Run the 'dev-hmr' build
106
114
 
@@ -116,6 +124,7 @@ module Shakapacker
116
124
  bin/shakapacker-dev-server --no-hot # Disable HMR
117
125
  bin/shakapacker-dev-server --open # Open browser automatically
118
126
  bin/shakapacker-dev-server --debug-shakapacker # Debug with Node inspector
127
+ bin/shakapacker-dev-server --trace-deprecation # Show Node deprecation traces
119
128
 
120
129
  HELP
121
130
 
@@ -126,12 +135,13 @@ module Shakapacker
126
135
  Options managed by Shakapacker (configured in config/shakapacker.yml):
127
136
  --host Set from dev_server.host (default: localhost)
128
137
  --port Set from dev_server.port (default: 3035)
129
- --https Set from dev_server.server (http or https)
138
+ --https Requires dev_server.server: https before forwarding
130
139
  --config Set automatically to config/webpack/webpack.config.js
131
140
  or config/rspack/rspack.config.js
132
141
 
133
- Note: CLI flags for --host, --port, and --https are NOT supported.
134
- Configure these in config/shakapacker.yml instead.
142
+ Note: CLI flags for --host and --port are NOT supported.
143
+ Configure these in config/shakapacker.yml instead. --https is accepted
144
+ only when dev_server.server is already set to https.
135
145
  HELP
136
146
  end
137
147
 
@@ -260,15 +270,19 @@ module Shakapacker
260
270
  UNSUPPORTED_SWITCHES = %w[--host --port]
261
271
  private_constant :UNSUPPORTED_SWITCHES
262
272
  def detect_unsupported_switches!
263
- unsupported_switches = UNSUPPORTED_SWITCHES & @argv
273
+ # Host/port stay config-owned even after -- because Shakapacker assembles the dev-server command.
274
+ unsupported_switches = UNSUPPORTED_SWITCHES & bundler_argv
264
275
  if unsupported_switches.any?
265
- $stdout.puts "The following CLI switches are not supported by Shakapacker: #{unsupported_switches.join(' ')}. Please edit your command and try again."
266
- exit!
276
+ log_output.puts(
277
+ "The following CLI switches are not supported by Shakapacker: #{unsupported_switches.join(' ')}. " \
278
+ "Please set dev_server.host or dev_server.port in shakapacker.yml instead."
279
+ )
280
+ exit_after_shakapacker_flag_error(1)
267
281
  end
268
282
 
269
- if @argv.include?("--https") && !@https
270
- $stdout.puts "--https requires that 'server' in shakapacker.yml is set to 'https'"
271
- exit!
283
+ if bundler_argv.include?("--https") && !@https
284
+ log_output.puts "--https requires that 'server' in shakapacker.yml is set to 'https'"
285
+ exit_after_shakapacker_flag_error(1)
272
286
  end
273
287
  end
274
288
 
@@ -289,10 +303,21 @@ module Shakapacker
289
303
 
290
304
  cmd = build_cmd
291
305
 
306
+ detect_shakapacker_flags_in_passthrough!
307
+
308
+ # Shakapacker-owned Node flags must appear before --; passthrough args belong to the bundler.
292
309
  if @argv.delete("--debug-shakapacker")
293
310
  env["NODE_OPTIONS"] = "#{env["NODE_OPTIONS"]} --inspect-brk --trace-warnings"
294
311
  end
295
312
 
313
+ if @argv.delete("--trace-deprecation")
314
+ env["NODE_OPTIONS"] = "#{env["NODE_OPTIONS"]} --trace-deprecation"
315
+ end
316
+
317
+ if @argv.delete("--no-deprecation")
318
+ env["NODE_OPTIONS"] = "#{env["NODE_OPTIONS"]} --no-deprecation"
319
+ end
320
+
296
321
  # Add config file
297
322
  cmd += ["--config", @webpack_config]
298
323
 
@@ -307,7 +332,7 @@ module Shakapacker
307
332
  cmd += ["--hot"] if @hot && @hot != false
308
333
  end
309
334
 
310
- cmd += @argv
335
+ cmd += bundler_argv
311
336
 
312
337
  Dir.chdir(@app_path) do
313
338
  exec(env, *cmd)
@@ -30,7 +30,7 @@ module Shakapacker
30
30
  end
31
31
 
32
32
  def watched_files_digest
33
- if Rails.env.development?
33
+ if env.development?
34
34
  warn <<~MSG.strip
35
35
  Shakapacker::Compiler - Slow setup for development
36
36
  Prepare JS assets with either:
@@ -46,7 +46,7 @@ module Shakapacker
46
46
  files = Dir[*expanded_paths].reject { |f| File.directory?(f) }
47
47
  file_ids = files.sort.map { |f| "#{File.basename(f)}/#{Digest::SHA1.file(f).hexdigest}" }
48
48
 
49
- asset_host = Shakapacker.config.asset_host.to_s
49
+ asset_host = config.asset_host.to_s
50
50
  Digest::SHA1.hexdigest(file_ids.join("/").concat(asset_host))
51
51
  end
52
52
 
@@ -56,7 +56,7 @@ module Shakapacker
56
56
  end
57
57
 
58
58
  def compilation_digest_path
59
- config.cache_path.join("last-compilation-digest-#{Shakapacker.env}")
59
+ config.cache_path.join("last-compilation-digest-#{env}")
60
60
  end
61
61
  end
62
62
  end