Sha256: df9d8edc23ab2611fe03fe2b0cb4732eb5ca0c37976ad06b7c9fb2dd417fa105

Contents?: true

Size: 1.34 KB

Versions: 2

Compression:

Stored size: 1.34 KB

Contents

# frozen_string_literal: true

require "fileutils"

module Pakyow
  module Assets
    class Precompiler
      def initialize(app)
        @app = app
      end

      def precompile!
        assets.each do |asset|
          precompile_asset!(asset)
        end

        packs.each do |pack|
          if pack.javascripts?
            precompile_asset!(pack.javascripts)
          end

          if pack.stylesheets?
            precompile_asset!(pack.stylesheets)
          end
        end
      end

      def precompile_asset!(asset)
        compile_path = File.join(@app.config.assets.compile_path, asset.public_path)
        FileUtils.mkdir_p(File.dirname(compile_path))

        asset_content = String.new
        asset.each do |content|
          asset_content << content
        end

        File.open(compile_path, "w+") do |file|
          file.write(asset_content)
        end

        if @app.config.assets.source_maps && source_map = asset.source_map
          File.open(compile_path + ".map", "w+") do |file|
            file.write(source_map.read)
          end
        end
      end

      private

      def assets
        @app.state(:asset) + @app.plugs.flat_map { |plug|
          plug.state(:asset)
        }
      end

      def packs
        @app.state(:pack) + @app.plugs.flat_map { |plug|
          plug.state(:pack)
        }
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
pakyow-assets-1.0.0.rc2 lib/pakyow/assets/precompiler.rb
pakyow-assets-1.0.0.rc1 lib/pakyow/assets/precompiler.rb