Sha256: a25d14c57c5d67ceef144144b0eeadd229dd95f69326db72f191e425b9630298

Contents?: true

Size: 1.56 KB

Versions: 19

Compression:

Stored size: 1.56 KB

Contents

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

module SewingKit
  module Webpack
    class Compiler
      class NodeNotInstalled < StandardError
        def initialize
          super(
            "nodejs is not installed.  " \
            "Try creating a `.buildpack` file in the root of the project with following content:

            https://github.com/heroku/heroku-buildpack-nodejs.git
            https://github.com/heroku/heroku-buildpack-ruby.git
            "
          )
        end
      end

      def compile
        raise NodeNotInstalled unless node_installed?
        raise NodeSewingKitNotRunnable, node_env unless File.exist?('node_modules/.bin/sewing-kit')

        result = Kernel.system(
          { 'NODE_ENV' => node_env },
          *command,
          chdir: Rails.root.to_s,
          out: $stdout,
          err: $stderr
        )

        unless result
          puts "sewing-kit compile failed with error code #{$CHILD_STATUS}"
          exit(1)
        end
        result
      end

      private

      def command
        [
          'node_modules/.bin/sewing-kit',
          'build',
          '--mode',
          node_env,
        ].concat(options).reject(&:empty?)
      end

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

      def options
        build_options = SewingKit.configuration.build_options
        return [] unless build_options

        build_options.map { |key, value| ["--#{key}", value] }.flatten
      end

      def node_installed?
        Kernel.system('node --version')
      end
    end
  end
end

Version data entries

19 entries across 19 versions & 1 rubygems

Version Path
sewing_kit-0.97.1 lib/sewing_kit/webpack/compiler.rb
sewing_kit-0.97.0 lib/sewing_kit/webpack/compiler.rb
sewing_kit-0.96.0 lib/sewing_kit/webpack/compiler.rb
sewing_kit-0.95.4 lib/sewing_kit/webpack/compiler.rb
sewing_kit-0.95.3 lib/sewing_kit/webpack/compiler.rb
sewing_kit-0.95.2 lib/sewing_kit/webpack/compiler.rb
sewing_kit-0.95.1 lib/sewing_kit/webpack/compiler.rb
sewing_kit-0.95.0 lib/sewing_kit/webpack/compiler.rb
sewing_kit-0.94.0 lib/sewing_kit/webpack/compiler.rb
sewing_kit-0.93.0 lib/sewing_kit/webpack/compiler.rb
sewing_kit-0.92.1 lib/sewing_kit/webpack/compiler.rb
sewing_kit-0.92.0 lib/sewing_kit/webpack/compiler.rb
sewing_kit-0.91.0 lib/sewing_kit/webpack/compiler.rb
sewing_kit-0.32.0.pre.beta.pre.4 lib/sewing_kit/webpack/compiler.rb
sewing_kit-0.32.0.pre.beta.pre.3 lib/sewing_kit/webpack/compiler.rb
sewing_kit-0.32.0.pre.beta.pre.1 lib/sewing_kit/webpack/compiler.rb
sewing_kit-0.31.2 lib/sewing_kit/webpack/compiler.rb
sewing_kit-0.31.1 lib/sewing_kit/webpack/compiler.rb
sewing_kit-0.31.0 lib/sewing_kit/webpack/compiler.rb