Class: KBuilder::Webpack5::WebpackJsonFactory

Inherits:
Object
  • Object
show all
Defined in:
lib/k_builder/webpack5/webpack_json_factory.rb

Overview

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

Class Method Summary collapse

Class Method Details

.build_from_json(json) ⇒ Object



11
12
13
# 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| ... } ⇒ Object

Yields:

  • (obj)


137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# 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| ... } ⇒ Object

Yields:

  • (obj)


104
105
106
107
108
109
110
111
112
113
# 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| ... } ⇒ Object

Yields:

  • (obj)


82
83
84
85
86
87
88
89
90
91
92
93
94
# 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| ... } ⇒ Object

Yields:

  • (obj)


181
182
183
184
185
186
187
188
189
190
191
192
# 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| ... } ⇒ Object

Yields:

  • (obj)


62
63
64
65
66
67
68
69
70
71
72
73
# 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_serverObject



127
128
129
130
131
132
# 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_entriesObject



96
97
98
99
100
101
# 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_entryObject



75
76
77
78
79
# File 'lib/k_builder/webpack5/webpack_json_factory.rb', line 75

def self.opinion_entry
  lambda { |json|
    json.entry = './src'
  }
end

.opinion_mini_css_extractObject

rubocop:enable Metrics/AbcSize



174
175
176
177
178
# 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_modeObject



55
56
57
58
59
# File 'lib/k_builder/webpack5/webpack_json_factory.rb', line 55

def self.opinion_mode
  lambda { |json|
    json.mode = 'development'
  }
end

.root_scope {|obj| ... } ⇒ Object

rubocop:enable Metrics/ParameterLists

Yields:

  • (obj)


39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# 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| ... } ⇒ Object

Yields:

  • (obj)


115
116
117
118
119
120
121
122
123
124
125
# 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| ... } ⇒ Object

rubocop:disable Metrics/ParameterLists

Yields:

  • (obj)


16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# 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