Skip to content

Commit

Permalink
Merge EmberCLI-generated Manifest with Sprockets'
Browse files Browse the repository at this point in the history
Closes [#298]

Expose Ember's fingerprinted assets to Sprockets.

No longer support Sprockets-based helpers for Rails 3.2, given the
difference in how the Asset Pipeline works post-4.0.

[#298]: #298
  • Loading branch information
seanpdoyle committed Dec 2, 2015
1 parent 7badc25 commit 316e2ef
Show file tree
Hide file tree
Showing 14 changed files with 393 additions and 11 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
master
------

* Merge EmberCLI-generate manifest into Sprockets'. [#316]
* Delete previous build output on application boot instead of on process exit.
[#308]

[#316]: https://github.com/thoughtbot/ember-cli-rails/pull/316
[#308]: https://github.com/thoughtbot/ember-cli-rails/pull/308

0.5.6
Expand Down
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,10 @@ In addition to rendering the EmberCLI generated `index.html`, you can inject the
<%= include_ember_stylesheet_tags :frontend %>
```

**NOTE**

These helpers are only available for Rails versions `>= 4.0`.

### Multiple Ember CLI apps

In the initializer you may specify multiple Ember CLI apps, each of which can be
Expand Down Expand Up @@ -510,10 +514,14 @@ jQuery and Handlebars are the main use cases for this flag.
This project supports:

* Ruby versions `>= 2.1.0`
* Rails `3.2.x` and `>=4.1.x`.
* Rails versions `3.2.x` and `>=4.1.x`.

[Rendering EmberCLI-generated assets through Sprockets](asset-helpers) is
**NOT** supported for Rails `3.2.x`.

To learn more about supported versions and upgrades, read the [upgrading guide].

[asset-helpers]: #rendering-the-embercli-generated-js-and-css
[upgrading guide]: /UPGRADING.md

## Contributing
Expand Down
44 changes: 37 additions & 7 deletions app/helpers/ember_rails_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,7 @@

module EmberRailsHelper
def include_ember_index_html(name, &block)
warn <<-MSG.strip_heredoc
The `include_ember_index_html` helper has been deprecated.
Rename all invocations to `render_ember_app`
MSG
Warnings.warn_include_index_html

render_ember_app(name, &block)
end
Expand All @@ -20,10 +16,44 @@ def render_ember_app(name, &block)
end

def include_ember_script_tags(name, **options)
javascript_include_tag(*EmberCli[name].sprockets.assets, options)
Warnings.warn_asset_helper

javascript_include_tag(*EmberCli[name].sprockets.javascript_assets, options)
end

def include_ember_stylesheet_tags(name, **options)
stylesheet_link_tag(*EmberCli[name].sprockets.assets, options)
Warnings.warn_asset_helper

stylesheet_link_tag(*EmberCli[name].sprockets.stylesheet_assets, options)
end

module Warnings
def self.warn_include_index_html
warn <<-MSG.strip_heredoc
The `include_ember_index_html` helper has been deprecated.
Rename all invocations to `render_ember_app`
MSG
end

def self.warn_asset_helper
if Rails::VERSION::MAJOR < 4
warn <<-MSG.strip_heredoc
`ember-cli-rails` no longer supports Sprockets-based helpers for Rails
versions below 4.0.
Replace usage of
* `include_ember_script_tags`
* `include_ember_stylesheet_tags`
with `render_ember_app` invocations.
To learn more, please read:
* https://github.com/thoughtbot/ember-cli-rails#configuring-the-ember-controller
* https://github.com/thoughtbot/ember-cli-rails/pull/316
MSG
end
end
end
end
2 changes: 2 additions & 0 deletions lib/ember_cli/app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ def compile
@shell.compile
@build.check!
copy_index_html_file
sprockets.update_manifest!
true
end
end
Expand Down Expand Up @@ -82,6 +83,7 @@ def build_and_watch
prepare
@shell.build_and_watch
copy_index_html_file
sprockets.update_manifest!
end

def prepare
Expand Down
56 changes: 56 additions & 0 deletions lib/ember_cli/assets.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
module EmberCli
class Assets
def initialize(app_name:, ember_app_name:, manifest:)
@app_name = app_name
@ember_app_name = ember_app_name
@manifest = manifest
end

def javascripts
if empty_manifest?
fallback_assets
else
[
latest_matching(%r{#{app_name}/assets/vendor(.*)\.js\z}),
latest_matching(%r{#{app_name}/assets/#{ember_app_name}(.*)\.js\z}),
]
end
end

def stylesheets
if empty_manifest?
fallback_assets
else
[
latest_matching(%r{#{app_name}/assets/vendor(.*)\.css\z}),
latest_matching(%r{#{app_name}/assets/#{ember_app_name}(.*)\.css\z}),
]
end
end

private

attr_reader :app_name, :ember_app_name, :manifest

def fallback_assets
["#{app_name}/assets/vendor", "#{app_name}/assets/#{ember_app_name}"]
end

def empty_manifest?
files.empty?
end

def latest_matching(regex)
file, = files.
select { |key, _| key =~ regex }.
sort_by { |_, data| data["mtime"] }.
last

file
end

def files
manifest.files
end
end
end
14 changes: 14 additions & 0 deletions lib/ember_cli/manifest.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
require "sprockets"

module EmberCli
class Manifest
def initialize(environment, path)
@manifest = ::Sprockets::Manifest.new(environment, path)
end

def merge_into!(other)
other.assets.merge!(@manifest.assets)
other.files.merge!(@manifest.files)
end
end
end
14 changes: 14 additions & 0 deletions lib/ember_cli/missing_manifest.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module EmberCli
class MissingManifest
def merge_into!(*)
end

def files
{}
end

def assets
{}
end
end
end
8 changes: 8 additions & 0 deletions lib/ember_cli/path_set.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ def build_error_file
@build_error_file ||= tmp.join("error.txt")
end

def manifest
manifests.first
end

def bower
@bower ||= begin
bower_path = app_options.fetch(:bower_path) { configuration.bower_path }
Expand Down Expand Up @@ -109,6 +113,10 @@ def bundler

delegate :name, :options, to: :app, prefix: true

def manifests
Pathname.glob(app_assets.join("assets", "manifest*.json"))
end

def default_root
rails_root.join(app_name)
end
Expand Down
47 changes: 45 additions & 2 deletions lib/ember_cli/sprockets.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
require "sprockets"
require "ember_cli/errors"
require "non-stupid-digest-assets"
require "ember_cli/html_page"
require "ember_cli/manifest"
require "ember_cli/missing_manifest"
require "ember_cli/assets"

module EmberCli
class Sprockets
class AssetPipelineError < BuildError; end

def initialize(app)
@app = app
end
Expand All @@ -14,6 +19,10 @@ def register!
register_or_raise!(NonStupidDigestAssets.whitelist)
end

def update_manifest!
ember_manifest.merge_into!(rails_manifest)
end

def index_html(head:, body:)
html_page = HtmlPage.new(
content: app.index_file.read,
Expand All @@ -24,14 +33,26 @@ def index_html(head:, body:)
html_page.render
end

def assets
["#{app.name}/assets/vendor", "#{app.name}/assets/#{ember_app_name}"]
def javascript_assets
assets.javascripts
end

def stylesheet_assets
assets.stylesheets
end

private

attr_reader :app

def assets
Assets.new(
app_name: app.name,
ember_app_name: ember_app_name,
manifest: rails_manifest,
)
end

def ember_app_name
@ember_app_name ||= app.options.fetch(:name) { package_json.fetch(:name) }
end
Expand All @@ -41,6 +62,28 @@ def package_json
JSON.parse(app.paths.package_json_file.read).with_indifferent_access
end

def ember_manifest
@ember_manifest ||= begin
if ember_manifest_path.present? && ember_manifest_path.exist?
Manifest.new(Rails.env, ember_manifest_path)
else
MissingManifest.new
end
end
end

def ember_manifest_path
app.paths.manifest
end

def rails_manifest
if Rails.application.respond_to?(:assets_manifest)
Rails.application.assets_manifest
else
MissingManifest.new
end
end

def asset_matcher
%r{\A#{app.name}/}
end
Expand Down
10 changes: 9 additions & 1 deletion spec/lib/ember_cli/app_spec.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
require "ember-cli-rails"

describe EmberCli::App do
describe "#compile" do
it "exits with exit status of 0" do
passed = EmberCli["my-app"].compile

expect(passed).to be true
end
end

describe "#test" do
it "exits with exit status of 0" do
passed = silence_stream(STDOUT) { EmberCli["my-app"].test }
passed = EmberCli["my-app"].test

expect(passed).to be true
end
Expand Down

0 comments on commit 316e2ef

Please sign in to comment.