2017-06-14 16:13:43 +00:00
|
|
|
import _ from 'lodash'
|
|
|
|
import { join, resolve } from 'path'
|
|
|
|
import { existsSync } from 'fs'
|
2017-08-15 01:06:56 +00:00
|
|
|
import { isUrl, isPureObject } from 'utils'
|
2017-06-14 16:13:43 +00:00
|
|
|
|
2017-08-16 07:40:10 +00:00
|
|
|
const Options = {}
|
|
|
|
|
|
|
|
export default Options
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
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)
|
2017-06-14 16:13:43 +00:00
|
|
|
|
|
|
|
// Resolve dirs
|
2017-07-31 22:24:10 +00:00
|
|
|
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')
|
2017-06-14 16:13:43 +00:00
|
|
|
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')
|
2017-06-14 16:13:43 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2017-08-18 10:26:19 +00:00
|
|
|
// Normalize loadingIndicator
|
|
|
|
if (!isPureObject(options.loadingIndicator)) {
|
|
|
|
options.loadingIndicator = { name: options.loadingIndicator }
|
|
|
|
}
|
|
|
|
|
|
|
|
// Apply defaults to loadingIndicator
|
|
|
|
options.loadingIndicator = Object.assign({
|
|
|
|
name: 'circle',
|
2017-08-18 12:22:54 +00:00
|
|
|
color: '#3B8070',
|
2017-08-18 10:26:19 +00:00
|
|
|
background: 'white'
|
|
|
|
}, options.loadingIndicator)
|
|
|
|
|
2017-08-15 01:06:56 +00:00
|
|
|
// Postcss
|
|
|
|
// 1. Check if it is explicitly disabled by false value
|
|
|
|
// ... Disable all postcss loaders
|
|
|
|
// 2. Check if any standard source of postcss config exists
|
|
|
|
// ... Make postcss = true letting loaders find this kind of config
|
|
|
|
// 3. Else (Easy Usage)
|
|
|
|
// ... Auto merge it with defaults
|
|
|
|
if (options.build.postcss !== false) {
|
|
|
|
// Detect postcss config existence
|
|
|
|
// https://github.com/michael-ciniawsky/postcss-load-config
|
|
|
|
let postcssConfigExists = false
|
|
|
|
for (let dir of [options.srcDir, options.rootDir]) {
|
|
|
|
for (let file of ['postcss.config.js', '.postcssrc.js', '.postcssrc', '.postcssrc.json', '.postcssrc.yaml']) {
|
|
|
|
if (existsSync(resolve(dir, file))) {
|
|
|
|
postcssConfigExists = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (postcssConfigExists) break
|
|
|
|
}
|
|
|
|
|
|
|
|
// Default postcss options
|
|
|
|
if (postcssConfigExists) {
|
2017-08-14 21:13:08 +00:00
|
|
|
options.build.postcss = true
|
|
|
|
}
|
2017-08-15 01:06:56 +00:00
|
|
|
|
|
|
|
// Normalize & Apply default plugins
|
|
|
|
if (Array.isArray(options.build.postcss)) {
|
|
|
|
options.build.postcss = { plugins: options.build.postcss }
|
|
|
|
}
|
|
|
|
if (isPureObject(options.build.postcss)) {
|
|
|
|
options.build.postcss = Object.assign({
|
|
|
|
sourceMap: options.build.cssSourceMap,
|
|
|
|
plugins: {
|
2017-08-17 20:46:32 +00:00
|
|
|
// https://github.com/postcss/postcss-import
|
|
|
|
'postcss-import': {
|
|
|
|
root: options.rootDir,
|
|
|
|
path: [
|
|
|
|
options.srcDir,
|
|
|
|
options.rootDir,
|
|
|
|
options.modulesDir
|
|
|
|
]
|
|
|
|
},
|
2017-08-15 01:06:56 +00:00
|
|
|
// https://github.com/postcss/postcss-url
|
|
|
|
'postcss-url': {},
|
|
|
|
// http://cssnext.io/postcss
|
|
|
|
'postcss-cssnext': {}
|
|
|
|
}
|
|
|
|
}, options.build.postcss)
|
|
|
|
}
|
2017-08-14 21:13:08 +00:00
|
|
|
}
|
|
|
|
|
2017-08-05 00:58:26 +00:00
|
|
|
// Debug errors
|
2017-08-05 21:48:43 +00:00
|
|
|
if (options.debug === undefined) {
|
|
|
|
options.debug = options.dev
|
2017-08-05 00:58:26 +00:00
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
|
2017-08-18 13:44:34 +00:00
|
|
|
// If no server-side rendering, add appear true transition
|
|
|
|
if (options.render.ssr === false) {
|
|
|
|
options.transition.appear = true
|
|
|
|
}
|
|
|
|
|
2017-06-14 16:13:43 +00:00
|
|
|
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: {
|
2017-08-18 12:46:03 +00:00
|
|
|
ssr: 'only'
|
2017-07-11 00:24:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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',
|
2017-08-05 21:48:43 +00:00
|
|
|
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-08-14 14:03:07 +00:00
|
|
|
cssSourceMap: true,
|
2017-07-11 00:24:39 +00:00
|
|
|
ssr: undefined,
|
2017-06-11 14:17:36 +00:00
|
|
|
publicPath: '/_nuxt/',
|
|
|
|
filenames: {
|
2017-08-16 07:24:59 +00:00
|
|
|
css: 'common.[contenthash].css',
|
2017-06-11 14:17:36 +00:00
|
|
|
manifest: 'manifest.[hash].js',
|
2017-08-18 07:17:56 +00:00
|
|
|
vendor: 'common.[chunkhash].js',
|
2017-08-16 10:36:27 +00:00
|
|
|
app: 'app.[chunkhash].js',
|
|
|
|
chunk: '[name].[chunkhash].js'
|
2017-06-11 14:17:36 +00:00
|
|
|
},
|
|
|
|
vendor: [],
|
|
|
|
plugins: [],
|
|
|
|
babel: {},
|
2017-08-15 01:06:56 +00:00
|
|
|
postcss: {},
|
2017-06-11 14:17:36 +00:00
|
|
|
templates: [],
|
2017-07-09 09:40:37 +00:00
|
|
|
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,
|
2017-08-13 19:12:35 +00:00
|
|
|
sortClassName: false,
|
2017-06-11 14:17:36 +00:00
|
|
|
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
|
|
|
|
},
|
2017-08-18 10:29:37 +00:00
|
|
|
loadingIndicator: {},
|
2017-06-11 14:17:36 +00:00
|
|
|
transition: {
|
|
|
|
name: 'page',
|
2017-08-18 13:44:34 +00:00
|
|
|
mode: 'out-in',
|
|
|
|
appear: false
|
2017-06-11 14:17:36 +00:00
|
|
|
},
|
|
|
|
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: {}
|
|
|
|
}
|
|
|
|
}
|