webpack_manifest 0.1.0 → 0.2.0

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
- SHA256:
3
- metadata.gz: eb2ef747217989c47a8781f7e049328a6794f4e069cfe014776dfa4e8670c85e
4
- data.tar.gz: d465f2d48d57638ab060f146a19f5eee0b41e5f2b8f43626522d054f62c4bdb2
2
+ SHA1:
3
+ metadata.gz: 3013414efc1024e5d354d79e7f5fc1a2101550ff
4
+ data.tar.gz: 36a6b4a171d94efd5f04f3e7c5dd86ff3e09ef7b
5
5
  SHA512:
6
- metadata.gz: 2b5a3fc1c9dca56074dc26efd3a8127a3a81775e45de0aa3ede5de04818281c35e9cb4e9425d0b7f398355c99eaf6f0ac0dce587f80aaebf8908ea707a800aaa
7
- data.tar.gz: f224eaa75f9f44118f7f13e9b8f1f658a02b9462bf495bdd92a36d94de1617fe03f1049f09fee5ef32a51562ef4f31f446d2d41660283d85b58810129da8e263
6
+ metadata.gz: 7296e015122881f20c785bc7c764c43b87eda0abae490db1a228bb764d90a5626e2e65927d9ffffeecfd534cfa11ddbaa9e029de95cb76cc6adfd4c239b9e370
7
+ data.tar.gz: 3e93b85fbd321b7367e347268053d4380e51082199ab55ad8ef7926616c8f00878c9cd6d6a8f4d708fc647ceada2b582da71dcaa95f19ad078fc1cb859d69e12
@@ -1,3 +1,13 @@
1
+ # 0.2.0 / 2018-10-18
2
+
3
+ ## Enhancements
4
+
5
+ * Multiple manifest files support
6
+
7
+ ## Breaking changes
8
+
9
+ * Changed internal and public APIs
10
+
1
11
  # 0.1.0 / 2018-10-17
2
12
 
3
13
  Initial release
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # WebpackManifest
2
2
 
3
- WebpackManifest is a gem that integrate ruby with npm's [webpack-manifest-plugin](https://www.npmjs.com/package/webpack-manifest-plugin).
3
+ WebpackManifest is a gem that integrates Rails with npm's [webpack-manifest-plugin](https://www.npmjs.com/package/webpack-manifest-plugin) without [webpacker](https://github.com/rails/webpacker).
4
4
 
5
5
  ## Features
6
6
 
@@ -27,39 +27,127 @@ Or install it yourself as:
27
27
 
28
28
  ### Rails view helpers
29
29
 
30
- * `#asset_bundle_path`
31
- * `#javascript_bundle_tag`
32
- * `#stylesheet_bundle_tag`
33
- * `#image_bundle_tag`
30
+ #### `asset_bundle_path`
31
+
32
+ This is a wrapper of [asset_path](https://api.rubyonrails.org/classes/ActionView/Helpers/AssetUrlHelper.html#method-i-asset_path). You can set any options of `asset_path`.
33
+
34
+ Given entry point name is resolved according to definition of manifest.
35
+
36
+ ```ruby
37
+ asset_bundle_path 'calendar.css'
38
+ # => "/assets/web/pack/calendar-1016838bab065ae1e122.css"
39
+
40
+ asset_bundle_path 'icon/favicon.ico'
41
+ # => "/assets/web/pack/icon/favicon-1016838bab065ae1e122.ico"
42
+ ```
43
+
44
+ #### `javascript_bundle_tag`
45
+
46
+ This is a wrapper of [javascript_include_tag](https://api.rubyonrails.org/classes/ActionView/Helpers/AssetTagHelper.html#method-i-javascript_include_tag). You can set any options of `javascript_include_tag`.
47
+
48
+ Given entry point name is resolved according to definition of manifest.
49
+
50
+ ```ruby
51
+ javascript_bundle_tag 'calendar', 'data-turbolinks-track': 'reload'
52
+ # => <script src="/assets/web/pack/calendar-1016838bab065ae1e314.js"
53
+ # data-turbolinks-track="reload"></script>
54
+
55
+ javascript_bundle_tag 'orders/app'
56
+ # => <script src="/assets/web/pack/orders/app-1016838bab065ae1e314.js"></script>
57
+ ```
58
+
59
+ #### `stylesheet_bundle_tag`
60
+
61
+ This is a wrapper of [stylesheet_link_tag](https://api.rubyonrails.org/classes/ActionView/Helpers/AssetTagHelper.html#method-i-stylesheet_link_tag). You can set any options of `stylesheet_link_tag`.
62
+
63
+ Given entry point name is resolved according to definition of manifest.
64
+
65
+ ```ruby
66
+ stylesheet_bundle_tag 'calendar', 'data-turbolinks-track': 'reload'
67
+ # => <link rel="stylesheet" media="screen"
68
+ # href="/assets/web/pack/calendar-1016838bab065ae1e122.css"
69
+ # data-turbolinks-track="reload" />
70
+
71
+ stylesheet_bundle_tag 'orders/style'
72
+ # => <link rel="stylesheet" media="screen"
73
+ # href="/assets/web/pack/orders/style-1016838bab065ae1e122.css" />
74
+ ```
75
+
76
+ #### `image_bundle_tag`
77
+
78
+ This is a wrapper of [image_tag](https://api.rubyonrails.org/classes/ActionView/Helpers/AssetTagHelper.html#method-i-image_tag). You can set any options of `image_tag`.
79
+
80
+ Given entry point name is resolved according to definition of manifest.
81
+
82
+ ```ruby
83
+ image_bundle_tag 'icon.png'
84
+ # => <img src="/assets/pack/icon-1016838bab065ae1e314.png" />
85
+
86
+ image_bundle_tag "icon.png", size: "16x10", alt: "Edit Entry"
87
+ # => <img src="/assets/pack/icon-1016838bab065ae1e314.png" width="16"
88
+ height="10" alt="Edit Entry" />
89
+ ```
34
90
 
35
91
  ### Configuration
36
92
 
93
+ After installed, configure your Rails app by adding a new file `config/initializers/webpack_manifest.rb` as below.
94
+
95
+ ```rb
96
+ WebpackManifest::Rails.configuration do |c|
97
+ # By default c.cache is `false`, which means application always parses given
98
+ # manifest json. Disableing cache is useful in development.
99
+ # Set `true` if you would like to cache manifest in memory, for example in production.
100
+ c.cache = !Rails.env.development?
101
+
102
+ # Register a path to a manifest file here.
103
+ c.manifest = Rails.root.join('public', 'assets', 'manifest.json')
104
+ end
105
+ ```
106
+
107
+ ### Multiple manifest files support
108
+
109
+ This is optional. You can register multiple manifest files for the view helpers. This feature must be useful if your Rails project have several sites, then asset bundling process is separated.
110
+
111
+ For example, your project serve for two sites, `shop` and `admin` from each separated manifest file. You can register each as below.
112
+
37
113
  ```rb
38
114
  # In config/initializers/webpack_manifest.rb
39
115
 
40
- cache = if Rails.env.development?
41
- false
42
- else
43
- true
44
- end
45
- manifest = WebpackManifest::Manifest.new(
46
- Rails.root.join('public', 'assets', 'manifest.json'),
47
- cache: cache,
48
- )
116
+ WebpackManifest::Rails.configuration do |c|
117
+ c.cache = !Rails.env.development?
118
+
119
+ # If you have manifest files more than one, register them by `c.add` instead
120
+ # of `c.manifest=`. Note that the first registered one(e.g. `shop` in this
121
+ # example) is recognized as a default manifest.
122
+ c.add :shop, Rails.root.join('public', 'assets', 'manifest-shop.json')
123
+ c.add :admin, Rails.root.join('public', 'assets', 'manifest-admin.json')
124
+ end
125
+ ```
126
+
127
+ Then you can resolve paths with view helpers by passing `manifest:` option.
128
+
49
129
  ```
130
+ # This resolves a path by shop's manifest json.
131
+ javascript_bundle_tag('item_group_editor', manifest: :shop)
132
+
133
+ # This resolves a path by admin's manifest json.
134
+ asset_bundle_tag('favicon.ico', manifest: :admin)
135
+
136
+ # This resolves a path by shop's manifest json implicitly because the first one is marked as a default.
137
+ javascript_bundle_tag('item_group_editor')
138
+ ```
139
+
140
+ ## TODO
50
141
 
142
+ * Provides configuration generator for Rails initializers
51
143
 
52
- ### Multiple manifest support
144
+ ## Acknowledgement
53
145
 
54
- TBD
146
+ Special thanks to [@f_subal](https://twitter.com/f_subal) and his awesome blog [post](https://inside.pixiv.blog/subal/4615)(japanese).
55
147
 
56
- ## Roadmap
148
+ ## Alternatives
57
149
 
58
- * [x] Implement manifest class
59
- * [x] Implement Rails view helpers
60
- * [ ] Configuration generator for Rails initializers
61
- * [ ] Multiple manifest files support
62
- * [ ] Write document more
150
+ * [ed-mare/webpack_manifest_plugin](https://github.com/ed-mare/webpack_manifest_plugin)
63
151
 
64
152
  ## Development
65
153
 
@@ -6,9 +6,12 @@ module WebpackManifest
6
6
  class Manifest
7
7
  class MissingEntryError < StandardError; end
8
8
 
9
- def initialize(path_or_hash, cache: false)
10
- @path_or_hash = path_or_hash
11
- @cache_manifest = cache
9
+ attr_reader :path
10
+ attr_writer :cache
11
+
12
+ def initialize(path, cache: false)
13
+ @path = path
14
+ @cache = cache
12
15
  end
13
16
 
14
17
  def lookup!(name)
@@ -23,10 +26,14 @@ module WebpackManifest
23
26
  data.values
24
27
  end
25
28
 
29
+ def cache_enabled?
30
+ @cache
31
+ end
32
+
26
33
  private
27
34
 
28
35
  def data
29
- if @cache_manifest
36
+ if cache_enabled?
30
37
  @data ||= load_data
31
38
  else
32
39
  load_data
@@ -34,14 +41,12 @@ module WebpackManifest
34
41
  end
35
42
 
36
43
  def load_data
37
- return @path_or_hash if @path_or_hash.is_a? Hash
38
- path = @path_or_hash
39
- File.exist?(path) ? JSON.parse(File.read(path)) : {}
44
+ File.exist?(@path) ? JSON.parse(File.read(@path)) : {}
40
45
  end
41
46
 
42
47
  def handle_missing_entry(name)
43
48
  raise MissingEntryError, <<~MSG
44
- Can not find #{name} in #{@path_or_hash}.
49
+ Can not find #{name} in #{@path}.
45
50
  Your manifest contains:
46
51
  #{JSON.pretty_generate(@data)}
47
52
  MSG
@@ -2,11 +2,17 @@
2
2
 
3
3
  module WebpackManifest
4
4
  module Rails
5
+ require 'webpack_manifest/rails/configuration'
5
6
  require 'webpack_manifest/rails/helper'
7
+ require 'webpack_manifest/rails/manifest_repository'
6
8
 
7
9
  class << self
8
- # TODO: multiple manifest files support
9
- attr_accessor :manifest
10
+ def configuration(&block)
11
+ @configuration ||= Configuration.new
12
+ yield @configuration if block_given?
13
+ @configuration
14
+ end
15
+ attr_writer :configuration
10
16
  end
11
17
  end
12
18
  end
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ module WebpackManifest
4
+ module Rails
5
+ class Configuration
6
+ attr_reader :manifests
7
+
8
+ def initialize
9
+ # default values
10
+ @cache = false
11
+ @manifests = ManifestRepository.new
12
+ end
13
+
14
+ def cache
15
+ @cache
16
+ end
17
+
18
+ def cache=(v)
19
+ @manifests.all_manifests.each { |m| m.cache = v }
20
+ @cache = v
21
+ end
22
+
23
+ # Register a single manifest as a default.
24
+ def manifest=(path)
25
+ @manifests.default = @manifests.add '', path, cache: @cache
26
+ end
27
+
28
+ # Register a manfiest. You can register multiple files by calling `#add`.
29
+ def add(key, path)
30
+ @manifests.add(key, path, cache: @cache)
31
+ end
32
+ end
33
+ end
34
+ end
@@ -7,11 +7,9 @@ module WebpackManifest::Rails::Helper
7
7
  #
8
8
  # <%= asset_bundle_path 'calendar.css' %> # => "/assets/web/pack/calendar-1016838bab065ae1e122.css"
9
9
  # <%= asset_bundle_path 'icon/favicon.ico' %> # => "/assets/web/pack/icon/favicon-1016838bab065ae1e122.ico"
10
- def asset_bundle_path(name, **options)
11
- asset_path(
12
- WebpackManifest::Rails.manifest.lookup!(name.to_s),
13
- **options,
14
- )
10
+ def asset_bundle_path(name, manifest: nil, **options)
11
+ manifest = get_manifest_by_key(manifest)
12
+ asset_path(manifest.lookup!(name.to_s), **options)
15
13
  end
16
14
 
17
15
  # Example:
@@ -21,8 +19,8 @@ module WebpackManifest::Rails::Helper
21
19
  #
22
20
  # <%= javascript_bundle_tag 'orders/app' %> # =>
23
21
  # <script src="/assets/web/pack/orders/app-1016838bab065ae1e314.js"></script>
24
- def javascript_bundle_tag(*names, **options)
25
- javascript_include_tag(*sources_from_manifest(names, 'js'), **options)
22
+ def javascript_bundle_tag(*names, manifest: nil, **options)
23
+ javascript_include_tag(*sources_from_manifest(names, 'js', key: manifest), **options)
26
24
  end
27
25
 
28
26
  # Examples:
@@ -34,8 +32,8 @@ module WebpackManifest::Rails::Helper
34
32
  # <%= stylesheet_bundle_tag 'orders/style' %> # =>
35
33
  # <link rel="stylesheet" media="screen"
36
34
  # href="/assets/web/pack/orders/style-1016838bab065ae1e122.css" />
37
- def stylesheet_bundle_tag(*names, **options)
38
- stylesheet_link_tag(*sources_from_manifest(names, 'css'), **options)
35
+ def stylesheet_bundle_tag(*names, manifest: nil, **options)
36
+ stylesheet_link_tag(*sources_from_manifest(names, 'css', key: manifest), **options)
39
37
  end
40
38
 
41
39
  # Examples:
@@ -45,16 +43,20 @@ module WebpackManifest::Rails::Helper
45
43
  #
46
44
  # <%= image_bundle_tag "icon.png", size: "16x10", alt: "Edit Entry"
47
45
  # <img src="/assets/pack/icon-1016838bab065ae1e314.png" width="16" height="10" alt="Edit Entry" />
48
- def image_bundle_tag(name, **options)
49
- image_tag(
50
- WebpackManifest::Rails.manifest.lookup!(name.to_s),
51
- **options,
52
- )
46
+ def image_bundle_tag(name, manifest: nil, **options)
47
+ manifest = get_manifest_by_key(manifest)
48
+ image_tag(manifest.lookup!(name.to_s), **options)
53
49
  end
54
50
 
55
51
  private
56
52
 
57
- def sources_from_manifest(names, ext)
58
- names.map { |name| WebpackManifest::Rails.manifest.lookup!(name + '.' + ext) }
53
+ def sources_from_manifest(names, ext, key: nil)
54
+ manifest = get_manifest_by_key(key)
55
+ names.map { |name| manifest.lookup!(name + '.' + ext) }
56
+ end
57
+
58
+ def get_manifest_by_key(key = nil)
59
+ repository = WebpackManifest::Rails.configuration.manifests
60
+ key.nil? ? repository.default : repository.get(key)
59
61
  end
60
62
  end
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ module WebpackManifest
4
+ module Rails
5
+ class ManifestRepository
6
+ attr_accessor :default
7
+
8
+ def initialize
9
+ @manifests = {}
10
+ @default = nil # a pointer to a default manifest
11
+ end
12
+
13
+ def all_manifests
14
+ @manifests.values
15
+ end
16
+
17
+ # @private
18
+ def add(key, path, **options)
19
+ manifest = WebpackManifest::Manifest.new(path, options)
20
+ # Mark a first one as a default
21
+ @default = manifest if @manifests.empty?
22
+ @manifests[key.to_sym] = manifest
23
+ end
24
+
25
+ def get(key)
26
+ @manifests[key.to_sym]
27
+ end
28
+ end
29
+ end
30
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module WebpackManifest
4
- VERSION = "0.1.0"
4
+ VERSION = "0.2.0"
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: webpack_manifest
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
  - Nobuhiro Nikushi
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-10-16 00:00:00.000000000 Z
11
+ date: 2018-10-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: actionview
@@ -80,7 +80,9 @@ files:
80
80
  - lib/webpack_manifest.rb
81
81
  - lib/webpack_manifest/manifest.rb
82
82
  - lib/webpack_manifest/rails.rb
83
+ - lib/webpack_manifest/rails/configuration.rb
83
84
  - lib/webpack_manifest/rails/helper.rb
85
+ - lib/webpack_manifest/rails/manifest_repository.rb
84
86
  - lib/webpack_manifest/version.rb
85
87
  - webpack_manifest.gemspec
86
88
  homepage: https://github.com/nikushi/webpack_manifest
@@ -103,7 +105,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
103
105
  version: '0'
104
106
  requirements: []
105
107
  rubyforge_project:
106
- rubygems_version: 2.7.7
108
+ rubygems_version: 2.6.8
107
109
  signing_key:
108
110
  specification_version: 4
109
111
  summary: A gem to integrate ruby with npm's webpack-manifest-plugin package