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',