Nuxt/lib/common/options.mjs

162 lines
4.6 KiB
JavaScript
Raw Normal View History

2018-03-16 19:11:24 +00:00
import path from 'path'
import fs from 'fs'
2018-03-16 17:23:15 +00:00
2018-03-16 19:52:17 +00:00
import _ from 'lodash'
import Debug from 'debug'
2018-03-16 16:12:06 +00:00
import { isUrl, isPureObject } from '../common/utils'
2018-03-16 17:23:15 +00:00
import modes from './modes'
import defaults from './nuxt.config'
const debug = Debug('nuxt:build')
debug.color = 2 // Force green color
const Options = {}
2018-03-16 16:12:06 +00:00
export default Options
Options.from = function (_options) {
// Clone options to prevent unwanted side-effects
const options = Object.assign({}, _options)
// Normalize options
if (options.loading === true) {
delete options.loading
}
2018-01-13 05:22:11 +00:00
if (
options.router &&
options.router.middleware &&
!Array.isArray(options.router.middleware)
) {
options.router.middleware = [options.router.middleware]
}
if (options.router && typeof options.router.base === 'string') {
options._routerBaseSpecified = true
}
if (typeof options.transition === 'string') {
options.transition = { name: options.transition }
}
2017-09-08 10:42:00 +00:00
if (typeof options.layoutTransition === 'string') {
options.layoutTransition = { name: options.layoutTransition }
}
if (typeof options.extensions === 'string') {
2018-01-13 05:22:11 +00:00
options.extensions = [options.extensions]
}
const hasValue = v => typeof v === 'string' && v
options.rootDir = hasValue(options.rootDir) ? options.rootDir : process.cwd()
// Apply defaults by ${buildDir}/dist/build.config.js
// TODO: Unsafe operation.
2018-03-16 17:23:15 +00:00
// const buildDir = options.buildDir || defaults.buildDir
// const buildConfig = resolve(options.rootDir, buildDir, 'build.config.js')
// if (existsSync(buildConfig)) {
// _.defaultsDeep(options, require(buildConfig))
// }
// Apply defaults
2018-03-16 17:23:15 +00:00
_.defaultsDeep(options, defaults)
// Resolve dirs
2018-01-13 05:22:11 +00:00
options.srcDir = hasValue(options.srcDir)
2018-03-16 19:11:24 +00:00
? path.resolve(options.rootDir, options.srcDir)
2018-01-13 05:22:11 +00:00
: options.rootDir
2018-03-16 19:11:24 +00:00
options.buildDir = path.resolve(options.rootDir, options.buildDir)
options.cacheDir = path.resolve(options.rootDir, options.cacheDir)
2017-12-13 01:09:38 +00:00
// Populate modulesDir
options.modulesDir = []
2017-12-28 16:49:56 +00:00
.concat(options.modulesDir)
2018-03-16 19:11:24 +00:00
.concat(path.join(options.nuxtDir, 'node_modules'))
2017-12-13 01:09:38 +00:00
.filter(dir => hasValue(dir))
2018-03-16 19:11:24 +00:00
.map(dir => path.resolve(options.rootDir, dir))
2017-12-12 12:57:01 +00:00
// Sanitize extensions
if (options.extensions.indexOf('js') === -1) {
options.extensions.unshift('js')
}
// If app.html is defined, set the template path to the user template
2018-03-16 19:11:24 +00:00
options.appTemplatePath = path.resolve(options.buildDir, 'views/app.template.html')
if (fs.existsSync(path.join(options.srcDir, 'app.html'))) {
options.appTemplatePath = path.join(options.srcDir, 'app.html')
}
2017-06-14 17:32:25 +00:00
// Ignore publicPath on dev
2017-06-19 15:47:31 +00:00
/* istanbul ignore if */
2017-06-14 17:32:25 +00:00
if (options.dev && isUrl(options.build.publicPath)) {
2018-03-16 17:23:15 +00:00
options.build.publicPath = defaults.build.publicPath
2017-06-14 17:32:25 +00:00
}
// If store defined, update store options to true unless explicitly disabled
if (
options.store !== false &&
2018-03-16 19:11:24 +00:00
fs.existsSync(path.join(options.srcDir, options.dir.store)) &&
fs.readdirSync(path.join(options.srcDir, options.dir.store))
.find(filename => filename !== 'README.md' && filename[0] !== '.')
) {
2017-06-14 17:38:07 +00:00
options.store = true
2017-06-14 17:32:25 +00:00
}
2017-08-18 10:26:19 +00:00
// Normalize loadingIndicator
if (!isPureObject(options.loadingIndicator)) {
options.loadingIndicator = { name: options.loadingIndicator }
}
2018-01-10 16:36:32 +00:00
// Apply default hash to CSP option
if (options.render.csp === true) {
options.render.csp = { hashAlgorithm: 'sha256' }
}
2017-08-18 10:26:19 +00:00
// Apply defaults to loadingIndicator
2018-01-13 05:22:11 +00:00
options.loadingIndicator = Object.assign(
{
name: 'pulse',
color: '#dbe1ec',
background: 'white'
},
options.loadingIndicator
)
2017-08-18 10:26:19 +00:00
2017-08-20 08:38:38 +00:00
// cssSourceMap
if (options.build.cssSourceMap === undefined) {
options.build.cssSourceMap = options.dev
}
// babel cacheDirectory
if (options.build.babel.cacheDirectory === undefined) {
options.build.babel.cacheDirectory = options.dev
}
// Debug errors
if (options.debug === undefined) {
options.debug = options.dev
}
2018-01-15 09:44:44 +00:00
// Normalize ignore
options.ignore = options.ignore ? [].concat(options.ignore) : []
// Append ignorePrefix glob to ignore
if (typeof options.ignorePrefix === 'string') {
options.ignore.push(`**/${options.ignorePrefix}*.*`)
}
2017-08-19 13:22:53 +00:00
// Apply mode preset
2018-03-16 17:23:15 +00:00
const modePreset = modes[options.mode || 'universal'] || modes['universal']
2017-08-19 13:22:53 +00:00
_.defaultsDeep(options, modePreset)
2017-07-11 00:24:39 +00:00
2017-08-18 13:44:34 +00:00
// If no server-side rendering, add appear true transition
2017-11-24 15:44:07 +00:00
/* istanbul ignore if */
if (options.render.ssr === false && options.transition) {
2017-08-18 13:44:34 +00:00
options.transition.appear = true
}
2018-01-27 00:20:03 +00:00
// We assume the SPA fallback path is 404.html (for GitHub Pages, Surge, etc.)
if (options.generate.fallback === true) {
options.generate.fallback = '404.html'
}
return options
}