makestatic-core-standard
Version:
Standard lifecycle configuration
133 lines (117 loc) • 3.99 kB
JavaScript
const LifeCycle = require('makestatic-core')
/**
* Standard lifecycle configuration for the core library.
*
* This standard configuration is used by the command line interface.
*
* Configures the lifecycle with standard plugins for these phases:
*
* + `clean`: Adds the `clean-output` plugin.
* + `build`: Adds the `http-cache` plugin.
* + `load`: Adds the `sources-loader` plugin.
* + `pack`: Adds the `pack-webpack` plugin unless `pack` is `false`.
* + `resolve`: Adds the `resolve-file` plugin.
* + `emit`: Adds the `emit-gzip` plugin if `gzip` is `true`.
* + `manifest`: Adds the `manifest` plugin if `manifest` is `true`.
* + `write`: Adds the `write-file` plugin.
* + `deploy`: Adds the `deploy-site` plugin if a provider is given.
*
* Plugins are merged with any existing definitions in the lifecycle so as not
* to conflict with user-defined lifecycle configurations for the phases that
* are modified however the plugins configured here take precedence in terms
* of execution order. In practice, due to the nature of the lifecycle
* execution the plugin order should not make any difference to the result as
* a plugin can only be guaranteed a previous plugin has completed execution if
* it exists in a preceeding phase.
*
* @class StandardLifeCycle
* @inherits LifeCycle
*
* @see /docs/api/cli/ Command Line Interface
* @see /docs/api/core/ Core Library
* @see /docs/api/clean-output/ Clean Output
* @see /docs/api/http-cache/ HTTP Agent
* @see /docs/api/sources-loader/ Sources Loader
* @see /docs/api/pack-webpack/ Pack Webpack
* @see /docs/api/resolve-file/ Resolve File
* @see /docs/api/emit-gzip/ Emit Gzip
* @see /docs/api/write-file/ Write File
* @see /docs/api/deploy-site/ Deploy Site
*/
class StandardLifeCycle extends LifeCycle {
/**
* Create a StandardLifeCycle.
*
* @constructor StandardLifeCycle
* @inherits LifeCycle
*
* @param {Object} options configuration options.
*/
constructor (options = {}) {
super(options)
}
/**
* Configures various plugins for the lifecycle phases.
*
* @function getLifeCycleConfig
* @member StandardLifeCycle
* @param {Object} opts map of computed options.
* @param {Object} argv configuration overrides.
*
* @returns lifecycle configuration map.
*/
getLifecycleConfig (opts = {}, argv = {}) {
let conf = opts.lifecycle || {}
// the phases we will modify
const phases = [
'clean',
'build',
'load',
'pack',
'resolve',
'emit',
'manifest',
'write',
'deploy'
]
// ensure we have arrays to push plugins onto
phases.forEach((phase) => {
let def = conf[phase] || []
// wrap simple definitions
if (def && !Array.isArray(def)) {
def = [def]
}
conf[phase] = def
})
conf.clean.unshift(require('makestatic-clean-output'))
conf.build.unshift(require('makestatic-http-cache'))
// always configure sources
conf.load.unshift({
plugin: require('makestatic-sources-loader'),
options: {
// do not buffer if using webpack
buffer: !(argv.pack === undefined || argv.pack === true)
}
})
// configure pack phase
if (argv.pack !== false) {
conf.pack.unshift(require('makestatic-pack-webpack'))
}
conf.resolve.unshift(require('makestatic-resolve-file'))
// generate static gzip files
if (argv.gzip === true) {
conf.emit.push(require('makestatic-emit-gzip'))
}
if (argv.manifest === true) {
conf.manifest.unshift(require('makestatic-manifest'))
}
// always configure write phase
conf.write.unshift(require('makestatic-write-file'))
/* istanbul ignore next: always in test env */
if (argv.provider && process.env.NODE_ENV !== 'test') {
conf.deploy.unshift(require('makestatic-deploy-site'))
}
return conf
}
}
module.exports = StandardLifeCycle