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: should listen for config file changes when specifying the app directory #36570

Merged
merged 4 commits into from
May 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 0 additions & 15 deletions packages/next/bin/next.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,18 +144,3 @@ commands[command]()
process.exit(0)
}
})

if (command === 'dev') {
const { CONFIG_FILES } = require('../shared/lib/constants')
const { watchFile } = require('fs')

for (const CONFIG_FILE of CONFIG_FILES) {
watchFile(`${process.cwd()}/${CONFIG_FILE}`, (cur: any, prev: any) => {
if (cur.size > 0 || prev.size > 0) {
console.log(
`\n> Found a change in ${CONFIG_FILE}. Restart the server to see the changes in effect.`
)
}
})
}
}
14 changes: 13 additions & 1 deletion packages/next/cli/next-dev.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
#!/usr/bin/env node
import arg from 'next/dist/compiled/arg/index.js'
import { existsSync } from 'fs'
import { existsSync, watchFile } from 'fs'
import { startServer } from '../server/lib/start-server'
import { printAndExit } from '../server/lib/utils'
import * as Log from '../build/output/log'
import { startedDevelopmentServer } from '../build/output'
import { cliCommand } from '../bin/next'
import isError from '../lib/is-error'
import { getProjectDir } from '../lib/get-project-dir'
import { CONFIG_FILES } from '../shared/lib/constants'
import path from 'path'

const nextDev: cliCommand = (argv) => {
const validArgs: arg.Spec = {
Expand Down Expand Up @@ -128,6 +130,16 @@ const nextDev: cliCommand = (argv) => {
}
process.nextTick(() => process.exit(1))
})

for (const CONFIG_FILE of CONFIG_FILES) {
watchFile(path.join(dir, CONFIG_FILE), (cur: any, prev: any) => {
if (cur.size > 0 || prev.size > 0) {
console.log(
`\n> Found a change in ${CONFIG_FILE}. Restart the server to see the changes in effect.`
)
}
})
}
}

export { nextDev }
49 changes: 49 additions & 0 deletions test/development/watch-config-file/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { createNext } from 'e2e-utils'
import { check } from 'next-test-utils'
import { NextInstance } from 'test/lib/next-modes/base'

describe('watch-config-file', () => {
let next: NextInstance

beforeAll(async () => {
next = await createNext({
files: {
'pages/index.js': `
export default function Page() {
return <p>hello world</p>
}
`,
'next.config.js': `
const nextConfig = {
reactStrictMode: true,
}

module.exports = nextConfig
`,
},
dependencies: {},
skipStart: true,
})
})
afterAll(() => next.destroy())

it('should output config file change', async () => {
// next dev test-dir
await next.start(true)

await next.patchFile(
'next.config.js',
`
/** changed **/
const nextConfig = {
reactStrictMode: true,
}
module.exports = nextConfig`
)

await check(
() => next.cliOutput,
/Found a change in next.config.js. Restart the server to see the changes in effect./
)
})
})
2 changes: 1 addition & 1 deletion test/lib/next-modes/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ export class NextInstance {
return {}
}
public async setup(): Promise<void> {}
public async start(): Promise<void> {}
public async start(useDirArg: boolean = false): Promise<void> {}
public async stop(): Promise<void> {
this.isStopping = true
if (this.childProcess) {
Expand Down
8 changes: 5 additions & 3 deletions test/lib/next-modes/next-dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,21 @@ export class NextDevInstance extends NextInstance {
return this._cliOutput || ''
}

public async start() {
public async start(useDirArg: boolean = false) {
if (this.childProcess) {
throw new Error('next already started')
}
let startArgs = ['yarn', 'next']
let startArgs = ['yarn', 'next', useDirArg && this.testDir].filter(
Boolean
) as string[]

if (this.startCommand) {
startArgs = this.startCommand.split(' ')
}

await new Promise<void>((resolve) => {
this.childProcess = spawn(startArgs[0], startArgs.slice(1), {
cwd: this.testDir,
cwd: useDirArg ? process.cwd() : this.testDir,
stdio: ['ignore', 'pipe', 'pipe'],
shell: false,
env: {
Expand Down