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



34
35
36
37
38
39
40
# File 'lib/webpack_native/webpack_native_helper.rb', line 34

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_image_url(file_name, **options) ⇒ Object



20
21
22
# File 'lib/webpack_native/webpack_native_helper.rb', line 20

def webpack_image_url(file_name, **options)
  image_tag("/webpack_native/#{file_name}", **options)
end

#webpack_javascript_url(asset, **html_options) ⇒ Object



11
12
13
14
15
16
17
18
# File 'lib/webpack_native/webpack_native_helper.rb', line 11

def webpack_javascript_url(asset, **html_options)
  html_options = html_options.merge(
    type: "text/javascript",
    src: "/webpack_native/#{webpack_manifest_file.fetch("#{asset}.js")}"
  )
  ("script".freeze, nil, html_options).html_safe
  # or tag.script(html_options).html_safe
end

#webpack_stylesheet_url(asset, **html_options) ⇒ Object



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

def webpack_stylesheet_url(asset, **html_options)
  html_options = html_options.merge(
    href: "/webpack_native/#{webpack_manifest_file.fetch("#{asset}.css")}",
    rel: "stylesheet"
  )
  tag.link(html_options).html_safe
end