class Minipack::Configuration
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.
Constants
- BUILD_CACHE_KEY_DEFAULT
- ROOT_DEFAULT_ID
Public Class Methods
# 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
Initializes a new instance of Configuration
class.
@param [Configuration,nil] parent refenrece to the parent configuration instance.
# 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
Public Instance Methods
Register a sub configuration with a site name, with a manifest file optionally. You can configure per site.
@param [Symbol] id uniq name of the site @param [String] path path of the manifest file @yieldparam [Configuration] config a sub configuration instance is sent to the block
# 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
@return [String]
# File lib/minipack/configuration.rb, line 166 def cache_path File.join(root_path, 'tmp', 'cache', 'minipack') end
# File lib/minipack/configuration.rb, line 121 def children Collection.new(@children.values) end
CSS is extracted in the webpack build
@return [Boolean]
# File lib/minipack/configuration.rb, line 173 def extract_css? extract_css end
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.
# File lib/minipack/configuration.rb, line 129 def leaves col = @children.empty? ? [self] : @children.values Collection.new(col) end
TODO: This will be moved to Minipack.manifests in the future.
# 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
Resolve base_path as an absolute path
@return [String]
# File lib/minipack/configuration.rb, line 153 def resolved_base_path File.expand_path(base_path || '.', root_path) end
Resolve build_cache_key as absolute paths
@return [Array<String>]
# 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
Private Instance Methods
# File lib/minipack/configuration.rb, line 183 def leaf? !root? end
# File lib/minipack/configuration.rb, line 179 def root? @parent.nil? end