ember-cli-rails-assets 0.8.1 → 0.9.0

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: a14199abf9cfa5f0faf079ea10fbc134cf701d48d016da69f7d43ed64256fbdf
4
+ data.tar.gz: b5d351892f33578fcae89ccdb77966393b2682f634dfe5f3d0ee384e42e095b3
5
5
  SHA512:
6
- metadata.gz: '084c574b46d27829d332586bd8e3766ab3e4a1205a373d1c2d1b2a9e2f89e1924393b22b1ed418021e3c30e047ae92357b06c72d89bb5e6b796ec51f7693589c'
7
- data.tar.gz: 521eee2cc26d824b6e6862b428213cfdd2b1f8bb2827c366d7a0b59688d066818d991a0b9e27065771d1c118a868247a47cf1b301da6cafad1f325c4b0679261
6
+ metadata.gz: 52c74aa8f6993613dd16c7e762260ce2818654d043ac80b1627392ac2fbd3cdcbcc3231a9b67125f107b1de4b0aab97f52aaa7f6f9f9fa34025a7fb92a160244
7
+ data.tar.gz: 4c639e39672b4cbc07281153c20e3a27d68f233e20a0abd3ab2b03ae130c71c807db7a91acbbcb05121d130fb8f4484e9baedfa1d386da0cf5eca171013c70d7
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
@@ -7,12 +7,16 @@ module EmberCliRailsAssetsHelper
7
7
  app = EmberCli[name]
8
8
  app.build
9
9
 
10
- paths = EmberCli::Assets::Paths.new(app)
11
-
12
- if paths.vite?
13
- vite_ember_script_tags(paths, prepend)
10
+ if dev_server?(app)
11
+ dev_server_ember_script_tags(app)
14
12
  else
15
- classic_ember_script_tags(app, prepend)
13
+ paths = EmberCli::Assets::Paths.new(app)
14
+
15
+ if paths.vite?
16
+ vite_ember_script_tags(paths, prepend)
17
+ else
18
+ classic_ember_script_tags(app, prepend)
19
+ end
16
20
  end
17
21
  end
18
22
 
@@ -22,7 +26,7 @@ module EmberCliRailsAssetsHelper
22
26
 
23
27
  paths = EmberCli::Assets::Paths.new(app)
24
28
 
25
- if paths.vite?
29
+ if dev_server?(app) || paths.vite?
26
30
  raise EmberCli::Assets::NotSupportedError, <<~MSG
27
31
  `include_ember_stylesheet_tags` does not support Vite-based
28
32
  applications (`ember-cli >= 6.8`).
@@ -42,6 +46,22 @@ module EmberCliRailsAssetsHelper
42
46
 
43
47
  private
44
48
 
49
+ # Whether ember-cli-rails serves the application from Vite's development
50
+ # server. Older ember-cli-rails releases have no development server, so
51
+ # feature-detect the reader.
52
+ def dev_server?(app)
53
+ app.respond_to?(:dev_server?) && app.dev_server?
54
+ end
55
+
56
+ # The application is served by Vite's development server, so read
57
+ # `index.html` from the server instead of a build directory, and rewrite
58
+ # its root-relative URLs to absolute URLs on the server — the same way
59
+ # ember-cli-rails serves the document itself. `app.build` has already
60
+ # booted the server.
61
+ def dev_server_ember_script_tags(app)
62
+ vite_startup_tags(app.dev_server.index_html, prefix: app.dev_server.origin)
63
+ end
64
+
45
65
  # Classic builds ship a fixed set of scripts (vendor and app) resolved
46
66
  # through the asset map, so emit a plain script tag per JavaScript asset
47
67
  # with `prepend` joined onto each path.
@@ -58,15 +78,24 @@ module EmberCliRailsAssetsHelper
58
78
  # the tags required for startup (including the config meta tag and
59
79
  # stylesheets) and remap root-absolute paths onto the mount point.
60
80
  def vite_ember_script_tags(paths, prepend)
61
- document = Nokogiri::HTML5(paths.index_html.read)
62
- prefix = prepend.to_s.chomp("/")
81
+ vite_startup_tags(paths.index_html.read, prefix: prepend.to_s.chomp("/"))
82
+ end
83
+
84
+ # Extracts the tags a Vite-based application needs to boot from an
85
+ # `index.html` document, with `prefix` joined onto every root-relative
86
+ # URL. Protocol-relative URLs (`//`) are left alone.
87
+ def vite_startup_tags(html, prefix:)
88
+ document = Nokogiri::HTML5(html)
63
89
 
64
90
  tags = document.css(
65
91
  'meta[name$="/config/environment"], link[rel="stylesheet"], link[rel="modulepreload"], script'
66
92
  ).map do |tag|
67
93
  %w(href src).each do |attribute|
68
94
  value = tag[attribute]
69
- tag[attribute] = "#{prefix}#{value}" if value&.start_with?("/")
95
+
96
+ if value&.start_with?("/") && !value.start_with?("//")
97
+ tag[attribute] = "#{prefix}#{value}"
98
+ end
70
99
  end
71
100
  tag.to_html.html_safe
72
101
  end
@@ -1,5 +1,5 @@
1
1
  module EmberCli
2
2
  module Assets
3
- VERSION = "0.8.1"
3
+ VERSION = "0.9.0"
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.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sean Doyle