Skip to content

Commit 7abcc21

Browse files
committed
configuration.rb, manifest.rb to handle no compile
This fix addresses the cases where compile is disabled: 1. If React on Rails depends on Webpacker, but the React on Rails installer has not yet generated a webpacker.yml file or a React on Rails installation is not using Webpacker, then the gem loading of Webpacker will crash due to the missing config file. 2. If React on Rails has its own helper to compile assets for tests, then the manifest might need to get reloaded after Webpacker statically loads.
1 parent 700c92b commit 7abcc21

File tree

2 files changed

+33
-9
lines changed

2 files changed

+33
-9
lines changed

lib/webpacker/configuration.rb

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,15 +47,22 @@ def fetch(key)
4747
end
4848

4949
def data
50-
if env.development?
51-
refresh
52-
else
50+
if env.production?
5351
@data ||= load
52+
else
53+
refresh
5454
end
5555
end
5656

5757
def load
58-
YAML.load(config_path.read)[env].deep_symbolize_keys
58+
if config_path.exist? &&
59+
(@parsed_mtime.nil? ||
60+
((config_mtime = File.mtime(config.public_manifest_path)) > @parsed_mtime))
61+
@parsed_mtime = config_mtime
62+
YAML.load(config_path.read)[env].deep_symbolize_keys
63+
else
64+
{}
65+
end
5966
end
6067

6168
def defaults

lib/webpacker/manifest.rb

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,19 +42,36 @@ def find(name)
4242

4343
def handle_missing_entry(name)
4444
raise Webpacker::Manifest::MissingEntryError,
45-
"Can't find #{name} in #{config.public_manifest_path}. Is webpack still compiling?"
45+
"Can't find #{name} in #{config.public_manifest_path}. Manifest contains: #{@data}"
46+
end
47+
48+
def missing_file_from_manifest_error(bundle_name)
49+
msg = <<-MSG
50+
Webpacker can't find #{bundle_name} in #{config.public_manifest_path}. Possible causes:
51+
1. You are hot reloading.
52+
2. You want to set Configuration.compile to true for your environment.
53+
3. Webpack has not re-run to reflect updates.
54+
4. You have misconfigured Webpacker's config/webpacker.yml file.
55+
5. Your Webpack configuration is not creating a manifest.
56+
Your manifest contains:
57+
#{@data.to_json}
58+
MSG
59+
raise(Webpacker::FileLoader::NotFoundError.new(msg))
4660
end
4761

4862
def data
49-
if env.development?
50-
refresh
51-
else
63+
if env.production?
5264
@data ||= load
65+
else
66+
refresh
5367
end
5468
end
5569

5670
def load
57-
if config.public_manifest_path.exist?
71+
if config.public_manifest_path.exist? &&
72+
(@parsed_mtime.nil? ||
73+
((manifest_mtime = File.mtime(config.public_manifest_path)) > @parsed_mtime))
74+
@parsed_mtime = manifest_mtime
5875
JSON.parse config.public_manifest_path.read
5976
else
6077
{}

0 commit comments

Comments
 (0)