ember-cli-rails-assets 0.9.0 → 0.9.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a14199abf9cfa5f0faf079ea10fbc134cf701d48d016da69f7d43ed64256fbdf
4
- data.tar.gz: b5d351892f33578fcae89ccdb77966393b2682f634dfe5f3d0ee384e42e095b3
3
+ metadata.gz: b93721604a8a9ba4f8f875767c6d95a84fd44cb973a8cd34713f9d1c9148e917
4
+ data.tar.gz: 7d262c1513d85634eb62d9f5eda6326be49ebb1aa9d48492d194852afe488884
5
5
  SHA512:
6
- metadata.gz: 52c74aa8f6993613dd16c7e762260ce2818654d043ac80b1627392ac2fbd3cdcbcc3231a9b67125f107b1de4b0aab97f52aaa7f6f9f9fa34025a7fb92a160244
7
- data.tar.gz: 4c639e39672b4cbc07281153c20e3a27d68f233e20a0abd3ab2b03ae130c71c807db7a91acbbcb05121d130fb8f4484e9baedfa1d386da0cf5eca171013c70d7
6
+ metadata.gz: 636d49bed887eae52fb1d74078fd195b5e9d861235004c2225c0a23b940b5ccdfdcc3e6933f3ceef2e57c501caaadd68d30b7c644d22f2dced62b709ee3b3999
7
+ data.tar.gz: f62f364b4ee98dca076cd7dd6ff11b35687e658d85df97781c42f15f1e54610e5e946afdf5bfb36d0aa6dd248a1c69669562fc52b10a4810b87156a3b3d1cbc8
@@ -1,6 +1,7 @@
1
1
  require "ember_cli/assets/errors"
2
2
  require "ember_cli/assets/lookup"
3
3
  require "ember_cli/assets/paths"
4
+ require "ember_cli/assets/url"
4
5
 
5
6
  module EmberCliRailsAssetsHelper
6
7
  def include_ember_script_tags(name, prepend: "")
@@ -39,7 +40,7 @@ module EmberCliRailsAssetsHelper
39
40
  assets = EmberCli::Assets::Lookup.new(app)
40
41
 
41
42
  assets.stylesheet_assets.
42
- map { |src| [prepend, src].join }.
43
+ map { |src| mount_asset(src, prepend) }.
43
44
  map { |src| %{<link rel="stylesheet" href="#{src}">}.html_safe }.
44
45
  inject(&:+)
45
46
  end
@@ -69,11 +70,21 @@ module EmberCliRailsAssetsHelper
69
70
  assets = EmberCli::Assets::Lookup.new(app)
70
71
 
71
72
  assets.javascript_assets.
72
- map { |src| [prepend, src].join }.
73
+ map { |src| mount_asset(src, prepend) }.
73
74
  map { |src| %{<script src="#{src}"></script>}.html_safe }.
74
75
  inject(&:+)
75
76
  end
76
77
 
78
+ # `prepend` mounts the build's assets onto a path (or another host), so it only applies to the URLs that point into the build.
79
+ # Assets hosted elsewhere (a CDN, a font service) are already absolute.
80
+ def mount_asset(src, prepend)
81
+ if EmberCli::Assets::Url.remote?(src)
82
+ src
83
+ else
84
+ [prepend, src].join
85
+ end
86
+ end
87
+
77
88
  # Vite builds boot from ES modules declared in dist/index.html, so extract
78
89
  # the tags required for startup (including the config meta tag and
79
90
  # stylesheets) and remap root-absolute paths onto the mount point.
@@ -1,5 +1,6 @@
1
1
  require 'nokogiri'
2
2
  require "ember_cli/assets/errors"
3
+ require "ember_cli/assets/url"
3
4
 
4
5
  module EmberCli
5
6
  module Assets
@@ -13,35 +14,51 @@ module EmberCli
13
14
  def javascripts
14
15
  assert_asset_map!
15
16
 
16
- Nokogiri::HTML(index_html.read).css('script').map {|s|
17
- filename = File.basename(s['src'])
18
- asset_matching(/#{Regexp.escape(filename)}\z/)
19
- }
17
+ document.css('script').filter_map { |script| asset_for(script['src']) }
20
18
  end
21
19
 
22
20
  def stylesheets
23
21
  assert_asset_map!
24
22
 
25
- Nokogiri::HTML(index_html.read).css('link[rel="stylesheet"]').map {|s|
26
- filename = File.basename(s['href'])
27
- asset_matching(/#{Regexp.escape(filename)}\z/)
28
- }
23
+ document.css('link[rel="stylesheet"]').filter_map { |link| asset_for(link['href']) }
29
24
  end
30
25
 
31
26
  private
32
27
 
33
28
  attr_reader :name, :asset_map, :index_html
34
29
 
35
- def asset_matching(regex)
36
- matching_asset = files.detect { |asset| asset =~ regex }
30
+ def document
31
+ @document ||= Nokogiri::HTML(index_html.read)
32
+ end
33
+
34
+ # Assets hosted outside the build (a CDN, a font service) have no entry in the asset map, so their URL is emitted untouched.
35
+ # A tag without a URL (an inline `<script>`) references no asset at all.
36
+ def asset_for(url)
37
+ if url.to_s.empty?
38
+ nil
39
+ elsif Url.remote?(url)
40
+ url
41
+ else
42
+ asset_matching(url)
43
+ end
44
+ end
37
45
 
38
- if matching_asset.to_s.empty?
39
- raise_missing_asset(regex)
46
+ def asset_matching(url)
47
+ matching_asset = path_suffixes(url).find { |suffix| files.include?(suffix) }
48
+
49
+ unless matching_asset
50
+ raise_missing_asset(url)
40
51
  end
41
52
 
42
53
  prepend + matching_asset
43
54
  end
44
55
 
56
+ def path_suffixes(url)
57
+ segments = url.split("/").reject(&:empty?)
58
+
59
+ segments.each_index.map { |index| segments[index..].join("/") }
60
+ end
61
+
45
62
  def prepend
46
63
  asset_map["prepend"].to_s
47
64
  end
@@ -54,8 +71,8 @@ module EmberCli
54
71
  asset_map["assets"] || {}
55
72
  end
56
73
 
57
- def raise_missing_asset(regex)
58
- raise BuildError.new("Failed to find assets matching `#{regex}`")
74
+ def raise_missing_asset(url)
75
+ raise BuildError.new("Failed to find assets matching `#{url}`")
59
76
  end
60
77
 
61
78
  def assert_asset_map!
@@ -1,3 +1,5 @@
1
+ require "pathname"
2
+
1
3
  module EmberCli
2
4
  module Assets
3
5
  class DirectoryAssetMap
@@ -18,7 +20,7 @@ module EmberCli
18
20
 
19
21
  def files_with_data
20
22
  files.reduce({}) do |manifest, file|
21
- name = File.basename(file.path)
23
+ name = file.relative_path_from(directory).to_s
22
24
 
23
25
  manifest[name] = name
24
26
 
@@ -26,9 +28,8 @@ module EmberCli
26
28
  end
27
29
  end
28
30
 
29
-
30
31
  def files
31
- directory.children.map { |path| File.new(path) }
32
+ directory.glob("**/*", File::FNM_DOTMATCH).select(&:file?)
32
33
  end
33
34
  end
34
35
  end
@@ -0,0 +1,14 @@
1
+ module EmberCli
2
+ module Assets
3
+ # URLs in a built `index.html` either point into the build output or somewhere else entirely (a CDN, a font service).
4
+ # Only the former are resolved against the build and mounted onto the Rails application.
5
+ module Url
6
+ # Matches a URL that resolves outside the build: one carrying a scheme (`https://example.com/app.css`, `data:…`), and a protocol-relative one (`//example.com/app.css`).
7
+ REMOTE = %r{\A(?:[a-zA-Z][a-zA-Z0-9+.\-]*:|//)}
8
+
9
+ def self.remote?(url)
10
+ REMOTE.match?(url.to_s)
11
+ end
12
+ end
13
+ end
14
+ end
@@ -1,5 +1,5 @@
1
1
  module EmberCli
2
2
  module Assets
3
- VERSION = "0.9.0"
3
+ VERSION = "0.9.2"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ember-cli-rails-assets
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.0
4
+ version: 0.9.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sean Doyle
@@ -43,6 +43,7 @@ files:
43
43
  - lib/ember_cli/assets/errors.rb
44
44
  - lib/ember_cli/assets/lookup.rb
45
45
  - lib/ember_cli/assets/paths.rb
46
+ - lib/ember_cli/assets/url.rb
46
47
  - lib/ember_cli/assets/version.rb
47
48
  homepage: https://github.com/tricknotes/ember-cli-rails-assets
48
49
  licenses:
@@ -62,7 +63,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
62
63
  - !ruby/object:Gem::Version
63
64
  version: '0'
64
65
  requirements: []
65
- rubygems_version: 4.0.16
66
+ rubygems_version: 4.0.20
66
67
  specification_version: 4
67
68
  summary: Include EmberCLI-generated JavaScript and CSS stylesheet tags in your Rails
68
69
  layouts