Sha256: af22fe274367937868f59d364e22b4081d215851c18791f8ba2d99170c2b6870

Contents?: true

Size: 1.73 KB

Versions: 11

Compression:

Stored size: 1.73 KB

Contents

require "shellwords"
require "socket"
require "webpacker/configuration"
require "webpacker/dev_server"
require "webpacker/runner"

module Webpacker
  class DevServerRunner < Webpacker::Runner
    def run
      load_config
      detect_port!
      execute_cmd
    end

    private
      def load_config
        app_root = Pathname.new(@app_path)

        config = Configuration.new(
          root_path: app_root,
          config_path: app_root.join("config/webpacker.yml"),
          env: ENV["RAILS_ENV"]
        )

        dev_server = DevServer.new(config)

        @hostname          = dev_server.host
        @port              = dev_server.port
        @pretty            = dev_server.pretty?

      rescue Errno::ENOENT, NoMethodError
        $stdout.puts "webpack dev_server configuration not found in #{config.config_path}[#{ENV["RAILS_ENV"]}]."
        $stdout.puts "Please run bundle exec rails webpacker:install to install Webpacker"
        exit!
      end

      def detect_port!
        server = TCPServer.new(@hostname, @port)
        server.close

      rescue Errno::EADDRINUSE
        $stdout.puts "Another program is running on port #{@port}. Set a new port in #{@config_file} for dev_server"
        exit!
      end

      def execute_cmd
        env = Webpacker::Compiler.env

        cmd = if node_modules_bin_exist?
          ["#{@node_modules_bin_path}/webpack-dev-server"]
        else
          ["yarn", "webpack-dev-server"]
        end
        cmd += ["--config", @webpack_config]
        cmd += ["--progress", "--color"] if @pretty

        Dir.chdir(@app_path) do
          Kernel.exec env, *cmd
        end
      end

      def node_modules_bin_exist?
        File.exist?("#{@node_modules_bin_path}/webpack-dev-server")
      end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
webpacker-4.0.2 lib/webpacker/dev_server_runner.rb
webpacker-4.0.1 lib/webpacker/dev_server_runner.rb
webpacker-4.0.0 lib/webpacker/dev_server_runner.rb
webpacker-4.0.0.rc.8 lib/webpacker/dev_server_runner.rb
webpacker-4.0.0.rc.7 lib/webpacker/dev_server_runner.rb
webpacker-4.0.0.rc.6 lib/webpacker/dev_server_runner.rb
webpacker-4.0.0.rc.5 lib/webpacker/dev_server_runner.rb
webpacker-4.0.0.rc.4 lib/webpacker/dev_server_runner.rb
webpacker-4.0.0.rc.3 lib/webpacker/dev_server_runner.rb
webpacker-4.0.0.rc.2 lib/webpacker/dev_server_runner.rb
webpacker-4.0.0.rc.1 lib/webpacker/dev_server_runner.rb