Nuxt/lib/common/options.js

208 lines
4.7 KiB
JavaScript
Raw Normal View History

import _ from 'lodash'
import { join, resolve } from 'path'
import { existsSync } from 'fs'
2017-06-16 12:42:45 +00:00
import { isUrl } from 'utils'
2017-06-16 12:42:45 +00:00
export default function Options (_options) {
// Clone options to prevent unwanted side-effects
const options = Object.assign({}, _options)
// Normalize options
if (options.loading === true) {
delete options.loading
}
if (options.router && typeof options.router.middleware === 'string') {
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 }
}
// Apply defaults
2017-07-30 12:20:58 +00:00
_.defaultsDeep(options, Options.defaults)
// Resolve dirs
const hasValue = v => typeof v === 'string' && v
options.rootDir = hasValue(options.rootDir) ? options.rootDir : process.cwd()
options.srcDir = hasValue(options.srcDir) ? resolve(options.rootDir, options.srcDir) : options.rootDir
options.modulesDir = resolve(options.rootDir, hasValue(options.modulesDir) ? options.modulesDir : 'node_modules')
options.buildDir = join(options.rootDir, options.buildDir)
// If app.html is defined, set the template path to the user template
2017-06-16 12:42:45 +00:00
options.appTemplatePath = resolve(options.buildDir, 'views/app.template.html')
if (existsSync(join(options.srcDir, 'app.html'))) {
options.appTemplatePath = 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)) {
2017-07-30 12:20:58 +00:00
options.build.publicPath = Options.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 && existsSync(join(options.srcDir, 'store'))) {
2017-06-14 17:38:07 +00:00
options.store = true
2017-06-14 17:32:25 +00:00
}
// Debug errors
if (options.debug === undefined) {
options.debug = options.dev
}
2017-07-11 00:24:39 +00:00
// Resolve mode
let mode = options.mode
if (typeof mode === 'function') {
mode = mode()
}
if (typeof mode === 'string') {
2017-07-30 12:20:58 +00:00
mode = Options.modes[mode]
2017-07-11 00:24:39 +00:00
}
// Apply mode
_.defaultsDeep(options, mode)
return options
}
2017-07-30 12:20:58 +00:00
Options.modes = {
2017-07-11 00:24:39 +00:00
universal: {
build: {
ssr: true
},
render: {
ssr: true
}
},
spa: {
build: {
ssr: false
},
render: {
ssr: false
}
},
static: {
build: {
ssr: true
},
render: {
ssr: 'static'
}
}
}
2017-07-30 12:20:58 +00:00
Options.defaults = {
2017-07-11 00:24:39 +00:00
mode: 'universal',
dev: process.env.NODE_ENV !== 'production',
debug: undefined, // Will be equal to dev if not provided
2017-06-11 14:17:36 +00:00
buildDir: '.nuxt',
2017-06-18 13:48:25 +00:00
nuxtAppDir: resolve(__dirname, '../lib/app/'), // Relative to dist
2017-06-11 14:17:36 +00:00
build: {
analyze: false,
extractCSS: false,
2017-07-11 00:24:39 +00:00
ssr: undefined,
2017-06-11 14:17:36 +00:00
publicPath: '/_nuxt/',
filenames: {
css: 'common.[chunkhash].css',
manifest: 'manifest.[hash].js',
vendor: 'vendor.bundle.[chunkhash].js',
app: 'nuxt.bundle.[chunkhash].js',
chunk: '[id].nuxt.bundle.[chunkhash].js'
2017-06-11 14:17:36 +00:00
},
vendor: [],
plugins: [],
babel: {},
autoprefixer: {
browsers: ['last 3 versions']
},
postcss: [],
2017-06-11 14:17:36 +00:00
templates: [],
watch: [],
devMiddleware: {},
hotMiddleware: {}
2017-06-11 14:17:36 +00:00
},
generate: {
dir: 'dist',
routes: [],
2017-07-09 10:00:08 +00:00
concurrency: 500,
2017-06-11 14:17:36 +00:00
interval: 0,
minify: {
collapseBooleanAttributes: true,
collapseWhitespace: true,
decodeEntities: true,
minifyCSS: true,
minifyJS: true,
processConditionalComments: true,
removeAttributeQuotes: false,
removeComments: false,
removeEmptyAttributes: true,
removeOptionalTags: true,
removeRedundantAttributes: true,
removeScriptTypeAttributes: false,
removeStyleLinkTypeAttributes: false,
removeTagWhitespace: false,
sortAttributes: true,
sortClassName: true,
trimCustomFragments: true,
useShortDoctype: true
}
},
env: {},
head: {
meta: [],
link: [],
style: [],
script: []
},
plugins: [],
css: [],
modules: [],
layouts: {},
serverMiddleware: [],
ErrorPage: null,
loading: {
color: 'black',
failedColor: 'red',
height: '2px',
duration: 5000
},
transition: {
name: 'page',
mode: 'out-in'
},
router: {
mode: 'history',
base: '/',
2017-06-13 17:58:04 +00:00
routes: [],
2017-06-11 14:17:36 +00:00
middleware: [],
linkActiveClass: 'nuxt-link-active',
linkExactActiveClass: 'nuxt-link-exact-active',
extendRoutes: null,
2017-07-04 16:30:01 +00:00
scrollBehavior: null,
fallback: false
2017-06-11 14:17:36 +00:00
},
render: {
2017-07-04 21:50:43 +00:00
bundleRenderer: {},
2017-06-20 12:48:25 +00:00
resourceHints: true,
2017-07-11 00:24:39 +00:00
ssr: undefined,
2017-06-11 14:17:36 +00:00
http2: {
push: false
},
static: {},
gzip: {
threshold: 0
},
etag: {
weak: true // Faster for responses > 5KB
}
},
watchers: {
webpack: {},
chokidar: {}
}
}