Class: Shakapacker::BuildConfigLoader

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config_file_path = nil) ⇒ BuildConfigLoader

Returns a new instance of BuildConfigLoader.



7
8
9
# File 'lib/shakapacker/build_config_loader.rb', line 7

def initialize(config_file_path = nil)
  @config_file_path = config_file_path || File.join(Dir.pwd, "config", "shakapacker-builds.yml")
end

Instance Attribute Details

#config_file_pathObject (readonly)

Returns the value of attribute config_file_path.



5
6
7
# File 'lib/shakapacker/build_config_loader.rb', line 5

def config_file_path
  @config_file_path
end

Instance Method Details

#exists?Boolean

Returns:

  • (Boolean)


11
12
13
# File 'lib/shakapacker/build_config_loader.rb', line 11

def exists?
  File.exist?(@config_file_path)
end

#list_buildsObject



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/shakapacker/build_config_loader.rb', line 82

def list_builds
  config = load_config
  builds = config["builds"]

  puts "\nAvailable builds in #{@config_file_path}:\n\n"

  builds.each do |name, build|
    bundler = build["bundler"] || config["default_bundler"] || "webpack (default)"
    outputs = build["outputs"] ? build["outputs"].join(", ") : "missing (invalid)"

    puts "  #{name}"
    puts "    Description: #{build["description"]}" if build["description"]
    puts "    Bundler: #{bundler}"
    puts "    Outputs: #{outputs}"
    puts ""
  end
end

#load_build(build_name) ⇒ Object



15
16
17
18
19
20
21
22
23
# File 'lib/shakapacker/build_config_loader.rb', line 15

def load_build(build_name)
  unless exists?
    raise ArgumentError, "Config file not found: #{@config_file_path}\n" \
                        "Run 'bin/shakapacker --init' to generate a sample config file."
  end

  config = load_config
  fetch_build_or_raise(config, build_name)
end

#resolve_build_config(build_name, default_bundler: "webpack") ⇒ 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
59
60
61
62
63
64
# File 'lib/shakapacker/build_config_loader.rb', line 25

def resolve_build_config(build_name, default_bundler: "webpack")
  config = load_config
  build = fetch_build_or_raise(config, build_name)

  # Resolve bundler with precedence: build.bundler > config.default_bundler > default_bundler
  bundler = build["bundler"] || config["default_bundler"] || default_bundler

  # Get environment variables
  environment = build["environment"] || {}

  # Get config file path if specified
  config_file = build["config"]
  if config_file
    # Expand ${BUNDLER} variable
    config_file = config_file.gsub("${BUNDLER}", bundler)
  end

  # Get bundler_env for --env flags
  bundler_env = build["bundler_env"] || {}

  # Get outputs
  outputs = build["outputs"] || []

  # Validate outputs
  if outputs.empty?
    raise ArgumentError, "Build '#{build_name}' has empty outputs array. " \
                        "Please specify at least one output type (client, server, or all)."
  end

  {
    name: build_name,
    description: build["description"],
    bundler: bundler,
    dev_server: build["dev_server"],
    environment: environment,
    bundler_env: bundler_env,
    outputs: outputs,
    config_file: config_file
  }
end

#uses_dev_server?(build_config) ⇒ Boolean

Returns:

  • (Boolean)


66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/shakapacker/build_config_loader.rb', line 66

def uses_dev_server?(build_config)
  # Check explicit dev_server flag first (preferred)
  # Only return early if the value is explicitly set (not nil)
  return build_config[:dev_server] unless build_config[:dev_server].nil?

  # Fallback: check environment variables for backward compatibility
  env = build_config[:environment]
  return false unless env

  # Handle both string "true" and boolean true from YAML
  %w[WEBPACK_SERVE HMR].any? do |key|
    value = env[key]
    value.to_s.strip.casecmp("true").zero?
  end
end