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'
|
2018-04-13 06:50:39 +00:00
|
|
|
import consola from 'consola'
|
2018-09-14 07:06:44 +00:00
|
|
|
import { isPureObject, isUrl, guardDir } from '../common/utils'
|
2017-06-14 16:13:43 +00:00
|
|
|
|
2018-03-16 17:23:15 +00:00
|
|
|
import modes from './modes'
|
|
|
|
import defaults from './nuxt.config'
|
|
|
|
|
2018-09-14 07:06:44 +00:00
|
|
|
// hasValue utility
|
|
|
|
const hasValue = v => typeof v === 'string' && v
|
|
|
|
|
2017-08-16 07:40:10 +00:00
|
|
|
const Options = {}
|
|
|
|
|
2018-03-16 16:12:06 +00:00
|
|
|
export default Options
|
2017-08-16 07:40:10 +00:00
|
|
|
|
|
|
|
Options.from = function (_options) {
|
2017-06-14 16:13:43 +00:00
|
|
|
// 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)
|
|
|
|
) {
|
2017-06-14 16:13:43 +00:00
|
|
|
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 }
|
|
|
|
}
|
2018-01-04 17:10:17 +00:00
|
|
|
if (typeof options.extensions === 'string') {
|
2018-01-13 05:22:11 +00:00
|
|
|
options.extensions = [options.extensions]
|
2018-01-04 17:10:17 +00:00
|
|
|
}
|
2017-06-14 16:13:43 +00:00
|
|
|
|
2018-10-09 12:07:23 +00:00
|
|
|
options.globalName = (_.isString(options.globalName) && /^[a-zA-Z]+$/.test(options.globalName))
|
|
|
|
? options.globalName.toLowerCase()
|
|
|
|
: 'nuxt'
|
|
|
|
|
2018-09-14 07:06:44 +00:00
|
|
|
// Resolve rootDir
|
|
|
|
options.rootDir = hasValue(options.rootDir) ? path.resolve(options.rootDir) : process.cwd()
|
2017-12-04 19:52:50 +00:00
|
|
|
|
|
|
|
// Apply defaults by ${buildDir}/dist/build.config.js
|
2017-12-13 02:17:44 +00:00
|
|
|
// TODO: Unsafe operation.
|
2018-03-16 17:23:15 +00:00
|
|
|
// const buildDir = options.buildDir || defaults.buildDir
|
2017-12-13 02:17:44 +00:00
|
|
|
// const buildConfig = resolve(options.rootDir, buildDir, 'build.config.js')
|
|
|
|
// if (existsSync(buildConfig)) {
|
|
|
|
// _.defaultsDeep(options, require(buildConfig))
|
|
|
|
// }
|
|
|
|
|
2017-06-14 16:13:43 +00:00
|
|
|
// Apply defaults
|
2018-03-16 17:23:15 +00:00
|
|
|
_.defaultsDeep(options, defaults)
|
2017-06-14 16:13:43 +00:00
|
|
|
|
2018-09-14 07:06:44 +00:00
|
|
|
// Check srcDir and generate.dir excistence
|
|
|
|
const hasSrcDir = hasValue(options.srcDir)
|
|
|
|
const hasGenerateDir = hasValue(options.generate.dir)
|
|
|
|
|
|
|
|
// Resolve srcDir
|
|
|
|
options.srcDir = hasSrcDir
|
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-09-14 07:06:44 +00:00
|
|
|
|
|
|
|
// Resolve buildDir
|
2018-03-16 19:11:24 +00:00
|
|
|
options.buildDir = path.resolve(options.rootDir, options.buildDir)
|
2017-06-14 16:13:43 +00:00
|
|
|
|
2018-09-14 07:06:44 +00:00
|
|
|
// Protect rootDir against buildDir
|
|
|
|
guardDir(options, 'rootDir', 'buildDir')
|
|
|
|
|
|
|
|
if (hasGenerateDir) {
|
|
|
|
// Resolve generate.dir
|
|
|
|
options.generate.dir = path.resolve(options.rootDir, options.generate.dir)
|
|
|
|
|
|
|
|
// Protect rootDir against buildDir
|
|
|
|
guardDir(options, 'rootDir', 'generate.dir')
|
|
|
|
}
|
|
|
|
|
|
|
|
if (hasSrcDir) {
|
|
|
|
// Protect srcDir against buildDir
|
|
|
|
guardDir(options, 'srcDir', 'buildDir')
|
|
|
|
|
|
|
|
if (hasGenerateDir) {
|
|
|
|
// Protect srcDir against generate.dir
|
|
|
|
guardDir(options, 'srcDir', 'generate.dir')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-13 01:09:38 +00:00
|
|
|
// Populate modulesDir
|
|
|
|
options.modulesDir = []
|
2017-12-28 16:49:56 +00:00
|
|
|
.concat(options.modulesDir)
|
2018-10-12 22:31:19 +00:00
|
|
|
.concat(path.join(options.nuxtDir, 'node_modules')).filter(hasValue)
|
2018-03-16 19:11:24 +00:00
|
|
|
.map(dir => path.resolve(options.rootDir, dir))
|
2017-12-12 12:57:01 +00:00
|
|
|
|
2018-10-12 22:31:19 +00:00
|
|
|
const mandatoryExtensions = ['js', 'mjs']
|
2018-01-04 17:10:17 +00:00
|
|
|
|
2018-10-12 22:31:19 +00:00
|
|
|
options.extensions = mandatoryExtensions
|
|
|
|
.filter(ext => !options.extensions.includes(ext))
|
|
|
|
.concat(options.extensions)
|
2018-03-16 20:04:54 +00:00
|
|
|
|
2017-06-14 16:13:43 +00:00
|
|
|
// If app.html is defined, set the template path to the user template
|
2018-08-10 15:23:09 +00:00
|
|
|
if (options.appTemplatePath === undefined) {
|
|
|
|
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')
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
options.appTemplatePath = path.resolve(options.srcDir, options.appTemplatePath)
|
2017-06-14 16:13:43 +00:00
|
|
|
}
|
|
|
|
|
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
|
2018-02-27 13:38:00 +00:00
|
|
|
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))
|
2018-02-27 13:38:00 +00:00
|
|
|
.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
|
|
|
}
|
|
|
|
|
2018-03-20 09:13:36 +00:00
|
|
|
// SPA loadingIndicator
|
|
|
|
if (options.loadingIndicator) {
|
|
|
|
// Normalize loadingIndicator
|
|
|
|
if (!isPureObject(options.loadingIndicator)) {
|
|
|
|
options.loadingIndicator = { name: options.loadingIndicator }
|
|
|
|
}
|
|
|
|
|
|
|
|
// Apply defaults
|
|
|
|
options.loadingIndicator = Object.assign(
|
|
|
|
{
|
2018-03-25 18:35:13 +00:00
|
|
|
name: 'default',
|
2018-03-25 19:10:39 +00:00
|
|
|
color: (options.loading && options.loading.color) || '#D3D3D3',
|
2018-03-25 18:35:13 +00:00
|
|
|
color2: '#F5F5F5',
|
2018-03-25 19:10:39 +00:00
|
|
|
background: (options.manifest && options.manifest.theme_color) || 'white',
|
2018-03-25 18:35:13 +00:00
|
|
|
dev: options.dev,
|
|
|
|
loading: options.messages.loading
|
2018-03-20 09:13:36 +00:00
|
|
|
},
|
|
|
|
options.loadingIndicator
|
|
|
|
)
|
2017-08-18 10:26:19 +00:00
|
|
|
}
|
2018-01-10 16:36:32 +00:00
|
|
|
|
2018-03-21 06:20:14 +00:00
|
|
|
// Debug errors
|
|
|
|
if (options.debug === undefined) {
|
|
|
|
options.debug = options.dev
|
|
|
|
}
|
|
|
|
|
2018-01-10 16:31:27 +00:00
|
|
|
// Apply default hash to CSP option
|
2018-07-01 12:39:30 +00:00
|
|
|
const csp = options.render.csp
|
|
|
|
const cspDefaults = {
|
|
|
|
hashAlgorithm: 'sha256',
|
|
|
|
allowedSources: undefined,
|
2018-07-30 16:04:02 +00:00
|
|
|
policies: undefined,
|
|
|
|
reportOnly: options.debug
|
2018-07-01 12:39:30 +00:00
|
|
|
}
|
|
|
|
if (csp) {
|
|
|
|
options.render.csp = _.defaults(_.isObject(csp) ? csp : {}, cspDefaults)
|
2018-01-10 16:31:27 +00:00
|
|
|
}
|
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
|
|
|
|
}
|
|
|
|
|
2018-03-09 06:30:50 +00:00
|
|
|
// babel cacheDirectory
|
|
|
|
if (options.build.babel.cacheDirectory === undefined) {
|
2018-04-02 12:00:58 +00:00
|
|
|
options.build.babel.cacheDirectory = options.dev
|
2018-03-09 06:30:50 +00:00
|
|
|
}
|
|
|
|
|
2018-08-08 19:05:23 +00:00
|
|
|
// vue config
|
|
|
|
const vueConfig = options.vue.config
|
|
|
|
|
|
|
|
if (vueConfig.silent === undefined) {
|
|
|
|
vueConfig.silent = !options.dev
|
|
|
|
}
|
|
|
|
if (vueConfig.performance === undefined) {
|
|
|
|
vueConfig.performance = options.dev
|
|
|
|
}
|
|
|
|
|
2018-09-08 21:08:25 +00:00
|
|
|
// merge custom env with variables
|
|
|
|
const eligibleEnvVariables = _.pick(process.env, Object.keys(process.env).filter(k => k.startsWith('NUXT_ENV_')))
|
|
|
|
Object.assign(options.env, eligibleEnvVariables)
|
|
|
|
|
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}*.*`)
|
|
|
|
}
|
|
|
|
|
2018-09-08 20:18:14 +00:00
|
|
|
// Compression middleware legacy
|
|
|
|
if (options.render.gzip) {
|
2018-09-13 15:07:34 +00:00
|
|
|
consola.warn('render.gzip is deprecated and will be removed in a future version! Please switch to render.compressor')
|
2018-09-08 20:18:14 +00:00
|
|
|
options.render.compressor = options.render.gzip
|
|
|
|
delete options.render.gzip
|
|
|
|
}
|
|
|
|
|
2017-08-19 13:22:53 +00:00
|
|
|
// Apply mode preset
|
2018-08-10 07:19:19 +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 */
|
2017-11-16 10:56:41 +00:00
|
|
|
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'
|
|
|
|
}
|
|
|
|
|
2018-08-12 12:39:43 +00:00
|
|
|
if (options.build.stats === 'none' || options.build.quiet === true) {
|
2018-03-29 07:11:34 +00:00
|
|
|
options.build.stats = false
|
|
|
|
}
|
|
|
|
|
2018-04-13 06:50:39 +00:00
|
|
|
// Vendor backward compatibility with nuxt 1.x
|
|
|
|
if (typeof options.build.vendor !== 'undefined') {
|
|
|
|
delete options.build.vendor
|
|
|
|
consola.warn('vendor has been deprecated due to webpack4 optimization')
|
2018-04-12 20:42:50 +00:00
|
|
|
}
|
|
|
|
|
2018-03-21 13:36:52 +00:00
|
|
|
// TODO: remove when mini-css-extract-plugin supports HMR
|
|
|
|
if (options.dev) {
|
|
|
|
options.build.extractCSS = false
|
|
|
|
}
|
2018-05-06 19:48:19 +00:00
|
|
|
|
2018-09-30 16:09:06 +00:00
|
|
|
// Enable minimize for production builds
|
|
|
|
if (options.build.optimization.minimize === undefined) {
|
|
|
|
options.build.optimization.minimize = !options.dev
|
|
|
|
}
|
|
|
|
|
|
|
|
// Enable optimizeCSS only when extractCSS is enabled
|
|
|
|
if (options.build.optimizeCSS === undefined) {
|
|
|
|
options.build.optimizeCSS = options.build.extractCSS ? {} : false
|
|
|
|
}
|
|
|
|
|
2018-09-10 08:27:01 +00:00
|
|
|
const loaders = options.build.loaders
|
|
|
|
const vueLoader = loaders.vue
|
|
|
|
if (vueLoader.productionMode === undefined) {
|
|
|
|
vueLoader.productionMode = !options.dev
|
|
|
|
}
|
|
|
|
const styleLoaders = [
|
|
|
|
'css', 'cssModules', 'less',
|
|
|
|
'sass', 'scss', 'stylus', 'vueStyle'
|
|
|
|
]
|
|
|
|
for (const name of styleLoaders) {
|
|
|
|
const loader = loaders[name]
|
|
|
|
if (loader && loader.sourceMap === undefined) {
|
|
|
|
loader.sourceMap = Boolean(options.build.cssSourceMap)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-06 19:48:19 +00:00
|
|
|
// include SFCs in node_modules
|
2018-07-31 13:10:24 +00:00
|
|
|
options.build.transpile = [].concat(options.build.transpile || [])
|
|
|
|
.map(module => module instanceof RegExp ? module : new RegExp(module))
|
2018-05-06 19:48:19 +00:00
|
|
|
|
2018-08-13 16:16:25 +00:00
|
|
|
if (options.build.quiet === true) {
|
|
|
|
consola.level = 0
|
|
|
|
}
|
|
|
|
|
2017-06-14 16:13:43 +00:00
|
|
|
return options
|
|
|
|
}
|