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

Add warning during next build when sharp is missing #27933

Merged
merged 9 commits into from Aug 11, 2021
10 changes: 10 additions & 0 deletions packages/next/build/index.ts
Expand Up @@ -930,6 +930,16 @@ export default async function build(
return returnValue
})

if (isNextImageImported) {
try {
require(process.env.NEXT_SHARP_PATH || 'sharp')
} catch (e) {
Log.warn(
'Detected `next/image` usage without `sharp`. https://nextjs.org/docs/messages/sharp-missing-in-production'
)
}
}

if (customAppGetInitialProps) {
console.warn(
chalk.bold.yellow(`Warning: `) +
Expand Down
35 changes: 24 additions & 11 deletions test/integration/image-optimizer/test/index.test.js
Expand Up @@ -25,8 +25,8 @@ const largeSize = 1080 // defaults defined in server/config.ts
let nextOutput
let appPort
let app

const sharpMissingText = `For production Image Optimization with Next.js, the optional 'sharp' package is strongly recommended`
const sharpRuntimeWarning = `For production Image Optimization with Next.js, the optional 'sharp' package is strongly recommended`
const sharpBuildWarning = 'Detected `next/image` usage without `sharp`.'

async function fsToJson(dir, output = {}) {
const files = await fs.readdir(dir)
Expand Down Expand Up @@ -727,12 +727,20 @@ function runTests({ w, isDev, domains = [], ttl, isSharp }) {
})

if (isDev || isSharp) {
it('should not have sharp missing warning', () => {
expect(nextOutput).not.toContain(sharpMissingText)
it('should not have runtime sharp missing warning', () => {
expect(nextOutput).not.toContain(sharpRuntimeWarning)
})

it('should not have runtime sharp missing warning', () => {
expect(buildOutput).not.toContain(sharpBuildWarning)
})
} else {
it('should have sharp missing warning', () => {
expect(nextOutput).toContain(sharpMissingText)
it('should have runtime sharp missing warning', () => {
expect(nextOutput).toContain(sharpRuntimeWarning)
})

it('should have build time sharp missing warning', () => {
expect(buildOutput).toContain(sharpBuildWarning)
})
}
}
Expand Down Expand Up @@ -911,7 +919,8 @@ describe('Image Optimizer', () => {
})
nextOutput = ''
nextConfig.replace('{ /* replaceme */ }', json)
await nextBuild(appDir)
const out = await nextBuild(appDir, [], { stderr: true })
buildOutput = out.stderr
appPort = await findPort()
app = await nextStart(appDir, appPort, {
onStderr(msg) {
Expand Down Expand Up @@ -949,7 +958,8 @@ describe('Image Optimizer', () => {
},
}`
)
await nextBuild(appDir)
const out = await nextBuild(appDir, [], { stderr: true })
buildOutput = out.stderr
appPort = await findPort()
app = await nextStart(appDir, appPort)
})
Expand Down Expand Up @@ -1025,7 +1035,8 @@ describe('Image Optimizer', () => {
},
}`
nextConfig.replace('{ /* replaceme */ }', newConfig)
await nextBuild(appDir)
const out = await nextBuild(appDir, [], { stderr: true })
buildOutput = out.stderr
appPort = await findPort()
app = await nextStart(appDir, appPort)
})
Expand Down Expand Up @@ -1133,7 +1144,8 @@ describe('Image Optimizer', () => {
const size = 384 // defaults defined in server/config.ts
beforeAll(async () => {
nextOutput = ''
await nextBuild(appDir)
const out = await nextBuild(appDir, [], { stderr: true })
buildOutput = out.stderr
appPort = await findPort()
app = await nextStart(appDir, appPort, {
onStderr(msg) {
Expand Down Expand Up @@ -1168,7 +1180,8 @@ describe('Image Optimizer', () => {
})
nextOutput = ''
nextConfig.replace('{ /* replaceme */ }', json)
await nextBuild(appDir)
const out = await nextBuild(appDir, [], { stderr: true })
buildOutput = out.stderr
appPort = await findPort()
app = await nextStart(appDir, appPort, {
onStderr(msg) {
Expand Down