Class: Shakapacker::Runner

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

Direct Known Subclasses

DevServerRunner, RspackRunner, WebpackRunner

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) ⇒ Runner

Returns a new instance of Runner.



60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/shakapacker/runner.rb', line 60

def initialize(argv)
  @argv = argv

  @app_path              = File.expand_path(".", Dir.pwd)
  @shakapacker_config    = ENV["SHAKAPACKER_CONFIG"] || File.join(@app_path, "config/shakapacker.yml")
  @config                = Configuration.new(
    root_path: Pathname.new(@app_path),
    config_path: Pathname.new(@shakapacker_config),
    env: ENV["RAILS_ENV"] || ENV["NODE_ENV"] || "development"
  )
  @webpack_config        = find_assets_bundler_config

  Shakapacker::Utils::Manager.error_unless_package_manager_is_obvious!
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



10
11
12
# File 'lib/shakapacker/runner.rb', line 10

def config
  @config
end

Class Method Details

.run(argv) ⇒ Object



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

def self.run(argv)
  $stdout.sync = true
  Shakapacker.ensure_node_env!

  # Create a single runner instance to avoid loading configuration twice.
  # We extend it with the appropriate build command based on the bundler type.
  runner = new(argv)

  if runner.config.rspack?
    require_relative "rspack_runner"
    # Extend the runner instance with rspack-specific methods
    # This avoids creating a new RspackRunner which would reload the configuration
    runner.extend(Module.new do
      def build_cmd
        package_json.manager.native_exec_command("rspack")
      end

      def assets_bundler_commands
        BASE_COMMANDS + %w[build watch]
      end
    end)
    runner.run
  else
    require_relative "webpack_runner"
    # Extend the runner instance with webpack-specific methods
    # This avoids creating a new WebpackRunner which would reload the configuration
    runner.extend(Module.new do
      def build_cmd
        package_json.manager.native_exec_command("webpack")
      end
    end)
    runner.run
  end
end

Instance Method Details

#package_jsonObject



75
76
77
# File 'lib/shakapacker/runner.rb', line 75

def package_json
  @package_json ||= PackageJson.read(@app_path)
end

#runObject



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

def run
  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
  puts "[Shakapacker] Base command: #{cmd.join(" ")}"

  if @argv.delete("--debug-shakapacker")
    puts "[Shakapacker] Debug mode enabled (--debug-shakapacker)"
    env["NODE_OPTIONS"] = "#{env["NODE_OPTIONS"]} --inspect-brk"
  end

  if @argv.delete "--trace-deprecation"
    puts "[Shakapacker] Trace deprecation enabled (--trace-deprecation)"
    env["NODE_OPTIONS"] = "#{env["NODE_OPTIONS"]} --trace-deprecation"
  end

  if @argv.delete "--no-deprecation"
    puts "[Shakapacker] Deprecation warnings disabled (--no-deprecation)"
    env["NODE_OPTIONS"] = "#{env["NODE_OPTIONS"]} --no-deprecation"
  end

  # Commands are not compatible with --config option.
  if (@argv & assets_bundler_commands).empty?
    puts "[Shakapacker] Adding config file: #{@webpack_config}"
    cmd += ["--config", @webpack_config]
  else
    puts "[Shakapacker] Skipping config file (running assets bundler command: #{(@argv & assets_bundler_commands).join(", ")})"
  end

  cmd += @argv
  puts "[Shakapacker] Final command: #{cmd.join(" ")}"
  puts "[Shakapacker] Working directory: #{@app_path}"

  Dir.chdir(@app_path) do
    Kernel.exec env, *cmd
  end
end