Class: Minipack::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/minipack/configuration.rb

Overview

1-level or 2-levels configuration system. With the typical single site usecase, only the root instance exists as a singleton. If you manage more then one site, each configuration is stored at the 2nd level of the configuration tree.

Defined Under Namespace

Classes: Collection, Error

Constant Summary collapse

ROOT_DEFAULT_ID =
:''
BUILD_CACHE_KEY_DEFAULT =
[
  'package.json',
  'package-lock.json',
  'yarn.lock',
  'webpack.config.js',
  'webpackfile.js',
  'config/webpack.config.js',
  'config/webpackfile.js',
  'app/javascripts/**/*',
].freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parent = nil) ⇒ Configuration

Initializes a new instance of Configuration class.

Parameters:

  • parent (Configuration, nil) (defaults to: nil)

    refenrece to the parent configuration instance.



91
92
93
94
95
96
# File 'lib/minipack/configuration.rb', line 91

def initialize(parent = nil)
  @parent = parent
  # Only a root instance can have children, which are sub configurations each site.
  @children = {}
  @config = {}
end

Class Method Details

.config_attr(prop, default: nil) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
# File 'lib/minipack/configuration.rb', line 40

def config_attr(prop, default: nil)
  define_method(prop) do
    @config.fetch(prop) do
      @parent ? @parent.public_send(prop) : default
    end
  end

  define_method("#{prop}=".to_sym) do |v|
    @config[prop] = v
  end
end

Instance Method Details

#add(id, path = nil) {|config| ... } ⇒ Object

Register a sub configuration with a site name, with a manifest file optionally. You can configure per site.

Parameters:

  • id (Symbol)

    uniq name of the site

  • path (String) (defaults to: nil)

    path of the manifest file

Yield Parameters:

  • config (Configuration)

    a sub configuration instance is sent to the block

Raises:



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/minipack/configuration.rb', line 104

def add(id, path = nil)
  raise Error, 'Defining a sub configuration under a sub is not allowed' if leaf?

  id = id.to_sym
  config = self.class.new(self)
  config.id = id
  config.manifest = path unless path.nil?

  # Link the root to the child
  @children[id] = config

  # The sub configuration can be configured within a block
  yield config if block_given?

  config
end

#cache_pathString

Returns:

  • (String)


166
167
168
# File 'lib/minipack/configuration.rb', line 166

def cache_path
  File.join(root_path, 'tmp', 'cache', 'minipack')
end

#childrenObject



121
122
123
# File 'lib/minipack/configuration.rb', line 121

def children
  Collection.new(@children.values)
end

#extract_css?Boolean

CSS is extracted in the webpack build

Returns:

  • (Boolean)


173
174
175
# File 'lib/minipack/configuration.rb', line 173

def extract_css?
  extract_css
end

#leavesObject

Return scoped leaf nodes in self and children. This method is useful to get the concrete(enabled, or active) configuration instances. Each leaf inherit parameters from parent, so leaves always become active.



129
130
131
132
# File 'lib/minipack/configuration.rb', line 129

def leaves
  col = @children.empty? ? [self] : @children.values
  Collection.new(col)
end

#manifestsObject

TODO: This will be moved to Minipack.manifests in the future.

Raises:



135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/minipack/configuration.rb', line 135

def manifests
  raise Error, 'Calling #manifests is only allowed from a root' unless root?

  repo = ManifestRepository.new
  #  Determine if a single manifest mode or multiple manifests(multiple site) mode
  targets =  @children.empty? ? [self] : @children.values
  targets.each do |config|
    # Skip sites that a manifest file is not configured
    next if config.manifest.nil?

    repo.add(config.id, config.manifest, cache: config.cache)
  end
  repo
end

#resolved_base_pathString

Resolve base_path as an absolute path

Returns:

  • (String)


153
154
155
# File 'lib/minipack/configuration.rb', line 153

def resolved_base_path
  File.expand_path(base_path || '.', root_path)
end

#resolved_build_cache_keyArray<String>

Resolve build_cache_key as absolute paths

Returns:

  • (Array<String>)


160
161
162
163
# File 'lib/minipack/configuration.rb', line 160

def resolved_build_cache_key
  base = resolved_base_path
  build_cache_key.map { |path| File.expand_path(path, base) }
end