Sha256: 64bc7fb5cc5ac85968bd9e3d226cebf32615370bedf92d7380333aeae3c338ba

Contents?: true

Size: 1.35 KB

Versions: 10

Compression:

Stored size: 1.35 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

      private

      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


      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

10 entries across 10 versions & 1 rubygems

Version Path
pakyow-assets-1.0.6 lib/pakyow/assets/precompiler.rb
pakyow-assets-1.0.5 lib/pakyow/assets/precompiler.rb
pakyow-assets-1.0.4 lib/pakyow/assets/precompiler.rb
pakyow-assets-1.0.3 lib/pakyow/assets/precompiler.rb
pakyow-assets-1.0.2 lib/pakyow/assets/precompiler.rb
pakyow-assets-1.0.1 lib/pakyow/assets/precompiler.rb
pakyow-assets-1.0.0 lib/pakyow/assets/precompiler.rb
pakyow-assets-1.0.0.rc5 lib/pakyow/assets/precompiler.rb
pakyow-assets-1.0.0.rc4 lib/pakyow/assets/precompiler.rb
pakyow-assets-1.0.0.rc3 lib/pakyow/assets/precompiler.rb