Commit 39020c43 authored by Luis Alvarez D's avatar Luis Alvarez D Committed by Joe Haddad

Improve user errors for invalid `pageExtensions` (#10178)

* Added validation for pageExtensions and tests

* Removed log

* Test for undefined
parent 275a6555
......@@ -105,6 +105,35 @@ function assignDefaults(userConfig: { [key: string]: any }) {
}
}
if (key === 'pageExtensions') {
const pageExtensions = userConfig[key]
if (pageExtensions === undefined) {
delete userConfig[key]
return
}
if (!Array.isArray(pageExtensions)) {
throw new Error(
`Specified pageExtensions is not an array of strings, found "${pageExtensions}". Please update this config or remove it.`
)
}
if (!pageExtensions.length) {
throw new Error(
`Specified pageExtensions is an empty array. Please update it with the relevant extensions or remove it.`
)
}
pageExtensions.forEach(ext => {
if (typeof ext !== 'string') {
throw new Error(
`Specified pageExtensions is not an array of strings, found "${ext}" of type "${typeof ext}". Please update this config or remove it.`
)
}
})
}
const maybeObject = userConfig[key]
if (!!maybeObject && maybeObject.constructor === Object) {
userConfig[key] = {
......
{
"presets": [
"next/babel",
"@zeit/next-typescript/babel"
]
}
\ No newline at end of file
const withTypescript = require('@zeit/next-typescript')
module.exports = withTypescript({
onDemandEntries: {
// Make sure entries are not getting disposed.
maxInactiveAge: 1000 * 60 * 60,
},
})
import React from 'react'
if (typeof window !== 'undefined' && !(window as any).HMR_RANDOM_NUMBER) {
;(window as any).HMR_RANDOM_NUMBER = Math.random()
}
export default class Counter extends React.Component {
state = { count: 0 }
incr() {
const { count } = this.state
this.setState({ count: count + 1 })
}
render() {
return (
<div>
<p>COUNT: {this.state.count}</p>
<button onClick={() => this.incr()}>Increment</button>
</div>
)
}
}
export default () => 'Hello World'
{
"presets": [
["next/babel", {
"preset-env": {
"modules": "commonjs"
}
}]
]
}
\ No newline at end of file
/* eslint-env jest */
import webdriver from 'next-webdriver'
import { readFileSync, writeFileSync } from 'fs'
import { join } from 'path'
import { waitFor } from 'next-test-utils'
export default (context, renderViaHTTP) => {
describe('Hot Module Reloading', () => {
it('should reload typescript file without refresh', async () => {
let browser
const pagePath = join(__dirname, '../', 'pages', 'hmr', 'some-page.tsx')
const originalContent = readFileSync(pagePath, 'utf8')
const editedContent = originalContent.replace('Increment', 'INCREMENT')
try {
browser = await webdriver(context.appPort, '/hmr/some-page')
const randomNumber = await browser.eval('window.HMR_RANDOM_NUMBER')
const originalButtonText = await browser.elementByCss('button').text()
expect(originalButtonText).toBe('Increment')
// Change the about.js page
writeFileSync(pagePath, editedContent, 'utf8')
// wait for 5 seconds
await waitFor(5000)
const randomNumberAfterEdit = await browser.eval(
'window.HMR_RANDOM_NUMBER'
)
expect(randomNumberAfterEdit).toBe(randomNumber)
const updatedButtonText = await browser.elementByCss('button').text()
expect(updatedButtonText).toBe('INCREMENT')
} finally {
// restore the about page content.
writeFileSync(pagePath, originalContent, 'utf8')
if (browser) {
await browser.close()
}
}
})
})
}
/* eslint-env jest */
/* global jasmine */
import { join } from 'path'
import { renderViaHTTP, findPort, launchApp, killApp } from 'next-test-utils'
import fs from 'fs-extra'
import { runNextCommand } from 'next-test-utils'
// test suits
import hmr from './hmr'
const context = {}
jasmine.DEFAULT_TIMEOUT_INTERVAL = 1000 * 60 * 5
const appDir = join(__dirname, '..')
const nextConfig = join(appDir, 'next.config.js')
describe('Page Extensions', () => {
beforeAll(async () => {
context.appPort = await findPort()
context.server = await launchApp(join(__dirname, '../'), context.appPort)
it('should use the default pageExtensions if set to undefined', async () => {
await fs.writeFile(
nextConfig,
`module.exports = { pageExtensions: undefined }`
)
const { stdout } = await runNextCommand(['build', appDir], { stdout: true })
await fs.remove(nextConfig)
expect(stdout).toContain('Compiled successfully')
})
// pre-build all pages at the start
await Promise.all([renderViaHTTP(context.appPort, '/hmr/some-page')])
it('should throw if pageExtensions is not an array', async () => {
await fs.writeFile(nextConfig, `module.exports = { pageExtensions: null }`)
const { stderr } = await runNextCommand(['build', appDir], { stderr: true })
await fs.remove(nextConfig)
expect(stderr).toContain(
'Specified pageExtensions is not an array of strings, found "null". Please update this config or remove it'
)
})
afterAll(() => killApp(context.server))
hmr(context, (p, q) => renderViaHTTP(context.appPort, p, q))
it('should throw if pageExtensions is an empty array', async () => {
await fs.writeFile(nextConfig, `module.exports = { pageExtensions: [] }`)
const { stderr } = await runNextCommand(['build', appDir], { stderr: true })
await fs.remove(nextConfig)
expect(stderr).toContain(
'Specified pageExtensions is an empty array. Please update it with the relevant extensions or remove it'
)
})
it('should throw if pageExtensions has invalid extensions', async () => {
await fs.writeFile(
nextConfig,
`module.exports = { pageExtensions: ['js', 123] }`
)
const { stderr } = await runNextCommand(['build', appDir], { stderr: true })
await fs.remove(nextConfig)
expect(stderr).toContain(
'Specified pageExtensions is not an array of strings, found "123" of type "number". Please update this config or remove it'
)
})
it('should throw if @zeit/next-typescript is used', async () => {
await fs.writeFile(
nextConfig,
`const withTypescript = require('@zeit/next-typescript')
module.exports = withTypescript()
`
)
const { stderr } = await runNextCommand(['build', appDir], { stderr: true })
await fs.remove(nextConfig)
expect(stderr).toContain(
'@zeit/next-typescript is no longer needed since Next.js has built-in support for TypeScript now'
)
})
})
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