Class: Sprockets::Webpack::WebpackDriver

Inherits:
Object
  • Object
show all
Defined in:
lib/sprockets/webpack/webpack_driver.rb

Instance Method Summary collapse

Constructor Details

#initialize(config, entry) ⇒ WebpackDriver

Returns a new instance of WebpackDriver.



8
9
10
11
12
13
# File 'lib/sprockets/webpack/webpack_driver.rb', line 8

def initialize(config, entry)
  @tempfile = Tempfile.new('bundle.XXXXXXXXX.js')
  @config_guard = FileGuard.new(config)
  cmd = "node #{compiler_js_path} #{config} #{entry} #{path.dirname} #{path.basename} 2>&1"
  @io = IO.popen({ 'NODE_ENV' => env }, cmd, 'r+')
end

Instance Method Details

#compileObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/sprockets/webpack/webpack_driver.rb', line 15

def compile
  errors = []

  @io.puts(@config_guard.detect_change? ? 'RELOAD' : '') # starts compilation

  bm = Benchmark.measure do
    while (line = read_line.strip) != 'WEBPACK::EOF'
      errors << line
    end
  end

  OpenStruct.new(errors: errors, data: path.read, benchmark: bm)
rescue Errno::EPIPE, IOError
  errors.unshift '----- NODE BACKTRACE -----'
  errors.unshift "#{$ERROR_INFO.message}. Please check webpack.conf.js and restart web server"
  errors.unshift
  OpenStruct.new(
    errors: errors,
    data: 'console.error("Broken pipe. Webpack died.")',
    benchmark: "<#{@io.pid} died>")
end