class KBuilder::Webpack5::WebpackBuilder

Configuration currently comes from KBuilder and stores template folders and target folders if configured

Attributes

webpack_rc[W]

In memory representation of .webpack-rc.json and generator for webpack.config.js attr_writer :webpack_config

Public Class Methods

new(configuration = nil) click to toggle source
Calls superclass method
# File lib/k_builder/webpack5/webpack_builder.rb, line 11
def initialize(configuration = nil)
  super(configuration)

  @factory = KBuilder::Webpack5::WebpackJsonFactory
end

Public Instance Methods

entries(&block) click to toggle source
# File lib/k_builder/webpack5/webpack_builder.rb, line 102
def entries(&block)
  if @webpack_rc.entries.nil?
    @webpack_rc.entries = if block
                            @factory.entries(&block)
                          else
                            entries_opts = { opinion: @factory.opinion_entries }
                            @factory.entries(**entries_opts)
                          end
  end
  write_webpack_rc

  self
end
entry(**entry_opts, &block) click to toggle source
# File lib/k_builder/webpack5/webpack_builder.rb, line 88
def entry(**entry_opts, &block)
  if @webpack_rc.entry.nil?
    @webpack_rc.entry = if block
                          @factory.entry(&block)
                        else
                          entry_opts = { opinion: @factory.opinion_entry } if entry_opts.empty?
                          @factory.entry(**entry_opts)
                        end
  end
  write_webpack_rc

  self
end
mode(**mode_opts, &block) click to toggle source
# File lib/k_builder/webpack5/webpack_builder.rb, line 74
def mode(**mode_opts, &block)
  if @webpack_rc.mode.nil?
    @webpack_rc.mode = if block
                         @factory.mode(&block)
                       else
                         mode_opts = { opinion: @factory.opinion_mode } if mode_opts.empty?
                         @factory.mode(**mode_opts)
                       end
  end
  write_webpack_rc

  self
end
pause(seconds = 1) click to toggle source
# File lib/k_builder/webpack5/webpack_builder.rb, line 163
def pause(seconds = 1)
  sleep(seconds)

  self
end
plugin_mini_css_extract(**mini_css_extract_opts, &block) click to toggle source

Plugins

# File lib/k_builder/webpack5/webpack_builder.rb, line 118
def plugin_mini_css_extract(**mini_css_extract_opts, &block)
  ensure_plugins

  if @webpack_rc.plugins.mini_css_extract.nil?
    @webpack_rc.plugins.mini_css_extract = if block
                                             @factory.mini_css_extract(&block)
                                           else
                                             mini_css_extract_opts = { opinion: @factory.opinion_mini_css_extract } if mini_css_extract_opts.empty?
                                             @factory.mini_css_extract(**mini_css_extract_opts)
                                           end

    @webpack_rc.root_scope.require_mini_css_extract_plugin = true
  end

  write_webpack_rc

  self
end
Also aliased as: plugin_split_css
plugin_split_css(**mini_css_extract_opts, &block)
vscode() click to toggle source

Debug method to open the webpack_config file in vscode ToDo: Maybe remove

# File lib/k_builder/webpack5/webpack_builder.rb, line 155
def vscode
  puts "cd #{target_folder}"
  puts webpack_rc_file
  rc "code #{webpack_rc_file}"

  self
end
webpack_dev_server(**dev_server_opts, &block) click to toggle source
# File lib/k_builder/webpack5/webpack_builder.rb, line 60
def webpack_dev_server(**dev_server_opts, &block)
  if @webpack_rc.dev_server.nil?
    @webpack_rc.dev_server = if block
                               @factory.dev_server(&block)
                             else
                               dev_server_opts = { opinion: @factory.opinion_dev_server } if dev_server_opts.empty?
                               @factory.dev_server(**dev_server_opts)
                             end
  end
  write_webpack_rc

  self
end
webpack_init() click to toggle source

Webpack init will create .webconfig-rc.json

# File lib/k_builder/webpack5/webpack_builder.rb, line 51
def webpack_init
  File.delete(webpack_rc_file) if File.exist?(webpack_rc_file)

  @webpack_rc = @factory.webpack
  write_webpack_rc

  self
end
webpack_rc() click to toggle source

def webpack_config_file

# Output Path may not be enough, I may need a webpack output path
@webpack_config_file ||= File.join(target_folder, 'webpack.config.js')

end

# File lib/k_builder/webpack5/webpack_builder.rb, line 34
def webpack_rc
  return @webpack_rc if defined? @webpack_rc

  load_webpack_rc

  @webpack_rc
end
webpack_rc_file() click to toggle source
# File lib/k_builder/webpack5/webpack_builder.rb, line 42
def webpack_rc_file
  @webpack_rc_file ||= File.join(target_folder, '.webpack-rc.json')
end

Private Instance Methods

ensure_plugins() click to toggle source
# File lib/k_builder/webpack5/webpack_builder.rb, line 191
def ensure_plugins
  @webpack_rc.plugins = OpenStruct.new if @webpack_rc.plugins.nil?
end
load_webpack_rc() click to toggle source

Load the existing .webpack-rc.json into memory

# File lib/k_builder/webpack5/webpack_builder.rb, line 172
def load_webpack_rc
  raise KBuilder::Webpack5::Error, '.webpack-rc.json does not exist' unless File.exist?(webpack_rc_file)

  content = File.read(webpack_rc_file)
  @webpack_rc = JSON.parse(content, object_class: KBuilder::Webpack5::JsonData)

  self
end
write_webpack_rc() click to toggle source
# File lib/k_builder/webpack5/webpack_builder.rb, line 181
def write_webpack_rc
  content = JSON.pretty_generate(@webpack_rc.as_json)

  FileUtils.mkdir_p(File.dirname(webpack_rc_file))

  File.write(webpack_rc_file, content)

  self
end