From 99d7177d9089f77c3c7d1e3d0e79df74d6be0357 Mon Sep 17 00:00:00 2001 From: Clark Du Date: Tue, 5 Dec 2017 03:52:50 +0800 Subject: [PATCH] feat(build): add build.includeConfig to embed nuxt.config in dist after building (#2313) * feat(build): add build.includeConfig to embed nuxt.config in dist after building * refactor: remove build.includeConfig and Options.safeKeys --- lib/builder/builder.js | 9 +++++++++ lib/common/options.js | 16 ++++++++++++++-- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/lib/builder/builder.js b/lib/builder/builder.js index d1ad08d6a0..9b42142c30 100644 --- a/lib/builder/builder.js +++ b/lib/builder/builder.js @@ -17,6 +17,7 @@ import serverWebpackConfig from './webpack/server.config.js' import dllWebpackConfig from './webpack/dll.config.js' import vueLoaderConfig from './webpack/vue-loader.config' import styleLoader from './webpack/style-loader' +import { Options } from 'common' const debug = Debug('nuxt:build') debug.color = 2 // Force green color @@ -62,6 +63,8 @@ export default class Builder { // Stop watching on nuxt.close() if (this.options.dev) { this.nuxt.hook('close', () => this.unwatch()) + } else { + this.nuxt.hook('build:done', () => this.generateConfig()) } } @@ -559,6 +562,12 @@ export default class Builder { // Stop webpack middleware await this.webpackDevMiddleware.close() } + + async generateConfig() { + const config = resolve(this.options.buildDir, 'build.config.js') + const options = _.omit(this.options, Options.unsafeKeys) + await writeFile(config, `module.exports = ${JSON.stringify(options, null, ' ')}`, 'utf8') + } } const STATUS = { diff --git a/lib/common/options.js b/lib/common/options.js index 34e7b3e34a..95bfcff16b 100755 --- a/lib/common/options.js +++ b/lib/common/options.js @@ -32,12 +32,19 @@ Options.from = function (_options) { options.layoutTransition = { name: options.layoutTransition } } + const hasValue = v => typeof v === 'string' && v + options.rootDir = hasValue(options.rootDir) ? options.rootDir : process.cwd() + + // Apply defaults by ${buildDir}/dist/build.config.js + const buildDir = options.buildDir || Options.defaults.buildDir + const buildConfig = resolve(options.rootDir, buildDir, 'build.config.js') + if (existsSync(buildConfig)) { + _.defaultsDeep(options, require(buildConfig)) + } // Apply defaults _.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.buildDir = resolve(options.rootDir, options.buildDir) options.cacheDir = resolve(options.rootDir, options.cacheDir) @@ -185,6 +192,11 @@ Options.modes = { } } +Options.unsafeKeys = [ + 'rootDir', 'srcDir', 'buildDir', 'modulesDir', 'cacheDir', 'nuxtDir', + 'nuxtAppDir', 'build', 'generate', 'router.routes', 'appTemplatePath' +] + Options.defaults = { mode: 'universal', dev: process.env.NODE_ENV !== 'production',