Skip to content

Commit

Permalink
Better error messages and API tweaks (#12)
Browse files Browse the repository at this point in the history
* Added better error messages for missing files.
* Added API to check if the manifest exists.
* Added CONTRIBUTING.md doc
  • Loading branch information
justin808 committed May 27, 2017
1 parent 5e2a026 commit e0975f5
Show file tree
Hide file tree
Showing 11 changed files with 153 additions and 32 deletions.
3 changes: 1 addition & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,4 @@ cache:
install:
- bundle install
script:
- bundle exec rubocop
- bundle exec rake test
- bundle exec rake
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ Contributors: please follow the recommendations outlined at [keepachangelog.com]
## [Unreleased]
*Please add entries here for your pull requests.*

### Changed
* Added better error messages for missing files.
* Added api to check if the manifest exists

## [2.0.0] - 2017-05-23
All in [#9](https://github.com/shakacode/webpacker_lite/pull/9) by [justin808](https://github.com/justin808) with help from [conturbo](https://github.com/conturbo) on the tests.
Expand Down
70 changes: 70 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# Tips for Contributors

## Summary

For non-doc fixes:

* Provide changelog entry in the [unreleased section of the CHANGELOG.md](https://github.com/shakacode/react_on_rails/blob/master/CHANGELOG.md#unreleased).
* Ensure CI passes and that you added a test that passes with the fix and fails without the fix.
* Optionally, squash all commits down to one with a nice commit message *ONLY* once final review is given. Make sure this single commit is rebased on top of master.
* Please address all code review comments.
* Ensure that docs are updated accordingly if a feature is added.

## Commit Messages

From [How to Write a Git Commit Message](http://chris.beams.io/posts/git-commit/)

#### The seven rules of a great git commit message
> Keep in mind: This has all been said before.
1. Separate subject from body with a blank line
1. Limit the subject line to 50 characters
1. Capitalize the subject line
1. Do not end the subject line with a period
1. Use the imperative mood in the subject line
1. Wrap the body at 72 characters
1. Use the body to explain what and why vs. how


## To run:

### Tests
```
rake test
```

### Linting

```
rake rubocop
```

or to autofix:

```
bundle exec rubocop -a
```

### All ci

```sh
rake
```

# Configuring to test changes with your app

Use the relative path syntax in your gemfile.

```ruby
gem "webpacker_lite", path: "../../../webpacker_lite"
```

# Advice for Project Maintainers and Contributors

What do project maintainers do? What sort of work is involved? [sstephenson](https://github.com/sstephenson) wrote in the [turbolinks](https://github.com/turbolinks/turbolinks) repo:

> [Why this is not still fully merged?](https://github.com/turbolinks/turbolinks/pull/124#issuecomment-239826060)
# Releasing


4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ This example config shows how we use different output directories for the webpac
default: &default
manifest: manifest.json
# Used in your webpack configuration. Must be created in the
# webpack_public_output_dir folder
# webpack_public_output_dir folder.

development:
<<: *default
Expand Down Expand Up @@ -141,13 +141,15 @@ production:
## Other Helpers: Getting the asset path

The `asset_pack_path` helper provides the path of any given asset that's been compiled by webpack.
Note, the real file path is the subdirectory of the public.

For example, if you want to create a `<link rel="prefetch">` or `<img />`
for an asset used in your pack code you can reference them like this in your view,

```erb
<img src="<%= asset_pack_path 'calendar.png' %>" />
<% # => <img src="/webpack/calendar.png" /> %>
<% # real file path "public/webpack/calendar.png" /> %>
```

## Webpack Helper
Expand Down
17 changes: 16 additions & 1 deletion Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,26 @@
require "bundler/gem_tasks"
require "rake/testtask"

# Executes a string or an array of strings in a shell in the given directory
def sh_in_dir(dir, shell_commands)
shell_commands = [shell_commands] if shell_commands.is_a?(String)
shell_commands.each { |shell_command| sh %(cd #{dir} && #{shell_command.strip}) }
end

def gem_root
File.expand_path("../.", __FILE__)
end

Rake::TestTask.new(:test) do |t|
t.libs << "test"
t.libs << "lib"
t.test_files = FileList["test/**/*_test.rb"]
t.verbose = true
end

task default: :test
desc "Run Rubocop as shell"
task :rubocop do
sh_in_dir(gem_root, "bundle exec rubocop .")
end

task default: [:test, :rubocop]
6 changes: 3 additions & 3 deletions lib/webpacker_lite.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
module WebpackerLite
def self.bootstrap
WebpackerLite::Env.load
WebpackerLite::Configuration.load
WebpackerLite::Manifest.load
WebpackerLite::Env.load_instance
WebpackerLite::Configuration.load_instance
WebpackerLite::Manifest.load_instance
end

def env
Expand Down
11 changes: 6 additions & 5 deletions lib/webpacker_lite/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@
require "webpacker_lite/env"

class WebpackerLite::Configuration < WebpackerLite::FileLoader
RAILS_WEB_PUBLIC = "public"

class << self
def manifest_path
Rails.root.join(webpack_public_output_dir,
configuration.fetch(:manifest, "manifest.json"))
end

def webpack_public_output_dir
Rails.root.join(
File.join("public", configuration.fetch(:webpack_public_output_dir, "webpack")))
Rails.root.join(RAILS_WEB_PUBLIC, configuration.fetch(:webpack_public_output_dir, "webpack"))
end

def base_path
Expand All @@ -37,8 +38,8 @@ def base_url
end

def configuration
load if WebpackerLite::Env.development?
raise WebpackerLite::FileLoader::FileLoaderError.new("WebpackerLite::Configuration.load must be called first") unless instance
load_instance if WebpackerLite::Env.development?
raise WebpackerLite::FileLoader::FileLoaderError.new("WebpackerLite::Configuration.load_instance must be called first") unless instance
instance.data
end

Expand All @@ -48,7 +49,7 @@ def file_path
end

private
def load
def load_data
return super unless File.exist?(@path)
HashWithIndifferentAccess.new(YAML.load(File.read(@path))[WebpackerLite::Env.current])
end
Expand Down
2 changes: 1 addition & 1 deletion lib/webpacker_lite/env.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def file_path
end

private
def load
def load_data
environments = File.exist?(@path) ? YAML.load(File.read(@path)).keys : [].freeze
return ENV["NODE_ENV"] if environments.include?(ENV["NODE_ENV"])
return Rails.env if environments.include?(Rails.env)
Expand Down
8 changes: 4 additions & 4 deletions lib/webpacker_lite/file_loader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,22 @@ class FileLoaderError < StandardError; end
attr_accessor :data

class << self
def load(path = file_path)
def load_instance(path = file_path)
self.instance = new(path)
end

def file_path
raise "Subclass of WebpackerLite::FileLoader should override this method"
raise FileLoaderError.new("Subclass of WebpackerLite::FileLoader should override this method")
end
end

private
def initialize(path)
@path = path
@data = load
@data = load_data
end

def load
def load_data
{}.freeze
end
end
53 changes: 42 additions & 11 deletions lib/webpacker_lite/manifest.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,30 +11,61 @@

class WebpackerLite::Manifest < WebpackerLite::FileLoader
class << self
def file_path
WebpackerLite::Configuration.manifest_path
# Helper method to determine if the manifest file exists.
def exist?
path_object = WebpackerLite::Configuration.manifest_path
path_object.exist?
end

def lookup(name)
load if WebpackerLite::Env.development? || instance.data.empty?
raise WebpackerLite::FileLoader::FileLoaderError.new("WebpackerLite::Manifest.load must be called first") unless instance
instance.data[name.to_s] || missing_file_error(name)
def file_path
WebpackerLite::Configuration.manifest_path
end

def missing_file_error(name)
def missing_file_from_manifest_error(bundle_name)
msg = <<-MSG
WebpackerLite can't find #{name} in your manifest #{file_path}. Possible causes:
1. You are hot reloading
2. Webpack is not running
WebpackerLite can't find #{bundle_name} in your manifest #{file_path}. Possible causes:
1. You are hot reloading.
2. Webpack has not re-run to reflect updates.
3. You have misconfigured WebpackerLite's config/webpacker_lite.yml file.
4. Your Webpack configuration is not creating a manifest.
MSG
raise(WebpackerLite::FileLoader::NotFoundError.new(msg))
end

# Same as lookup, but raises an error.
def lookup!(name)
lookup(name) || missing_file_from_manifest_error(name)
end

# Find the real file name from the manifest key.
def lookup(name)
instance.confirm_manifest_exists

load_instance if WebpackerLite::Env.development? || instance.data.empty?
raise WebpackerLite::FileLoader::FileLoaderError.new("WebpackerLite::Manifest.load must be called first") unless instance
instance.data[name.to_s]
end
end

def confirm_manifest_exists
raise missing_manifest_file_error(@path) unless File.exist?(@path)
end

private
def load

def missing_manifest_file_error(path_object)
msg = <<-MSG
WebpackerLite can't find the manifest file: #{path_object}
Possible causes:
1. You have not invoked webpack.
2. You have misconfigured WebpackerLite's config/webpacker_lite.yml file.
3. Your Webpack configuration is not creating a manifest.
MSG
raise(WebpackerLite::FileLoader::NotFoundError.new(msg))
end

def load_data
return super unless File.exist?(@path)
JSON.parse(File.read(@path))
end
Expand Down
8 changes: 4 additions & 4 deletions test/manifest_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,21 @@ def test_lookup_exception
asset_file = "calendar.js"
msg = <<-MSG
WebpackerLite can't find #{asset_file} in your manifest #{manifest_path}. Possible causes:
1. You are hot reloading
2. Webpack is not running
1. You are hot reloading.
2. Webpack has not re-run to reflect updates.
3. You have misconfigured WebpackerLite's config/webpacker_lite.yml file.
4. Your Webpack configuration is not creating a manifest.
MSG

error = assert_raises WebpackerLite::FileLoader::NotFoundError do
WebpackerLite::Manifest.lookup(asset_file)
WebpackerLite::Manifest.lookup!(asset_file)
end

assert_equal error.message, msg
end

def test_lookup_success
asset_file = "bootstrap.js"
assert_equal WebpackerLite::Manifest.lookup(asset_file), "bootstrap-300631c4f0e0f9c865bc.js"
assert_equal WebpackerLite::Manifest.lookup!(asset_file), "bootstrap-300631c4f0e0f9c865bc.js"
end
end

0 comments on commit e0975f5

Please sign in to comment.