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 +4 -4
- data/README.md +14 -2
- data/lib/isolate_assets/assets.rb +1 -1
- data/lib/isolate_assets/engine_extension.rb +26 -11
- data/lib/isolate_assets/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 3ac311515e556ca2199cbfdb72559a19bffed148c18184c1ac364e742b31d2d6
|
|
4
|
+
data.tar.gz: ff9aeb49469e913c4b48c20ee1b82790778410f553606b62f10702a37373623a
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
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/
|
|
44
|
+
Place assets in the standard `app/assets/` directory:
|
|
45
45
|
|
|
46
46
|
```
|
|
47
47
|
my_engine/
|
|
48
48
|
app/
|
|
49
|
-
|
|
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+
|
|
@@ -2,32 +2,47 @@
|
|
|
2
2
|
|
|
3
3
|
module IsolateAssets
|
|
4
4
|
module EngineExtension
|
|
5
|
-
def isolate_assets(assets_subdir: "
|
|
5
|
+
def isolate_assets(assets_subdir: "assets")
|
|
6
6
|
engine_class = self
|
|
7
7
|
|
|
8
|
-
#
|
|
9
|
-
|
|
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
|
-
#
|
|
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
|