Sha256: 33e5c06223007a6fc80ae546b6db3d7df23481e065d6c8235d81b38f7519b2f0

Contents?: true

Size: 1.5 KB

Versions: 1

Compression:

Stored size: 1.5 KB

Contents

# frozen_string_literal: true
require 'sewing_kit/webpack/webpack'

module SewingKit
  module Webpack
    class Manifest
      class Base
        def initialize
          @metadata = nil
        end

        def clear_cache!
          @metadata = nil
        end

        def manifest
          current_metadata = metadata

          if current_metadata.is_a?(Array)
            all_assets = current_metadata.flat_map do |data|
              data['assets']['all'].values
            end

            # sewing-kit asset manifests return a numerically indexed hash, not a true array,
            # so we need to change it back to a hash for compatibility
            asset_hash = all_assets.each_with_index
              .collect { |asset, index| [index, asset] }
              .to_h

            {
              "all" => asset_hash,
            }
          else
            current_metadata['assets']
          end
        end

        def self.load_metadata_from_node
          begin
            stdout, stderr, status = Open3.capture3('node_modules/.bin/sewing-kit', 'manifest', "--mode=#{mode}")
          rescue => e
            raise NodeSewingKitNotRunnable.new(mode, e)
          end

          raise NodeSewingKitManifestMissing.new(mode, stderr) unless status.success?

          begin
            JSON.parse(stdout)
          rescue => e
            raise ManifestParseError, e
          end
        end

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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
sewing_kit-0.91.0 lib/sewing_kit/webpack/manifest/base.rb