Module: WebpackNative::WebpackNativeHelper

Defined in:
lib/webpack_native/webpack_native_helper.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.load_webpack_manifestObject

[View source]

121
122
123
124
125
126
127
# File 'lib/webpack_native/webpack_native_helper.rb', line 121

def load_webpack_manifest
  # Set WEBPACK_MANIFEST_PATH to point to the manifest file
  webpack_manifest_path = Rails.root.join('public', 'webpack_native', 'manifest.json')
  JSON.parse(File.read(webpack_manifest_path))
rescue Errno::ENOENT
  fail "The webpack manifest file does not exist. Hint: run webpack command."
end

Instance Method Details

#webpack_favicons(*args) ⇒ Object

result: <link rel=“apple-touch-icon” sizes=“180x180” href=“/apple-touch-icon.png”> <link rel=“icon” type=“image/png” sizes=“32x32” href=“/favicon-32x32.png”> <link rel=“icon” type=“image/png” sizes=“16x16” href=“/favicon-16x16.png”> <link rel=“manifest” href=“/site.webmanifest”>

[View source]

58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/webpack_native/webpack_native_helper.rb', line 58

def webpack_favicons(*args)
  tags = []
  args.each do |favicon|
    
    filename = File.basename(favicon, ".*") # exclude the extension
    ext = File.extname(favicon)
    
    if ext == '.webmanifest'
      manifest = favicon
      html_options = {
        rel: 'manifest',
        href: manifest
      }
      
    else
      mimetypes = {
        '.png' => 'image/png',
        '.jpg' => 'image/jpg',
        '.jpeg' => 'image/jpeg',
        '.webp' => 'image/webp',
        '.tiff' => 'image/tiff',
        '.svg' => 'image/svg+xml',
        '.ico' => 'image/x-icon'
      }
      
      html_options = {
        rel: filename == 'apple-touch-icon' ? 'apple-touch-icon' : 'icon',
        type: mimetypes[ext],
        href: favicon
      }
      
      sizes = filename[/(.+)?([0-9]{2,3}x[0-9]{2,3})(.+)?/, 2]
      
      sizes = '180×180' if filename == 'apple-touch-icon'
        
      html_options = html_options.merge({sizes: sizes}) unless sizes.nil?
      
    end
     
    tags << tag.link(html_options)
    
  end
  
  return tags.join.html_safe

end

#webpack_image_path(image, **options) ⇒ Object

[View source]

43
44
45
# File 'lib/webpack_native/webpack_native_helper.rb', line 43

def webpack_image_path(image, **options)
  image_path(webpack_native_lookup(image), **options)
end

#webpack_image_tag(image, **options) ⇒ Object

[View source]

35
36
37
# File 'lib/webpack_native/webpack_native_helper.rb', line 35

def webpack_image_tag(image, **options)
  image_tag(webpack_native_lookup(image), **options)
end

#webpack_image_url(image, **options) ⇒ Object

[View source]

39
40
41
# File 'lib/webpack_native/webpack_native_helper.rb', line 39

def webpack_image_url(image, **options)
  image_url(webpack_native_lookup(image), **options)
end

#webpack_javascript_path(asset, **options) ⇒ Object

[View source]

31
32
33
# File 'lib/webpack_native/webpack_native_helper.rb', line 31

def webpack_javascript_path(asset, **options)
  path_to_asset(webpack_native_lookup("#{asset.gsub('.js', '')}.js"), options)
end

#webpack_javascript_tag(asset, **html_options) ⇒ Object

[View source]

19
20
21
22
23
24
25
26
# File 'lib/webpack_native/webpack_native_helper.rb', line 19

def webpack_javascript_tag(asset, **html_options)
  html_options = html_options.merge(
    type: "text/javascript",
    src: webpack_javascript_path(asset)
  )
  ("script".freeze, nil, html_options).html_safe
  # or tag.script(html_options).html_safe
end

#webpack_javascript_url(asset, **options) ⇒ Object

[View source]

28
29
30
# File 'lib/webpack_native/webpack_native_helper.rb', line 28

def webpack_javascript_url(asset, **options)
  url_to_asset(webpack_native_lookup("#{asset.gsub('.js', '')}.js"), options)
end

#webpack_stylesheet_path(asset, **options) ⇒ Object

[View source]

11
12
13
# File 'lib/webpack_native/webpack_native_helper.rb', line 11

def webpack_stylesheet_path(asset, **options)
  path_to_asset(webpack_native_lookup("#{asset.gsub('.css', '')}.css"), options)
end

#webpack_stylesheet_tag(asset, **html_options) ⇒ Object

[View source]

3
4
5
6
7
8
9
# File 'lib/webpack_native/webpack_native_helper.rb', line 3

def webpack_stylesheet_tag(asset, **html_options)
  html_options = html_options.merge(
    href: webpack_stylesheet_path(asset),
    rel: "stylesheet"
  )
  tag.link(html_options).html_safe
end

#webpack_stylesheet_url(asset, **options) ⇒ Object

[View source]

15
16
17
# File 'lib/webpack_native/webpack_native_helper.rb', line 15

def webpack_stylesheet_url(asset, **options)
  url_to_asset(webpack_native_lookup("#{asset.gsub('.css', '')}.css"), options)
end