bun_bun_bundle 0.7.0 → 0.8.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: 1cfd07c4a4982f58446af9f2deb5230a4225e3c733165b37038ba08fbb8bb29a
4
- data.tar.gz: 6dcc33af22e42b4a6cd260185f9bae495657ed19307a8f57c3a141d58c411376
3
+ metadata.gz: 17cca9ef2aafb44594e5ea2e2abfcd6dfa4a35b3af61f636a0a90ee57383d21a
4
+ data.tar.gz: '08ef01b7f75cb09e069e62cd40c477f58ea5c78da6e16761066e70a4970f8f7f'
5
5
  SHA512:
6
- metadata.gz: 7bea055db0ae0841a0d29fed3e785d71b9b956edcf6c37d8c5b03c340e0d29d324300439c817b29131a4978ab021b4f3686701537c7f8b371fbf578bf95bf765
7
- data.tar.gz: 1fbc83c7ac9e3918e583a2ee23239006e6507f9a047234068b0e96d9995654f123291ff5359ce2b55ee7344e1ececc108c5b456e13658ddf8df4f1e1996c8a62
6
+ metadata.gz: d515a1e655140edc0cfe616ff3aafaada50844eea8941700d406f9a5f560406a0da79302653ee665e0a92051175b364e845fdabfec105ceba13cbf03b30f3029
7
+ data.tar.gz: cb9386659db82669c47ec2a82982137d3d5e0b44a95b5c13aee0afcdee7b2a889def2b1dc63f1918fa665b9dac83e9cafd912c2d7d23b37c7748c9b02e0938f4
data/README.md CHANGED
@@ -9,7 +9,7 @@ Works with Rails, Hanami, or any Rack app.
9
9
 
10
10
  - **Lightning fast.** Bun's native bundler builds assets in milliseconds.
11
11
  - **CSS hot-reloading.** Instant changes without a full page refresh.
12
- - **Asset fingerprinting.** Content-based hashing in every environment.
12
+ - **Asset fingerprinting.** Fast, content-based file hashing.
13
13
  - **No surprises in production.** Dev and prod go through the same pipeline.
14
14
  - **Extensible.** Plugins are simple, tiny JavaScript files.
15
15
  - **One dependency: Bun.** Everything is included, no other dev dependencies.
@@ -139,13 +139,13 @@ BunBunBundle.asset_host = 'https://cdn.example.com'
139
139
  All helpers are prefixed with `bun_` to avoid conflicts with existing framework
140
140
  helpers:
141
141
 
142
- | Helper | Description |
143
- | -------------------------------- | ------------------------------------------------------- |
144
- | `bun_asset('images/logo.png')` | Returns the fingerprinted asset path |
145
- | `bun_js_tag('js/app.js')` | Generates a `<script>` tag |
146
- | `bun_css_tag('css/app.css')` | Generates a `<link>` tag (with `data-bun-entry` in dev) |
147
- | `bun_img_tag('images/logo.png')` | Generates an `<img>` tag |
148
- | `bun_reload_tag` | Live reload script (only renders in development) |
142
+ | Helper | Description |
143
+ | -------------------------------- | ------------------------------------------------ |
144
+ | `bun_asset('images/logo.png')` | Returns the fingerprinted asset path |
145
+ | `bun_js_tag('js/app.js')` | Generates a `<script>` tag |
146
+ | `bun_css_tag('css/app.css')` | Generates a `<link>` tag |
147
+ | `bun_img_tag('images/logo.png')` | Generates an `<img>` tag |
148
+ | `bun_reload_tag` | Live reload script (only renders in development) |
149
149
 
150
150
  All tag helpers accept additional HTML attributes:
151
151
 
@@ -176,7 +176,7 @@ bun_bun_bundle dev
176
176
  # Build assets
177
177
  bun_bun_bundle build
178
178
 
179
- # Build with minification
179
+ # Build with fingerprinting and minification
180
180
  bun_bun_bundle build --prod
181
181
 
182
182
  # Development with verbose WebSocket logging
@@ -80,6 +80,8 @@ export default {
80
80
  },
81
81
 
82
82
  fingerprint(name, ext, content) {
83
+ if (!this.prod) return `${name}${ext}`
84
+
83
85
  const hash = Bun.hash(content).toString(16).slice(0, 8)
84
86
  return `${name}-${hash}${ext}`
85
87
  },
@@ -209,15 +211,7 @@ export default {
209
211
 
210
212
  reload(type = 'full') {
211
213
  setTimeout(() => {
212
- const payload = {type}
213
-
214
- if (type === 'css') {
215
- payload.paths = Object.values(this.manifest)
216
- .filter(p => p.endsWith('.css'))
217
- .map(p => `${this.config.publicPath}/${p}`)
218
- }
219
-
220
- const message = JSON.stringify(payload)
214
+ const message = JSON.stringify({type})
221
215
  for (const client of this.wsClients) {
222
216
  try {
223
217
  client.send(message)
@@ -44,9 +44,9 @@ module BunBunBundle
44
44
  # # => '<link href="/assets/css/app.css" type="text/css" rel="stylesheet">'
45
45
  #
46
46
  def bun_css_tag(source, **options)
47
- href = bun_asset(source)
48
- attrs = { type: 'text/css', rel: 'stylesheet' }.merge(options).merge(href: href)
49
- attrs[:data_bun_entry] = source if BunBunBundle.development?
47
+ attrs = { type: 'text/css', rel: 'stylesheet' }
48
+ .merge(options)
49
+ .merge(href: bun_href_with_timestamp(bun_asset(source)))
50
50
  bun_safe(%(<link #{bun_html_attrs(attrs)}>))
51
51
  end
52
52
 
@@ -85,5 +85,12 @@ module BunBunBundle
85
85
  def bun_escape_attr(value)
86
86
  value.to_s.gsub('&', '&amp;').gsub('"', '&quot;').gsub('<', '&lt;').gsub('>', '&gt;')
87
87
  end
88
+
89
+ def bun_href_with_timestamp(href)
90
+ return href unless href.start_with?(BunBunBundle.config.public_path)
91
+ return href if href.match?(/-[0-9a-f]{8}\.css$/)
92
+
93
+ "#{href}#{href.include?('?') ? '&' : '?'}bust=#{(Time.now.to_f * 1000).to_i}"
94
+ end
88
95
  end
89
96
  end
@@ -44,6 +44,10 @@ module BunBunBundle
44
44
  entries.key?(key)
45
45
  end
46
46
 
47
+ def css_entry_points
48
+ entries.keys.select { |k| k.end_with?('.css') }
49
+ end
50
+
47
51
  private
48
52
 
49
53
  def find_similar(key, tolerance: 4)
@@ -22,10 +22,14 @@ module BunBunBundle
22
22
  return '' unless BunBunBundle.development?
23
23
 
24
24
  config = BunBunBundle.config
25
+ css_paths = BunBunBundle.manifest.css_entry_points.map do |key|
26
+ "#{config.public_path}/#{key}"
27
+ end
25
28
 
26
29
  html = <<~HTML
27
30
  <script>
28
31
  (() => {
32
+ const cssPaths = #{css_paths.to_json};
29
33
  const ws = new WebSocket('#{config.dev_server.ws_url}')
30
34
  let connected = false
31
35
 
@@ -33,10 +37,13 @@ module BunBunBundle
33
37
  const data = JSON.parse(event.data)
34
38
 
35
39
  if (data.type === 'css') {
36
- document.querySelectorAll('link[data-bun-entry]').forEach(link => {
37
- const entry = link.dataset.bunEntry
38
- const path = data.paths.find(p => p.replace(/-[0-9a-f]{8}\\.css$/, '') === entry)
39
- if (path) link.href = path
40
+ document.querySelectorAll('link[rel="stylesheet"]').forEach(link => {
41
+ const linkPath = new URL(link.href).pathname.split('?')[0]
42
+ if (cssPaths.some(p => linkPath.startsWith(p))) {
43
+ const url = new URL(link.href)
44
+ url.searchParams.set('bust', Date.now())
45
+ link.href = url.toString()
46
+ }
40
47
  })
41
48
  console.log('\\u25b8 CSS reloaded')
42
49
  } else if (data.type === 'error') {
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module BunBunBundle
4
- VERSION = '0.7.0'
4
+ VERSION = '0.8.0'
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bun_bun_bundle
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.0
4
+ version: 0.8.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Wout Fierens