Module: Jekyll::Webpack

Defined in:
lib/jekyll/webpack.rb,
lib/jekyll/webpack/version.rb,
lib/jekyll/webpack/debouncer.rb

Defined Under Namespace

Classes: Debouncer, Error

Constant Summary collapse

VERSION =
"0.2.7"
@@webpack_debouncers =
{}

Class Method Summary collapse

Class Method Details

.build(site) ⇒ Object



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

def self.build(site)
  only_in = site.config.dig('webpack', 'only_in')
  debounce = site.config.dig('webpack', 'debounce')

  site_dest = site.dest
  webpack_dist_path = File.expand_path('dist', site.dest)

  if only_in
    entries = []
    array_or_scalar(only_in) { |entry| entries << site_dest.end_with?(entry) }
    return unless entries.any? true
  end

  if debounce
    @@webpack_debouncers[site.dest] = Debouncer.new(site, debounce) unless @@webpack_debouncers[site.dest]
    @@webpack_debouncers[site.dest].build { webpack_exec(site_dest) }
  else
    webpack_exec(site_dest)
  end

  cleanup(site)
end

.cleanup(site) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/jekyll/webpack.rb', line 51

def self.cleanup(site)
  cleanup_files = site.config.dig('webpack', 'cleanup_files')

  if cleanup_files
    array_or_scalar(cleanup_files) do |dest_for_clean|
      path = File.expand_path(dest_for_clean, site.dest)

      if File.exist?(path) || Dir.exist?(path)
        FileUtils.rm_rf(path)
      end
    end
  end
end

.webpack_exec(site_dest) ⇒ Object

Raises:



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/jekyll/webpack.rb', line 39

def self.webpack_exec(site_dest)
  stdout, stderr, status = Open3.capture3(
    "../node_modules/.bin/webpack",
    chdir: File.expand_path(site_dest)
  )

  runtime_error = stdout =~ /ERROR in|SyntaxError/

  raise Error, stderr if stderr.size > 0
  raise Error, stdout if !runtime_error.nil?
end