Sha256: ff7bca7a2bb578d1ce9dc2749445e7adf374bc170c59ecd8905d3cf16ac92eb5

Contents?: true

Size: 1.48 KB

Versions: 1

Compression:

Stored size: 1.48 KB

Contents

module WebpackManifestPlugin
  # View helpers available in the application.
  module ViewHelpers
    # Webpack manages asset paths in your CSS and Javascript files but not in your Rails
    # templates. The npm webpack-manifest-plugin generates a json file (see example
    # below). 'webpack_manifest_path' uses this file to inject the correct path.
    #
    # {
    #   "common.css": "/assets/stylesheets/common.css",
    #   "common.js": "/assets/javascripts/common.js",
    #   "fonts/fontawesome-webfont.ttf?v=4.6.3": "/assets/fonts/fontawesome-webfont.ttf",
    #   "images/panel_bg.png": "/assets/images/panel_bg.png"
    # }
    #
    # Specify an asset hash key like so:
    #
    # <img src="<%= webpack_manifest_path("images/panel_bg.png") %>" width="40px"></img>
    # <script src="<%= webpack_manifest_path("common.js") %>"></script>
    # <link href="<%= webpack_manifest_path("common.css") %>" rel="stylesheet" type="text/css">
    #
    # Returns asset path if key exists, otherwise returns the key itself. If an exception occurs,
    # it logs the error and returns an empty string.
    def webpack_manifest_path(key)
      webpack_manifest_configs[key] || key
    rescue StandardError => e
      WebpackManifestPlugin.logger.warn "Error parsing webpack manifest JSON. #{e.message}"
      ''
    end

    # Manifest configs as a hash.
    def webpack_manifest_configs
      c = WebpackManifestPlugin.configuration
      c.manifest || (@webpack_manifest_configs ||= c.load_manifest)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
webpack_manifest_plugin-0.1.2 lib/webpack_manifest_plugin/view_helpers.rb