propshaft 0.4.3 → 0.6.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 64b20599050fe01e901ae888401cbb42d70afe00a2865a02c72731a397d575d8
4
- data.tar.gz: b8d2e9685fa4c35823988833e88c25af61aa869e557b3f50d84748b16ab0bc24
3
+ metadata.gz: 4fb19ffec4a1b49b3b461cb49ef3f726923bbb3b3a0c4b86e26eaed855208572
4
+ data.tar.gz: 9412606330821085f921519f5a09e96080f49be193a2a675be063dabe8fc1dcd
5
5
  SHA512:
6
- metadata.gz: df7a65b2077f7724a1b123ce3106f1eef57c73599eb6fd87836364d61e1fc4412da84af2ce1f78e3d538905a2e2d4c1ba18f82f0d4bafc899c89a8c63ae25347
7
- data.tar.gz: edf2d528c8e1a925baac66f229e42ce3d817b59e764e676c90e4c87d00b06e7897ba5d649bfc32479b1c20adc5166a3bbb4dd5d13907ed00faae30ba8f1122f3
6
+ metadata.gz: 6fa6e1ab77e53f99a3270567dfda307e798af395e87ff4ecebc47ddadf33839be364d23f24168d7c9050d8844ae9d2ebd119b9bd5bf722ac61c1a98fc7a82312
7
+ data.tar.gz: 4cdb5cc250bef4cfb43cf4d58cbecaa0fe001ac3f3fae51906733ff64b53f06388899f1c26f0aa5e06b4f98b000e294ccb2688e93c15bd4c546500b40c085f19
data/README.md CHANGED
@@ -1,27 +1,26 @@
1
1
  # Propshaft
2
2
 
3
- Propshaft is an asset pipeline library for Rails. It's built for an era where bundling assets to save on HTTP connections is no longer urgent, where JavaScript and CSS are either compiled by dedicated Node.js bundlers or served directly to the browsers, and where increases in bandwidth have made the need for minification less pressing. These factors allow for a dramatically simpler and faster asset pipeline compared to previous options, like Sprockets.
3
+ Propshaft is an asset pipeline library for Rails. It's built for an era where bundling assets to save on HTTP connections is no longer urgent, where JavaScript and CSS are either compiled by dedicated Node.js bundlers or served directly to the browsers, and where increases in bandwidth have made the need for minification less pressing. These factors allow for a dramatically simpler and faster asset pipeline compared to previous options, like [Sprockets](https://github.com/rails/sprockets-rails).
4
4
 
5
5
  So that's what Propshaft doesn't do. Here's what it does provide:
6
6
 
7
7
  1. **Configurable load path**: You can register directories from multiple places in your app and gems, and reference assets from all of these paths as though they were one.
8
8
  1. **Digest stamping**: All assets in the load path will be copied (or compiled) in a precompilation step for production that also stamps all of them with a digest hash, so you can use long-expiry cache headers for better performance. The digested assets can be referred to through their logical path because the processing leaves a manifest file that provides a way to translate.
9
9
  1. **Development server**: There's no need to precompile the assets in development. You can refer to them via the same asset_path helpers and they'll be served by a development server.
10
- 1. **Basic compilers**: Propshaft was explicitly not designed to provide full transpiler capabilities. You can get that better elsewhere. But it does offer a simple input->output compiler setup that by default is used to translate `asset-path` function calls in CSS to `url(digested-asset)` instead.
10
+ 1. **Basic compilers**: Propshaft was explicitly not designed to provide full transpiler capabilities. You can get that better elsewhere. But it does offer a simple input->output compiler setup that by default is used to translate `url(asset)` function calls in CSS to `url(digested-asset)` instead and source mapping comments likewise.
11
11
 
12
12
 
13
13
  ## Installation
14
14
 
15
- With Rails 7+, you can start a new application with propshaft using `rails new myapp -a propshaft` (pending the merge of [rails/rails#43261](https://github.com/rails/rails/pull/43261)).
16
-
15
+ With Rails 7+, you can start a new application with propshaft using `rails new myapp -a propshaft`. For existing applications, check the [upgrade guide](https://github.com/rails/propshaft/blob/main/UPGRADING.md) which contains step-by-step instructions.
17
16
 
18
17
  ## Usage
19
18
 
20
- Propshaft makes all the assets from all the paths it's been configured with through `config.assets.paths` available for serving and will copy all of them into `public/assets` when precompiling. This is unlike Sprockets, which did not copy over assets that hadn't been explicitly included in one of the bundled assets.
19
+ Propshaft makes all the assets from all the paths it's been configured with through `config.assets.paths` available for serving and will copy all of them into `public/assets` when precompiling. This is unlike Sprockets, which did not copy over assets that hadn't been explicitly included in one of the bundled assets.
21
20
 
22
- These assets can be referenced through their logical path using the normal helpers like `asset_path`, `image_tag`, `javascript_include_tag`, and all the other asset helper tags. These logical references are automatically converted into digest-aware paths in production when `assets:precompile` has been run (through a JSON mapping file found in `public/assets/.manifest.json`).
21
+ You can however exempt directories that have been added through the `config.assets.excluded_paths`. This is useful if you're for example using `app/assets/stylesheets` exclusively as a set of inputs to a compiler like Dart Sass for Rails, and you don't want these input files to be part of the load path.
23
22
 
24
- Additionally, Propshaft ships with a CSS function called `asset-path("image.svg")` that'll be compiled into `url("/assets/image-f2e1ec14d6856e1958083094170ca6119c529a73.svg")` when doing `assets:precompile`. This function is applied to all `.css` files.
23
+ These assets can be referenced through their logical path using the normal helpers like `asset_path`, `image_tag`, `javascript_include_tag`, and all the other asset helper tags. These logical references are automatically converted into digest-aware paths in production when `assets:precompile` has been run (through a JSON mapping file found in `public/assets/.manifest.json`).
25
24
 
26
25
 
27
26
  ## Bypassing the digest step
@@ -31,7 +30,7 @@ If you need to put multiple files that refer to each other through Propshaft, li
31
30
 
32
31
  ## Migrating from Sprockets
33
32
 
34
- Propshaft does a lot less than Sprockets, by design, so it might well be a fair bit of work to migrate if it's even desirable. This is particularly true if you rely on Sprockets to provide any form of transpiling, like CoffeeScript or Sass, or if you rely on any gems that do. You'll need to either stop transpiling or use a Node-based transpiler, like those in `jsbundling-rails` and `cssbundling-rails`.
33
+ Propshaft does a lot less than Sprockets, by design, so it might well be a fair bit of work to migrate if it's even desirable. This is particularly true if you rely on Sprockets to provide any form of transpiling, like CoffeeScript or Sass, or if you rely on any gems that do. You'll need to either stop transpiling or use a Node-based transpiler, like those in [`jsbundling-rails`](https://github.com/rails/jsbundling-rails) and [`cssbundling-rails`](https://github.com/rails/cssbundling-rails).
35
34
 
36
35
  On the other hand, if you're already bundling JavaScript and CSS through a Node-based setup, then Propshaft is going to slot in easily. Since you don't need another tool to bundle or transpile. Just to digest and serve.
37
36
 
@@ -15,7 +15,7 @@ class Propshaft::Assembly
15
15
  end
16
16
 
17
17
  def load_path
18
- @load_path ||= Propshaft::LoadPath.new(config.paths)
18
+ @load_path ||= Propshaft::LoadPath.new(config.paths, version: config.version)
19
19
  end
20
20
 
21
21
  def resolver
@@ -44,9 +44,11 @@ class Propshaft::Assembly
44
44
  end
45
45
  end
46
46
 
47
- def reveal
48
- load_path.assets.each do |asset|
49
- Propshaft.logger.info asset.logical_path
47
+ def reveal(path_type = :logical_path)
48
+ path_type = path_type.presence_in(%i[ logical_path path ]) || raise(ArgumentError, "Unknown path_type: #{path_type}")
49
+
50
+ load_path.assets.collect do |asset|
51
+ asset.send(path_type)
50
52
  end
51
53
  end
52
54
 
@@ -2,10 +2,10 @@ require "digest/sha1"
2
2
  require "action_dispatch/http/mime_type"
3
3
 
4
4
  class Propshaft::Asset
5
- attr_reader :path, :logical_path
5
+ attr_reader :path, :logical_path, :version
6
6
 
7
- def initialize(path, logical_path:)
8
- @path, @logical_path = path, Pathname.new(logical_path)
7
+ def initialize(path, logical_path:, version: nil)
8
+ @path, @logical_path, @version = path, Pathname.new(logical_path), version
9
9
  end
10
10
 
11
11
  def content
@@ -21,7 +21,7 @@ class Propshaft::Asset
21
21
  end
22
22
 
23
23
  def digest
24
- @digest ||= Digest::SHA1.hexdigest(content)
24
+ @digest ||= Digest::SHA1.hexdigest("#{content}#{version}")
25
25
  end
26
26
 
27
27
  def digested_path
@@ -42,6 +42,6 @@ class Propshaft::Asset
42
42
 
43
43
  private
44
44
  def already_digested?
45
- logical_path.to_s =~ /-([0-9a-f]{7,128})\.digested/
45
+ logical_path.to_s =~ /-([0-9a-zA-Z]{7,128})\.digested/
46
46
  end
47
47
  end
@@ -1,5 +1,4 @@
1
1
  # frozen_string_literal: true
2
- require "propshaft/errors"
3
2
 
4
3
  class Propshaft::Compilers::CssAssetUrls
5
4
  attr_reader :assembly
@@ -1,5 +1,7 @@
1
- module Propshaft::Helper
2
- def compute_asset_path(path, options = {})
3
- Rails.application.assets.resolver.resolve(path)
1
+ module Propshaft
2
+ module Helper
3
+ def compute_asset_path(path, options = {})
4
+ Rails.application.assets.resolver.resolve(path) || raise(MissingAssetError.new(path))
5
+ end
4
6
  end
5
7
  end
@@ -1,10 +1,11 @@
1
1
  require "propshaft/asset"
2
2
 
3
3
  class Propshaft::LoadPath
4
- attr_reader :paths
4
+ attr_reader :paths, :version
5
5
 
6
- def initialize(paths = [])
7
- @paths = Array(paths).collect { |path| Pathname.new(path) }
6
+ def initialize(paths = [], version: nil)
7
+ @paths = Array(paths).collect { |path| Pathname.new(path) }
8
+ @version = version
8
9
  end
9
10
 
10
11
  def find(asset_name)
@@ -45,9 +46,9 @@ class Propshaft::LoadPath
45
46
  def assets_by_path
46
47
  @cached_assets_by_path ||= Hash.new.tap do |mapped|
47
48
  paths.each do |path|
48
- all_files_from_tree(path).each do |file|
49
+ without_dotfiles(all_files_from_tree(path)).each do |file|
49
50
  logical_path = file.relative_path_from(path)
50
- mapped[logical_path.to_s] ||= Propshaft::Asset.new(file, logical_path: logical_path)
51
+ mapped[logical_path.to_s] ||= Propshaft::Asset.new(file, logical_path: logical_path, version: version)
51
52
  end if path.exist?
52
53
  end
53
54
  end
@@ -57,6 +58,10 @@ class Propshaft::LoadPath
57
58
  path.children.flat_map { |child| child.directory? ? all_files_from_tree(child) : child }
58
59
  end
59
60
 
61
+ def without_dotfiles(files)
62
+ files.reject { |file| file.basename.to_s.starts_with?(".") }
63
+ end
64
+
60
65
  def clear_cache
61
66
  @cached_assets_by_path = nil
62
67
  end
@@ -1,27 +1,31 @@
1
1
  require "rails"
2
- require "rails/railtie"
3
2
  require "active_support/ordered_options"
4
3
 
5
- # FIXME: There's gotta be a better way than this hack?
6
- class Rails::Engine < Rails::Railtie
7
- initializer "propshaft.append_assets_path", group: :all do |app|
8
- app.config.assets.paths.unshift(*paths["vendor/assets"].existent_directories)
9
- app.config.assets.paths.unshift(*paths["lib/assets"].existent_directories)
10
- app.config.assets.paths.unshift(*paths["app/assets"].existent_directories)
11
- end
12
- end
13
-
14
4
  module Propshaft
15
5
  class Railtie < ::Rails::Railtie
16
6
  config.assets = ActiveSupport::OrderedOptions.new
17
- config.assets.paths = []
18
- config.assets.prefix = "/assets"
19
- config.assets.compilers = [
7
+ config.assets.paths = []
8
+ config.assets.excluded_paths = []
9
+ config.assets.version = "1"
10
+ config.assets.prefix = "/assets"
11
+ config.assets.compilers = [
20
12
  [ "text/css", Propshaft::Compilers::CssAssetUrls ],
21
13
  [ "text/css", Propshaft::Compilers::SourceMappingUrls ],
22
14
  [ "text/javascript", Propshaft::Compilers::SourceMappingUrls ]
23
15
  ]
24
16
  config.assets.sweep_cache = Rails.env.development?
17
+ config.assets.server = Rails.env.development? || Rails.env.test?
18
+
19
+ # Register propshaft initializer to copy the assets path in all the Rails Engines.
20
+ # This makes possible for us to keep all `assets` config in this Railtie, but still
21
+ # allow engines to automatically register their own paths.
22
+ Rails::Engine.initializer "propshaft.append_assets_path", group: :all do |app|
23
+ app.config.assets.paths.unshift(*paths["vendor/assets"].existent_directories)
24
+ app.config.assets.paths.unshift(*paths["lib/assets"].existent_directories)
25
+ app.config.assets.paths.unshift(*paths["app/assets"].existent_directories)
26
+
27
+ app.config.assets.paths = app.config.assets.paths.without(Array(app.config.assets.excluded_paths).collect(&:to_s))
28
+ end
25
29
 
26
30
  config.after_initialize do |app|
27
31
  config.assets.output_path ||=
@@ -29,8 +33,10 @@ module Propshaft
29
33
 
30
34
  app.assets = Propshaft::Assembly.new(app.config.assets)
31
35
 
32
- app.routes.prepend do
33
- mount app.assets.server => app.assets.config.prefix
36
+ if config.assets.server
37
+ app.routes.prepend do
38
+ mount app.assets.server => app.assets.config.prefix
39
+ end
34
40
  end
35
41
 
36
42
  ActiveSupport.on_load(:action_view) do
@@ -16,6 +16,13 @@ namespace :assets do
16
16
 
17
17
  desc "Print all the assets available in config.assets.paths"
18
18
  task reveal: :environment do
19
- Rails.application.assets.reveal
19
+ puts Rails.application.assets.reveal(:logical_path).join("\n")
20
+ end
21
+
22
+ namespace :reveal do
23
+ desc "Print the full path of assets available in config.assets.paths"
24
+ task full: :environment do
25
+ puts Rails.application.assets.reveal(:path).join("\n")
26
+ end
20
27
  end
21
28
  end
@@ -9,8 +9,12 @@ module Propshaft::Resolver
9
9
  def resolve(logical_path)
10
10
  if asset = load_path.find(logical_path)
11
11
  File.join prefix, asset.digested_path
12
- else
13
- raise Propshaft::MissingAssetError.new(logical_path)
12
+ end
13
+ end
14
+
15
+ def read(logical_path)
16
+ if asset = load_path.find(logical_path)
17
+ asset.content
14
18
  end
15
19
  end
16
20
  end
@@ -9,8 +9,12 @@ module Propshaft::Resolver
9
9
  def resolve(logical_path)
10
10
  if asset_path = parsed_manifest[logical_path]
11
11
  File.join prefix, asset_path
12
- else
13
- raise Propshaft::MissingAssetError.new(logical_path)
12
+ end
13
+ end
14
+
15
+ def read(logical_path)
16
+ if asset_path = parsed_manifest[logical_path]
17
+ manifest_path.dirname.join(asset_path).read
14
18
  end
15
19
  end
16
20
 
@@ -34,7 +34,7 @@ class Propshaft::Server
34
34
  private
35
35
  def extract_path_and_digest(env)
36
36
  full_path = Rack::Utils.unescape(env["PATH_INFO"].to_s.sub(/^\//, ""))
37
- digest = full_path[/-([0-9a-f]{7,128})\.(?!digested)[^.]+\z/, 1]
37
+ digest = full_path[/-([0-9a-zA-Z]{7,128})\.(?!digested)[^.]+\z/, 1]
38
38
  path = digest ? full_path.sub("-#{digest}", "") : full_path
39
39
 
40
40
  [ path, digest ]
@@ -1,3 +1,3 @@
1
1
  module Propshaft
2
- VERSION = "0.4.3"
2
+ VERSION = "0.6.1"
3
3
  end
data/lib/propshaft.rb CHANGED
@@ -7,5 +7,6 @@ module Propshaft
7
7
  end
8
8
 
9
9
  require "propshaft/assembly"
10
+ require "propshaft/errors"
10
11
  require "propshaft/helper"
11
12
  require "propshaft/railtie"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: propshaft
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.3
4
+ version: 0.6.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - David Heinemeier Hansson
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-12-04 00:00:00.000000000 Z
11
+ date: 2022-02-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: actionpack
@@ -16,42 +16,42 @@ dependencies:
16
16
  requirements:
17
17
  - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: 7.0.0.alpha2
19
+ version: 7.0.0
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
- version: 7.0.0.alpha2
26
+ version: 7.0.0
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: activesupport
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
31
  - - ">="
32
32
  - !ruby/object:Gem::Version
33
- version: 7.0.0.alpha2
33
+ version: 7.0.0
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - ">="
39
39
  - !ruby/object:Gem::Version
40
- version: 7.0.0.alpha2
40
+ version: 7.0.0
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: railties
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
45
  - - ">="
46
46
  - !ruby/object:Gem::Version
47
- version: 7.0.0.alpha2
47
+ version: 7.0.0
48
48
  type: :runtime
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
52
  - - ">="
53
53
  - !ruby/object:Gem::Version
54
- version: 7.0.0.alpha2
54
+ version: 7.0.0
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: rack
57
57
  requirement: !ruby/object:Gem::Requirement
@@ -112,7 +112,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
112
112
  - !ruby/object:Gem::Version
113
113
  version: '0'
114
114
  requirements: []
115
- rubygems_version: 3.2.22
115
+ rubygems_version: 3.2.32
116
116
  signing_key:
117
117
  specification_version: 4
118
118
  summary: Deliver assets for Rails.