From d9f48228fe06e61c888a5dc52b5589079d927344 Mon Sep 17 00:00:00 2001 From: pooya parsa Date: Wed, 14 Oct 2020 10:30:44 +0200 Subject: [PATCH] fix(cli): rebuild if process.env changes in nuxt.config (#8200) --- packages/cli/src/utils/generate.js | 23 +++++++++++++++++++---- test/fixtures/full-static/nuxt.config.js | 6 ++++++ 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/packages/cli/src/utils/generate.js b/packages/cli/src/utils/generate.js index 92760f5b7051..2a37736d8c1d 100644 --- a/packages/cli/src/utils/generate.js +++ b/packages/cli/src/utils/generate.js @@ -59,13 +59,27 @@ export async function ensureBuild (cmd) { const currentBuildSnapshot = await snapshot(snapshotOptions) + // Detect process.env usage in nuxt.config + const processEnv = {} + if (nuxt.options._nuxtConfigFile) { + const configSrc = await fs.readFile(nuxt.options._nuxtConfigFile) + const envRegex = /process.env.(\w+)/g + let match + // eslint-disable-next-line no-cond-assign + while (match = envRegex.exec(configSrc)) { + processEnv[match[1]] = process.env[match[1]] + } + } + // Current build meta const currentBuild = { // @ts-ignore nuxtVersion: nuxt.constructor.version, ssr: nuxt.options.ssr, target: nuxt.options.target, - snapshot: currentBuildSnapshot + snapshot: currentBuildSnapshot, + env: nuxt.options.env, + 'process.env': processEnv } // Check if build can be skipped @@ -74,9 +88,10 @@ export async function ensureBuild (cmd) { const previousBuild = destr(fs.readFileSync(nuxtBuildFile, 'utf-8')) || {} // Quick diff - const needBuild = false - for (const field of ['nuxtVersion', 'ssr', 'target']) { - if (previousBuild[field] !== currentBuild[field]) { + let needBuild = false + for (const field of ['nuxtVersion', 'ssr', 'target', 'env', 'process.env']) { + if (JSON.stringify(previousBuild[field]) !== JSON.stringify(currentBuild[field])) { + needBuild = true consola.info(`Doing webpack rebuild because ${field} changed`) break } diff --git a/test/fixtures/full-static/nuxt.config.js b/test/fixtures/full-static/nuxt.config.js index d1cd81e00e8d..17f2cffb159b 100644 --- a/test/fixtures/full-static/nuxt.config.js +++ b/test/fixtures/full-static/nuxt.config.js @@ -11,6 +11,12 @@ export default { build: { publicPath: '/test/_nuxt/' }, + foo: { + shell: process.env.SHELL + }, + env: { + x: 123 + }, hooks: { export: { before ({ setPayload }) {