Skip to content

Commit 545bab2

Browse files
justin808claude
andcommitted
fix: implement robust Shakapacker webpack.config.js detection
Replaced the simplistic string matching with a comprehensive template-based detection system that: - Includes actual default templates from Shakapacker v6 and v7+ - Normalizes content by removing comments and whitespace for comparison - Handles variations in formatting and comment placement - Correctly identifies standard configs vs custom configurations This approach is much more reliable than keyword matching and reduces false positives when detecting whether a webpack.config.js file can be safely replaced. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]>
1 parent c9582dc commit 545bab2

File tree

1 file changed

+55
-5
lines changed

1 file changed

+55
-5
lines changed

lib/generators/react_on_rails/base_generator.rb

Lines changed: 55 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -120,11 +120,61 @@ def handle_custom_webpack_config(base_path, config, webpack_config_path)
120120
end
121121

122122
def standard_shakapacker_config?(content)
123-
# Check if it's the standard Shakapacker config
124-
content.include?("generateWebpackConfig") &&
125-
content.include?("shakapacker") &&
126-
!content.include?("envSpecificConfig") &&
127-
!content.include?("env.nodeEnv")
123+
# Get the expected default config based on Shakapacker version
124+
expected_configs = shakapacker_default_configs
125+
126+
# Check if the content matches any of the known default configurations
127+
expected_configs.any? { |config| content_matches_template?(content, config) }
128+
end
129+
130+
def content_matches_template?(content, template)
131+
# Normalize whitespace and compare
132+
normalize_config_content(content) == normalize_config_content(template)
133+
end
134+
135+
def normalize_config_content(content)
136+
# Remove comments, normalize whitespace, and clean up for comparison
137+
content.gsub(%r{//.*$}, "") # Remove single-line comments
138+
.gsub(%r{/\*.*?\*/}m, "") # Remove multi-line comments
139+
.gsub(/\s+/, " ") # Normalize whitespace
140+
.strip
141+
end
142+
143+
def shakapacker_default_configs
144+
configs = []
145+
146+
# Shakapacker v7+ (generateWebpackConfig function)
147+
configs << <<~CONFIG
148+
// See the shakacode/shakapacker README and docs directory for advice on customizing your webpackConfig.
149+
const { generateWebpackConfig } = require('shakapacker')
150+
151+
const webpackConfig = generateWebpackConfig()
152+
153+
module.exports = webpackConfig
154+
CONFIG
155+
156+
# Shakapacker v6 (webpackConfig object)
157+
configs << <<~CONFIG
158+
const { webpackConfig } = require('shakapacker')
159+
160+
// See the shakacode/shakapacker README and docs directory for advice on customizing your webpackConfig.
161+
162+
module.exports = webpackConfig
163+
CONFIG
164+
165+
# Also check without comments for variations
166+
configs << <<~CONFIG
167+
const { generateWebpackConfig } = require('shakapacker')
168+
const webpackConfig = generateWebpackConfig()
169+
module.exports = webpackConfig
170+
CONFIG
171+
172+
configs << <<~CONFIG
173+
const { webpackConfig } = require('shakapacker')
174+
module.exports = webpackConfig
175+
CONFIG
176+
177+
configs
128178
end
129179

130180
def react_on_rails_config?(content)

0 commit comments

Comments
 (0)