From ec616f109b7f90083f90f399d66943660e87bd82 Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Fri, 16 Mar 2018 20:53:15 +0330 Subject: [PATCH] refactor options --- lib/common/modes.js | 18 ++++ lib/common/nuxt.config.js | 177 +++++++++++++++++++++++++++++++ lib/common/options.js | 213 ++------------------------------------ lib/core/renderer.js | 4 +- 4 files changed, 205 insertions(+), 207 deletions(-) create mode 100644 lib/common/modes.js create mode 100644 lib/common/nuxt.config.js diff --git a/lib/common/modes.js b/lib/common/modes.js new file mode 100644 index 0000000000..676aea8b6d --- /dev/null +++ b/lib/common/modes.js @@ -0,0 +1,18 @@ +export default { + universal: { + build: { + ssr: true + }, + render: { + ssr: true + } + }, + spa: { + build: { + ssr: false + }, + render: { + ssr: false + } + } +} diff --git a/lib/common/nuxt.config.js b/lib/common/nuxt.config.js new file mode 100644 index 0000000000..a1505263c8 --- /dev/null +++ b/lib/common/nuxt.config.js @@ -0,0 +1,177 @@ +import { resolve } from 'path' + +export default { + mode: 'universal', + dev: process.env.NODE_ENV !== 'production', + debug: undefined, // Will be equal to dev if not provided + buildDir: '.nuxt', + cacheDir: '.cache', + nuxtDir: resolve(__dirname, '../..'), + nuxtAppDir: resolve(__dirname, '../app'), + modulesDir: ['node_modules'], // ~> relative to options.rootDir + ignorePrefix: '-', + ignore: [ + '**/*.test.*' + ], + extensions: [], + build: { + analyze: false, + profile: process.argv.includes('--profile'), + splitPages: true, + maxChunkSize: false, + extractCSS: false, + cssSourceMap: undefined, + ssr: undefined, + publicPath: '/_nuxt/', + filenames: { + css: '[name].[contenthash].css', + manifest: 'manifest.[hash].js', + app: '[name].[chunkhash].js', + chunk: '[name].[chunkhash].js' + }, + styleResources: {}, + plugins: [], + babel: { + babelrc: false + }, + postcss: {}, + templates: [], + watch: [], + devMiddleware: {}, + hotMiddleware: {}, + stats: { + chunks: false, + children: false, + modules: false, + colors: true, + excludeAssets: [ + /.map$/, + /index\..+\.html$/, + /vue-ssr-client-manifest.json/ + ] + } + }, + generate: { + dir: 'dist', + routes: [], + concurrency: 500, + interval: 0, + subFolders: true, + fallback: '200.html', + minify: { + collapseBooleanAttributes: true, + collapseWhitespace: false, + 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: false, + 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, + rtl: false + }, + loadingIndicator: {}, + transition: { + name: 'page', + mode: 'out-in', + appear: false, + appearClass: 'appear', + appearActiveClass: 'appear-active', + appearToClass: 'appear-to' + }, + layoutTransition: { + name: 'layout', + mode: 'out-in' + }, + dir: { + assets: 'assets', + layouts: 'layouts', + middleware: 'middleware', + pages: 'pages', + static: 'static', + store: 'store' + }, + router: { + mode: 'history', + base: '/', + routes: [], + middleware: [], + linkActiveClass: 'nuxt-link-active', + linkExactActiveClass: 'nuxt-link-exact-active', + extendRoutes: null, + scrollBehavior: null, + parseQuery: false, + stringifyQuery: false, + fallback: false + }, + render: { + bundleRenderer: {}, + resourceHints: true, + ssr: undefined, + http2: { + push: false, + shouldPush: null + }, + static: { + prefix: true + }, + gzip: { + threshold: 0 + }, + etag: { + weak: false + }, + csp: { + enabled: false, + hashAlgorithm: 'sha256', + allowedSources: undefined, + policies: undefined + } + }, + watchers: { + webpack: {}, + chokidar: {} + }, + editor: undefined, + hooks: null, + messages: { + error_404: 'This page could not be found', + server_error: 'Server error', + nuxtjs: 'Nuxt.js', + back_to_home: 'Back to the home page', + server_error_details: + 'An error occurred in the application and your page could not be served. If you are the application owner, check your logs for details.', + client_error: 'Error', + client_error_details: + 'An error occurred while rendering the page. Check developer tools console for details.' + } +} diff --git a/lib/common/options.js b/lib/common/options.js index d02782fc83..b3ecf1212b 100644 --- a/lib/common/options.js +++ b/lib/common/options.js @@ -2,8 +2,12 @@ import _ from 'lodash' import Debug from 'debug' import { join, resolve } from 'path' import { existsSync, readdirSync } from 'fs' + import { isUrl, isPureObject } from '../common/utils' +import modes from './modes' +import defaults from './nuxt.config' + const debug = Debug('nuxt:build') debug.color = 2 // Force green color @@ -44,14 +48,14 @@ Options.from = function (_options) { // Apply defaults by ${buildDir}/dist/build.config.js // TODO: Unsafe operation. - // const buildDir = options.buildDir || Options.defaults.buildDir + // const buildDir = options.buildDir || defaults.buildDir // const buildConfig = resolve(options.rootDir, buildDir, 'build.config.js') // if (existsSync(buildConfig)) { // _.defaultsDeep(options, require(buildConfig)) // } // Apply defaults - _.defaultsDeep(options, Options.defaults) + _.defaultsDeep(options, defaults) // Resolve dirs options.srcDir = hasValue(options.srcDir) @@ -81,7 +85,7 @@ Options.from = function (_options) { // Ignore publicPath on dev /* istanbul ignore if */ if (options.dev && isUrl(options.build.publicPath)) { - options.build.publicPath = Options.defaults.build.publicPath + options.build.publicPath = defaults.build.publicPath } // If store defined, update store options to true unless explicitly disabled @@ -138,8 +142,7 @@ Options.from = function (_options) { } // Apply mode preset - let modePreset = - Options.modes[options.mode || 'universal'] || Options.modes['universal'] + const modePreset = modes[options.mode || 'universal'] || modes['universal'] _.defaultsDeep(options, modePreset) // If no server-side rendering, add appear true transition @@ -155,203 +158,3 @@ Options.from = function (_options) { return options } - -Options.modes = { - universal: { - build: { - ssr: true - }, - render: { - ssr: true - } - }, - spa: { - build: { - ssr: false - }, - render: { - ssr: false - } - } -} - -// Options.unsafeKeys = [ -// 'rootDir', 'srcDir', 'buildDir', 'modulesDir', 'cacheDir', 'nuxtDir', -// 'nuxtAppDir', 'build', 'generate', 'router.routes', 'appTemplatePath' -// ] - -Options.defaults = { - mode: 'universal', - dev: process.env.NODE_ENV !== 'production', - debug: undefined, // Will be equal to dev if not provided - buildDir: '.nuxt', - cacheDir: '.cache', - nuxtDir: resolve(__dirname, '../..'), - nuxtAppDir: resolve(__dirname, '../app'), - modulesDir: ['node_modules'], // ~> relative to options.rootDir - ignorePrefix: '-', - ignore: [ - '**/*.test.*' - ], - extensions: [], - build: { - analyze: false, - profile: process.argv.includes('--profile'), - splitPages: true, - maxChunkSize: false, - extractCSS: false, - cssSourceMap: undefined, - ssr: undefined, - publicPath: '/_nuxt/', - filenames: { - css: '[name].[contenthash].css', - manifest: 'manifest.[hash].js', - app: '[name].[chunkhash].js', - chunk: '[name].[chunkhash].js' - }, - styleResources: {}, - plugins: [], - babel: { - babelrc: false - }, - postcss: {}, - templates: [], - watch: [], - devMiddleware: {}, - hotMiddleware: {}, - stats: { - chunks: false, - children: false, - modules: false, - colors: true, - excludeAssets: [ - /.map$/, - /index\..+\.html$/, - /vue-ssr-client-manifest.json/ - ] - } - }, - generate: { - dir: 'dist', - routes: [], - concurrency: 500, - interval: 0, - subFolders: true, - fallback: '200.html', - minify: { - collapseBooleanAttributes: true, - collapseWhitespace: false, - 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: false, - 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, - rtl: false - }, - loadingIndicator: {}, - transition: { - name: 'page', - mode: 'out-in', - appear: false, - appearClass: 'appear', - appearActiveClass: 'appear-active', - appearToClass: 'appear-to' - }, - layoutTransition: { - name: 'layout', - mode: 'out-in' - }, - dir: { - assets: 'assets', - layouts: 'layouts', - middleware: 'middleware', - pages: 'pages', - static: 'static', - store: 'store' - }, - router: { - mode: 'history', - base: '/', - routes: [], - middleware: [], - linkActiveClass: 'nuxt-link-active', - linkExactActiveClass: 'nuxt-link-exact-active', - extendRoutes: null, - scrollBehavior: null, - parseQuery: false, - stringifyQuery: false, - fallback: false - }, - render: { - bundleRenderer: {}, - resourceHints: true, - ssr: undefined, - http2: { - push: false, - shouldPush: null - }, - static: { - prefix: true - }, - gzip: { - threshold: 0 - }, - etag: { - weak: false - }, - csp: { - enabled: false, - hashAlgorithm: 'sha256', - allowedSources: undefined, - policies: undefined - } - }, - watchers: { - webpack: {}, - chokidar: {} - }, - editor: undefined, - hooks: null, - messages: { - error_404: 'This page could not be found', - server_error: 'Server error', - nuxtjs: 'Nuxt.js', - back_to_home: 'Back to the home page', - server_error_details: - 'An error occurred in the application and your page could not be served. If you are the application owner, check your logs for details.', - client_error: 'Error', - client_error_details: - 'An error occurred while rendering the page. Check developer tools console for details.' - } -} diff --git a/lib/core/renderer.js b/lib/core/renderer.js index 885cf03554..396e59f45b 100644 --- a/lib/core/renderer.js +++ b/lib/core/renderer.js @@ -12,7 +12,7 @@ import launchMiddleware from 'launch-editor-middleware' import crypto from 'crypto' import { setAnsiColors, isUrl, waitFor, timeout } from '../common/utils' -import Options from '../common/options' +import defaults from '../common/nuxt.config' import MetaRenderer from './meta' import errorMiddleware from './middleware/error' @@ -202,7 +202,7 @@ export default class Renderer { get publicPath() { return isUrl(this.options.build.publicPath) - ? Options.defaults.build.publicPath + ? defaults.build.publicPath : this.options.build.publicPath }