sewing_kit 0.8.0 → 0.27.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
2
  SHA1:
3
- metadata.gz: c158fee225e3c96470ca32e8af145fc261e9de9d
4
- data.tar.gz: 41b8baaa9f319ce1bc985383f7e2a1fe6168fb3e
3
+ metadata.gz: 767dcb5c1f9643937e519b07940c286ef42c1c6a
4
+ data.tar.gz: 3744a04508c81a91b90bdf72d5d0f43eb5ee17e8
5
5
  SHA512:
6
- metadata.gz: c3b3adcaa26e1185fd5515387d0eb2c1c965a79f432b350601569a1a9f634175c3a703fa5ba89d151e7e185177eb8d4afc124f50cf43420391a55fcc1b4d047d
7
- data.tar.gz: 35d946b5848e8353213ab8d81f7627524e435e5220d70f04f70065808f78a62dd6a948e9a28d672e683120812ffaa1b48d310cc41ce21f48c1b59a49b69d10bb
6
+ metadata.gz: deaefac1ed2b3d39f233bdfbaa6d23c7fa32f8a8d827878d1b416e26782f5f5d42fde18487a733653d570d4f293a3747230a90c28601f9c2602e0a43c0885534
7
+ data.tar.gz: c1b7e41ef24ba24aff7d0812b3da10b879c4618e991266d8c58674ec67cf039058339f9dc0a2149a89f7f9d2d74d011fb57eb5f76db19c642775f5ee0dc9c832
data/README.md CHANGED
@@ -28,8 +28,8 @@ sewing_kit looks for JavaScript in `app/ui/index.js`. The code in `index.js` (a
28
28
  The `main` bundle is imported into `erb` files using Rails helpers:
29
29
 
30
30
  ```erb
31
- <%= sewing_kit_link_tag *sewing_kit_asset_paths('main') %>
32
- <%= sewing_kit_script_tag *sewing_kit_asset_paths('main') %>
31
+ <%= sewing_kit_link_tag *sewing_kit_assets('main') %>
32
+ <%= sewing_kit_script_tag *sewing_kit_assets('main') %>
33
33
  ```
34
34
 
35
35
  **Note:** CSS `<link>` tags appear only in production; in development, CSS is embedded within the `main.js` bundle.
@@ -1,4 +1,4 @@
1
1
  # frozen_string_literal: true
2
2
  module SewingKit
3
- VERSION = "0.8.0"
3
+ VERSION = "0.27.0"
4
4
  end
@@ -11,35 +11,38 @@ module SewingKit
11
11
  class UnknownJavaScriptAssetError < StandardError
12
12
  end
13
13
 
14
- def sewing_kit_asset_paths(entrypoint_name, extension: 'js')
14
+ Asset = Struct.new(:path, :integrity)
15
+
16
+ def sewing_kit_assets(entrypoint_name, extension: 'js')
15
17
  return '' unless entrypoint_name.present?
16
18
 
17
- paths = SewingKit::Webpack::Manifest.asset_paths(entrypoint_name)
18
- return '' unless paths && paths[extension]
19
+ assets = SewingKit::Webpack::Manifest.asset_dependencies(entrypoint_name)
20
+ return [] unless assets && assets[extension]
19
21
 
20
- entry_paths = paths[extension]
21
- entry_paths.unshift('/webpack/assets/dll/vendor.js') if serve_development_assets? && extension == 'js'
22
- entry_paths
22
+ dependencies = assets[extension]
23
+ dependencies.unshift(dll_asset) if serve_development_assets? && extension == 'js'
24
+ dependencies.map do |raw_asset|
25
+ Asset.new(raw_asset['path'], raw_asset['integrity'])
26
+ end
23
27
  end
24
28
 
25
- def sewing_kit_link_tag(*paths)
26
- options = paths.extract_options!
29
+ def sewing_kit_link_tag(*assets)
30
+ options = assets.extract_options!
27
31
 
28
- tags = paths.uniq.map do |path|
29
- next '' if path == ''
30
- create_asset_tag(:link, path, options)
32
+ tags = assets.uniq.map do |asset|
33
+ next '' if asset.path == ''
34
+ create_asset_tag(:link, asset, options)
31
35
  end
32
36
 
33
37
  safe_join(tags, "\n")
34
38
  end
35
39
 
36
- def sewing_kit_script_tag(*paths)
37
- options = paths.extract_options!
38
-
39
- tags = paths.uniq.map do |path|
40
- next '' if path == ''
40
+ def sewing_kit_script_tag(*assets)
41
+ options = assets.extract_options!
42
+ tags = assets.map do |asset|
43
+ next '' if asset.path == ''
41
44
 
42
- create_asset_tag(:script, path, options)
45
+ create_asset_tag(:script, asset, options)
43
46
  end
44
47
 
45
48
  safe_join(tags, "\n")
@@ -47,46 +50,33 @@ module SewingKit
47
50
 
48
51
  private
49
52
 
50
- def create_asset_tag(tag_type, path, tag_options)
53
+ def create_asset_tag(tag_type, asset, tag_options)
51
54
  raise ArgumentError, "Invalid tag type: #{tag_type}" unless [:script, :link].include? tag_type
52
55
 
53
56
  options = tag_options.clone
54
57
 
55
- if tag_options[:integrity]
56
- file_hash = extract_hash(path)
57
- options[:integrity] = maybe_integrity(file_hash)
58
+ if tag_options[:integrity] && asset.integrity
59
+ options[:integrity] = asset.integrity
60
+ else
61
+ options.delete(:integrity)
58
62
  end
59
63
 
60
64
  case tag_type
61
65
  when :script
62
- content_tag(:script, '', options.reverse_merge(src: path))
66
+ content_tag(:script, '', options.reverse_merge(src: asset.path))
63
67
  when :link
64
- tag(:link, options.reverse_merge(href: path, rel: 'stylesheet'))
68
+ tag(:link, options.reverse_merge(href: asset.path, rel: 'stylesheet'))
65
69
  end
66
70
  end
67
71
 
68
- def maybe_integrity(file_hash)
69
- return nil unless file_hash
70
- encode_hash(file_hash)
71
- end
72
-
73
- def encode_hash(hash)
74
- # sewing-kit's default config places a sha256 hash in the filename. Browsers expect integrity
75
- # to be the Base64 encoded version of the binary hash prefixed with the hashing algorithm
76
- binary_hash = Array(hash).pack('H*')
77
- "sha256-#{Base64.strict_encode64(binary_hash)}"
78
- end
79
-
80
- def extract_hash(path)
81
- # Consumes sewing-kit's [name]-[chunkhash].[ext] format
82
- return unless path =~ /.*-[A-Za-z0-9]{64}\.(js|css)/
83
- path.rpartition('-').last.split('.').first
84
- end
85
-
86
72
  def serve_development_assets?
87
73
  return false if ENV['SK_SIMULATE_PRODUCTION'] == '1'
88
74
  Rails.env.development?
89
75
  end
76
+
77
+ def dll_asset
78
+ Asset.new('/webpack/assets/dll/vendor.js')
79
+ end
90
80
  end
91
81
  end
92
82
  end
@@ -47,8 +47,8 @@ module SewingKit
47
47
 
48
48
  class << self
49
49
  # :nodoc:
50
- def asset_paths(entrypoint_name)
51
- instance.asset_paths(entrypoint_name)
50
+ def asset_dependencies(entrypoint_name)
51
+ instance.asset_dependencies(entrypoint_name)
52
52
  end
53
53
 
54
54
  def clear_cache!
@@ -61,7 +61,9 @@ module SewingKit
61
61
 
62
62
  def instance
63
63
  return @instance if @instance
64
- @instance = if Rails.env.development? && ENV['SK_SIMULATE_PRODUCTION'] != '1'
64
+
65
+ mode = ENV['NODE_ENV'] || Rails.env.to_s
66
+ @instance = if mode == 'development' && ENV['SK_SIMULATE_PRODUCTION'] != '1'
65
67
  Development.new
66
68
  else
67
69
  Production.new
@@ -8,7 +8,7 @@ module SewingKit
8
8
  end
9
9
 
10
10
  # :nodoc:
11
- def asset_paths(entrypoint_name)
11
+ def asset_dependencies(entrypoint_name)
12
12
  metadata['entrypoints'][entrypoint_name]
13
13
  end
14
14
 
@@ -22,7 +22,7 @@ module SewingKit
22
22
 
23
23
  def load_metadata_from_node
24
24
  begin
25
- stdout, * = Open3.capture3('node_modules/.bin/sewing-kit', 'manifest', "-- mode #{mode}")
25
+ stdout, * = Open3.capture3('node_modules/.bin/sewing-kit', 'manifest', "--mode=#{mode}")
26
26
  rescue => e
27
27
  raise NodeSewingKitNotRunnable.new(mode, e)
28
28
  end
@@ -34,10 +34,10 @@ module SewingKit
34
34
  end
35
35
  end
36
36
 
37
- private
37
+ protected
38
38
 
39
39
  def mode
40
- Rails.env.to_s || 'production'
40
+ ENV['NODE_ENV'] || Rails.env.to_s || 'production'
41
41
  end
42
42
  end
43
43
  end
@@ -47,6 +47,12 @@ module SewingKit
47
47
  raise ManifestParseError, e
48
48
  end
49
49
  end
50
+
51
+ protected
52
+
53
+ def mode
54
+ 'development'
55
+ end
50
56
  end
51
57
  end
52
58
  end
@@ -9,7 +9,7 @@ module SewingKit
9
9
  @metadata_path = nil
10
10
 
11
11
  # :nodoc:
12
- def asset_paths(entrypoint_name)
12
+ def asset_dependencies(entrypoint_name)
13
13
  metadata['entrypoints'][entrypoint_name]
14
14
  end
15
15
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sewing_kit
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.0
4
+ version: 0.27.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Chris Sauve