ember-cli-rails-assets 0.8.1 → 0.9.1

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: f38df79bd5263f5e6d124fd2ddeff3e0f724a6a967e1c165d4799a946f557c51
4
- data.tar.gz: 36afc4bff115269fa3cb626e5d52aed8f8a8cddfb09b13b8e621602a9b72ec3c
3
+ metadata.gz: '09d4079f97e405aafa40b0b3663fdeaed6ae362e55e84839563856169225aed7'
4
+ data.tar.gz: 2f329df8fb400ff28c6ddb7105589a7a76ff7054606e9007d6955d2090848e61
5
5
  SHA512:
6
- metadata.gz: '084c574b46d27829d332586bd8e3766ab3e4a1205a373d1c2d1b2a9e2f89e1924393b22b1ed418021e3c30e047ae92357b06c72d89bb5e6b796ec51f7693589c'
7
- data.tar.gz: 521eee2cc26d824b6e6862b428213cfdd2b1f8bb2827c366d7a0b59688d066818d991a0b9e27065771d1c118a868247a47cf1b301da6cafad1f325c4b0679261
6
+ metadata.gz: 97f6951bc89beb25aa963e554679415758069fdecb817753c8ce2148137669d159e8bc9384e22cd778ea2aebe371d3b57c9b530188700b442a27f7d8fb07586d
7
+ data.tar.gz: 574368c875b462fd04a08613e8fece7ed186e4b95a8918e3429e37251c3385d3993795feb2c58d34c2777058e0b67323151f0ed73a2de3494512fa686ed065fd
data/README.md CHANGED
@@ -67,6 +67,12 @@ extracted from the generated `index.html`:
67
67
  <%= include_ember_script_tags :frontend %>
68
68
  ```
69
69
 
70
+ When ember-cli-rails serves the application from Vite's development server in
71
+ `development`, `include_ember_script_tags` reads `index.html` from the server
72
+ instead of a build directory, and emits the startup tags with absolute URLs
73
+ pointing at the server. Nothing is built, so changes to the application are
74
+ picked up by reloading the page.
75
+
70
76
  `include_ember_stylesheet_tags` only supports classic applications, and raises
71
77
  an error when called for a Vite-based application.
72
78
 
@@ -130,12 +136,13 @@ To render the asset elements without using a `<base>` element, pass the URL or
130
136
  path to prepend:
131
137
 
132
138
  ```erb
133
- <-- assuming Ember's assets are mounted to `"/"` -->
139
+ <!-- assuming Ember's assets are mounted to `"/"` -->
134
140
  <%= include_ember_script_tags :frontend, prepend: "/" %>
135
141
  <%= include_ember_stylesheet_tags :frontend, prepend: "/" %>
136
142
 
137
- <%= include_ember_script_tags :admin_panel, prepend: "/" %>
138
- <%= include_ember_stylesheet_tags :admin_panel, prepend: "/" %>
143
+ <!-- assuming Ember's assets are mounted to `"/admin_panel/"` -->
144
+ <%= include_ember_script_tags :admin_panel, prepend: "/admin_panel/" %>
145
+ <%= include_ember_stylesheet_tags :admin_panel, prepend: "/admin_panel/" %>
139
146
  ```
140
147
 
141
148
  ## EmberCLI support
data/Rakefile CHANGED
File without changes
@@ -1,18 +1,23 @@
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: "")
7
8
  app = EmberCli[name]
8
9
  app.build
9
10
 
10
- paths = EmberCli::Assets::Paths.new(app)
11
-
12
- if paths.vite?
13
- vite_ember_script_tags(paths, prepend)
11
+ if dev_server?(app)
12
+ dev_server_ember_script_tags(app)
14
13
  else
15
- classic_ember_script_tags(app, prepend)
14
+ paths = EmberCli::Assets::Paths.new(app)
15
+
16
+ if paths.vite?
17
+ vite_ember_script_tags(paths, prepend)
18
+ else
19
+ classic_ember_script_tags(app, prepend)
20
+ end
16
21
  end
17
22
  end
18
23
 
@@ -22,7 +27,7 @@ module EmberCliRailsAssetsHelper
22
27
 
23
28
  paths = EmberCli::Assets::Paths.new(app)
24
29
 
25
- if paths.vite?
30
+ if dev_server?(app) || paths.vite?
26
31
  raise EmberCli::Assets::NotSupportedError, <<~MSG
27
32
  `include_ember_stylesheet_tags` does not support Vite-based
28
33
  applications (`ember-cli >= 6.8`).
@@ -35,13 +40,29 @@ module EmberCliRailsAssetsHelper
35
40
  assets = EmberCli::Assets::Lookup.new(app)
36
41
 
37
42
  assets.stylesheet_assets.
38
- map { |src| [prepend, src].join }.
43
+ map { |src| mount_asset(src, prepend) }.
39
44
  map { |src| %{<link rel="stylesheet" href="#{src}">}.html_safe }.
40
45
  inject(&:+)
41
46
  end
42
47
 
43
48
  private
44
49
 
50
+ # Whether ember-cli-rails serves the application from Vite's development
51
+ # server. Older ember-cli-rails releases have no development server, so
52
+ # feature-detect the reader.
53
+ def dev_server?(app)
54
+ app.respond_to?(:dev_server?) && app.dev_server?
55
+ end
56
+
57
+ # The application is served by Vite's development server, so read
58
+ # `index.html` from the server instead of a build directory, and rewrite
59
+ # its root-relative URLs to absolute URLs on the server — the same way
60
+ # ember-cli-rails serves the document itself. `app.build` has already
61
+ # booted the server.
62
+ def dev_server_ember_script_tags(app)
63
+ vite_startup_tags(app.dev_server.index_html, prefix: app.dev_server.origin)
64
+ end
65
+
45
66
  # Classic builds ship a fixed set of scripts (vendor and app) resolved
46
67
  # through the asset map, so emit a plain script tag per JavaScript asset
47
68
  # with `prepend` joined onto each path.
@@ -49,24 +70,43 @@ module EmberCliRailsAssetsHelper
49
70
  assets = EmberCli::Assets::Lookup.new(app)
50
71
 
51
72
  assets.javascript_assets.
52
- map { |src| [prepend, src].join }.
73
+ map { |src| mount_asset(src, prepend) }.
53
74
  map { |src| %{<script src="#{src}"></script>}.html_safe }.
54
75
  inject(&:+)
55
76
  end
56
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
+
57
88
  # Vite builds boot from ES modules declared in dist/index.html, so extract
58
89
  # the tags required for startup (including the config meta tag and
59
90
  # stylesheets) and remap root-absolute paths onto the mount point.
60
91
  def vite_ember_script_tags(paths, prepend)
61
- document = Nokogiri::HTML5(paths.index_html.read)
62
- prefix = prepend.to_s.chomp("/")
92
+ vite_startup_tags(paths.index_html.read, prefix: prepend.to_s.chomp("/"))
93
+ end
94
+
95
+ # Extracts the tags a Vite-based application needs to boot from an
96
+ # `index.html` document, with `prefix` joined onto every root-relative
97
+ # URL. Protocol-relative URLs (`//`) are left alone.
98
+ def vite_startup_tags(html, prefix:)
99
+ document = Nokogiri::HTML5(html)
63
100
 
64
101
  tags = document.css(
65
102
  'meta[name$="/config/environment"], link[rel="stylesheet"], link[rel="modulepreload"], script'
66
103
  ).map do |tag|
67
104
  %w(href src).each do |attribute|
68
105
  value = tag[attribute]
69
- tag[attribute] = "#{prefix}#{value}" if value&.start_with?("/")
106
+
107
+ if value&.start_with?("/") && !value.start_with?("//")
108
+ tag[attribute] = "#{prefix}#{value}"
109
+ end
70
110
  end
71
111
  tag.to_html.html_safe
72
112
  end
File without changes
@@ -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,25 +14,35 @@ 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
 
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(/#{Regexp.escape(File.basename(url))}\z/)
43
+ end
44
+ end
45
+
35
46
  def asset_matching(regex)
36
47
  matching_asset = files.detect { |asset| asset =~ regex }
37
48
 
File without changes
File without changes
File without changes
File without changes
File without changes
@@ -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.8.1"
3
+ VERSION = "0.9.1"
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.8.1
4
+ version: 0.9.1
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: