class KBuilder::Webpack5::WebpackJsonFactory

Factory helps give shape to the JSON structure.

Helps: Because the underlying structure is a typeless and contractless

OpenStruct, the developer could put any values they like in here.
This factory helps to articulate the JSON contract

Public Class Methods

build_from_json(json) click to toggle source
# File lib/k_builder/webpack5/webpack_json_factory.rb, line 11
def self.build_from_json(json)
  # ToDo, build up the WebStruct by applying each JSON structure
end
dev_server(opinion: nil, **opts) { |obj| ... } click to toggle source

webpack.js.org/configuration/dev-server/ github.com/webpack/webpack-dev-server rubocop:disable Metrics/AbcSize

# File lib/k_builder/webpack5/webpack_json_factory.rb, line 137
def self.dev_server(opinion: nil, **opts)
  obj = KBuilder::Webpack5::JsonData.new

  # Let the software lead/architect's opinion decide default configuration
  opinion&.call(obj)

  # https://github.com/webpack/webpack-dev-server/tree/master/examples/cli/public
  obj.open      = opts[:open]            unless opts[:open].nil?         # true
  obj.localhost = opts[:localhost]       unless opts[:localhost].nil?    # localhost

  # https://github.com/webpack/webpack-dev-server/tree/master/examples/cli/watch-static
  obj.static = opts[:static]             unless opts[:static].nil? # static: ['assets', 'css']

  yield obj if block_given?

  # Samples
  # devServer: {
  #   open: true,
  #   host: 'localhost'
  # }
  #
  # devServer: {
  #   contentBase: path.join(__dirname, 'dist'),
  #   compress: true,
  #   port: 9000,
  # },

  # TIPS
  # - If you're having trouble, navigating to the /webpack-dev-server route will show where files are served. For example, http://localhost:9000/webpack-dev-server.
  # - If you want to manually recompile the bundle, navigating to the /invalidate route will invalidate the current compilation of the bundle and recompile it for you via webpack-dev-middleware.
  #   Depending on your configuration, URL may look like http://localhost:9000/invalidate.
  # - HTML template is required to serve the bundle, usually it is an index.html file. Make sure that script references are added into HTML, webpack-dev-server doesn't inject them automatically.

  obj
end
entries(opinion: nil) { |obj| ... } click to toggle source

webpack.js.org/concepts/entry-points/

# File lib/k_builder/webpack5/webpack_json_factory.rb, line 104
def self.entries(opinion: nil)
  obj = KBuilder::Webpack5::JsonData.new

  # Let the software lead/architect's opinion decide default configuration
  opinion&.call(obj)

  yield obj if block_given?

  obj
end
entry(opinion: nil, **opts) { |obj| ... } click to toggle source

webpack.js.org/concepts/entry-points/

# File lib/k_builder/webpack5/webpack_json_factory.rb, line 82
def self.entry(opinion: nil, **opts)
  obj = KBuilder::Webpack5::JsonData.new

  # Let the software lead/architect's opinion decide default configuration
  opinion&.call(obj)

  # https://webpack.js.org/configuration/
  obj.entry = opts[:entry] unless opts[:entry].nil?

  yield obj if block_given?

  obj
end
mini_css_extract(opinion: nil, **opts) { |obj| ... } click to toggle source

webpack.js.org/plugins/mini-css-extract-plugin/

# File lib/k_builder/webpack5/webpack_json_factory.rb, line 181
def self.mini_css_extract(opinion: nil, **opts)
  obj = KBuilder::Webpack5::JsonData.new

  # Let the software lead/architect's opinion decide default configuration
  opinion&.call(obj)

  obj.filename = opts[:filename] unless opts[:filename].nil?

  yield obj if block_given?

  obj
end
mode(opinion: nil, **opts) { |obj| ... } click to toggle source

webpack.js.org/configuration/mode/

# File lib/k_builder/webpack5/webpack_json_factory.rb, line 62
def self.mode(opinion: nil, **opts)
  obj = KBuilder::Webpack5::JsonData.new

  # Let the software lead/architect's opinion decide default configuration
  opinion&.call(obj)

  obj.mode = opts[:mode] unless opts[:mode].nil?

  yield obj if block_given?

  obj
end
opinion_dev_server() click to toggle source
# File lib/k_builder/webpack5/webpack_json_factory.rb, line 127
def self.opinion_dev_server
  lambda { |json|
    json.open      = true
    json.localhost = 'localhost'
  }
end
opinion_entries() click to toggle source
# File lib/k_builder/webpack5/webpack_json_factory.rb, line 96
def self.opinion_entries
  lambda { |json|
    json.home = './src/home.js'
    json.about = './src/about.js'
  }
end
opinion_entry() click to toggle source
# File lib/k_builder/webpack5/webpack_json_factory.rb, line 75
def self.opinion_entry
  lambda { |json|
    json.entry = './src'
  }
end
opinion_mini_css_extract() click to toggle source

rubocop:enable Metrics/AbcSize

# File lib/k_builder/webpack5/webpack_json_factory.rb, line 174
def self.opinion_mini_css_extract
  lambda { |json|
    json.filename = 'main.[contenthash].css'
  }
end
opinion_mode() click to toggle source
# File lib/k_builder/webpack5/webpack_json_factory.rb, line 55
def self.opinion_mode
  lambda { |json|
    json.mode = 'development'
  }
end
root_scope() { |obj| ... } click to toggle source

rubocop:enable Metrics/ParameterLists

# File lib/k_builder/webpack5/webpack_json_factory.rb, line 39
def self.root_scope
  obj = KBuilder::Webpack5::JsonData.new

  obj.require_path = false
  obj.require_webpack = false
  obj.require_mini_css_extract_plugin = false
  obj.require_html_webpack_plugin = false
  obj.require_workbox_webpack_plugin = false
  obj.require_autoprefixer = false
  obj.require_precss = false

  yield obj if block_given?

  obj
end
settings() { |obj| ... } click to toggle source
# File lib/k_builder/webpack5/webpack_json_factory.rb, line 115
def self.settings
  obj = KBuilder::Webpack5::JsonData.new

  # Render the TIPS

  obj.tips = false

  yield obj if block_given?

  obj
end
webpack( settings: WebpackJsonFactory.settings, root_scope: WebpackJsonFactory.root_scope, entry: nil, entries: nil, plugins: nil, dev_server: nil ) { |obj| ... } click to toggle source

rubocop:disable Metrics/ParameterLists

# File lib/k_builder/webpack5/webpack_json_factory.rb, line 16
def self.webpack(
  settings: WebpackJsonFactory.settings,
  root_scope: WebpackJsonFactory.root_scope,
  entry: nil,
  entries: nil,
  plugins: nil,
  dev_server: nil
)
  obj = KBuilder::Webpack5::JsonData.new

  obj.root_scope  = root_scope
  obj.entry       = entry unless entry.nil?
  obj.entries     = entries unless entries.nil?
  obj.dev_server  = dev_server unless dev_server.nil?
  obj.plugins     = plugins unless plugins.nil?
  obj.settings    = settings

  yield obj if block_given?

  obj
end