-
-
Notifications
You must be signed in to change notification settings - Fork 205
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge EmberCLI-generated Manifest with Sprockets'
- Loading branch information
1 parent
14775a5
commit 10894b7
Showing
11 changed files
with
308 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
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 latest_matching(regex) | ||
file, = files. | ||
select { |key| key =~ regex }. | ||
sort_by { |_, data| data["mtime"] }. | ||
reverse. | ||
first | ||
|
||
file | ||
end | ||
|
||
def files | ||
manifest.files | ||
end | ||
|
||
def empty_manifest? | ||
files.empty? | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
require "ember_cli/assets" | ||
|
||
describe EmberCli::Assets do | ||
describe "#javascripts" do | ||
it "includes the most recent javascript build artifacts" do | ||
manifest = build_manifest( | ||
files: { | ||
"not-a-match" => {}, | ||
"foo/assets/bar-abc123.js" => { "mtime" => 1.day.ago.iso8601 }, | ||
"foo/assets/bar-def456.js" => { "mtime" => 2.days.ago.iso8601 }, | ||
"foo/assets/vendor-abc123.js" => { "mtime" => 1.day.ago.iso8601 }, | ||
"foo/assets/vendor-def456.js" => { "mtime" => 2.days.ago.iso8601 }, | ||
}, | ||
) | ||
assets = build_assets( | ||
app_name: "foo", | ||
ember_app_name: "bar", | ||
manifest: manifest, | ||
) | ||
|
||
javascripts = assets.javascripts | ||
|
||
expect(javascripts).to match_array([ | ||
"foo/assets/bar-abc123.js", | ||
"foo/assets/vendor-abc123.js", | ||
]) | ||
end | ||
|
||
context "when the manifest is empty" do | ||
it "falls back to the default assets" do | ||
assets = build_assets( | ||
manifest: build_empty_manifest, | ||
app_name: "foo", | ||
ember_app_name: "bar", | ||
) | ||
|
||
javascripts = assets.javascripts | ||
|
||
expect(javascripts).to match_array([ | ||
"foo/assets/vendor", | ||
"foo/assets/bar", | ||
]) | ||
end | ||
end | ||
end | ||
|
||
describe "#stylesheets" do | ||
it "includes the most recent stylesheet build artifacts" do | ||
manifest = build_manifest( | ||
files: { | ||
"not-a-match" => {}, | ||
"foo/assets/bar-abc123.css" => { "mtime" => 1.day.ago.iso8601 }, | ||
"foo/assets/bar-def456.css" => { "mtime" => 2.days.ago.iso8601 }, | ||
"foo/assets/vendor-abc123.css" => { "mtime" => 1.day.ago.iso8601 }, | ||
"foo/assets/vendor-def456.css" => { "mtime" => 2.days.ago.iso8601 }, | ||
}, | ||
) | ||
assets = build_assets( | ||
app_name: "foo", | ||
ember_app_name: "bar", | ||
manifest: manifest, | ||
) | ||
|
||
stylesheets = assets.stylesheets | ||
|
||
expect(stylesheets).to match_array([ | ||
"foo/assets/bar-abc123.css", | ||
"foo/assets/vendor-abc123.css", | ||
]) | ||
end | ||
|
||
context "when the manifest is empty" do | ||
it "falls back to the default assets" do | ||
assets = build_assets( | ||
manifest: build_empty_manifest, | ||
app_name: "foo", | ||
ember_app_name: "bar", | ||
) | ||
|
||
stylesheets = assets.stylesheets | ||
|
||
expect(stylesheets).to match_array([ | ||
"foo/assets/vendor", | ||
"foo/assets/bar", | ||
]) | ||
end | ||
end | ||
end | ||
end | ||
|
||
def build_assets(manifest: build_manifest, **options) | ||
EmberCli::Assets.new(options.merge(manifest: manifest)) | ||
end | ||
|
||
def build_manifest(files: {}) | ||
double(files: files) | ||
end | ||
|
||
def build_empty_manifest | ||
build_manifest | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
require "ember_cli/manifest" | ||
|
||
describe EmberCli::Manifest do | ||
describe "#merge_into!" do | ||
it "merges the manifest into the provided manifest" do | ||
manifest_file = create_json_file( | ||
"assets" => { | ||
"asset" => "path/to/asset.js", | ||
}, | ||
"files" => { | ||
"file" => "path/to/file.js", | ||
}, | ||
) | ||
manifest = EmberCli::Manifest.new("test", manifest_file.path) | ||
rails_manifest = OpenStruct.new(assets: {}, files: {}) | ||
|
||
manifest.merge_into!(rails_manifest) | ||
|
||
expect(rails_manifest.assets).to eq("asset" => "path/to/asset.js") | ||
expect(rails_manifest.files).to eq("file" => "path/to/file.js") | ||
end | ||
end | ||
|
||
def create_json_file(json) | ||
tempfile = Tempfile.new("json") | ||
tempfile.write(JSON.dump(json)) | ||
tempfile.close | ||
|
||
tempfile | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.