Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(cli): rebuild if process.env changes in nuxt.config #8200

Merged
merged 2 commits into from Oct 14, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 19 additions & 4 deletions packages/cli/src/utils/generate.js
Expand Up @@ -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
Expand All @@ -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
}
Expand Down
6 changes: 6 additions & 0 deletions test/fixtures/full-static/nuxt.config.js
Expand Up @@ -11,6 +11,12 @@ export default {
build: {
publicPath: '/test/_nuxt/'
},
foo: {
shell: process.env.SHELL
},
env: {
x: 123
},
hooks: {
export: {
before ({ setPayload }) {
Expand Down