Class: Hippo::Webpack

Inherits:
Object
  • Object
show all
Defined in:
lib/hippo/webpack.rb,
lib/hippo/webpack/client_config.rb

Defined Under Namespace

Classes: ClientConfig, NotFound

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeWebpack

Returns a new instance of Webpack.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/hippo/webpack.rb', line 16

def initialize
    @assets = {}
    root = Hippo::Extensions.controlling.root_path

    @config = ClientConfig.new
    @config.invoke_all
    @driver = WebpackDriver::Configuration.new(
        directory: root,
        logger: Hippo.logger,
        file: root.join('config', 'webpack.config.js'),
        cmd_line_flags: webpack_cmd_line_flags,
        environment: { NODE_ENV: Hippo.env.production? ? 'production' : 'development' }
    )
    @process = @driver.launch(development: !Hippo.env.production?)
end

Instance Attribute Details

#assetsObject (readonly)

Returns the value of attribute assets.



10
11
12
# File 'lib/hippo/webpack.rb', line 10

def assets
  @assets
end

#driverObject (readonly)

Returns the value of attribute driver.



10
11
12
# File 'lib/hippo/webpack.rb', line 10

def driver
  @driver
end

#processObject (readonly)

Returns the value of attribute process.



10
11
12
# File 'lib/hippo/webpack.rb', line 10

def process
  @process
end

Class Method Details

.using_ssl?Boolean

Returns:

  • (Boolean)


12
13
14
# File 'lib/hippo/webpack.rb', line 12

def self.using_ssl?
    ENV['SSL_CERT_PATH'] && ENV['SSL_KEY_PATH']
end

Instance Method Details

#busy?Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/hippo/webpack.rb', line 59

def busy?
    driver.process.in_progress?
end

#file(entry, raise_on_not_found: true) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/hippo/webpack.rb', line 63

def file(entry, raise_on_not_found: true)
    if Webpack.stub
        "#{entry}.js"
    else
        asset = @driver.process.assets[entry]
        if asset
            asset.file
        elsif !raise_on_not_found
            raise NotFound, "asset '#{entry}' was not found"
        end
    end
end

#hostObject



76
77
78
79
# File 'lib/hippo/webpack.rb', line 76

def host
    Hippo.env.production? ? "" :
        "//#{@driver.process.host}:#{@driver.process.port}"
end

#startObject



49
50
51
# File 'lib/hippo/webpack.rb', line 49

def start
    process.start if Hippo.env.development? && !Hippo::Webpack.stub
end

#wait_until_availableObject



53
54
55
56
57
# File 'lib/hippo/webpack.rb', line 53

def wait_until_available
    return if Hippo.env.production?
    Hippo.logger.warn("waiting while compilation in progress") if busy?
    sleep 0.5 while busy?
end

#webpack_cmd_line_flagsObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/hippo/webpack.rb', line 32

def webpack_cmd_line_flags
    return [] if Hippo.env.production?
    flags = [
        '--hot', '--inline',
        '--host', (ENV['HOST'] || 'localhost'),
        '--port', '8889',
    ]
    if Webpack.using_ssl?
        flags += [
            '--https',
            '--cert', ENV['SSL_CERT_PATH'],
            '--key',  ENV['SSL_KEY_PATH']
        ]
    end
    flags
end