turbo-hmr 0.0.3 → 0.1.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: b7d0d145ec7f949979c3f8feed47425454e251c0e879ed58fb26f2a5751c50dc
4
- data.tar.gz: c054cb758e1c26089051d26626b291d17ec5104069a01527b26416746b615116
3
+ metadata.gz: 64e362e4162185dd9fc3496a6ca0f049ab720c8caa8a251c45c390411b794146
4
+ data.tar.gz: fea261349db01d0cdf75d934874bafb986864b7467ea86fbfeda2e8d139688a7
5
5
  SHA512:
6
- metadata.gz: 1a33e27bc25960dc80f2fcc4fa0e762eb9fbf5279c0c2903a5de43345a0f3222490fc2b92d22405f7bd347d2a27b0fd83c10f761851808355458790e464ab7c8
7
- data.tar.gz: f3d085c2574dbd7f18f029ecf03ffae2ce1e32e3fb36174f3eff4af5732248aa59b17b31cc280b9feb4ef4d21d36a8a7ae0d7bf131027257e8d1a070d91683be
6
+ metadata.gz: 7aab32066453454a81fdb6e8e108e2d5f407605a116323da036e28c1e6c9f968335fed9a0a110499eb129855827486f92a6fe39294cb63f5b935dde3ddf51db0
7
+ data.tar.gz: 4ab16041aaf0ca785439919c63feee23a9d07f44092692c15364c8499bb38e2b5c3f3ba8006a3cbae0b26e41f92b2d4775862b6c8dec8f1f89e06378eb87e835
@@ -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,8 @@ 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
+ if (!html) return
21
+ this.pendingImportmap = this.extractImportmapFromHtml(html)
23
22
  }
24
23
 
25
24
  handleBeforeRender(event) {
@@ -63,12 +62,23 @@ class TurboHmr {
63
62
  }
64
63
  }
65
64
 
66
- extractImportmap(doc) {
65
+ extractImportmapFromDoc(doc) {
67
66
  const importmapScript = doc.querySelector('script[type="importmap"]')
68
67
  if (!importmapScript) return null
68
+ return this.parseImportmap(importmapScript.textContent)
69
+ }
70
+
71
+ extractImportmapFromHtml(html) {
72
+ // Avoid DOMParser: parsing HTML with inline style="..." attributes triggers
73
+ // style-src CSP violations in Firefox. Extract the importmap <script> content via regex.
74
+ const match = html.match(/<script[^>]*\btype\s*=\s*["']importmap["'][^>]*>([\s\S]*?)<\/script>/i)
75
+ if (!match) return null
76
+ return this.parseImportmap(match[1])
77
+ }
69
78
 
79
+ parseImportmap(text) {
70
80
  try {
71
- return JSON.parse(importmapScript.textContent)
81
+ return JSON.parse(text)
72
82
  } catch (e) {
73
83
  console.error("turbo-hmr: Failed to parse importmap", e)
74
84
  return null
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Turbo
4
4
  module Hmr
5
- VERSION = "0.0.3"
5
+ VERSION = "0.1.1"
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.3
4
+ version: 0.1.1
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: []