turbo-hmr 0.0.2 → 0.1.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: 19b1d88e6a573c42db7da38e73953a295aad477aebcf2476be112260cb7f38c5
4
- data.tar.gz: 49137426f94f8da10d9aa630c2a051c59b47a8d4ea306ac5d8be4540172be5f3
3
+ metadata.gz: d78f1140f5ff9dacba63f4e12179411e37d791be3f77d06e9fed0541e5aa6c56
4
+ data.tar.gz: accaeef06f24858b22805398c543818caae5630cf4116ec307ee9f24b5a027b7
5
5
  SHA512:
6
- metadata.gz: 9782bd09faa3a1e2d878fbc5c93b53b1a5f64a2991cee2a29e4b8117d4fa2f8eee8dc2bc701c7ef4a804e374345f4f60b56ea49d8dbd70a0fd0337ef0f2845f9
7
- data.tar.gz: 9efc011f8d9c1620be37e2e6a5dd12842193f0adca30d39f1ef6aa10035c8d21d98e7ef63f928c6d701fca613372c3053af8f3f2852423fd5920c9fa08413066
6
+ metadata.gz: 6ef0326372951be494683776e414502f9bb922fa7c67832bc5fd422cdd5caa793c6f3c723d51845e9468a51833145bd45ec9c07776867c74adaaa98ff4a99849
7
+ data.tar.gz: e635500967145ec77204b73eb575326c657e0a6e7558162befe8d75f638026b99c3bb8770abb3e44c0a96da602b4b50955764e2db4b946452c12c970a469a659
@@ -8,7 +8,7 @@ class TurboHmr {
8
8
 
9
9
  start(application) {
10
10
  this.application = application
11
- this.currentImportmap = this.extractImportmap(document)
11
+ this.currentImportmap = this.extractImportmapFromDoc(document)
12
12
 
13
13
  document.addEventListener("turbo:before-fetch-response", this.handleBeforeFetchResponse.bind(this))
14
14
  document.addEventListener("turbo:before-render", this.handleBeforeRender.bind(this))
@@ -17,9 +17,7 @@ class TurboHmr {
17
17
  async handleBeforeFetchResponse(event) {
18
18
  const response = event.detail.fetchResponse
19
19
  const html = await response.responseHTML
20
- const parser = new DOMParser()
21
- const doc = parser.parseFromString(html, "text/html")
22
- this.pendingImportmap = this.extractImportmap(doc)
20
+ this.pendingImportmap = this.extractImportmapFromHtml(html)
23
21
  }
24
22
 
25
23
  handleBeforeRender(event) {
@@ -63,12 +61,23 @@ class TurboHmr {
63
61
  }
64
62
  }
65
63
 
66
- extractImportmap(doc) {
64
+ extractImportmapFromDoc(doc) {
67
65
  const importmapScript = doc.querySelector('script[type="importmap"]')
68
66
  if (!importmapScript) return null
67
+ return this.parseImportmap(importmapScript.textContent)
68
+ }
69
+
70
+ extractImportmapFromHtml(html) {
71
+ // Avoid DOMParser: parsing HTML with inline style="..." attributes triggers
72
+ // style-src CSP violations in Firefox. Extract the importmap <script> content via regex.
73
+ const match = html.match(/<script[^>]*\btype\s*=\s*["']importmap["'][^>]*>([\s\S]*?)<\/script>/i)
74
+ if (!match) return null
75
+ return this.parseImportmap(match[1])
76
+ }
69
77
 
78
+ parseImportmap(text) {
70
79
  try {
71
- return JSON.parse(importmapScript.textContent)
80
+ return JSON.parse(text)
72
81
  } catch (e) {
73
82
  console.error("turbo-hmr: Failed to parse importmap", e)
74
83
  return null
@@ -11,8 +11,8 @@ module Turbo
11
11
  end
12
12
 
13
13
  initializer "turbo_hmr.importmap_helper" do
14
- ActiveSupport.on_load(:action_controller_base) do
15
- helper Turbo::Hmr::ImportmapHelper
14
+ ActiveSupport.on_load(:action_controller) do
15
+ ActionController::Base.helper Turbo::Hmr::ImportmapHelper
16
16
  end
17
17
  end
18
18
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Turbo
4
4
  module Hmr
5
- VERSION = "0.0.2"
5
+ VERSION = "0.1.0"
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: turbo-hmr
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Micah Geisel
@@ -91,7 +91,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
91
91
  - !ruby/object:Gem::Version
92
92
  version: '0'
93
93
  requirements: []
94
- rubygems_version: 3.6.9
94
+ rubygems_version: 4.0.6
95
95
  specification_version: 4
96
96
  summary: Hot Module Replacement for Turbo
97
97
  test_files: []