Skip to content

Commit 99d7177

Browse files
clarkdopi0
authored andcommitted
feat(build): add build.includeConfig to embed nuxt.config in dist after building (nuxt#2313)
* feat(build): add build.includeConfig to embed nuxt.config in dist after building * refactor: remove build.includeConfig and Options.safeKeys
1 parent aeb13eb commit 99d7177

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

lib/builder/builder.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import serverWebpackConfig from './webpack/server.config.js'
1717
import dllWebpackConfig from './webpack/dll.config.js'
1818
import vueLoaderConfig from './webpack/vue-loader.config'
1919
import styleLoader from './webpack/style-loader'
20+
import { Options } from 'common'
2021

2122
const debug = Debug('nuxt:build')
2223
debug.color = 2 // Force green color
@@ -62,6 +63,8 @@ export default class Builder {
6263
// Stop watching on nuxt.close()
6364
if (this.options.dev) {
6465
this.nuxt.hook('close', () => this.unwatch())
66+
} else {
67+
this.nuxt.hook('build:done', () => this.generateConfig())
6568
}
6669
}
6770

@@ -559,6 +562,12 @@ export default class Builder {
559562
// Stop webpack middleware
560563
await this.webpackDevMiddleware.close()
561564
}
565+
566+
async generateConfig() {
567+
const config = resolve(this.options.buildDir, 'build.config.js')
568+
const options = _.omit(this.options, Options.unsafeKeys)
569+
await writeFile(config, `module.exports = ${JSON.stringify(options, null, ' ')}`, 'utf8')
570+
}
562571
}
563572

564573
const STATUS = {

lib/common/options.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,19 @@ Options.from = function (_options) {
3232
options.layoutTransition = { name: options.layoutTransition }
3333
}
3434

35+
const hasValue = v => typeof v === 'string' && v
36+
options.rootDir = hasValue(options.rootDir) ? options.rootDir : process.cwd()
37+
38+
// Apply defaults by ${buildDir}/dist/build.config.js
39+
const buildDir = options.buildDir || Options.defaults.buildDir
40+
const buildConfig = resolve(options.rootDir, buildDir, 'build.config.js')
41+
if (existsSync(buildConfig)) {
42+
_.defaultsDeep(options, require(buildConfig))
43+
}
3544
// Apply defaults
3645
_.defaultsDeep(options, Options.defaults)
3746

3847
// Resolve dirs
39-
const hasValue = v => typeof v === 'string' && v
40-
options.rootDir = hasValue(options.rootDir) ? options.rootDir : process.cwd()
4148
options.srcDir = hasValue(options.srcDir) ? resolve(options.rootDir, options.srcDir) : options.rootDir
4249
options.buildDir = resolve(options.rootDir, options.buildDir)
4350
options.cacheDir = resolve(options.rootDir, options.cacheDir)
@@ -185,6 +192,11 @@ Options.modes = {
185192
}
186193
}
187194

195+
Options.unsafeKeys = [
196+
'rootDir', 'srcDir', 'buildDir', 'modulesDir', 'cacheDir', 'nuxtDir',
197+
'nuxtAppDir', 'build', 'generate', 'router.routes', 'appTemplatePath'
198+
]
199+
188200
Options.defaults = {
189201
mode: 'universal',
190202
dev: process.env.NODE_ENV !== 'production',

0 commit comments

Comments
 (0)