isolate_assets 0.1.0 → 0.2.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: b42e51e51aa619c08d02e6d2e7c02aca58aab0cbab94b7d629449940bb2d32df
4
- data.tar.gz: a5872849a66dd7b213a3341e6e905af3403b1d66bf2d00c911e137b8b9411fd5
3
+ metadata.gz: 3ac311515e556ca2199cbfdb72559a19bffed148c18184c1ac364e742b31d2d6
4
+ data.tar.gz: ff9aeb49469e913c4b48c20ee1b82790778410f553606b62f10702a37373623a
5
5
  SHA512:
6
- metadata.gz: 03bce00b9fb6117c4c0e2b202e38a99dd37b89dd60d4f767f94d3a7ca253d4b48dbd5da62d83852f054ded4466151cdc6c4aa65e20ad96564e65e806f7414dea
7
- data.tar.gz: af6aae32f9f05cac171c7e9f761f877ae07afa7419656bf6996424e2c474ab082cb431794e7b339b7e43d9c9744e6341e2183d36b0654638e134088586746113
6
+ metadata.gz: 0f25291c4ff1393aa72c764d52492e9baaba70f61880ce31403292b15796120a3f1e751cc53db01a8a9fe2e3eb4524ef20767a60bd739cbe3dc2dd96e0b915e5
7
+ data.tar.gz: c4aac80bac4d4fc9cf59367afa765024e6f2f3574da76637159875c617a3ddae5b0bbbfd175cd4afad1eed32aae5bb0fa316c53a753045ea738383dcb4401b1c
data/README.md CHANGED
@@ -41,12 +41,12 @@ end
41
41
 
42
42
  ### 2. Add your assets
43
43
 
44
- Place assets in `app/engine_assets/`:
44
+ Place assets in the standard `app/assets/` directory:
45
45
 
46
46
  ```
47
47
  my_engine/
48
48
  app/
49
- engine_assets/
49
+ assets/
50
50
  javascripts/
51
51
  application.js
52
52
  components/
@@ -56,6 +56,8 @@ my_engine/
56
56
  theme.css
57
57
  ```
58
58
 
59
+ IsolateAssets automatically excludes your engine's `app/assets/` directory from the host app's asset pipeline (Sprockets/Propshaft), so your assets won't conflict with or be processed by the host application.
60
+
59
61
  ### 3. Include the helper
60
62
 
61
63
  In your engine's application helper:
@@ -101,6 +103,16 @@ Example output:
101
103
  </script>
102
104
  ```
103
105
 
106
+ ## How it works
107
+
108
+ IsolateAssets automatically excludes your engine's asset directories from `config.assets.paths`, so Sprockets, Propshaft, and dartsass-rails won't process them. Your assets are served exclusively through the isolate_assets controller with their own fingerprinting and caching.
109
+
110
+ This exclusion relies on filtering `config.assets.paths` after engines register their directories. While this works with current Rails asset tools, future versions could change path discovery. For guaranteed isolation, use a non-standard directory:
111
+
112
+ ```ruby
113
+ isolate_assets assets_subdir: "isolated_assets" # uses app/isolated_assets/
114
+ ```
115
+
104
116
  ## Requirements
105
117
 
106
118
  - Ruby 3.2+
@@ -4,7 +4,7 @@ module IsolateAssets
4
4
  class Assets
5
5
  attr_reader :engine, :assets_subdir
6
6
 
7
- def initialize(engine:, assets_subdir: "engine_assets")
7
+ def initialize(engine:, assets_subdir: "assets")
8
8
  @engine = engine
9
9
  @assets_subdir = assets_subdir
10
10
  @fingerprints = {}
@@ -2,32 +2,47 @@
2
2
 
3
3
  module IsolateAssets
4
4
  module EngineExtension
5
- def isolate_assets(assets_subdir: "engine_assets")
5
+ def isolate_assets(assets_subdir: "assets")
6
6
  engine_class = self
7
7
 
8
- # Create a unique controller class for this engine
9
- controller_class = Class.new(IsolateAssets::Controller)
8
+ # Exclude engine assets from host's asset pipeline
9
+ initializer "#{engine_name}.isolate_assets.exclude_from_pipeline", before: :load_config_initializers do |app|
10
+ asset_base = engine_class.root.join("app", assets_subdir)
11
+ app.config.assets.excluded_paths ||= []
12
+ if asset_base.exist?
13
+ asset_base.children.select(&:directory?).each do |subdir|
14
+ app.config.assets.excluded_paths << subdir.to_s
15
+ end
16
+ end
17
+ end
18
+
19
+ # Sprockets doesn't respect excluded_paths, so filter manually
20
+ initializer "#{engine_name}.isolate_assets.filter_asset_paths", after: :load_config_initializers do |app|
21
+ if app.config.assets.excluded_paths.present?
22
+ excluded = app.config.assets.excluded_paths.map(&:to_s)
23
+ app.config.assets.paths = app.config.assets.paths.reject do |path|
24
+ excluded.include?(path.to_s)
25
+ end
26
+ end
27
+ end
10
28
 
11
- # Register an initializer to set up assets when Rails boots
12
29
  initializer "#{engine_name}.isolate_assets", before: :set_routes_reloader do
13
30
  assets = IsolateAssets::Assets.new(engine: engine_class, assets_subdir: assets_subdir)
14
31
 
15
- # Create a unique helper module for this engine with the assets baked in
32
+ # Subclass the controller so that multiple engines using this gem get their own controller
33
+ controller_class = Class.new(IsolateAssets::Controller)
34
+ controller_class.isolated_assets = assets
35
+
36
+ # Hack in the helpers. There's gotta be a better way than this...
16
37
  helper_module = Module.new do
17
38
  define_method(:isolated_assets) { assets }
18
39
  include IsolateAssets::Helper
19
40
  end
20
-
21
- # Wire up the controller
22
- controller_class.isolated_assets = assets
23
-
24
- # Store on the engine's namespace module
25
41
  if engine_class.respond_to?(:railtie_namespace) && engine_class.railtie_namespace
26
42
  engine_class.railtie_namespace.singleton_class.define_method(:isolated_assets) { assets }
27
43
  engine_class.railtie_namespace.singleton_class.define_method(:isolated_assets_helper) { helper_module }
28
44
  end
29
45
 
30
- # Draw routes
31
46
  engine_class.routes.prepend do
32
47
  get "/assets/*file", to: controller_class.action(:show), as: :isolated_asset
33
48
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module IsolateAssets
4
- VERSION = "0.1.0"
4
+ VERSION = "0.2.0"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: isolate_assets
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Micah Geisel