Unverified Commit a21a8fcc authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

Add error for invalid distDir value (#10177)

* Add error for invalid distDir value

* Add check for null/undefined distDir
Co-authored-by: 's avatarJoe Haddad <timer150@gmail.com>
parent eb38d223
...@@ -83,10 +83,26 @@ function assignDefaults(userConfig: { [key: string]: any }) { ...@@ -83,10 +83,26 @@ function assignDefaults(userConfig: { [key: string]: any }) {
experimentalWarning() experimentalWarning()
} }
if (key === 'distDir' && userConfig[key] === 'public') { if (key === 'distDir') {
throw new Error( if (typeof userConfig[key] !== 'string') {
`The 'public' directory is reserved in Next.js and can not be set as the 'distDir'. https://err.sh/zeit/next.js/can-not-output-to-public` userConfig[key] = defaultConfig.distDir
) }
const userDistDir = userConfig[key].trim()
// don't allow public as the distDir as this is a reserved folder for
// public files
if (userDistDir === 'public') {
throw new Error(
`The 'public' directory is reserved in Next.js and can not be set as the 'distDir'. https://err.sh/zeit/next.js/can-not-output-to-public`
)
}
// make sure distDir isn't an empty string which can result the provided
// directory being deleted in development mode
if (userDistDir.length === 0) {
throw new Error(
`Invalid distDir provided, distDir can not be an empty string. Please remove this config or set it to undefined`
)
}
} }
const maybeObject = userConfig[key] const maybeObject = userConfig[key]
......
/* eslint-env jest */ /* eslint-env jest */
/* global jasmine */ /* global jasmine */
import fs from 'fs-extra'
import { join } from 'path' import { join } from 'path'
import { existsSync } from 'fs'
import { BUILD_ID_FILE } from 'next/constants' import { BUILD_ID_FILE } from 'next/constants'
import { import {
nextServer, nextStart,
nextBuild, nextBuild,
startApp, findPort,
stopApp, killApp,
renderViaHTTP, renderViaHTTP,
} from 'next-test-utils' } from 'next-test-utils'
jasmine.DEFAULT_TIMEOUT_INTERVAL = 1000 * 60 * 2
const appDir = join(__dirname, '../') const appDir = join(__dirname, '../')
const nextConfig = join(appDir, 'next.config.js')
let appPort let appPort
let server
let app let app
jasmine.DEFAULT_TIMEOUT_INTERVAL = 1000 * 60 * 5
describe('Production Usage', () => { describe('Production Usage', () => {
beforeAll(async () => { describe('With basic usage', () => {
await nextBuild(appDir) beforeAll(async () => {
app = nextServer({ await nextBuild(appDir)
dir: join(__dirname, '../'), appPort = await findPort()
dev: false, app = await nextStart(appDir, appPort)
quiet: true,
}) })
afterAll(() => killApp(app))
server = await startApp(app)
appPort = server.address().port
})
afterAll(() => stopApp(server))
describe('With basic usage', () => {
it('should render the page', async () => { it('should render the page', async () => {
const html = await renderViaHTTP(appPort, '/') const html = await renderViaHTTP(appPort, '/')
expect(html).toMatch(/Hello World/) expect(html).toMatch(/Hello World/)
}) })
})
describe('File locations', () => { it('should build the app within the given `dist` directory', async () => {
it('should build the app within the given `dist` directory', () => {
expect( expect(
existsSync(join(__dirname, `/../dist/${BUILD_ID_FILE}`)) await fs.exists(join(__dirname, `/../dist/${BUILD_ID_FILE}`))
).toBeTruthy() ).toBeTruthy()
}) })
it('should not build the app within the default `.next` directory', () => { it('should not build the app within the default `.next` directory', async () => {
expect( expect(
existsSync(join(__dirname, `/../.next/${BUILD_ID_FILE}`)) await fs.exists(join(__dirname, `/../.next/${BUILD_ID_FILE}`))
).toBeFalsy() ).toBeFalsy()
}) })
}) })
it('should throw error with invalid distDir', async () => {
const origNextConfig = await fs.readFile(nextConfig, 'utf8')
await fs.writeFile(nextConfig, `module.exports = { distDir: '' }`)
const { stderr } = await nextBuild(appDir, [], { stderr: true })
await fs.writeFile(nextConfig, origNextConfig)
expect(stderr).toContain(
'Invalid distDir provided, distDir can not be an empty string. Please remove this config or set it to undefined'
)
})
it('should handle null/undefined distDir', async () => {
const origNextConfig = await fs.readFile(nextConfig, 'utf8')
await fs.writeFile(nextConfig, `module.exports = { distDir: null }`)
const { stderr } = await nextBuild(appDir, [], { stderr: true })
await fs.writeFile(nextConfig, origNextConfig)
expect(stderr.length).toBe(0)
})
}) })
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment