Sha256: f176c3bd8cabf2c223be9538e0e34524695fa3b34d0bedf84030fee986f1a0a5

Contents?: true

Size: 1.58 KB

Versions: 7

Compression:

Stored size: 1.58 KB

Contents

require 'uri'

module SewingKit
  module Webpack
    # Webpack manifest loading, caching & entry point retrieval
    class Manifest
      # Raised if we can't read our webpack manifest for whatever reason
      class ManifestLoadError < StandardError
        def initialize(message, orig)
          super "#{message} (original error #{orig})"
        end
      end

      # Raised if a supplied entry point does not exist in the webpack manifest
      class EntryPointMissingError < StandardError
      end

      class << self
        # :nodoc:
        def asset_paths(source)
          paths = manifest[source]
          if paths
            paths
          else
            raise EntryPointMissingError, "Can't find entry point '#{source}' in webpack manifest"
          end
        end

        def manifest
          if ::Rails.env.production?
            # Cache at class level, as JSON loading/parsing can be expensive.
            @manifest ||= load_manifest
          else
            # Don't cache outside of production; manifest may change.
            load_manifest
          end
        end

        private

        def manifest_bundled?
          !manifest["errors"].any? { |error| error.include? "Module build failed" }
        end

        def load_manifest
          JSON.parse(`node_modules/.bin/sewing-kit manifest --mode #{mode}`)
        rescue => e
          raise ManifestLoadError.new("Could not load compiled manifest - have you run `rake sewing_kit:build`?", e)
        end

        def mode
          ENV['NODE_ENV'] || Rails.env.to_s || 'production'
        end
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
sewing_kit-0.5.8 lib/sewing_kit/webpack/manifest.rb
sewing_kit-0.5.7 lib/sewing_kit/webpack/manifest.rb
sewing_kit-0.5.6 lib/sewing_kit/webpack/manifest.rb
sewing_kit-0.5.5 lib/sewing_kit/webpack/manifest.rb
sewing_kit-0.5.4 lib/sewing_kit/webpack/manifest.rb
sewing_kit-0.5.2 lib/sewing_kit/webpack/manifest.rb
sewing_kit-0.5.1 lib/sewing_kit/webpack/manifest.rb